CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/QCDAnalysis/UEAnalysis/src/AnalysisRootpleProducerOnlyMC.cc

Go to the documentation of this file.
00001 // Authors: F. Ambroglini, L. Fano'
00002 #include <QCDAnalysis/UEAnalysis/interface/AnalysisRootpleProducerOnlyMC.h>
00003  
00004 using namespace edm;
00005 using namespace std;
00006 using namespace reco;
00007 
00008 class GreaterPt
00009 {
00010 public:
00011   bool operator()( const math::XYZTLorentzVector& a, const math::XYZTLorentzVector& b) 
00012   {
00013     return a.pt() > b.pt();
00014   }
00015 };
00016 
00017 class GenJetSort
00018 {
00019 public:
00020   bool operator()(const GenJet& a, const GenJet& b) 
00021   {
00022     return a.pt() > b.pt();
00023   }
00024 };
00025 
00026 
00027 void AnalysisRootpleProducerOnlyMC::store()
00028 {
00029   AnalysisTree->Fill();
00030 
00031   NumberMCParticles=0;
00032   NumberInclusiveJet=0;
00033   NumberChargedJet=0;
00034 }
00035 
00036 void AnalysisRootpleProducerOnlyMC::fillEventInfo(int e)
00037 {
00038   EventKind = e;
00039 }
00040 
00041 void AnalysisRootpleProducerOnlyMC::fillMCParticles(float p, float pt, float eta, float phi)
00042 {
00043   MomentumMC[NumberMCParticles]=p;
00044   TransverseMomentumMC[NumberMCParticles]=pt;
00045   EtaMC[NumberMCParticles]=eta;
00046   PhiMC[NumberMCParticles]=phi;
00047   NumberMCParticles++;
00048 }
00049 
00050 void AnalysisRootpleProducerOnlyMC::fillInclusiveJet(float p, float pt, float eta, float phi)
00051 {
00052   MomentumIJ[NumberInclusiveJet]=p;
00053   TransverseMomentumIJ[NumberInclusiveJet]=pt;
00054   EtaIJ[NumberInclusiveJet]=eta;
00055   PhiIJ[NumberInclusiveJet]=phi;
00056   NumberInclusiveJet++;
00057 }
00058 
00059 void AnalysisRootpleProducerOnlyMC::fillChargedJet(float p, float pt, float eta, float phi)
00060 {
00061   MomentumCJ[NumberChargedJet]=p;
00062   TransverseMomentumCJ[NumberChargedJet]=pt;
00063   EtaCJ[NumberChargedJet]=eta;
00064   PhiCJ[NumberChargedJet]=phi;
00065   NumberChargedJet++;
00066 }
00067 
00068 AnalysisRootpleProducerOnlyMC::AnalysisRootpleProducerOnlyMC( const ParameterSet& pset )
00069 {
00070   mcEvent = pset.getUntrackedParameter<InputTag>("MCEvent",std::string(""));
00071   genJetCollName = pset.getUntrackedParameter<InputTag>("GenJetCollectionName",std::string(""));
00072   chgJetCollName = pset.getUntrackedParameter<InputTag>("ChgGenJetCollectionName",std::string(""));
00073   chgGenPartCollName = pset.getUntrackedParameter<InputTag>("ChgGenPartCollectionName",std::string(""));
00074 
00075   piG = acos(-1.);
00076   NumberMCParticles=0;
00077   NumberInclusiveJet=0;
00078   NumberChargedJet=0;
00079 }
00080 
00081 
00082 void AnalysisRootpleProducerOnlyMC::beginJob()
00083 {
00084   // use TFileService for output to root file 
00085   AnalysisTree = fs->make<TTree>("AnalysisTree","MBUE Analysis Tree ");
00086  
00087   // process type
00088   AnalysisTree->Branch("EventKind",&EventKind,"EventKind/I");
00089   
00090   // store p, pt, eta, phi for particles and jets
00091 
00092   // GenParticles at hadron level
00093   AnalysisTree->Branch("NumberMCParticles",&NumberMCParticles,"NumberMCParticles/I");
00094   AnalysisTree->Branch("MomentumMC",MomentumMC,"MomentumMC[NumberMCParticles]/F");
00095   AnalysisTree->Branch("TransverseMomentumMC",TransverseMomentumMC,"TransverseMomentumMC[NumberMCParticles]/F");
00096   AnalysisTree->Branch("EtaMC",EtaMC,"EtaMC[NumberMCParticles]/F");
00097   AnalysisTree->Branch("PhiMC",PhiMC,"PhiMC[NumberMCParticles]/F");
00098 
00099   // GenJets
00100   AnalysisTree->Branch("NumberInclusiveJet",&NumberInclusiveJet,"NumberInclusiveJet/I");
00101   AnalysisTree->Branch("MomentumIJ",MomentumIJ,"MomentumIJ[NumberInclusiveJet]/F");
00102   AnalysisTree->Branch("TrasverseMomentumIJ",TransverseMomentumIJ,"TransverseMomentumIJ[NumberInclusiveJet]/F");
00103   AnalysisTree->Branch("EtaIJ",EtaIJ,"EtaIJ[NumberInclusiveJet]/F");
00104   AnalysisTree->Branch("PhiIJ",PhiIJ,"PhiIJ[NumberInclusiveJet]/F");
00105   
00106   // jets from charged GenParticles
00107   AnalysisTree->Branch("NumberChargedJet",&NumberChargedJet,"NumberChargedJet/I");
00108   AnalysisTree->Branch("MomentumCJ",MomentumCJ,"MomentumCJ[NumberChargedJet]/F");
00109   AnalysisTree->Branch("TrasverseMomentumCJ",TransverseMomentumCJ,"TransverseMomentumCJ[NumberChargedJet]/F");
00110   AnalysisTree->Branch("EtaCJ",EtaCJ,"EtaCJ[NumberChargedJet]/F");
00111   AnalysisTree->Branch("PhiCJ",PhiCJ,"PhiCJ[NumberChargedJet]/F");
00112   
00113 
00114   // alternative storage method:
00115   // save TClonesArrays of TLorentzVectors
00116   // i.e. store 4-vectors of particles and jets
00117 
00118   MonteCarlo = new TClonesArray("TLorentzVector", 10000);
00119   AnalysisTree->Branch("MonteCarlo", "TClonesArray", &MonteCarlo, 128000, 0);
00120   
00121   InclusiveJet = new TClonesArray("TLorentzVector", 10000);
00122   AnalysisTree->Branch("InclusiveJet", "TClonesArray", &InclusiveJet, 128000, 0);
00123 
00124   ChargedJet = new TClonesArray("TLorentzVector", 10000);
00125   AnalysisTree->Branch("ChargedJet", "TClonesArray", &ChargedJet, 128000, 0);
00126 }
00127 
00128   
00129 void AnalysisRootpleProducerOnlyMC::analyze( const Event& e, const EventSetup& )
00130 {
00131 
00132   e.getByLabel( mcEvent           , EvtHandle        ) ;
00133   e.getByLabel( chgGenPartCollName, CandHandleMC     );
00134   e.getByLabel( chgJetCollName    , ChgGenJetsHandle );
00135   e.getByLabel( genJetCollName    , GenJetsHandle    );
00136   
00137   const HepMC::GenEvent* Evt = EvtHandle->GetEvent() ;
00138   
00139   EventKind = Evt->signal_process_id();
00140   
00141   std::vector<math::XYZTLorentzVector> GenPart;
00142   std::vector<GenJet> ChgGenJetContainer;
00143   std::vector<GenJet> GenJetContainer;
00144   
00145   GenPart.clear();
00146   ChgGenJetContainer.clear();
00147   GenJetContainer.clear();
00148   
00149   ChargedJet->Clear();
00150   InclusiveJet->Clear();
00151   MonteCarlo->Clear();
00152 
00153   if (ChgGenJetsHandle->size()){
00154 
00155     for ( GenJetCollection::const_iterator it(ChgGenJetsHandle->begin()), itEnd(ChgGenJetsHandle->end());
00156           it!=itEnd; ++it)
00157       {
00158         ChgGenJetContainer.push_back(*it);
00159       }
00160     
00161     std::stable_sort(ChgGenJetContainer.begin(),ChgGenJetContainer.end(),GenJetSort());
00162     
00163     std::vector<GenJet>::const_iterator it(ChgGenJetContainer.begin()), itEnd(ChgGenJetContainer.end());
00164     for ( int iChargedJet(0); it != itEnd; ++it, ++iChargedJet)
00165       {
00166         fillChargedJet(it->p(),it->pt(),it->eta(),it->phi());
00167         new((*ChargedJet)[iChargedJet]) TLorentzVector(it->px(), it->py(), it->pz(), it->energy());
00168       }
00169   }
00170   
00171   if (GenJetsHandle->size()){
00172     
00173     for ( GenJetCollection::const_iterator it(GenJetsHandle->begin()), itEnd(GenJetsHandle->end());
00174           it!=itEnd; ++it )
00175       {
00176         GenJetContainer.push_back(*it);
00177       }
00178 
00179     std::stable_sort(GenJetContainer.begin(),GenJetContainer.end(),GenJetSort());
00180 
00181     std::vector<GenJet>::const_iterator it(GenJetContainer.begin()), itEnd(GenJetContainer.end()); 
00182     for ( int iInclusiveJet(0); it != itEnd; ++it, ++iInclusiveJet)
00183       {
00184         fillInclusiveJet(it->p(),it->pt(),it->eta(),it->phi());
00185         new((*InclusiveJet)[iInclusiveJet]) TLorentzVector(it->px(), it->py(), it->pz(), it->energy());
00186       }
00187   }
00188   
00189   if (CandHandleMC->size()){
00190 
00191     for (vector<GenParticle>::const_iterator it(CandHandleMC->begin()), itEnd(CandHandleMC->end());
00192          it != itEnd;it++)
00193       {
00194         GenPart.push_back(it->p4());
00195       }
00196 
00197     std::stable_sort(GenPart.begin(),GenPart.end(),GreaterPt());
00198 
00199     std::vector<math::XYZTLorentzVector>::const_iterator it(GenPart.begin()), itEnd(GenPart.end());
00200     for( int iMonteCarlo(0); it != itEnd; ++it, ++iMonteCarlo )
00201       {
00202         fillMCParticles(it->P(),it->Pt(),it->Eta(),it->Phi());
00203         new((*MonteCarlo)[iMonteCarlo]) TLorentzVector(it->Px(), it->Py(), it->Pz(), it->E()); 
00204       }
00205   }
00206 
00207   store();
00208 }
00209 
00210 void AnalysisRootpleProducerOnlyMC::endJob()
00211 {
00212 }
00213