CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BasicGenParticleValidation.cc
Go to the documentation of this file.
1 /*class BasicGenParticleValidation
2  *
3  * Class to fill dqm monitor elements from existing EDM file
4  *
5  */
6 
8 
9 #include "CLHEP/Units/defs.h"
10 #include "CLHEP/Units/PhysicalConstants.h"
11 
12 using namespace edm;
13 
15  wmanager_(iPSet,consumesCollector()),
16  hepmcCollection_(iPSet.getParameter<edm::InputTag>("hepmcCollection")),
17  genparticleCollection_(iPSet.getParameter<edm::InputTag>("genparticleCollection")),
18  genjetCollection_(iPSet.getParameter<edm::InputTag>("genjetsCollection")),
19  matchPr_(iPSet.getParameter<double>("matchingPrecision")),
20  verbosity_(iPSet.getUntrackedParameter<unsigned int>("verbosity",0))
21 {
22  dbe = 0;
24 
25  hepmcCollectionToken_=consumes<HepMCProduct>(hepmcCollection_);
26  genparticleCollectionToken_=consumes<reco::GenParticleCollection>(genparticleCollection_);
27  genjetCollectionToken_=consumes<reco::GenJetCollection>(genjetCollection_);
28 
29 }
30 
32 
34 {
35  if(dbe){
37  dbe->setCurrentFolder("Generator/GenParticles");
38 
40 
41  // Number of analyzed events
42  nEvt = dbe->book1D("nEvt", "n analyzed Events", 1, 0., 1.);
43 
45  genPMultiplicity = dbe->book1D("genPMultiplicty", "Log(No. all GenParticles)", 50, -1, 5); //Log
46  //difference in HepMC and reco multiplicity
47  genMatched = dbe->book1D("genMatched", "Difference reco - matched", 50, -25, 25);
48  //multiple matching
49  multipleMatching = dbe->book1D("multipleMatching", "multiple reco HepMC matching", 50, 0, 50);
50  //momentum difference of matched particles
51  matchedResolution = dbe->book1D("matchedResolution", "log10(momentum difference of matched particles)", 70, -10., -3.);
52 
53  // GenJet general distributions
54  genJetMult = dbe->book1D("genJetMult", "GenJet multiplicity", 50, 0, 50);
55  genJetEnergy = dbe->book1D("genJetEnergy", "Log10(GenJet energy)", 60, -1, 5);
56  genJetPt = dbe->book1D("genJetPt", "Log10(GenJet pt)", 60, -1, 5);
57  genJetEta = dbe->book1D("genJetEta", "GenJet eta", 220, -11, 11);
58  genJetPhi = dbe->book1D("genJetPhi", "GenJet phi", 360, -180, 180);
59  genJetDeltaEtaMin = dbe->book1D("genJetDeltaEtaMin", "GenJet minimum rapidity gap", 30, 0, 30);
60 
61  genJetPto1 = dbe->book1D("genJetPto1", "GenJet multiplicity above 1 GeV", 50, 0, 50);
62  genJetPto10 = dbe->book1D("genJetPto10", "GenJet multiplicity above 10 GeV", 50, 0, 50);
63  genJetPto100 = dbe->book1D("genJetPto100", "GenJet multiplicity above 100 GeV", 50, 0, 50);
64  genJetCentral = dbe->book1D("genJetCentral", "GenJet multiplicity |eta|.lt.2.5", 50, 0, 50);
65 
66  genJetTotPt = dbe->book1D("genJetTotPt", "Log10(GenJet total pt)", 100, -5, 5);
67 
68  }
69  return;
70 }
71 
74 {
76  iSetup.getData( fPDGTable );
77  return;
78 }
79 void BasicGenParticleValidation::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){return;}
81 {
82 
83  unsigned int initSize = 1000;
84 
87  iEvent.getByToken(hepmcCollectionToken_, evt);
88 
89  //Get HepMC EVENT
90  HepMC::GenEvent *myGenEvent = new HepMC::GenEvent(*(evt->GetEvent()));
91 
92  double weight = wmanager_.weight(iEvent);
93 
94  nEvt->Fill(0.5, weight);
95 
96  std::vector<const HepMC::GenParticle*> hepmcGPCollection;
97  std::vector<int> barcodeList;
98  hepmcGPCollection.reserve(initSize);
99  barcodeList.reserve(initSize);
100 
101  //Looping through HepMC::GenParticle collection to search for status 1 particles
102  for (HepMC::GenEvent::particle_const_iterator iter = myGenEvent->particles_begin(); iter != myGenEvent->particles_end(); ++iter){
103  if ( (*iter)->status() == 1) {
104  hepmcGPCollection.push_back(*iter);
105  barcodeList.push_back((*iter)->barcode());
106  if ( verbosity_ > 0 ) {
107  std::cout << "HepMC " << std::setw(14) << std::fixed << (*iter)->pdg_id() << std::setw(14) << std::fixed << (*iter)->momentum().px() << std::setw(14) << std::fixed
108  << (*iter)->momentum().py() << std::setw(14) << std::fixed << (*iter)->momentum().pz() << std::endl;
109  }
110  }
111  }
112 
113 
114  // Gather information on the reco::GenParticle collection
116  iEvent.getByToken(genparticleCollectionToken_, genParticles );
117 
118  std::vector<const reco::GenParticle*> particles;
119  particles.reserve(initSize);
120  for (reco::GenParticleCollection::const_iterator iter=genParticles->begin();iter!=genParticles->end();++iter){
121  if ( (*iter).status() == 1) {
122  particles.push_back(&*iter);
123  if ( verbosity_ > 0 ) {
124  std::cout << "reco " << std::setw(14) << std::fixed << (*iter).pdgId() << std::setw(14) << std::fixed << (*iter).px()
125  << std::setw(14) << std::fixed << (*iter).py() << std::setw(14) << std::fixed << (*iter).pz() << std::endl;
126  }
127  }
128  }
129 
130  unsigned int nReco = particles.size();
131  unsigned int nHepMC = hepmcGPCollection.size();
132 
133  genPMultiplicity->Fill(std::log10(nReco), weight);
134 
135  // Define vector containing index of hepmc corresponding to the reco::GenParticle
136  std::vector<int> hepmcMatchIndex;
137  hepmcMatchIndex.reserve(initSize);
138 
139  // Matching procedure
140 
141  // Check array size consistency
142 
143  if ( nReco != nHepMC ) {
144  edm::LogWarning("CollectionSizeInconsistency") << "Collection size inconsistency: HepMC::GenParticle = " << nHepMC << " reco::GenParticle = " << nReco;
145  }
146 
147  // Match each HepMC with a reco
148 
149  for ( unsigned int i = 0; i < nReco; ++i ){
150  for ( unsigned int j = 0; j < nHepMC; ++j ){
151  if ( matchParticles( hepmcGPCollection[j], particles[i] ) ) {
152  hepmcMatchIndex.push_back((int)j);
153  if ( hepmcGPCollection[j]->momentum().rho() != 0. ) {
154  double reso = 1.-particles[i]->p()/hepmcGPCollection[j]->momentum().rho();
155  if ( verbosity_ > 0 ) {
156  std::cout << "Matching momentum: reco = " << particles[i]->p() << " HepMC = "
157  << hepmcGPCollection[j]->momentum().rho() << " resoultion = " << reso << std::endl;
158  }
159  matchedResolution->Fill(std::log10(std::fabs(reso)),weight); }
160  continue;
161  }
162  }
163  }
164 
165  // Check unicity of matching
166 
167  unsigned int nMatched = hepmcMatchIndex.size();
168 
169  if ( nMatched != nReco ) {
170  edm::LogWarning("IncorrectMatching") << "Incorrect number of matched indexes: GenParticle = " << nReco << " matched indexes = " << nMatched;
171  }
172  genMatched->Fill(int(nReco-nMatched),weight);
173 
174  unsigned int nWrMatch = 0;
175 
176  for ( unsigned int i = 0; i < nMatched; ++i ){
177  for (unsigned int j = i+1; j < nMatched; ++j ){
178  if ( hepmcMatchIndex[i] == hepmcMatchIndex[j] ) {
179  int theIndex = hepmcMatchIndex[i];
180  edm::LogWarning("DuplicatedMatching") << "Multiple matching occurencies for GenParticle barcode = " << barcodeList[theIndex];
181  nWrMatch++;
182  }
183  }
184  }
185  multipleMatching->Fill(int(nWrMatch),weight);
186 
187  // Gather information in the GenJet collection
189  iEvent.getByToken(genjetCollectionToken_, genJets );
190 
191  int nJets = 0;
192  int nJetso1 = 0;
193  int nJetso10 = 0;
194  int nJetso100 = 0;
195  int nJetsCentral = 0;
196  double totPt = 0.;
197 
198  std::vector<double> jetEta;
199  jetEta.reserve(initSize);
200 
201  for (reco::GenJetCollection::const_iterator iter=genJets->begin();iter!=genJets->end();++iter){
202  nJets++;
203  double pt = (*iter).pt();
204  totPt += pt;
205  if (pt > 1.) nJetso1++;
206  if (pt > 10.) nJetso10++;
207  if (pt > 100.) nJetso100++;
208  double eta = (*iter).eta();
209  if ( std::fabs(eta) < 2.5 ) nJetsCentral++;
210  jetEta.push_back(eta);
211 
212  genJetEnergy->Fill(std::log10((*iter).energy()),weight);
213  genJetPt->Fill(std::log10(pt),weight);
214  genJetEta->Fill(eta,weight);
215  genJetPhi->Fill((*iter).phi()/CLHEP::degree,weight);
216  }
217 
218  genJetMult->Fill(nJets,weight);
219  genJetPto1->Fill(nJetso1,weight);
220  genJetPto10->Fill(nJetso10,weight);
221  genJetPto100->Fill(nJetso100,weight);
222  genJetCentral->Fill(nJetsCentral,weight);
223 
224  genJetTotPt->Fill(std::log10(totPt),weight);
225 
226  double deltaEta = 999.;
227  if ( jetEta.size() > 1 ) {
228  for (unsigned int i = 0; i < jetEta.size(); i++){
229  for (unsigned int j = i+1; j < jetEta.size(); j++){
230  deltaEta = std::min(deltaEta,std::fabs(jetEta[i]-jetEta[j]));
231  }
232  }
233  }
234 
235  genJetDeltaEtaMin->Fill(deltaEta,weight);
236 
237  delete myGenEvent;
238 }//analyze
239 
241 
242  bool state = false;
243 
244  if ( hepmcP->pdg_id() != recoP->pdgId() ) return state;
245  if ( std::fabs(hepmcP->momentum().px()-recoP->px()) < std::fabs(matchPr_*hepmcP->momentum().px()) &&
246  std::fabs(hepmcP->momentum().py()-recoP->py()) < std::fabs(matchPr_*hepmcP->momentum().py()) &&
247  std::fabs(hepmcP->momentum().pz()-recoP->pz()) < std::fabs(matchPr_*hepmcP->momentum().pz())) {
248  state = true; }
249 
250  return state;
251 
252 }
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
int i
Definition: DBlmapReader.cc:9
edm::ESHandle< HepPDT::ParticleDataTable > fPDGTable
PDT table.
DQMStore * dbe
ME&#39;s &quot;container&quot;.
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:872
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
virtual int pdgId() const GCC11_FINAL
PDG identifier.
virtual void analyze(const edm::Event &, const edm::EventSetup &)
Definition: DDAxes.h:10
bool matchParticles(const HepMC::GenParticle *&, const reco::GenParticle *&)
T eta() const
virtual double pz() const GCC11_FINAL
z coordinate of momentum vector
virtual double py() const GCC11_FINAL
y coordinate of momentum vector
void getData(T &iHolder) const
Definition: EventSetup.h:67
void Fill(long long x)
int iEvent
Definition: GenABIO.cc:243
edm::EDGetTokenT< reco::GenJetCollection > genjetCollectionToken_
virtual double px() const GCC11_FINAL
x coordinate of momentum vector
int j
Definition: DBlmapReader.cc:9
edm::EDGetTokenT< edm::HepMCProduct > hepmcCollectionToken_
tuple cout
Definition: gather_cfg.py:121
BasicGenParticleValidation(const edm::ParameterSet &)
int weight
Definition: histoStyle.py:50
double weight(const edm::Event &)
virtual void endRun(const edm::Run &, const edm::EventSetup &)
edm::EDGetTokenT< reco::GenParticleCollection > genparticleCollectionToken_
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:584
Definition: Run.h:41