2 from PhysicsTools.Heppy.analyzers.core.VertexHistograms
import VertexHistograms
3 from PhysicsTools.Heppy.analyzers.core.Analyzer
import Analyzer
4 from PhysicsTools.Heppy.analyzers.core.AutoHandle
import AutoHandle
5 from PhysicsTools.HeppyCore.statistics.average
import Average
6 from PhysicsTools.Heppy.physicsutils.PileUpSummaryInfo
import PileUpSummaryInfo
7 import PhysicsTools.HeppyCore.framework.config
as cfg
9 from ROOT
import TFile, TH1F
12 '''Computes pile-up weights for MC from the pile up histograms for MC and data.
13 These histograms should be set on the components as
14 puFileData, puFileMC attributes, as is done here:
16 http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/UserCode/CMG/CMGTools/H2TauTau/Colin/test_tauMu_2012_cfg.py?view=markup
18 THESE HISTOGRAMS MUST BE CONSISTENT, SEE
19 https://twiki.cern.ch/twiki/bin/view/CMS/CMGToolsPileUpReweighting#Generating_pile_up_distributions
21 If the component is not MC, or if the puFileData and puFileMC are not
22 set for the component, the reweighting is not done.
24 The analyzer sets event.vertexWeight.
25 This weight is multiplied to the global event weight, event.eventWeight.
26 When using this analyzer, make sure that the VertexAnalyzer is disabled,
27 as you would be reweighting the MC PU distribution twice!
29 Additionally, this analyzer writes in the output an histogram containing the unweighting MC
30 pile-up distribution, to be used in input of the weighting for a later pass.
36 # build unweighted pu distribution using number of pile up interactions if False
37 # otherwise, use fill the distribution using number of true interactions
42 def __init__(self, cfg_ana, cfg_comp, looperName):
43 super(PileUpAnalyzer, self).
__init__(cfg_ana, cfg_comp, looperName)
47 if (hasattr(self.cfg_ana,
'makeHists'))
and (
not self.cfg_ana.makeHists):
50 self.
allVertices = self.cfg_ana.allVertices
if (hasattr(self.cfg_ana,
'allVertices'))
else "_AUTO_"
52 if self.cfg_comp.isMC
and self.
doHists:
57 if self.cfg_comp.isEmbed :
58 self.cfg_comp.puFileMC =
None
59 self.cfg_comp.puFileData =
None
61 if self.cfg_comp.isMC
or self.cfg_comp.isEmbed:
62 if not hasattr(self.cfg_comp,
"puFileMC")
or (self.cfg_comp.puFileMC
is None and self.cfg_comp.puFileData
is None):
65 assert( os.path.isfile(os.path.expandvars(self.cfg_comp.puFileMC)) )
66 assert( os.path.isfile(os.path.expandvars(self.cfg_comp.puFileData)) )
68 self.
mcfile = TFile( self.cfg_comp.puFileMC )
69 self.
mchist = self.mcfile.Get(
'pileup')
70 self.mchist.Scale( 1 / self.mchist.Integral() )
72 self.
datafile = TFile( self.cfg_comp.puFileData )
74 self.datahist.Scale( 1 / self.datahist.Integral() )
76 if self.mchist.GetNbinsX() != self.datahist.GetNbinsX():
77 raise ValueError(
'data and mc histograms must have the same number of bins')
78 if self.mchist.GetXaxis().GetXmin() != self.datahist.GetXaxis().GetXmin():
79 raise ValueError(
'data and mc histograms must have the same xmin')
80 if self.mchist.GetXaxis().GetXmax() != self.datahist.GetXaxis().GetXmax():
81 raise ValueError(
'data and mc histograms must have the same xmax')
85 self.mchandles[
'pusi'] = AutoHandle(
87 'std::vector<PileupSummaryInfo>'
90 self.handles[
'vertices'] = AutoHandle(
"offlineSlimmedPrimaryVertices",
'std::vector<reco::Vertex>', fallbackLabel=
"offlinePrimaryVertices" )
92 self.handles[
'vertices'] = AutoHandle( self.
allVertices,
'std::vector<reco::Vertex>' )
95 super(PileUpAnalyzer,self).
beginLoop(setup)
96 self.averages.add(
'vertexWeight',
Average(
'vertexWeight') )
100 self.readCollections( event.input )
102 if self.cfg_comp.isEmbed :
105 event.vertexWeight = 1
107 if self.cfg_comp.isMC:
108 event.pileUpInfo =
map( PileUpSummaryInfo,
109 self.mchandles[
'pusi'].product() )
110 for puInfo
in event.pileUpInfo:
111 if puInfo.getBunchCrossing()==0:
113 if self.cfg_ana.true
is False:
114 event.nPU = puInfo.nPU()
116 event.nPU = puInfo.nTrueInteractions()
119 self.rawmcpileup.hist.Fill( event.nPU )
121 if event.nPU
is None:
122 raise ValueError(
'nPU cannot be None! means that no pu info has been found for bunch crossing 0.')
123 elif self.cfg_comp.isEmbed:
124 vertices = self.handles[
'vertices'].product()
125 event.nPU = len(vertices)
130 bin = self.datahist.FindBin(event.nPU)
131 if bin<1
or bin>self.datahist.GetNbinsX():
132 event.vertexWeight = 0
134 data = self.datahist.GetBinContent(bin)
135 mc = self.mchist.GetBinContent(bin)
138 event.vertexWeight = data/mc
140 event.vertexWeight = 1
142 event.eventWeight *= event.vertexWeight
143 self.averages[
'vertexWeight'].
add( event.vertexWeight )
147 super(PileUpAnalyzer, self).
write(setup)
148 if self.cfg_comp.isMC
and self.
doHists:
149 self.rawmcpileup.write()
152 setattr(PileUpAnalyzer,
"defaultConfig", cfg.Analyzer(
153 class_object = PileUpAnalyzer,
enable
if component is embed return (has no trigger obj)
void add(const std::vector< const T * > &source, std::vector< const T * > &dest)
static std::string join(char **cmd)