CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VertexAnalyzer.py
Go to the documentation of this file.
1 import itertools
2 
3 from PhysicsTools.Heppy.analyzers.core.VertexHistograms import VertexHistograms
4 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
5 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
6 from PhysicsTools.HeppyCore.statistics.average import Average
7 from PhysicsTools.Heppy.physicsutils.PileUpSummaryInfo import PileUpSummaryInfo
9 
10 class VertexAnalyzer( Analyzer ):
11  """selects a list of good primary vertices,
12  and optionally add a pile-up weight to MC events.
13 
14  The list of good primary vertices is put in event.goodVertices.
15  if no good vertex is found, the process function returns False.
16 
17  The weight is put in event.vertexWeight, and is multiplied to
18  the global event weight, event.eventWeight.
19 
20  Example:
21 
22  vertexAna = cfg.Analyzer(
23  'VertexAnalyzer',
24  goodVertices = 'goodPVFilter',
25  vertexWeight = 'vertexWeightFall112011AB',
26  # uncomment the following line if you want a vertex weight = 1 (no weighting)
27  # fixedWeight = 1,
28  verbose = False
29  )
30 
31  If fixedWeight is set to None, the vertex weight is read from the EDM collection with module name
32  'vertexWeightFall112011AB'.
33  Otherwise, the weight is set to fixedWeight.
34 
35  The vertex weight collection was at some point produced in the PAT+CMG step,
36  and could directly be accessed from the PAT or CMG tuple.
37  In the most recent versions of the PAT+CMG tuple, this collection is not present anymore,
38  and an additional full framework process must be ran to produce this collection,
39  so that this analyzer can read it. An example cfg to do that can be found here:
40  http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/UserCode/CMG/CMGTools/H2TauTau/prod/vertexWeight2011_cfg.py?view=markup
41 
42 
43  """
44 
45  def __init__(self, cfg_ana, cfg_comp, looperName):
46  super(VertexAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)
47 
48  self.doHists=True
49  if (hasattr(self.cfg_ana,'makeHists')) and (not self.cfg_ana.makeHists):
50  self.doHists=False
51  if self.doHists:
52  self.pileup = VertexHistograms('/'.join([self.dirName,
53  'pileup.root']))
54 
55  self.allVertices = self.cfg_ana.allVertices if (hasattr(self.cfg_ana,'allVertices')) else "_AUTO_"
56 
57  def declareHandles(self):
58  super(VertexAnalyzer, self).declareHandles()
59  if self.allVertices == '_AUTO_':
60  self.handles['vertices'] = AutoHandle( "offlineSlimmedPrimaryVertices", 'std::vector<reco::Vertex>', fallbackLabel="offlinePrimaryVertices" )
61  else:
62  self.handles['vertices'] = AutoHandle( self.allVertices, 'std::vector<reco::Vertex>' )
63  self.fixedWeight = None
64  if self.cfg_comp.isMC:
65  if hasattr( self.cfg_ana, 'fixedWeight'):
66  self.fixedWeight = self.cfg_ana.fixedWeight
67  else:
68  self.mchandles['vertexWeight'] = AutoHandle( self.cfg_ana.vertexWeight,
69  'double' )
70 
71  self.mchandles['pusi'] = AutoHandle(
72  'addPileupInfo',
73  'std::vector<PileupSummaryInfo>'
74  )
75 
76  self.handles['rho'] = AutoHandle(
77  ('fixedGridRhoFastjetAll',''),
78  'double'
79  )
80 
81  def beginLoop(self, setup):
82  super(VertexAnalyzer,self).beginLoop(setup)
83  self.averages.add('vertexWeight', Average('vertexWeight') )
84  self.counters.addCounter('GoodVertex')
85  self.count = self.counters.counter('GoodVertex')
86  self.count.register('All Events')
87  self.count.register('Events With Good Vertex')
88 
89 
90  def process(self, event):
91  self.readCollections(event.input )
92  event.rho = self.handles['rho'].product()[0]
93  event.vertices = self.handles['vertices'].product()
94  event.goodVertices = filter(self.testGoodVertex,event.vertices)
95 
96 
97  self.count.inc('All Events')
98 
99 
100  event.vertexWeight = 1
101  if self.cfg_comp.isMC:
102  event.pileUpInfo = map( PileUpSummaryInfo,
103  self.mchandles['pusi'].product() )
104  if self.fixedWeight is None:
105  event.vertexWeight = self.mchandles['vertexWeight'].product()[0]
106  else:
107  event.vertexWeight = self.fixedWeight
108  event.eventWeight *= event.vertexWeight
109 
110  self.averages['vertexWeight'].add( event.vertexWeight )
111  if self.verbose:
112  print 'VertexAnalyzer: #vert = ', len(event.vertices), \
113  ', weight = ', event.vertexWeight
114 
115  # Check if events needs to be skipped if no good vertex is found (useful for generator level studies)
116  keepFailingEvents = False
117  if hasattr( self.cfg_ana, 'keepFailingEvents'):
118  keepFailingEvents = self.cfg_ana.keepFailingEvents
119  if len(event.goodVertices)==0:
120  event.passedVertexAnalyzer=False
121  if not keepFailingEvents:
122  return False
123  else:
124  event.passedVertexAnalyzer=True
125 
126  if self.doHists:
127  self.pileup.hist.Fill( len(event.goodVertices) )
128 #A.R. mindist is one of the slowest functions, default commented
129 # self.pileup.mindist.Fill( self.mindist(event.goodVertices) )
130 
131  self.count.inc('Events With Good Vertex')
132  return True
133 
134 
135  def testGoodVertex(self,vertex):
136  if vertex.isFake():
137  return False
138  if vertex.ndof()<=4:
139  return False
140  if abs(vertex.z())>24:
141  return False
142  if vertex.position().Rho()>2:
143  return False
144 
145  return True
146 
147  def mindist(self, vertices):
148  mindist = 999999
149  for comb in itertools.combinations(vertices, 2):
150  dist = abs(comb[0].z() - comb[1].z())
151  if dist<mindist:
152  mindist = dist
153  return mindist
154 
155  def write(self, setup):
156  super(VertexAnalyzer, self).write(setup)
157  if self.doHists:
158  self.pileup.write()
159 
160 setattr(VertexAnalyzer,"defaultConfig",cfg.Analyzer(
161  class_object=VertexAnalyzer,
162  vertexWeight = None,
163  fixedWeight = 1,
164  verbose = False
165  )
166 )
void add(const std::vector< const T * > &source, std::vector< const T * > &dest)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static std::string join(char **cmd)
Definition: RemoteFile.cc:18