CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Calibration/HcalCalibAlgos/src/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/GeneratorProducts/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()
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 
00164    edm::ESHandle<CaloGeometry> pG;
00165    iSetup.get<CaloGeometryRecord>().get(pG);
00166    geo = pG.product();
00167 
00168 
00169   using namespace edm;
00170   std::vector<Provenance const*> theProvenance;
00171   iEvent.getAllProvenance(theProvenance);
00172 //  for( std::vector<Provenance const*>::const_iterator ip = theProvenance.begin();
00173 //                                                      ip != theProvenance.end(); ip++)
00174 //  {
00175 //     cout<<" Print all module/label names "<<(**ip).moduleName()<<" "<<(**ip).moduleLabel()<<
00176 //     " "<<(**ip).productInstanceName()<<endl;
00177 //  }
00178 // Load generator information
00179 // write HEPEVT block into file
00180    run = iEvent.id().run();
00181    event = iEvent.id().event();
00182   (*myout_part)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00183   (*myout_jet)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00184   (*myout_hcal)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00185   (*myout_ecal)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00186   (*myout_photon)<<"Event "<<iEvent.id().run()<<" "<<iEvent.id().event()<<endl;
00187   
00188 
00189     std::vector<edm::InputTag>::const_iterator ic;
00190     int jettype = 0;
00191     int jetexist = -100;
00192     int reco = 1;
00193     double etlost = -100.1;
00194     
00195     NumRecoJets = 0;
00196     
00197      try {
00198        
00199        edm::Handle<reco::CaloJetCollection> jets;
00200        iEvent.getByLabel(nameProd_, jetCalo_, jets);
00201        reco::CaloJetCollection::const_iterator jet = jets->begin ();
00202        cout<<" Size of Calo jets "<<jets->size()<<endl;
00203        jettype++;
00204        
00205        if(jets->size() > 0 )
00206        {
00207          int ij = 0;
00208          for (; jet != jets->end (); jet++) 
00209          {
00210             cout<<" Jet et "<<(*jet).et()<<" "<<(*jet).eta()<<" "<<(*jet).phi()<<endl;
00211             ij++;
00212             if(ij<4) (*myout_jet)<<jettype<<" "<<reco<<" "<<ij<<" "<<(*jet).et()<<" "<<(*jet).eta()<<" "<<(*jet).phi()
00213             <<" "<<iEvent.id().event()<<endl;
00214             jetexist = ij;
00215             if( NumRecoJets < 8 )
00216             {
00217              JetRecoEt[NumRecoJets] = (*jet).et();
00218              JetRecoEta[NumRecoJets] = (*jet).eta();
00219              JetRecoPhi[NumRecoJets] = (*jet).phi();
00220              JetRecoType[NumRecoJets] = jettype;
00221              NumRecoJets++;
00222             }
00223          }
00224        }
00225      } catch (cms::Exception& e) { // can't find it!
00226        if (!allowMissingInputs_) {
00227          cout<< " Calojets are missed "<<endl;
00228          throw e;
00229         }        
00230      }
00231    
00232      cout<<" We filled CaloJet part "<<jetexist<<endl;
00233      
00234      if( jetexist < 0 ) (*myout_jet)<<jetexist<<" "<<reco<<" "<<etlost
00235                          <<" "<<etlost<<" "<<etlost
00236                          <<" "<<iEvent.id().event()<<endl;
00237 // Load EcalRecHits
00238   
00239     std::vector<edm::InputTag>::const_iterator i;
00240     vector<CaloRecHit> theRecHits;
00241       
00242     try {
00243       
00244       edm::Handle<EcalRecHitCollection> ec;
00245       iEvent.getByLabel(nameProd_, ecalInput_,ec);
00246       
00247        for(EcalRecHitCollection::const_iterator recHit = (*ec).begin();
00248                                                 recHit != (*ec).end(); ++recHit)
00249        {
00250 // EcalBarrel = 1, EcalEndcap = 2
00251 
00252          GlobalPoint pos = geo->getPosition(recHit->detid());
00253          theRecHits.push_back(*recHit);
00254 
00255          if( (*recHit).energy()> ecut[recHit->detid().subdetId()-1][0] )
00256                     (*myout_ecal)<<recHit->detid().subdetId()<<" "<<(*recHit).energy()<<" "<<pos.phi()<<" "<<pos.eta()
00257                                  <<" "<<iEvent.id().event()<<endl;
00258              
00259        } 
00260       
00261     } catch (cms::Exception& e) { // can't find it!
00262     if (!allowMissingInputs_) {
00263       cout<<" Ecal collection is missed "<<endl;
00264       throw e;
00265      } 
00266     }
00267 
00268      cout<<" Fill EcalRecHits "<<endl;
00269 //  cout<<" Start to get hbhe "<<endl;
00270 // Hcal Barrel and endcap for isolation
00271     try {
00272       edm::Handle<HBHERecHitCollection> hbhe;
00273       iEvent.getByLabel(nameProd_,hbheInput_,hbhe);
00274 
00275 //      (*myout_hcal)<<(*hbhe).size()<<endl;
00276   for(HBHERecHitCollection::const_iterator hbheItr = (*hbhe).begin();
00277 
00278       hbheItr != (*hbhe).end(); ++hbheItr)
00279       {
00280         DetId id = (hbheItr)->detid();
00281         GlobalPoint pos = geo->getPosition(hbheItr->detid());
00282         (*myout_hcal)<<id.subdetId()<<" "<<(*hbheItr).energy()<<" "<<pos.phi()<<
00283                                       " "<<pos.eta()<<" "<<iEvent.id().event()<<endl;
00284         theRecHits.push_back(*hbheItr);
00285 
00286       }
00287     } catch (cms::Exception& e) { // can't find it!
00288       if (!allowMissingInputs_) {
00289         cout<<" HBHE collection is missed "<<endl;
00290         throw e;
00291       }
00292     }
00293 
00294  
00295  for(int i = 0; i<9; i++)
00296  {
00297     for(int j= 0; j<10; j++) GammaIsoEcal[i][j] = 0.;
00298  }
00299 
00300 // Load Ecal clusters
00301  jetexist = -100; 
00302  int barrel = 1;
00303  NumRecoGamma = 0;
00304  
00305  try {
00306  int ij = 0;
00307   // Get island super clusters after energy correction
00308   Handle<reco::SuperClusterCollection> eclus;
00309   iEvent.getByLabel(nameProd_,gammaClus_, eclus);
00310   const reco::SuperClusterCollection* correctedSuperClusters=eclus.product();
00311   // loop over the super clusters and fill the histogram
00312   for(reco::SuperClusterCollection::const_iterator aClus = correctedSuperClusters->begin();
00313                                                            aClus != correctedSuperClusters->end(); aClus++) {
00314     double vet = aClus->energy()/cosh(aClus->eta());
00315     cout<<" Supercluster " << ij<<" Et "<< vet <<" energy "<<aClus->energy()<<" eta "<<aClus->eta()<<" Cut "<<CutOnEgammaEnergy_<<endl;
00316 
00317     if(vet>CutOnEgammaEnergy_) {
00318       ij++;
00319       float gammaiso_ecal[9] = {0.,0.,0.,0.,0.,0.,0.,0.,0.};
00320      for(vector<CaloRecHit>::iterator it = theRecHits.begin(); it != theRecHits.end(); it++)
00321       {
00322            GlobalPoint pos = geo->getPosition(it->detid());
00323            double eta = pos.eta();
00324            double phi = pos.phi();
00325            double deta = fabs(eta-aClus->eta());
00326            double dphi = fabs(phi-aClus->phi());
00327            if(dphi>4.*atan(1.)) dphi = 8.*atan(1.)-dphi;
00328            double dr = sqrt(deta*deta+dphi*dphi);
00329            
00330            double rmin = 0.07;
00331            if( fabs(aClus->eta()) > 1.47 ) rmin = 0.07*(fabs(aClus->eta())-.47)*1.2;
00332            if( fabs(aClus->eta()) > 2.2 ) rmin = 0.07*(fabs(aClus->eta())-.47)*1.4;
00333            
00334            int itype_ecal = 0;
00335            double ecutn = 0.;
00336            for (int i = 0; i<3; i++)
00337            {
00338              for (int j = 0; j<3; j++)
00339              {
00340              
00341                 if(it->detid().det() == DetId::Ecal ) 
00342                 {
00343                   if(it->detid().subdetId() == 1) ecutn = ecut[0][j];
00344                   if(it->detid().subdetId() == 2) ecutn = ecut[1][j];
00345                   if( dr>rmin && dr<risol[i])
00346                   {
00347                    if((*it).energy() > ecutn) gammaiso_ecal[itype_ecal] = gammaiso_ecal[itype_ecal]+(*it).energy()/cosh(eta);
00348                   } 
00349                 }
00350                 
00351                 if(it->detid().det() == DetId::Hcal ) 
00352                 {
00353                    ecutn = ecut[2][j];
00354                    if( dr>rmin && dr<risol[i])
00355                    {
00356                      if((*it).energy() > ecutn) 
00357                      {
00358                         gammaiso_ecal[itype_ecal] = gammaiso_ecal[itype_ecal]+(*it).energy()/cosh(eta);
00359                      }
00360                    }
00361                 } 
00362                 jetexist = ij;
00363                 itype_ecal++;
00364                 
00365              } // Ecal
00366            } // cycle on iso radii      
00367       } // cycle on rechits
00368       
00369       
00370 // Fill Tree      
00371            if( NumRecoGamma < 10 ) 
00372            {
00373             for (int ii = 0; ii<9 ; ii++)
00374             {
00375              GammaIsoEcal[ii][NumRecoGamma] = gammaiso_ecal[ii]; 
00376             } 
00377              EcalClusDet[NumRecoGamma] = 1;
00378              GammaRecoEt[NumRecoGamma] = vet;
00379              GammaRecoEta[NumRecoGamma] = aClus->eta();
00380              GammaRecoPhi[NumRecoGamma] = aClus->phi();
00381              NumRecoGamma++;
00382             }
00383     (*myout_photon)<<ij<<" "<<barrel<<" "<<vet<<" "<<aClus->eta()<<" "<<aClus->phi()<<" "<<iEvent.id().event()<<endl;
00384     (*myout_photon)<<ij<<" "<<gammaiso_ecal[0]<<" "<<gammaiso_ecal[1] <<" "<<gammaiso_ecal[2]<<" "<<gammaiso_ecal[3]
00385                    <<" "<<gammaiso_ecal[4]<<" "<<gammaiso_ecal[5]<<" "<<gammaiso_ecal[6]<<" "<<gammaiso_ecal[7]<<" "<<gammaiso_ecal[8]<<endl;
00386       
00387        jetexist = ij;
00388     } //vet  
00389   } // number of superclusters
00390   } catch (cms::Exception& e) { // can't find it!
00391     if (!allowMissingInputs_) {
00392        cout<<" Ecal barrel clusters are missed "<<endl;
00393        throw e;
00394     }   
00395   }
00396 
00397     cout<<" After iso cuts "<<jetexist<<endl;
00398 
00399     double ecluslost = -100.1;
00400     if(jetexist<0) (*myout_photon)<<jetexist<<" "<<barrel<<" "<<ecluslost<<" "<<ecluslost
00401                                   <<" "<<ecluslost<<" "<<iEvent.id().event()<<endl;
00402   
00403     cout<<" Event is ready "<<endl;
00404    
00405     myTree->Fill();
00406    
00407 } // analyze method
00408 } // namespace cms