CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/Validation/EventGenerator/plugins/BasicGenParticleValidation.cc

Go to the documentation of this file.
00001 /*class BasicGenParticleValidation
00002  *  
00003  *  Class to fill dqm monitor elements from existing EDM file
00004  *
00005  *  $Date: 2010/07/02 13:34:23 $
00006  *  $Revision: 1.2 $
00007  */
00008  
00009 #include "Validation/EventGenerator/interface/BasicGenParticleValidation.h"
00010 
00011 #include "CLHEP/Units/defs.h"
00012 #include "CLHEP/Units/PhysicalConstants.h"
00013 
00014 using namespace edm;
00015 
00016 BasicGenParticleValidation::BasicGenParticleValidation(const edm::ParameterSet& iPSet): 
00017   _wmanager(iPSet),
00018   hepmcCollection_(iPSet.getParameter<edm::InputTag>("hepmcCollection")),
00019   genparticleCollection_(iPSet.getParameter<edm::InputTag>("genparticleCollection")),
00020   genjetCollection_(iPSet.getParameter<edm::InputTag>("genjetsCollection")),
00021   matchPr_(iPSet.getParameter<double>("matchingPrecision")),
00022   verbosity_(iPSet.getUntrackedParameter<unsigned int>("verbosity",0))
00023 {    
00024   dbe = 0;
00025   dbe = edm::Service<DQMStore>().operator->();
00026 }
00027 
00028 BasicGenParticleValidation::~BasicGenParticleValidation() {}
00029 
00030 void BasicGenParticleValidation::beginJob()
00031 {
00032   if(dbe){
00034         dbe->setCurrentFolder("Generator/GenParticles");
00035         
00037     
00038     // Number of analyzed events
00039     nEvt = dbe->book1D("nEvt", "n analyzed Events", 1, 0., 1.);
00040 
00042         genPMultiplicity = dbe->book1D("genPMultiplicty", "Log(No. all GenParticles)", 50, -1, 5); //Log
00043     //difference in HepMC and reco multiplicity
00044     genMatched = dbe->book1D("genMatched", "Difference reco - matched", 50, -25, 25);
00045     //multiple matching
00046     multipleMatching = dbe->book1D("multipleMatching", "multiple reco HepMC matching", 50, 0, 50);
00047     //momentum difference of matched particles
00048     matchedResolution = dbe->book1D("matchedResolution", "log10(momentum difference of matched particles)", 70, -10., -3.);
00049 
00050     // GenJet general distributions
00051     genJetMult = dbe->book1D("genJetMult", "GenJet multiplicity", 50, 0, 50);
00052     genJetEnergy = dbe->book1D("genJetEnergy", "Log10(GenJet energy)", 60, -1, 5);
00053     genJetPt = dbe->book1D("genJetPt", "Log10(GenJet pt)", 60, -1, 5);
00054     genJetEta = dbe->book1D("genJetEta", "GenJet eta", 220, -11, 11);
00055     genJetPhi = dbe->book1D("genJetPhi", "GenJet phi", 360, -180, 180);
00056     genJetDeltaEtaMin = dbe->book1D("genJetDeltaEtaMin", "GenJet minimum rapidity gap", 30, 0, 30);
00057     
00058     genJetPto1 = dbe->book1D("genJetPto1", "GenJet multiplicity above 1 GeV", 50, 0, 50);
00059     genJetPto10 = dbe->book1D("genJetPto10", "GenJet multiplicity above 10 GeV", 50, 0, 50);
00060     genJetPto100 = dbe->book1D("genJetPto100", "GenJet multiplicity above 100 GeV", 50, 0, 50);
00061     genJetCentral = dbe->book1D("genJetCentral", "GenJet multiplicity |eta|.lt.2.5", 50, 0, 50);
00062 
00063     genJetTotPt = dbe->book1D("genJetTotPt", "Log10(GenJet total pt)", 100, -5, 5);
00064 
00065   }
00066   return;
00067 }
00068 
00069 void BasicGenParticleValidation::endJob(){return;}
00070 void BasicGenParticleValidation::beginRun(const edm::Run& iRun,const edm::EventSetup& iSetup)
00071 {
00073   iSetup.getData( fPDGTable );
00074   return;
00075 }
00076 void BasicGenParticleValidation::endRun(const edm::Run& iRun,const edm::EventSetup& iSetup){return;}
00077 void BasicGenParticleValidation::analyze(const edm::Event& iEvent,const edm::EventSetup& iSetup)
00078 { 
00079 
00080   unsigned int initSize = 1000;
00081 
00083   edm::Handle<HepMCProduct> evt;
00084   iEvent.getByLabel(hepmcCollection_, evt);
00085 
00086   //Get HepMC EVENT
00087   HepMC::GenEvent *myGenEvent = new HepMC::GenEvent(*(evt->GetEvent()));
00088 
00089   double weight = _wmanager.weight(iEvent);
00090 
00091   nEvt->Fill(0.5, weight);
00092 
00093   std::vector<const HepMC::GenParticle*> hepmcGPCollection;
00094   std::vector<int> barcodeList;
00095   hepmcGPCollection.reserve(initSize);
00096   barcodeList.reserve(initSize);
00097 
00098   //Looping through HepMC::GenParticle collection to search for status 1 particles
00099   for (HepMC::GenEvent::particle_const_iterator iter = myGenEvent->particles_begin(); iter != myGenEvent->particles_end(); ++iter){
00100     if ( (*iter)->status() == 1) {
00101       hepmcGPCollection.push_back(*iter);
00102       barcodeList.push_back((*iter)->barcode());
00103       if ( verbosity_ > 0 ) {
00104         std::cout << "HepMC " << std::setw(14) << std::fixed << (*iter)->pdg_id() << std::setw(14) << std::fixed << (*iter)->momentum().px() << std::setw(14) << std::fixed 
00105                   << (*iter)->momentum().py() << std::setw(14) << std::fixed << (*iter)->momentum().pz() << std::endl;
00106       }
00107     }
00108   }
00109   
00110 
00111   // Gather information on the reco::GenParticle collection
00112   edm::Handle<reco::GenParticleCollection> genParticles;
00113   iEvent.getByLabel(genparticleCollection_, genParticles );
00114   
00115   std::vector<const reco::GenParticle*> particles;
00116   particles.reserve(initSize);
00117   for (reco::GenParticleCollection::const_iterator iter=genParticles->begin();iter!=genParticles->end();++iter){
00118     if ( (*iter).status() == 1) { 
00119       particles.push_back(&*iter); 
00120       if ( verbosity_ > 0 ) {
00121         std::cout << "reco  " << std::setw(14) << std::fixed << (*iter).pdgId() << std::setw(14) << std::fixed << (*iter).px() 
00122                   << std::setw(14) << std::fixed << (*iter).py() << std::setw(14) << std::fixed << (*iter).pz() << std::endl;
00123       }
00124     }
00125   }
00126 
00127   unsigned int nReco = particles.size();
00128   unsigned int nHepMC = hepmcGPCollection.size();
00129 
00130   genPMultiplicity->Fill(std::log10(nReco), weight);
00131 
00132   // Define vector containing index of hepmc corresponding to the reco::GenParticle
00133   std::vector<int> hepmcMatchIndex;
00134   hepmcMatchIndex.reserve(initSize);
00135 
00136   // Matching procedure
00137 
00138   // Check array size consistency
00139 
00140   if ( nReco != nHepMC ) {
00141     edm::LogWarning("CollectionSizeInconsistency") << "Collection size inconsistency: HepMC::GenParticle = " << nHepMC << " reco::GenParticle = " << nReco;
00142   }
00143 
00144   // Match each HepMC with a reco
00145 
00146   for ( unsigned int i = 0; i < nReco; ++i ){
00147     for ( unsigned int j = 0; j < nHepMC; ++j ){
00148       if ( matchParticles( hepmcGPCollection[j], particles[i] ) ) { 
00149         hepmcMatchIndex.push_back((int)j); 
00150         if ( hepmcGPCollection[j]->momentum().rho() != 0. ) { 
00151           double reso = 1.-particles[i]->p()/hepmcGPCollection[j]->momentum().rho();
00152           if ( verbosity_ > 0 ) { 
00153             std::cout << "Matching momentum: reco = " << particles[i]->p() << " HepMC = " 
00154                       << hepmcGPCollection[j]->momentum().rho() << " resoultion = " << reso << std::endl;
00155           }
00156           matchedResolution->Fill(std::log10(std::fabs(reso)),weight); }
00157         continue; 
00158       }
00159     }
00160   }
00161 
00162   // Check unicity of matching
00163 
00164   unsigned int nMatched = hepmcMatchIndex.size();
00165   
00166   if ( nMatched != nReco ) {
00167     edm::LogWarning("IncorrectMatching") << "Incorrect number of matched indexes: GenParticle = " << nReco << " matched indexes = " << nMatched;
00168   }
00169   genMatched->Fill(int(nReco-nMatched),weight);
00170 
00171   unsigned int nWrMatch = 0;
00172 
00173   for ( unsigned int i = 0; i < nMatched; ++i ){
00174     for (unsigned int j = i+1; j < nMatched; ++j ){
00175       if ( hepmcMatchIndex[i] == hepmcMatchIndex[j] ) {
00176         int theIndex = hepmcMatchIndex[i];
00177         edm::LogWarning("DuplicatedMatching") << "Multiple matching occurencies for GenParticle barcode = " << barcodeList[theIndex];
00178         nWrMatch++;
00179       }
00180     }
00181   }
00182   multipleMatching->Fill(int(nWrMatch),weight);
00183 
00184   // Gather information in the GenJet collection
00185   edm::Handle<reco::GenJetCollection> genJets;
00186   iEvent.getByLabel(genjetCollection_, genJets );
00187 
00188   int nJets = 0;
00189   int nJetso1 = 0;
00190   int nJetso10 = 0;
00191   int nJetso100 = 0;
00192   int nJetsCentral = 0;
00193   double totPt = 0.;
00194   
00195   std::vector<double> jetEta;
00196   jetEta.reserve(initSize);
00197 
00198   for (reco::GenJetCollection::const_iterator iter=genJets->begin();iter!=genJets->end();++iter){
00199     nJets++;
00200     double pt = (*iter).pt();
00201     totPt += pt;
00202     if (pt > 1.) nJetso1++;
00203     if (pt > 10.) nJetso10++;
00204     if (pt > 100.) nJetso100++;
00205     double eta = (*iter).eta();
00206     if ( std::fabs(eta) < 2.5 ) nJetsCentral++;
00207     jetEta.push_back(eta);
00208 
00209     genJetEnergy->Fill(std::log10((*iter).energy()),weight);
00210     genJetPt->Fill(std::log10(pt),weight);
00211     genJetEta->Fill(eta,weight);
00212     genJetPhi->Fill((*iter).phi()/CLHEP::degree,weight);
00213   }
00214 
00215   genJetMult->Fill(nJets,weight);
00216   genJetPto1->Fill(nJetso1,weight);
00217   genJetPto10->Fill(nJetso10,weight);
00218   genJetPto100->Fill(nJetso100,weight);
00219   genJetCentral->Fill(nJetsCentral,weight);
00220 
00221   genJetTotPt->Fill(std::log10(totPt),weight);
00222 
00223   double deltaEta = 999.;
00224   if ( jetEta.size() > 1 ) {
00225     for (unsigned int i = 0; i < jetEta.size(); i++){
00226       for (unsigned int j = i+1; j < jetEta.size(); j++){
00227         deltaEta = std::min(deltaEta,std::fabs(jetEta[i]-jetEta[j]));
00228       }
00229     }
00230   }
00231 
00232   genJetDeltaEtaMin->Fill(deltaEta,weight);
00233 
00234   delete myGenEvent;
00235 }//analyze
00236 
00237 bool BasicGenParticleValidation::matchParticles(const HepMC::GenParticle*& hepmcP, const reco::GenParticle*& recoP){
00238 
00239   bool state = false;
00240 
00241   if ( hepmcP->pdg_id() != recoP->pdgId() ) return state;
00242   if ( std::fabs(hepmcP->momentum().px()-recoP->px()) < std::fabs(matchPr_*hepmcP->momentum().px()) && 
00243        std::fabs(hepmcP->momentum().py()-recoP->py()) < std::fabs(matchPr_*hepmcP->momentum().py()) && 
00244        std::fabs(hepmcP->momentum().pz()-recoP->pz()) < std::fabs(matchPr_*hepmcP->momentum().pz())) {
00245     state = true; }
00246 
00247   return state;
00248 
00249 }