CMS 3D CMS Logo

GammaJetAnalysis.cc

Go to the documentation of this file.
00001 // user include files
00002 #include "GammaJetAnalysis.h" 
00003 
00004 #include "FWCore/Framework/interface/Frameworkfwd.h"
00005 #include "FWCore/Framework/interface/EDAnalyzer.h"
00006 #include "FWCore/Framework/interface/Event.h" 
00007 /* #include "FWCore/Framework/interface/MakerMacros.h" */
00008 #include "FWCore/Framework/interface/ESHandle.h" 
00009 #include "FWCore/Framework/interface/EventSetup.h" 
00010 
00011 #include "DataFormats/Common/interface/Ref.h"
00012 #include "DataFormats/DetId/interface/DetId.h" 
00013 
00014 #include "Geometry/Records/interface/CaloGeometryRecord.h" 
00015 #include "Geometry/CaloGeometry/interface/CaloGeometry.h" 
00016 #include "DataFormats/GeometryVector/interface/GlobalPoint.h" 
00017 #include "DataFormats/CaloTowers/interface/CaloTowerCollection.h" 
00018 #include "DataFormats/CaloTowers/interface/CaloTowerDetId.h" 
00019 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h" 
00020 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h" 
00021 #include "DataFormats/JetReco/interface/CaloJetCollection.h" 
00022 #include "DataFormats/JetReco/interface/CaloJet.h" 
00023 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00024 //#include "DataFormats/JetReco/interface/GenJetfwd.h"
00025 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
00026 #include "DataFormats/JetReco/interface/GenJet.h"
00027 
00028 #include "DataFormats/JetReco/interface/CaloJetCollection.h"
00029 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00030 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00031 #include "DataFormats/EgammaReco/interface/ClusterShape.h"
00032 
00033 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
00034 #include "DataFormats/Provenance/interface/Provenance.h"
00035 #include "HepMC/GenParticle.h"
00036 #include "HepMC/GenVertex.h"
00037 #include "TFile.h"
00038 #include "TTree.h"
00039 
00040 
00041 using namespace std;
00042 namespace cms
00043 {
00044 GammaJetAnalysis::GammaJetAnalysis(const edm::ParameterSet& iConfig)
00045 {
00046   // get names of modules, producing object collections
00047 
00048   nameProd_ = iConfig.getUntrackedParameter<std::string>("nameProd");
00049   jetCalo_ = iConfig.getUntrackedParameter<std::string>("jetCalo","GammaJetJetBackToBackCollection");
00050   gammaClus_ = iConfig.getUntrackedParameter<std::string>("gammaClus","GammaJetGammaBackToBackCollection");
00051   ecalInput_=iConfig.getUntrackedParameter<std::string>("ecalInput","GammaJetEcalRecHitCollection");
00052   hbheInput_ = iConfig.getUntrackedParameter<std::string>("hbheInput");
00053   hoInput_ = iConfig.getUntrackedParameter<std::string>("hoInput");
00054   hfInput_ = iConfig.getUntrackedParameter<std::string>("hfInput");
00055   Tracks_ = iConfig.getUntrackedParameter<std::string>("Tracks","GammaJetTracksCollection");
00056   CutOnEgammaEnergy_  = iConfig.getParameter<double>("CutOnEgammaEnergy");
00057 
00058   myName = iConfig.getParameter<std::string> ("textout");
00059   useMC = iConfig.getParameter<bool>("useMCInfo"); 
00060   allowMissingInputs_=iConfig.getUntrackedParameter<bool>("AllowMissingInputs",false);
00061   // get name of output file with histogramms
00062   fOutputFileName = iConfig.getUntrackedParameter<string>("HistOutFile"); 
00063   risol[0] = 0.5;
00064   risol[1] = 0.7;
00065   risol[2] = 1.0;
00066   
00067   ecut[0][0] = 0.09;
00068   ecut[0][1] = 0.18;
00069   ecut[0][2] = 0.27;
00070   
00071   ecut[1][0] = 0.45;
00072   ecut[1][1] = 0.9;
00073   ecut[1][2] = 1.35;
00074   
00075   ecut[2][0] = 0.5;
00076   ecut[2][1] = 1.;
00077   ecut[2][2] = 1.5;
00078 }
00079 
00080 
00081 GammaJetAnalysis::~GammaJetAnalysis()
00082 {
00083  
00084    // do anything here that needs to be done at desctruction time
00085    // (e.g. close files, deallocate resources etc.)
00086 
00087 }
00088 
00089 void GammaJetAnalysis::beginJob( const edm::EventSetup& iSetup)
00090 {
00091    hOutputFile   = new TFile( fOutputFileName.c_str(), "RECREATE" ) ; 
00092    myTree = new TTree("GammaJet","GammaJet Tree");
00093    myTree->Branch("run",  &run, "run/I");
00094    myTree->Branch("event",  &event, "event/I");
00095    
00096    NumRecoJets = 0;
00097    NumGenJets = 0;
00098    NumRecoGamma = 0;
00099    NumRecoTrack = 0;
00100    NumPart = 0;
00101 // Jet block
00102    myTree->Branch("NumRecoJets", &NumRecoJets, "NumRecoJets/I");   
00103    myTree->Branch("JetRecoEt",  JetRecoEt, "JetRecoEt[10]/F");
00104    myTree->Branch("JetRecoEta",  JetRecoEta, "JetRecoEta[10]/F");
00105    myTree->Branch("JetRecoPhi",  JetRecoPhi, "JetRecoPhi[10]/F");
00106    myTree->Branch("JetRecoType",  JetRecoType, "JetRecoType[10]/F");
00107    
00108 // Gamma block for ECAL isolated gammas
00109    myTree->Branch("NumRecoGamma", &NumRecoGamma, "NumRecoGamma/I");
00110    myTree->Branch("EcalClusDet", &EcalClusDet, "EcalClusDet[20]/I");
00111    myTree->Branch("GammaRecoEt",  GammaRecoEt, "GammaRecoEt[20]/F");
00112    myTree->Branch("GammaRecoEta",  GammaRecoEta, "GammaRecoEta[20]/F");
00113    myTree->Branch("GammaRecoPhi",  GammaRecoPhi, "GammaRecoPhi[20]/F");
00114    myTree->Branch("GammaIsoEcal",  GammaIsoEcal, "GammaIsoEcal[9][20]/F");
00115 
00116 // Tracks block
00117    myTree->Branch("NumRecoTrack", &NumRecoTrack, "NumRecoTrack/I");
00118    myTree->Branch("TrackRecoEt",  TrackRecoEt, "TrackRecoEt[200]/F");
00119    myTree->Branch("TrackRecoEta",  TrackRecoEta, "TrackRecoEta[200]/F");
00120    myTree->Branch("TrackRecoPhi",  TrackRecoPhi, "TrackRecoPhi[200]/F");
00121 
00122 // end of tree declaration
00123 
00124    edm::ESHandle<CaloGeometry> pG;
00125    iSetup.get<CaloGeometryRecord>().get(pG);
00126    geo = pG.product();
00127 
00128   myout_part = new ofstream((myName+"_part.dat").c_str()); 
00129   if(!myout_part) cout << " Output file not open!!! "<<endl;
00130   myout_hcal = new ofstream((myName+"_hcal.dat").c_str()); 
00131   if(!myout_hcal) cout << " Output file not open!!! "<<endl;
00132   myout_ecal = new ofstream((myName+"_ecal.dat").c_str()); 
00133   if(!myout_ecal) cout << " Output file not open!!! "<<endl;
00134   
00135   myout_jet = new ofstream((myName+"_jet.dat").c_str()); 
00136   if(!myout_jet) cout << " Output file not open!!! "<<endl;
00137   myout_photon = new ofstream((myName+"_photon.dat").c_str()); 
00138   if(!myout_photon) cout << " Output file not open!!! "<<endl;
00139    
00140    
00141 }
00142 
00143 void GammaJetAnalysis::endJob()
00144 {
00145 
00146    cout << "===== Start writing user histograms =====" << endl;
00147    hOutputFile->SetCompressionLevel(2);
00148    hOutputFile->cd();
00149    myTree->Write();
00150    hOutputFile->Close() ;
00151    cout << "===== End writing user histograms =======" << endl; 
00152 }
00153 
00154 
00155 //
00156 // member functions
00157 //
00158 
00159 // ------------ method called to produce the data  ------------
00160 void
00161 GammaJetAnalysis::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00162 {
00163   using namespace edm;
00164   std::vector<Provenance const*> theProvenance;
00165   iEvent.getAllProvenance(theProvenance);
00166 //  for( std::vector<Provenance const*>::const_iterator ip = theProvenance.begin();
00167 //                                                      ip != theProvenance.end(); ip++)
00168 //  {
00169 //     cout<<" Print all module/label names "<<(**ip).moduleName()<<" "<<(**ip).moduleLabel()<<
00170 //     " "<<(**ip).productInstanceName()<<endl;
00171 //  }
00172 // Load generator information
00173 // write HEPEVT block into file
00174    run = iEvent.id().run();
00175    event = iEvent.id().event();
00176   (*myout_part)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00177   (*myout_jet)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00178   (*myout_hcal)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00179   (*myout_ecal)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00180   (*myout_photon)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00181   
00182 
00183     std::vector<edm::InputTag>::const_iterator ic;
00184     int jettype = 0;
00185     int jetexist = -100;
00186     int reco = 1;
00187     double etlost = -100.1;
00188     
00189     NumRecoJets = 0;
00190     
00191      try {
00192        
00193        edm::Handle<reco::CaloJetCollection> jets;
00194        iEvent.getByLabel(nameProd_, jetCalo_, jets);
00195        reco::CaloJetCollection::const_iterator jet = jets->begin ();
00196        cout<<" Size of Calo jets "<<jets->size()<<endl;
00197        jettype++;
00198        
00199        if(jets->size() > 0 )
00200        {
00201          int ij = 0;
00202          for (; jet != jets->end (); jet++) 
00203          {
00204             cout<<" Jet et "<<(*jet).et()<<" "<<(*jet).eta()<<" "<<(*jet).phi()<<endl;
00205             ij++;
00206             if(ij<4) (*myout_jet)<<jettype<<" "<<reco<<" "<<ij<<" "<<(*jet).et()<<" "<<(*jet).eta()<<" "<<(*jet).phi()
00207             <<" "<<iEvent.id().event()<<endl;
00208             jetexist = ij;
00209             if( NumRecoJets < 8 )
00210             {
00211              JetRecoEt[NumRecoJets] = (*jet).et();
00212              JetRecoEta[NumRecoJets] = (*jet).eta();
00213              JetRecoPhi[NumRecoJets] = (*jet).phi();
00214              JetRecoType[NumRecoJets] = jettype;
00215              NumRecoJets++;
00216             }
00217          }
00218        }
00219      } catch (cms::Exception& e) { // can't find it!
00220        if (!allowMissingInputs_) {
00221          cout<< " Calojets are missed "<<endl;
00222          throw e;
00223         }        
00224      }
00225    
00226      cout<<" We filled CaloJet part "<<jetexist<<endl;
00227      
00228      if( jetexist < 0 ) (*myout_jet)<<jetexist<<" "<<reco<<" "<<etlost
00229                          <<" "<<etlost<<" "<<etlost
00230                          <<" "<<iEvent.id().event()<<endl;
00231 // Load EcalRecHits
00232   
00233     std::vector<edm::InputTag>::const_iterator i;
00234     vector<CaloRecHit> theRecHits;
00235       
00236     try {
00237       
00238       edm::Handle<EcalRecHitCollection> ec;
00239       iEvent.getByLabel(nameProd_, ecalInput_,ec);
00240       
00241        for(EcalRecHitCollection::const_iterator recHit = (*ec).begin();
00242                                                 recHit != (*ec).end(); ++recHit)
00243        {
00244 // EcalBarrel = 1, EcalEndcap = 2
00245 
00246          GlobalPoint pos = geo->getPosition(recHit->detid());
00247          theRecHits.push_back(*recHit);
00248 
00249          if( (*recHit).energy()> ecut[recHit->detid().subdetId()-1][0] )
00250                     (*myout_ecal)<<recHit->detid().subdetId()<<" "<<(*recHit).energy()<<" "<<pos.phi()<<" "<<pos.eta()
00251                                  <<" "<<iEvent.id().event()<<endl;
00252              
00253        } 
00254       
00255     } catch (cms::Exception& e) { // can't find it!
00256     if (!allowMissingInputs_) {
00257       cout<<" Ecal collection is missed "<<endl;
00258       throw e;
00259      } 
00260     }
00261 
00262      cout<<" Fill EcalRecHits "<<endl;
00263 //  cout<<" Start to get hbhe "<<endl;
00264 // Hcal Barrel and endcap for isolation
00265     try {
00266       edm::Handle<HBHERecHitCollection> hbhe;
00267       iEvent.getByLabel(nameProd_,hbheInput_,hbhe);
00268 
00269 //      (*myout_hcal)<<(*hbhe).size()<<endl;
00270   for(HBHERecHitCollection::const_iterator hbheItr = (*hbhe).begin();
00271 
00272       hbheItr != (*hbhe).end(); ++hbheItr)
00273       {
00274         DetId id = (hbheItr)->detid();
00275         GlobalPoint pos = geo->getPosition(hbheItr->detid());
00276         (*myout_hcal)<<id.subdetId()<<" "<<(*hbheItr).energy()<<" "<<pos.phi()<<
00277                                       " "<<pos.eta()<<" "<<iEvent.id().event()<<endl;
00278         theRecHits.push_back(*hbheItr);
00279 
00280       }
00281     } catch (cms::Exception& e) { // can't find it!
00282       if (!allowMissingInputs_) {
00283         cout<<" HBHE collection is missed "<<endl;
00284         throw e;
00285       }
00286     }
00287 
00288  
00289  for(int i = 0; i<9; i++)
00290  {
00291     for(int j= 0; j<10; j++) GammaIsoEcal[i][j] = 0.;
00292  }
00293 
00294 // Load Ecal clusters
00295  jetexist = -100; 
00296  int barrel = 1;
00297  NumRecoGamma = 0;
00298  
00299  try {
00300  int ij = 0;
00301   // Get island super clusters after energy correction
00302   Handle<reco::SuperClusterCollection> eclus;
00303   iEvent.getByLabel(nameProd_,gammaClus_, eclus);
00304   const reco::SuperClusterCollection* correctedSuperClusters=eclus.product();
00305   // loop over the super clusters and fill the histogram
00306   for(reco::SuperClusterCollection::const_iterator aClus = correctedSuperClusters->begin();
00307                                                            aClus != correctedSuperClusters->end(); aClus++) {
00308     double vet = aClus->energy()/cosh(aClus->eta());
00309     cout<<" Supercluster " << ij<<" Et "<< vet <<" energy "<<aClus->energy()<<" eta "<<aClus->eta()<<" Cut "<<CutOnEgammaEnergy_<<endl;
00310 
00311     if(vet>CutOnEgammaEnergy_) {
00312       ij++;
00313       float gammaiso_ecal[9] = {0.,0.,0.,0.,0.,0.,0.,0.,0.};
00314      for(vector<CaloRecHit>::iterator it = theRecHits.begin(); it != theRecHits.end(); it++)
00315       {
00316            GlobalPoint pos = geo->getPosition(it->detid());
00317            double eta = pos.eta();
00318            double phi = pos.phi();
00319            double deta = fabs(eta-aClus->eta());
00320            double dphi = fabs(phi-aClus->phi());
00321            if(dphi>4.*atan(1.)) dphi = 8.*atan(1.)-dphi;
00322            double dr = sqrt(deta*deta+dphi*dphi);
00323            
00324            double rmin = 0.07;
00325            if( fabs(aClus->eta()) > 1.47 ) rmin = 0.07*(fabs(aClus->eta())-.47)*1.2;
00326            if( fabs(aClus->eta()) > 2.2 ) rmin = 0.07*(fabs(aClus->eta())-.47)*1.4;
00327            
00328            int itype_ecal = 0;
00329            double ecutn = 0.;
00330            for (int i = 0; i<3; i++)
00331            {
00332              for (int j = 0; j<3; j++)
00333              {
00334              
00335                 if(it->detid().det() == DetId::Ecal ) 
00336                 {
00337                   if(it->detid().subdetId() == 1) ecutn = ecut[0][j];
00338                   if(it->detid().subdetId() == 2) ecutn = ecut[1][j];
00339                   if( dr>rmin && dr<risol[i])
00340                   {
00341                    if((*it).energy() > ecutn) gammaiso_ecal[itype_ecal] = gammaiso_ecal[itype_ecal]+(*it).energy()/cosh(eta);
00342                   } 
00343                 }
00344                 
00345                 if(it->detid().det() == DetId::Hcal ) 
00346                 {
00347                    ecutn = ecut[2][j];
00348                    if( dr>rmin && dr<risol[i])
00349                    {
00350                      if((*it).energy() > ecutn) 
00351                      {
00352                         gammaiso_ecal[itype_ecal] = gammaiso_ecal[itype_ecal]+(*it).energy()/cosh(eta);
00353                      }
00354                    }
00355                 } 
00356                 jetexist = ij;
00357                 itype_ecal++;
00358                 
00359              } // Ecal
00360            } // cycle on iso radii      
00361       } // cycle on rechits
00362       
00363       
00364 // Fill Tree      
00365            if( NumRecoGamma < 10 ) 
00366            {
00367             for (int ii = 0; ii<9 ; ii++)
00368             {
00369              GammaIsoEcal[ii][NumRecoGamma] = gammaiso_ecal[ii]; 
00370             } 
00371              EcalClusDet[NumRecoGamma] = 1;
00372              GammaRecoEt[NumRecoGamma] = vet;
00373              GammaRecoEta[NumRecoGamma] = aClus->eta();
00374              GammaRecoPhi[NumRecoGamma] = aClus->phi();
00375              NumRecoGamma++;
00376             }
00377     (*myout_photon)<<ij<<" "<<barrel<<" "<<vet<<" "<<aClus->eta()<<" "<<aClus->phi()<<" "<<iEvent.id().event()<<endl;
00378     (*myout_photon)<<ij<<" "<<gammaiso_ecal[0]<<" "<<gammaiso_ecal[1] <<" "<<gammaiso_ecal[2]<<" "<<gammaiso_ecal[3]
00379                    <<" "<<gammaiso_ecal[4]<<" "<<gammaiso_ecal[5]<<" "<<gammaiso_ecal[6]<<" "<<gammaiso_ecal[7]<<" "<<gammaiso_ecal[8]<<endl;
00380       
00381        jetexist = ij;
00382     } //vet  
00383   } // number of superclusters
00384   } catch (cms::Exception& e) { // can't find it!
00385     if (!allowMissingInputs_) {
00386        cout<<" Ecal barrel clusters are missed "<<endl;
00387        throw e;
00388     }   
00389   }
00390 
00391     cout<<" After iso cuts "<<jetexist<<endl;
00392 
00393     double ecluslost = -100.1;
00394     if(jetexist<0) (*myout_photon)<<jetexist<<" "<<barrel<<" "<<ecluslost<<" "<<ecluslost
00395                                   <<" "<<ecluslost<<" "<<iEvent.id().event()<<endl;
00396   
00397     cout<<" Event is ready "<<endl;
00398    
00399     myTree->Fill();
00400    
00401 } // analyze method
00402 } // namespace cms

Generated on Tue Jun 9 17:25:35 2009 for CMSSW by  doxygen 1.5.4