CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Public Attributes | Static Public Attributes
core.PileUpAnalyzer.PileUpAnalyzer Class Reference
Inheritance diagram for core.PileUpAnalyzer.PileUpAnalyzer:

Public Member Functions

def __init__
 
def beginLoop
 
def declareHandles
 
def process
 
def write
 

Public Attributes

 allVertices
 
 datafile
 
 datahist
 
 doHists
 
 enable
 if component is embed return (has no trigger obj) More...
 
 mcfile
 
 mchist
 
 rawmcpileup
 

Static Public Attributes

 class_object = PileUpAnalyzer,
 
 makeHists = False
 
 true = True,
 

Detailed Description

Computes pile-up weights for MC from the pile up histograms for MC and data.
These histograms should be set on the components as
puFileData, puFileMC attributes, as is done here:

http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/UserCode/CMG/CMGTools/H2TauTau/Colin/test_tauMu_2012_cfg.py?view=markup

THESE HISTOGRAMS MUST BE CONSISTENT, SEE
https://twiki.cern.ch/twiki/bin/view/CMS/CMGToolsPileUpReweighting#Generating_pile_up_distributions

If the component is not MC, or if the puFileData and puFileMC are not
set for the component, the reweighting is not done. 

The analyzer sets event.vertexWeight.
This weight is multiplied to the global event weight, event.eventWeight.
When using this analyzer, make sure that the VertexAnalyzer is disabled,
as you would be reweighting the MC PU distribution twice!

Additionally, this analyzer writes in the output an histogram containing the unweighting MC
pile-up distribution, to be used in input of the weighting for a later pass. 

Example of use: 

puAna = cfg.Analyzer(
  "PileUpAnalyzer",
  # build unweighted pu distribution using number of pile up interactions if False
  # otherwise, use fill the distribution using number of true interactions
  true = True
  )

Definition at line 11 of file PileUpAnalyzer.py.

Constructor & Destructor Documentation

def core.PileUpAnalyzer.PileUpAnalyzer.__init__ (   self,
  cfg_ana,
  cfg_comp,
  looperName 
)

Definition at line 42 of file PileUpAnalyzer.py.

42 
43  def __init__(self, cfg_ana, cfg_comp, looperName):
44  super(PileUpAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)
45 
46  self.doHists=True
47 
48  if (hasattr(self.cfg_ana,'makeHists')) and (not self.cfg_ana.makeHists):
49  self.doHists=False
50 
51  self.allVertices = self.cfg_ana.allVertices if (hasattr(self.cfg_ana,'allVertices')) else "_AUTO_"
52 
53  if self.cfg_comp.isMC and self.doHists:
54  self.rawmcpileup = VertexHistograms('/'.join([self.dirName,
55  'rawMCPU.root']))
56  self.enable = True
57  ## if component is embed return (has no trigger obj)
58  if self.cfg_comp.isEmbed :
59  self.cfg_comp.puFileMC = None
60  self.cfg_comp.puFileData = None
61 
62  if self.cfg_comp.isMC or self.cfg_comp.isEmbed:
63  if not hasattr(self.cfg_comp,"puFileMC") or (self.cfg_comp.puFileMC is None and self.cfg_comp.puFileData is None):
64  self.enable = False
65  else:
66  assert( os.path.isfile(os.path.expandvars(self.cfg_comp.puFileMC)) )
67  assert( os.path.isfile(os.path.expandvars(self.cfg_comp.puFileData)) )
68 
69  self.mcfile = TFile( self.cfg_comp.puFileMC )
70  self.mchist = self.mcfile.Get('pileup')
71  self.mchist.Scale( 1 / self.mchist.Integral() )
72 
73  self.datafile = TFile( self.cfg_comp.puFileData )
74  self.datahist = self.datafile.Get('pileup')
75  self.datahist.Scale( 1 / self.datahist.Integral() )
76  # import pdb; pdb.set_trace()
77  if self.mchist.GetNbinsX() != self.datahist.GetNbinsX():
78  raise ValueError('data and mc histograms must have the same number of bins')
79  if self.mchist.GetXaxis().GetXmin() != self.datahist.GetXaxis().GetXmin():
80  raise ValueError('data and mc histograms must have the same xmin')
81  if self.mchist.GetXaxis().GetXmax() != self.datahist.GetXaxis().GetXmax():
82  raise ValueError('data and mc histograms must have the same xmax')
assert(m_qm.get())
enable
if component is embed return (has no trigger obj)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18

Member Function Documentation

def core.PileUpAnalyzer.PileUpAnalyzer.beginLoop (   self,
  setup 
)

Definition at line 96 of file PileUpAnalyzer.py.

References Average.

96 
97  def beginLoop(self, setup):
98  super(PileUpAnalyzer,self).beginLoop(setup)
99  self.averages.add('puWeight', Average('puWeight') )
100 
def core.PileUpAnalyzer.PileUpAnalyzer.declareHandles (   self)

Definition at line 83 of file PileUpAnalyzer.py.

References core.PileUpAnalyzer.PileUpAnalyzer.allVertices, core.AutoHandle.AutoHandle.handles, core.Analyzer.Analyzer.handles, HTTP.RequestManager.handles, and core.Analyzer.Analyzer.mchandles.

83 
84  def declareHandles(self):
85  super(PileUpAnalyzer, self).declareHandles()
86  self.mchandles['pusi'] = AutoHandle(
87  'slimmedAddPileupInfo',
88  'std::vector<PileupSummaryInfo>',
89  fallbackLabel="addPileupInfo"
90  )
91 
92  if self.allVertices == '_AUTO_':
93  self.handles['vertices'] = AutoHandle( "offlineSlimmedPrimaryVertices", 'std::vector<reco::Vertex>', fallbackLabel="offlinePrimaryVertices" )
94  else:
95  self.handles['vertices'] = AutoHandle( self.allVertices, 'std::vector<reco::Vertex>' )
def core.PileUpAnalyzer.PileUpAnalyzer.process (   self,
  event 
)

Definition at line 101 of file PileUpAnalyzer.py.

References Clusterizer1DCommons.add(), analyzer.Analyzer.averages, core.PileUpAnalyzer.PileUpAnalyzer.doHists, core.PileUpAnalyzer.PileUpAnalyzer.enable, core.AutoHandle.AutoHandle.handles, core.Analyzer.Analyzer.handles, HTTP.RequestManager.handles, python.multivaluedict.map(), core.Analyzer.Analyzer.mchandles, and core.Analyzer.Analyzer.readCollections().

Referenced by ConfigBuilder.ConfigBuilder.addExtraStream(), ConfigBuilder.ConfigBuilder.completeInputCommand(), ConfigBuilder.ConfigBuilder.doNotInlineEventContent(), ConfigBuilder.ConfigBuilder.PrintAllModules.leave(), ConfigBuilder.ConfigBuilder.prepare_HLT(), ConfigBuilder.ConfigBuilder.prepare_LHE(), ConfigBuilder.ConfigBuilder.prepare_PATFILTER(), ConfigBuilder.ConfigBuilder.prepare_VALIDATION(), ConfigBuilder.ConfigBuilder.renameHLTprocessInSequence(), ConfigBuilder.ConfigBuilder.renameInputTagsInSequence(), and ConfigBuilder.ConfigBuilder.scheduleSequence().

102  def process(self, event):
103  self.readCollections( event.input )
104  ## if component is embed return (has no trigger obj)
105  if self.cfg_comp.isEmbed :
106  return True
107 
108  event.puWeight = 1
109  event.nPU = None
110  event.pileUpVertex_z = []
111  event.pileUpVertex_ptHat = []
112  if self.cfg_comp.isMC:
113  event.pileUpInfo = map( PileUpSummaryInfo,
114  self.mchandles['pusi'].product() )
115  for puInfo in event.pileUpInfo:
116  if puInfo.getBunchCrossing()==0:
117  # import pdb; pdb.set_trace()
118  if self.cfg_ana.true is False:
119  event.nPU = puInfo.nPU()
120  else:
121  event.nPU = puInfo.nTrueInteractions()
122 
123  if self.doHists:
124  self.rawmcpileup.hist.Fill( event.nPU )
125 
126  ##get z position of on-time pile-up sorted by pt-hat
127  ptHat_zPositions = zip(puInfo.getPU_pT_hats(),puInfo.getPU_zpositions())
128  ptHat_zPositions.sort(reverse=True)
129  for ptHat_zPosition in ptHat_zPositions:
130  event.pileUpVertex_z.append(ptHat_zPosition[1])
131  event.pileUpVertex_ptHat.append(ptHat_zPosition[0])
132 
133  if event.nPU is None:
134  raise ValueError('nPU cannot be None! means that no pu info has been found for bunch crossing 0.')
135  elif self.cfg_comp.isEmbed:
136  vertices = self.handles['vertices'].product()
137  event.nPU = len(vertices)
138  else:
139  return True
140 
141  if self.enable:
142  bin = self.datahist.FindBin(event.nPU)
143  if bin<1 or bin>self.datahist.GetNbinsX():
144  event.puWeight = 0
145  else:
146  data = self.datahist.GetBinContent(bin)
147  mc = self.mchist.GetBinContent(bin)
148  #Protect 0 division!!!!
149  if mc !=0.0:
150  event.puWeight = data/mc
151  else:
152  event.puWeight = 1
153 
154  event.eventWeight *= event.puWeight
155  self.averages['puWeight'].add( event.puWeight )
156  return True
enable
if component is embed return (has no trigger obj)
void add(const std::vector< const T * > &source, std::vector< const T * > &dest)
def core.PileUpAnalyzer.PileUpAnalyzer.write (   self,
  setup 
)

Definition at line 157 of file PileUpAnalyzer.py.

References core.PileUpAnalyzer.PileUpAnalyzer.doHists.

158  def write(self, setup):
159  super(PileUpAnalyzer, self).write(setup)
160  if self.cfg_comp.isMC and self.doHists:
161  self.rawmcpileup.write()
162 
163 
setattr(PileUpAnalyzer,"defaultConfig", cfg.Analyzer(

Member Data Documentation

core.PileUpAnalyzer.PileUpAnalyzer.allVertices

Definition at line 50 of file PileUpAnalyzer.py.

Referenced by objects.VertexAnalyzer.VertexAnalyzer.declareHandles(), and core.PileUpAnalyzer.PileUpAnalyzer.declareHandles().

core.PileUpAnalyzer.PileUpAnalyzer.class_object = PileUpAnalyzer,
static

Definition at line 164 of file PileUpAnalyzer.py.

core.PileUpAnalyzer.PileUpAnalyzer.datafile

Definition at line 72 of file PileUpAnalyzer.py.

core.PileUpAnalyzer.PileUpAnalyzer.datahist

Definition at line 73 of file PileUpAnalyzer.py.

core.PileUpAnalyzer.PileUpAnalyzer.doHists

Definition at line 45 of file PileUpAnalyzer.py.

Referenced by objects.VertexAnalyzer.VertexAnalyzer.process(), core.PileUpAnalyzer.PileUpAnalyzer.process(), core.PileUpAnalyzer.PileUpAnalyzer.write(), and objects.VertexAnalyzer.VertexAnalyzer.write().

core.PileUpAnalyzer.PileUpAnalyzer.enable

if component is embed return (has no trigger obj)

Definition at line 55 of file PileUpAnalyzer.py.

Referenced by core.PileUpAnalyzer.PileUpAnalyzer.process().

core.PileUpAnalyzer.PileUpAnalyzer.makeHists = False
static

Definition at line 166 of file PileUpAnalyzer.py.

core.PileUpAnalyzer.PileUpAnalyzer.mcfile

Definition at line 68 of file PileUpAnalyzer.py.

core.PileUpAnalyzer.PileUpAnalyzer.mchist

Definition at line 69 of file PileUpAnalyzer.py.

core.PileUpAnalyzer.PileUpAnalyzer.rawmcpileup

Definition at line 53 of file PileUpAnalyzer.py.

core.PileUpAnalyzer.PileUpAnalyzer.true = True,
static

Definition at line 165 of file PileUpAnalyzer.py.