test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuScleFitPlotter.cc
Go to the documentation of this file.
1 // \class MuScleFitPlotter
2 // Plotter for simulated,generated and reco info of muons
3 //
4 // \author C.Mariotti, S.Bolognesi - INFN Torino / T.Dorigo, M.De Mattia - INFN Padova
5 // revised S. Casasso, E. Migliore - UniTo & INFN Torino
6 //
7 // ----------------------------------------------------------------------------------
8 
9 #include "MuScleFitPlotter.h"
10 #include "Histograms.h"
11 #include "MuScleFitUtils.h"
16 //#include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
26 #include <CLHEP/Vector/LorentzVector.h>
27 
28 #include "TFile.h"
29 #include "TTree.h"
30 #include "TMinuit.h"
31 #include <vector>
32 
33 // Constructor
34 // ----------
36  outputFile = new TFile(theGenInfoRootFileName.c_str(),"RECREATE");
37  fillHistoMap();
38 }
39 
41  outputFile->cd();
42  writeHistoMap();
43  outputFile->Close();
44 }
45 
46 // Find and store in histograms the generated resonance and muons
47 // --------------------------------------------------------------
49 {
50  // bool prova = false;
51  //Loop on generated particles
52  std::pair<reco::Particle::LorentzVector,reco::Particle::LorentzVector> muFromRes;
54 
55  int mothersFound[] = {0, 0, 0, 0, 0, 0};
56 
57  for( reco::GenParticleCollection::const_iterator mcIter=genParticles->begin(); mcIter!=genParticles->end(); ++mcIter ) {
58  int status = mcIter->status();
59  int pdgId = std::abs(mcIter->pdgId());
60  //Check if it's a resonance
61  if( status == 2 &&
62  ( pdgId==23 || pdgId==443 || pdgId==100443 ||
63  pdgId==553 || pdgId==100553 || pdgId==200553 ) ) {
64  genRes = mcIter->p4();
65  // std::cout << "mother's mother = " << mcIter->mother()->pdgId() << std::endl;
66  if( pdgId == 23 ) mapHisto["hGenResZ"]->Fill(genRes);
67  else if( pdgId == 443 || pdgId == 100443 ) mapHisto["hGenResJPsi"]->Fill(genRes);
68  else if( pdgId == 553 || pdgId == 100553 || pdgId == 200553 ) mapHisto["hGenResUpsilon1S"]->Fill(genRes);
69  }
70  //Check if it's a muon from a resonance
71  if( status==1 && pdgId==13 && !PATmuons) {
72  int momPdgId = std::abs(mcIter->mother()->pdgId());
73  if( momPdgId==23 || momPdgId==443 || momPdgId==100443 ||
74  momPdgId==553 || momPdgId==100553 || momPdgId==200553 ) {
75  if( momPdgId == 23 ) mothersFound[0] = 1;
76  if( momPdgId == 443 || momPdgId == 100443 ) mothersFound[5] = 1;
77  if( momPdgId == 553 || momPdgId == 100553 || momPdgId == 200553 ) mothersFound[3] = 1;
78  mapHisto["hGenMu"]->Fill(mcIter->p4());
79  std::cout<<"genmu "<<mcIter->p4()<<std::endl;
80  if(mcIter->charge()>0){
81  muFromRes.first = mcIter->p4();
82  // prova = true;
83  }
84  else muFromRes.second = mcIter->p4();
85  }
86  }//if PATmuons you don't have the info of the mother !!! Here I assume is a JPsi
87  if( status==1 && pdgId==13 && PATmuons) {
88  mothersFound[5] = 1;
89  mapHisto["hGenMu"]->Fill(mcIter->p4());
90  std::cout<<"genmu "<<mcIter->p4()<<std::endl;
91  if(mcIter->charge()>0){
92  muFromRes.first = mcIter->p4();
93  // prova = true;
94  }
95  else muFromRes.second = mcIter->p4();
96  }
97  }
98  // if(!prova)
99  // std::cout<<"hgenmumu not found"<<std::endl;
100 
101  if( mothersFound[0] == 1 ) {
102  mapHisto["hGenMuMuZ"]->Fill(muFromRes.first+muFromRes.second);
103  mapHisto["hGenResVSMuZ"]->Fill( muFromRes.first, genRes, 1 );
104  mapHisto["hGenResVSMuZ"]->Fill( muFromRes.second,genRes, -1 );
105  }
106  if( mothersFound[3] == 1 ) {
107  mapHisto["hGenMuMuUpsilon1S"]->Fill(muFromRes.first+muFromRes.second);
108  mapHisto["hGenResVSMuUpsilon1S"]->Fill( muFromRes.first, genRes, 1 );
109  mapHisto["hGenResVSMuUpsilon1S"]->Fill( muFromRes.second,genRes, -1 );
110  }
111  if( mothersFound[5] == 1 ) {
112  mapHisto["hGenMuMuJPsi"]->Fill(muFromRes.first+muFromRes.second);
113  mapHisto["hGenResVSMuJPsi"]->Fill( muFromRes.first, genRes, 1 );
114  mapHisto["hGenResVSMuJPsi"]->Fill( muFromRes.second,genRes, -1 );
115  }
116 
117  mapHisto["hGenResVsSelf"]->Fill( genRes, genRes, 1 );
118 }
119 
120 // Find and store in histograms the generated resonance and muons
121 // --------------------------------------------------------------
122 void MuScleFitPlotter::fillGen(const edm::HepMCProduct* evtMC, bool sherpaFlag_)
123 {
124  //Loop on generated particles
125  const HepMC::GenEvent* Evt = evtMC->GetEvent();
126  std::pair<reco::Particle::LorentzVector,reco::Particle::LorentzVector> muFromRes;
128 
129  int mothersFound[] = {0, 0, 0, 0, 0, 0};
130 
131  if( sherpaFlag_ ) {
132 
133  for (HepMC::GenEvent::particle_const_iterator part=Evt->particles_begin();
134  part!=Evt->particles_end(); part++) {
135  if (fabs((*part)->pdg_id())==13 && (*part)->status()==1) {//looks for muon in the final state
136  bool fromRes = false;
137  for (HepMC::GenVertex::particle_iterator mother = (*part)->production_vertex()->particles_begin(HepMC::ancestors);//loops on the mother of the final state muons
138  mother != (*part)->production_vertex()->particles_end(HepMC::ancestors); ++mother) {
139  unsigned int motherPdgId = (*mother)->pdg_id();
140  if( motherPdgId == 13 && (*mother)->status() == 3 ) fromRes = true;
141  }
142  if(fromRes){
143  if((*part)->pdg_id()==13){
144  muFromRes.first = (lorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
145  (*part)->momentum().pz(),(*part)->momentum().e()));
146  }
147  else if((*part)->pdg_id()==-13){
148  muFromRes.second = (lorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
149  (*part)->momentum().pz(),(*part)->momentum().e()));
150  }
151  }
152  }
153 
154  }
155  mapHisto["hGenResZ"]->Fill(muFromRes.first+muFromRes.second);
156  }
157  else{
158  for (HepMC::GenEvent::particle_const_iterator part=Evt->particles_begin();
159  part!=Evt->particles_end(); part++) {
160  int status = (*part)->status();
161  int pdgId = std::abs((*part)->pdg_id());
162  //std::cout<<"PDG ID "<< (*part)->pdg_id() <<" status "<< (*part)->status()
163  //<<" pt "<<(*part)->momentum().perp()<< " eta "<<(*part)->momentum().eta()<<std::endl ;
164  //Check if it's a resonance
165  // For sherpa the resonance is not saved. The muons from the resonance can be identified
166  // by having as mother a muon of status 3.
167 
168  if (pdgId==13 && status==1) {
169  if( status==2 &&
170  ( pdgId==23 || pdgId==443 || pdgId==100443 ||
171  pdgId==553 || pdgId==100553 || pdgId==200553 ) ) {
172  genRes = reco::Particle::LorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
173  (*part)->momentum().pz(),(*part)->momentum().e());
174 
175  if( pdgId == 23 ) mapHisto["hGenResZ"]->Fill(genRes);
176  if( pdgId == 443 ) mapHisto["hGenResJPsi"]->Fill(genRes);
177  if( pdgId == 553 ) {
178  // std::cout << "genRes mass = " << CLHEP::HepLorentzVector(genRes.x(),genRes.y(),genRes.z(),genRes.t()).m() << std::endl;
179  mapHisto["hGenResUpsilon1S"]->Fill(genRes);
180  }
181  }
182 
183  //Check if it's a muon from a resonance
184  if (pdgId==13 && status==1) {
185  bool fromRes=false;
186  for (HepMC::GenVertex::particle_iterator mother =
187  (*part)->production_vertex()->particles_begin(HepMC::ancestors);
188  mother != (*part)->production_vertex()->particles_end(HepMC::ancestors); ++mother) {
189  int motherPdgId = (*mother)->pdg_id();
190  if (motherPdgId==23 || motherPdgId==443 || motherPdgId==100443 ||
191  motherPdgId==553 || motherPdgId==100553 || motherPdgId==200553) {
192  fromRes=true;
193  if( motherPdgId == 23 ) mothersFound[0] = 1;
194  if( motherPdgId == 443 ) mothersFound[3] = 1;
195  if( motherPdgId == 553 ) mothersFound[5] = 1;
196  }
197  }
198 
199  if(fromRes) {
200  mapHisto["hGenMu"]->Fill(reco::Particle::LorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
201  (*part)->momentum().pz(),(*part)->momentum().e()));
202  mapHisto["hGenMuVSEta"]->Fill(reco::Particle::LorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
203  (*part)->momentum().pz(),(*part)->momentum().e()));
204  if((*part)->pdg_id()==-13)
205  muFromRes.first = (reco::Particle::LorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
206  (*part)->momentum().pz(),(*part)->momentum().e()));
207  else
208  muFromRes.second = (reco::Particle::LorentzVector((*part)->momentum().px(),(*part)->momentum().py(),
209  (*part)->momentum().pz(),(*part)->momentum().e()));
210  }
211  }
212  }
213  }
214  }
215  if( mothersFound[0] == 1 ) {
216  mapHisto["hGenMuMuZ"]->Fill(muFromRes.first+muFromRes.second);
217  mapHisto["hGenResVSMuZ"]->Fill( muFromRes.first, genRes, 1 );
218  mapHisto["hGenResVSMuZ"]->Fill( muFromRes.second,genRes, -1 );
219  }
220  if( mothersFound[3] == 1 ) {
221  mapHisto["hGenMuMuUpsilon1S"]->Fill(muFromRes.first+muFromRes.second);
222  mapHisto["hGenResVSMuUpsilon1S"]->Fill( muFromRes.first, genRes, 1 );
223  mapHisto["hGenResVSMuUpsilon1S"]->Fill( muFromRes.second,genRes, -1 );
224  }
225  if( mothersFound[5] == 1 ) {
226  mapHisto["hGenMuMuJPsi"]->Fill(muFromRes.first+muFromRes.second);
227  mapHisto["hGenResVSMuJPsi"]->Fill( muFromRes.first, genRes, 1 );
228  mapHisto["hGenResVSMuJPsi"]->Fill( muFromRes.second,genRes, -1 );
229  }
230  mapHisto["hGenResVsSelf"]->Fill( genRes, genRes, 1 );
231 }
232 
233 // Find and store in histograms the simulated resonance and muons
234 // --------------------------------------------------------------
236 {
237  std::vector<SimTrack> simMuons;
238 
239  //Loop on simulated tracks
240  for( edm::SimTrackContainer::const_iterator simTrack=simTracks->begin(); simTrack!=simTracks->end(); ++simTrack ) {
241  // Select the muons from all the simulated tracks
242  if (fabs((*simTrack).type())==13) {
243  simMuons.push_back(*simTrack);
244  mapHisto["hSimMu"]->Fill((*simTrack).momentum());
245  }
246  }
247  mapHisto["hSimMu"]->Fill(simMuons.size());
248 
249  // Recombine all the possible Z from simulated muons
250  if( simMuons.size() >= 2 ) {
251  for( std::vector<SimTrack>::const_iterator imu=simMuons.begin(); imu != simMuons.end(); ++imu ) {
252  for( std::vector<SimTrack>::const_iterator imu2=imu+1; imu2!=simMuons.end(); ++imu2 ) {
253  if (imu==imu2) continue;
254 
255  // Try all the pairs with opposite charge
256  if (((*imu).charge()*(*imu2).charge())<0) {
257  reco::Particle::LorentzVector Z = (*imu).momentum()+(*imu2).momentum();
258  mapHisto["hSimMuPMuM"]->Fill(Z);
259  }
260  }
261  }
262 
263  // Plots for the best possible simulated resonance
264  std::pair<SimTrack,SimTrack> simMuFromBestRes = MuScleFitUtils::findBestSimuRes(simMuons);
265  reco::Particle::LorentzVector bestSimZ = (simMuFromBestRes.first).momentum()+(simMuFromBestRes.second).momentum();
266  mapHisto["hSimBestRes"]->Fill(bestSimZ);
267  if (fabs(simMuFromBestRes.first.momentum().eta())<2.5 && fabs(simMuFromBestRes.second.momentum().eta())<2.5 &&
268  simMuFromBestRes.first.momentum().pt()>2.5 && simMuFromBestRes.second.momentum().pt()>2.5) {
269  mapHisto["hSimBestResVSMu"]->Fill (simMuFromBestRes.first.momentum(), bestSimZ, int(simMuFromBestRes.first.charge()));
270  mapHisto["hSimBestResVSMu"]->Fill (simMuFromBestRes.second.momentum(),bestSimZ, int(simMuFromBestRes.second.charge()));
271  }
272  }
273 }
274 
275 // Find and store in histograms the RIGHT simulated resonance and muons
276 // --------------------------------------------------------------
278 {
279  std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> simMuFromRes =
280  MuScleFitUtils::findSimMuFromRes(evtMC,simTracks);
281  //Fill resonance info
282  reco::Particle::LorentzVector rightSimRes = (simMuFromRes.first)+(simMuFromRes.second);
283  mapHisto["hSimRightRes"]->Fill(rightSimRes);
284  /*if ((fabs(simMuFromRes.first.Eta())<2.5 && fabs(simMuFromRes.second.Eta())<2.5)
285  && simMuFromRes.first.Pt()>2.5 && simMuFromRes.second.Pt()>2.5) {
286  }*/
287 }
288 
289 // // Find and store in histograms the reconstructed resonance and muons
290 // // --------------------------------------------------------------
291 // void MuScleFitPlotter::fillRec(std::vector<reco::LeafCandidate>& muons)
292 // {
293 // for(std::vector<reco::LeafCandidate>::const_iterator mu1 = muons.begin(); mu1!=muons.end(); mu1++){
294 // mapHisto["hRecMu"]->Fill(mu1->p4());
295 // mapHisto["hRecMuVSEta"]->Fill(mu1->p4());
296 // for(std::vector<reco::LeafCandidate>::const_iterator mu2 = muons.begin(); mu2!=muons.end(); mu2++){
297 // if (mu1->charge()<0 || mu2->charge()>0)
298 // continue;
299 // reco::Particle::LorentzVector Res (mu1->p4()+mu2->p4());
300 // mapHisto["hRecMuPMuM"]->Fill(Res);
301 // }
302 // }
303 // mapHisto["hRecMu"]->Fill(muons.size());
304 // }
305 
307 void MuScleFitPlotter::fillRec(std::vector<MuScleFitMuon>& muons)
308 {
309  for(std::vector<MuScleFitMuon>::const_iterator mu1 = muons.begin(); mu1!=muons.end(); mu1++){
310  mapHisto["hRecMu"]->Fill(mu1->p4());
311  mapHisto["hRecMuVSEta"]->Fill(mu1->p4());
312  for(std::vector<MuScleFitMuon>::const_iterator mu2 = muons.begin(); mu2!=muons.end(); mu2++){
313  if (mu1->charge()<0 || mu2->charge()>0)
314  continue;
315  reco::Particle::LorentzVector Res (mu1->p4()+mu2->p4());
316  mapHisto["hRecMuPMuM"]->Fill(Res);
317  }
318  }
319  mapHisto["hRecMu"]->Fill(muons.size());
320 }
321 
322 
324 void MuScleFitPlotter::fillTreeRec( const std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> > & savedPairs )
325 {
326  std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> >::const_iterator muonPair = savedPairs.begin();
327  for( ; muonPair != savedPairs.end(); ++muonPair ) {
328  mapHisto["hRecMu"]->Fill(muonPair->first);
329  mapHisto["hRecMuVSEta"]->Fill(muonPair->first);
330  mapHisto["hRecMu"]->Fill(muonPair->second);
331  mapHisto["hRecMuVSEta"]->Fill(muonPair->second);
332  reco::Particle::LorentzVector Res( muonPair->first+muonPair->second );
333  mapHisto["hRecMuPMuM"]->Fill(Res);
334  mapHisto["hRecMu"]->Fill(savedPairs.size());
335  }
336 }
337 
343 void MuScleFitPlotter::fillTreeGen( const std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> > & genPairs )
344 {
345  std::vector<std::pair<reco::Particle::LorentzVector, reco::Particle::LorentzVector> >::const_iterator genPair = genPairs.begin();
346  for( ; genPair != genPairs.end(); ++genPair ) {
347  reco::Particle::LorentzVector genRes(genPair->first+genPair->second);
348  mapHisto["hGenResZ"]->Fill(genRes);
349  mapHisto["hGenMu"]->Fill(genPair->first);
350  mapHisto["hGenMuVSEta"]->Fill(genPair->first);
351  mapHisto["hGenMuMuZ"]->Fill(genRes);
352  mapHisto["hGenResVSMuZ"]->Fill( genPair->first, genRes, 1 );
353  mapHisto["hGenResVSMuZ"]->Fill( genPair->second, genRes, -1 );
354  mapHisto["hGenMuMuUpsilon1S"]->Fill(genRes);
355  mapHisto["hGenResVSMuUpsilon1S"]->Fill( genPair->first, genRes, 1 );
356  mapHisto["hGenResVSMuUpsilon1S"]->Fill( genPair->second, genRes, -1 );
357  mapHisto["hGenMuMuJPsi"]->Fill(genRes);
358  mapHisto["hGenResVSMuJPsi"]->Fill( genPair->first, genRes, 1 );
359  mapHisto["hGenResVSMuJPsi"]->Fill( genPair->second, genRes, -1 );
360  mapHisto["hGenResVsSelf"]->Fill( genRes, genRes, 1 );
361  }
362 }
363 
364 // Histogram booking
365 // -----------------
367 
368  // Generated Z and muons
369  // ---------------------
370  mapHisto["hGenResJPsi"] = new HParticle ("hGenResJPsi", 3., 3.1);
371  mapHisto["hGenResUpsilon1S"] = new HParticle ("hGenResUpsilon1S", 9., 11.);
372  mapHisto["hGenResZ"] = new HParticle ("hGenResZ", 60., 120.);
373  mapHisto["hGenMu"] = new HParticle ("hGenMu");
374  mapHisto["hGenMuVSEta"] = new HPartVSEta ("hGenMuVSEta");
375 
376  mapHisto["hGenMuMuJPsi"] = new HParticle ("hGenMuMuJPsi",3., 3.1 );
377  mapHisto["hGenResVSMuJPsi"] = new HMassVSPart ("hGenResVSMuJPsi",3., 3.1);
378  mapHisto["hGenMuMuUpsilon1S"] = new HParticle ("hGenMuMuUpsilon1S", 9., 11.);
379  mapHisto["hGenResVSMuUpsilon1S"] = new HMassVSPart ("hGenResVSMuUpsilon1S", 9., 11.);
380  mapHisto["hGenMuMuZ"] = new HParticle ("hGenMuMuZ", 60., 120.);
381  mapHisto["hGenResVSMuZ"] = new HMassVSPart ("hGenResVSMuZ", 60., 120.);
382 
383  mapHisto["hGenResVsSelf"] = new HMassVSPart ("hGenResVsSelf");
384 
385  // Simulated resonance and muons
386  // -----------------------------
387  mapHisto["hSimMu"] = new HParticle ("hSimMu");
388 
389  mapHisto["hSimMuPMuM"] = new HParticle ("hSimMuPMuM");
390 
391  mapHisto["hSimBestMu"] = new HParticle ("hSimBestMu");
392  mapHisto["hSimBestRes"] = new HParticle ("hSimBestRes");
393  mapHisto["hSimBestResVSMu"] = new HMassVSPart ("hSimBestResVSMu");
394 
395  mapHisto["hSimRightRes"] = new HParticle ("hSimRightZ");
396 
397  // Reconstructed resonance and muons
398  // -----------------------------
399  mapHisto["hRecMu"] = new HParticle ("hRecMu");
400  mapHisto["hRecMuVSEta"] = new HPartVSEta ("hRecMuVSEta");
401  mapHisto["hRecMuPMuM"] = new HParticle ("hRecMuPMuM");
402 }
403 
404 
405 // Histogram saving
406 // -----------------
408  outputFile->cd();
409  for (std::map<std::string, Histograms*>::const_iterator histo=mapHisto.begin();
410  histo!=mapHisto.end(); histo++) {
411  (*histo).second->Write();
412  }
413 }
414 
const double Z[kNumberCalorimeter]
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
std::map< std::string, Histograms * > mapHisto
static std::pair< lorentzVector, lorentzVector > findSimMuFromRes(const edm::Handle< edm::HepMCProduct > &evtMC, const edm::Handle< edm::SimTrackContainer > &simTracks)
void fillGenSim(edm::Handle< edm::HepMCProduct > evtMC, edm::Handle< edm::SimTrackContainer > simTracks)
void fillTreeRec(const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &savedPairs)
Used when running on the root tree containing preselected muon pairs.
virtual ~MuScleFitPlotter()
MuScleFitPlotter(std::string)
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
static std::pair< SimTrack, SimTrack > findBestSimuRes(const std::vector< SimTrack > &simMuons)
void fillRec(std::vector< MuScleFitMuon > &muons)
Used when running on the root tree containing preselected muon pairs.
void fillSim(edm::Handle< edm::SimTrackContainer > simTracks)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:35
part
Definition: HCALResponse.h:20
void fillTreeGen(const std::vector< std::pair< reco::Particle::LorentzVector, reco::Particle::LorentzVector > > &genPairs)
tuple muons
Definition: patZpeak.py:38
void fillGen(const reco::GenParticleCollection *genParticles, bool=false)
tuple cout
Definition: gather_cfg.py:121
tuple status
Definition: ntuplemaker.py:245
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21