test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
embeddingAuxFunctions.cc
Go to the documentation of this file.
2 
15 
17 
18 #include <iostream>
19 #include <iomanip>
20 
21 #include <TGraph.h>
22 
23 namespace
24 {
25  bool higherPt(const reco::CandidateBaseRef& muon1, const reco::CandidateBaseRef& muon2)
26  {
27  return (muon1->pt() > muon2->pt());
28  }
29 }
30 
31 std::vector<reco::CandidateBaseRef> getSelMuons(const edm::Event& evt, const edm::InputTag& srcSelMuons)
32 {
33  std::vector<reco::CandidateBaseRef> selMuons;
34 
36  if ( evt.getByLabel(srcSelMuons, combCandidatesHandle) ) {
37  if ( combCandidatesHandle->size() >= 1 ) {
38  const reco::CompositeCandidate& combCandidate = combCandidatesHandle->at(0); // TF: use only the first combined candidate
39  for ( size_t idx = 0; idx < combCandidate.numberOfDaughters(); ++idx ) {
40  const reco::Candidate* daughter = combCandidate.daughter(idx);
41  reco::CandidateBaseRef selMuon;
42  if ( daughter->hasMasterClone() ) {
43  selMuon = daughter->masterClone();
44  }
45  if ( selMuon.isNull() )
46  throw cms::Exception("Configuration")
47  << "Collection 'selectedMuons' = " << srcSelMuons.label() << " of CompositeCandidates does not refer to daughters of valid type !!\n";
48  selMuons.push_back(selMuon);
49  }
50  }
51  } else {
53  edm::Handle<CandidateView> selMuonsHandle;
54  if ( evt.getByLabel(srcSelMuons, selMuonsHandle) ) {
55  for ( size_t idx = 0; idx < selMuonsHandle->size(); ++idx ) {
56  selMuons.push_back(reco::CandidateBaseRef(selMuonsHandle->refAt(idx)));
57  }
58  } else {
59  throw cms::Exception("Configuration")
60  << "Invalid input collection 'selectedMuons' = " << srcSelMuons.label() << " !!\n";
61  }
62  }
63 
64  // sort collection of selected muons by decreasing Pt
65  std::sort(selMuons.begin(), selMuons.end(), higherPt);
66 
67  return selMuons;
68 }
69 
70 reco::CandidateBaseRef getTheMuPlus(const std::vector<reco::CandidateBaseRef>& selMuons)
71 {
72 //--- return highest Pt muon of positive charge
73 //
74 // NOTE: function assumes that collection of muons passed as function argument is sorted by decreasing Pt
75 // (= as returned by 'getSelMuons' function)
76 
77  for ( std::vector<reco::CandidateBaseRef>::const_iterator selMuon = selMuons.begin();
78  selMuon != selMuons.end(); ++selMuon ) {
79  if ( (*selMuon)->charge() > +0.5 ) return (*selMuon);
80  }
81 
82  // no muon of positive charge found
83  return reco::CandidateBaseRef();
84 }
85 
86 reco::CandidateBaseRef getTheMuMinus(const std::vector<reco::CandidateBaseRef>& selMuons)
87 {
88 //--- return highest Pt muon of negative charge
89 //
90 // NOTE: function assumes that collection of muons passed as function argument is sorted by decreasing Pt
91 // (= as returned by 'getSelMuons' function)
92 
93  for ( std::vector<reco::CandidateBaseRef>::const_iterator selMuon = selMuons.begin();
94  selMuon != selMuons.end(); ++selMuon ) {
95  if ( (*selMuon)->charge() < -0.5 ) return (*selMuon);
96  }
97 
98  // no muon of negative charge found
99  return reco::CandidateBaseRef();
100 }
101 
103  TrackDetectorAssociator& trackAssociator, const TrackAssociatorParameters& trackAssociatorParameters,
104  const reco::Candidate* muon)
105 {
106  TrackDetMatchInfo trackDetMatchInfo;
107  const reco::Muon* recoMuon = dynamic_cast<const reco::Muon*>(muon);
108  if ( recoMuon && recoMuon->globalTrack().isNonnull() ) {
109  trackDetMatchInfo = trackAssociator.associate(evt, es, *recoMuon->globalTrack(), trackAssociatorParameters);
110  } else {
111  GlobalVector muonP3(muon->px(), muon->py(), muon->pz());
112  GlobalPoint muonVtx(muon->vertex().x(), muon->vertex().y(), muon->vertex().z());
113  trackDetMatchInfo = trackAssociator.associate(evt, es, muonP3, muonVtx, muon->charge(), trackAssociatorParameters);
114  }
115  return trackDetMatchInfo;
116 }
117 
118 void printMuonDetId(const edm::EventSetup& es, uint32_t rawDetId)
119 {
120  DetId detId(rawDetId);
121  std::cout << "detId = " << rawDetId << std::endl;
122  const GeomDet* geo = 0;
123  if ( detId.det() == DetId::Muon && detId.subdetId() == MuonSubdetId::CSC ) {
124  CSCDetId cscDetId(detId);
125  std::cout << " CSC:"
126  << " endcap = " << cscDetId.endcap() << ","
127  << " station = " << cscDetId.station() << ","
128  << " ring = " << cscDetId.ring() << ","
129  << " chamber = " << cscDetId.chamber() << ","
130  << " layer = " << cscDetId.layer() << std::endl;
131  edm::ESHandle<CSCGeometry> cscGeometry;
132  es.get<MuonGeometryRecord>().get(cscGeometry);
133  geo = cscGeometry->idToDet(detId);
134  } else if ( detId.det() == DetId::Muon && detId.subdetId() == MuonSubdetId::RPC ) {
135  RPCDetId rpcDetId(detId);
136  std::cout << " RPC:"
137  << " region = " << rpcDetId.region() << ","
138  << " ring = " << rpcDetId.ring() << ","
139  << " station = " << rpcDetId.station() << ","
140  << " sector = " << rpcDetId.sector() << ","
141  << " layer = " << rpcDetId.layer() << ","
142  << " subsector = " << rpcDetId.subsector() << ","
143  << " roll = " << rpcDetId.roll() << std::endl;
144  edm::ESHandle<RPCGeometry> rpcGeometry;
145  es.get<MuonGeometryRecord>().get(rpcGeometry);
146  geo = rpcGeometry->idToDet(detId);
147  } else if ( detId.det() == DetId::Muon && detId.subdetId() == MuonSubdetId::DT ) {
148  DTChamberId dtDetId(detId);
149  std::cout << " DT (chamber):"
150  << " wheel = " << dtDetId.wheel() << ","
151  << " station = " << dtDetId.station() << ","
152  << " sector = " << dtDetId.sector() << std::endl;
153  edm::ESHandle<DTGeometry> dtGeometry;
154  es.get<MuonGeometryRecord>().get(dtGeometry);
155  geo = dtGeometry->idToDet(detId);
156  } else {
157  std::cout << " WARNING: detId refers to unknown detector !!" << std::endl;
158  }
159  if ( geo ) {
160  std::cout << "(position: eta = " << geo->position().eta() << ", phi = " << geo->position().phi() << ", r = " << geo->position().perp() << ")" << std::endl;
161  }
162 }
163 
164 bool matchMuonDetId(uint32_t rawDetId1, uint32_t rawDetId2)
165 {
166  DetId detId1(rawDetId1);
167  DetId detId2(rawDetId2);
168  if ( detId1.det() == DetId::Muon && detId1.subdetId() == MuonSubdetId::CSC &&
169  detId2.det() == DetId::Muon && detId2.subdetId() == MuonSubdetId::CSC ) {
170  CSCDetId cscDetId1(detId1);
171  CSCDetId cscDetId2(detId2);
172  if ( cscDetId1.endcap() == cscDetId2.endcap() &&
173  cscDetId1.station() == cscDetId2.station() &&
174  cscDetId1.ring() == cscDetId2.ring() &&
175  cscDetId1.chamber() == cscDetId2.chamber() ) return true;
176  } else if ( detId1.det() == DetId::Muon && detId1.subdetId() == MuonSubdetId::RPC &&
177  detId2.det() == DetId::Muon && detId2.subdetId() == MuonSubdetId::RPC ) {
178  RPCDetId rpcDetId1(detId1);
179  RPCDetId rpcDetId2(detId2);
180  if ( rpcDetId1.region() == rpcDetId2.region() &&
181  rpcDetId1.ring() == rpcDetId2.ring() &&
182  rpcDetId1.station() == rpcDetId2.station() &&
183  rpcDetId1.sector() == rpcDetId2.sector() ) return true;
184  } else if ( detId1.det() == DetId::Muon && detId1.subdetId() == MuonSubdetId::DT &&
185  detId2.det() == DetId::Muon && detId2.subdetId() == MuonSubdetId::DT ) {
186  DTChamberId dtDetId1(detId1);
187  DTChamberId dtDetId2(detId2);
188  if ( dtDetId1.wheel() == dtDetId2.wheel() &&
189  dtDetId1.station() == dtDetId2.station() &&
190  dtDetId1.sector() == dtDetId2.sector() ) return true;
191  }
192  return false;
193 }
194 
195 void repairBarcodes(HepMC::GenEvent* genEvt)
196 {
197  // AB: Note that we cannot do the barcode re-assignment "inline" without first
198  // creating a copy of the vertex and particle collections, because otherwise
199  // changing a barcode might invalidate the iterator which is used for
200  // iterating over the collection.
201  const std::vector<HepMC::GenVertex*> vertices(genEvt->vertices_begin(), genEvt->vertices_end());
202  const std::vector<HepMC::GenParticle*> particles(genEvt->particles_begin(), genEvt->particles_end());
203 
204  int next_genVtx_barcode = 1;
205  for(std::vector<HepMC::GenVertex*>::const_iterator iter = vertices.begin(); iter != vertices.end(); ++iter, ++next_genVtx_barcode)
206  while(!(*iter)->suggest_barcode(-next_genVtx_barcode))
207  ++next_genVtx_barcode;
208 
209  int next_genParticle_barcode = 1;
210  for(std::vector<HepMC::GenParticle*>::const_iterator iter = particles.begin(); iter != particles.end(); ++iter, ++next_genParticle_barcode)
211  while(!(*iter)->suggest_barcode(next_genParticle_barcode))
212  ++next_genParticle_barcode;
213 }
214 
216  const reco::GenParticleCollection& genParticles, double dRmax, int status,
217  const std::vector<int>* pdgIds, bool pdgIdStrict)
218 {
219  //---------------------------------------------------------------------------
220  // NOTE: this function has been copied from TauAnalysis/CandidateTools/src/candidateAuxFunctions.cc
221  // in order to avoid a package dependency of TauAnalysis/MCEmbedding on TauAnalysis/CandidateTools
222  //---------------------------------------------------------------------------
223 
224  bool bestMatchMatchesPdgId = false;
225  const reco::GenParticle* bestMatch = 0;
226 
227  for ( reco::GenParticleCollection::const_iterator genParticle = genParticles.begin();
228  genParticle != genParticles.end(); ++genParticle ) {
229  bool matchesPdgId = false;
230  if ( pdgIds ) {
231  for ( std::vector<int>::const_iterator pdgId = pdgIds->begin(); pdgId != pdgIds->end(); ++pdgId ) {
232  if ( genParticle->pdgId() == (*pdgId) ) {
233  matchesPdgId = true;
234  break;
235  }
236  }
237  }
238 
239  // If we require strict PDG id checking, skip it if it doesn't match
240  if ( pdgIds && !matchesPdgId && pdgIdStrict ) continue;
241 
242  // Check if status matches - if not, skip it.
243  bool statusMatch = (status == -1 || genParticle->status() == status);
244  if ( !statusMatch ) continue;
245 
246  double dR = reco::deltaR(direction, genParticle->p4());
247  if ( dR > dRmax ) continue;
248 
249  // Check if higher than current best match
250  bool higherEnergyThanBestMatch = (bestMatch) ?
251  (genParticle->energy() > bestMatch->energy()) : true;
252 
253  // Check if old bestMatch was not a prefered ID and the new one is.
254  if ( bestMatchMatchesPdgId ) {
255  // If the old one matches, only use the new one if it is a better
256  // energy match
257  if ( matchesPdgId && higherEnergyThanBestMatch ) bestMatch = &(*genParticle);
258  } else {
259  // The old one doesn't match the prefferred list if it is either
260  // a better energy match or better pdgId match
261  if ( higherEnergyThanBestMatch || matchesPdgId ) {
262  bestMatch = &(*genParticle);
263  if ( matchesPdgId ) bestMatchMatchesPdgId = true;
264  }
265  }
266  }
267 
268  return bestMatch;
269 }
270 
271 void compGenParticleP4afterRad(const reco::GenParticle* mother, reco::Candidate::LorentzVector& particleP4_afterRad, int absPdgId)
272 {
273  unsigned numDaughters = mother->numberOfDaughters();
274  for ( unsigned iDaughter = 0; iDaughter < numDaughters; ++iDaughter ) {
275  const reco::GenParticle* daughter = mother->daughterRef(iDaughter).get();
276 
277  compGenParticleP4afterRad(daughter, particleP4_afterRad, absPdgId);
278  }
279 
280  if ( abs(mother->pdgId()) == absPdgId ) {
281  if ( mother->energy() < particleP4_afterRad.energy() ) particleP4_afterRad = mother->p4();
282  }
283 }
284 
286 {
287  return compGenParticleP4afterRad(mother, muonP4_afterRad, 13);
288 }
289 
291 {
292  return compGenParticleP4afterRad(mother, tauP4_afterRad, 15);
293 }
294 
295 void findMuons(const edm::Event& evt, const edm::InputTag& src,
296  reco::Candidate::LorentzVector& genMuonPlusP4, bool& genMuonPlus_found,
297  reco::Candidate::LorentzVector& genMuonMinusP4, bool& genMuonMinus_found)
298 {
299  //std::cout << "<findMuons>:" << std::endl;
300  //std::cout << " src = " << src << std::endl;
301  typedef std::vector<reco::Particle> ParticleCollection;
303  evt.getByLabel(src, muons);
304  int idx = 0;
305  for ( ParticleCollection::const_iterator muon = muons->begin();
306  muon != muons->end(); ++muon ) {
307  //std::cout << "muon #" << idx << ": Pt = " << muon->pt() << ", eta = " << muon->eta() << ", phi = " << muon->phi() << std::endl;
308  if ( muon->charge() > +0.5 ) {
309  genMuonPlusP4 = muon->p4();
310  genMuonPlus_found = true;
311  }
312  if ( muon->charge() < -0.5 ) {
313  genMuonMinusP4 = muon->p4();
314  genMuonMinus_found = true;
315  }
316  ++idx;
317  }
318 }
319 
320 double getDeDxForPbWO4(double p)
321 {
322  static TGraph* dedxGraphPbWO4 = NULL;
323 
324  static const double E[] = { 1.0, 1.40, 2.0, 3.0, 4.0, 8.0, 10.0,
325  14.0, 20.0, 30.0, 40.0, 80.0, 100.0,
326  140.0, 169.0, 200.0, 300.0, 400.0, 800.0, 1000.0,
327  1400.0, 2000.0, 3000.0, 4000.0, 8000.0 };
328  static const double DEDX[] = { 1.385, 1.440, 1.500, 1.569, 1.618, 1.743, 1.788,
329  1.862, 1.957, 2.101, 2.239, 2.778, 3.052,
330  3.603, 4.018, 4.456, 5.876, 7.333, 13.283, 16.320,
331  22.382, 31.625, 47.007, 62.559, 125.149 }; // in units of MeV
332  static const unsigned int N_ENTRIES = sizeof(E)/sizeof(E[0]);
333 
334  if ( !dedxGraphPbWO4 ) dedxGraphPbWO4 = new TGraph(N_ENTRIES, E, DEDX);
335 
336  return dedxGraphPbWO4->Eval(p)*1.e-3; // convert to GeV
337 }
338 
def bestMatch
Definition: deltar.py:139
int chamber() const
Definition: CSCDetId.h:68
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
reco::CandidateBaseRef getTheMuMinus(const std::vector< reco::CandidateBaseRef > &)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
T perp() const
Definition: PV3DBase.h:72
daughters::value_type daughterRef(size_type i) const
reference to daughter at given position
bool matchMuonDetId(uint32_t, uint32_t)
virtual double energy() const final
energy
virtual double pt() const =0
transverse momentum
virtual double pz() const =0
z coordinate of momentum vector
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
#define NULL
Definition: scimark2.h:8
TrackDetMatchInfo getTrackDetMatchInfo(const edm::Event &, const edm::EventSetup &, TrackDetectorAssociator &, const TrackAssociatorParameters &, const reco::Candidate *)
void compGenTauP4afterRad(const reco::GenParticle *, reco::Candidate::LorentzVector &)
void compGenParticleP4afterRad(const reco::GenParticle *mother, reco::Candidate::LorentzVector &particleP4_afterRad, int absPdgId)
int layer() const
Definition: CSCDetId.h:61
virtual size_type numberOfDaughters() const
number of daughters
int endcap() const
Definition: CSCDetId.h:93
edm::View< reco::Candidate > CandidateView
virtual bool hasMasterClone() const =0
static const int CSC
Definition: MuonSubdetId.h:13
const Surface::PositionType & position() const
The position (origin of the R.F.)
Definition: GeomDet.h:48
int roll() const
Definition: RPCDetId.h:120
int ring() const
Definition: RPCDetId.h:72
double getDeDxForPbWO4(double)
virtual size_t numberOfDaughters() const
number of daughters
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
virtual const Point & vertex() const =0
vertex position
const reco::GenParticle * findGenParticleForMCEmbedding(const reco::Candidate::LorentzVector &, const reco::GenParticleCollection &, double, int, const std::vector< int > *, bool)
virtual int charge() const =0
electric charge
virtual double py() const =0
y coordinate of momentum vector
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:418
void compGenMuonP4afterRad(const reco::GenParticle *, reco::Candidate::LorentzVector &)
virtual double px() const =0
x coordinate of momentum vector
int ring() const
Definition: CSCDetId.h:75
int layer() const
Definition: RPCDetId.h:108
Definition: DetId.h:18
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
bool isNull() const
Checks for null.
Definition: RefToBase.h:328
const T & get() const
Definition: EventSetup.h:56
virtual const Candidate * daughter(size_type) const
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
int sector() const
Sector id: the group of chambers at same phi (and increasing r)
Definition: RPCDetId.h:102
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:37
void repairBarcodes(HepMC::GenEvent *)
std::string const & label() const
Definition: InputTag.h:36
T eta() const
Definition: PV3DBase.h:76
virtual int pdgId() const final
PDG identifier.
edm::RefToBase< Candidate > CandidateBaseRef
persistent reference to an object in a collection of Candidate objects
Definition: CandidateFwd.h:31
int subsector() const
SubSector id : some sectors are divided along the phi direction in subsectors (from 1 to 4 in Barrel...
Definition: RPCDetId.h:114
tuple muons
Definition: patZpeak.py:38
static const int RPC
Definition: MuonSubdetId.h:14
int sector() const
Definition: DTChamberId.h:61
void printMuonDetId(const edm::EventSetup &, uint32_t)
TrackDetMatchInfo associate(const edm::Event &, const edm::EventSetup &, const FreeTrajectoryState &, const AssociatorParameters &)
T get() const
get a component
Definition: Candidate.h:217
int station() const
Definition: CSCDetId.h:86
tuple cout
Definition: gather_cfg.py:145
static const int DT
Definition: MuonSubdetId.h:12
void findMuons(const edm::Event &, const edm::InputTag &, reco::Candidate::LorentzVector &, bool &, reco::Candidate::LorentzVector &, bool &)
std::vector< reco::CandidateBaseRef > getSelMuons(const edm::Event &, const edm::InputTag &)
int station() const
Return the station number.
Definition: DTChamberId.h:51
reco::CandidateBaseRef getTheMuPlus(const std::vector< reco::CandidateBaseRef > &)
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
virtual const LorentzVector & p4() const final
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
tuple status
Definition: mps_update.py:57
virtual TrackRef globalTrack() const
reference to Track reconstructed in both tracked and muon detector
Definition: Muon.h:54
std::vector< reco::Particle > ParticleCollection
int region() const
Region id: 0 for Barrel, +/-1 For +/- Endcap.
Definition: RPCDetId.h:63
int station() const
Definition: RPCDetId.h:96
virtual const CandidateBaseRef & masterClone() const =0