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