CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/Calibration/HcalCalibAlgos/src/DiJetAnalyzer.cc

Go to the documentation of this file.
00001 #include "Calibration/HcalCalibAlgos/interface/DiJetAnalyzer.h"
00002 
00003 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00004 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00005 
00006 #include "DataFormats/JetReco/interface/CaloJet.h"
00007 #include "DataFormats/JetReco/interface/CaloJetCollection.h"
00008 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00009 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00010 #include "DataFormats/CaloTowers/interface/CaloTowerCollection.h"
00011 #include "DataFormats/CaloTowers/interface/CaloTowerDetId.h"
00012 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00013 #include "DataFormats/DetId/interface/DetId.h"
00014 
00015 #include "Calibration/Tools/interface/GenericMinL3Algorithm.h"
00016 #include "CondFormats/DataRecord/interface/HcalRespCorrsRcd.h"
00017 #include "CalibCalorimetry/HcalAlgos/interface/HcalDbASCIIIO.h"
00018 
00019 #include "FWCore/Utilities/interface/Exception.h"
00020 #include <fstream>
00021 
00022 
00023 
00024 #include "TString.h"
00025 #include "TFile.h"
00026 #include "TTree.h"
00027 #include "TObject.h"
00028 #include "TObjArray.h"
00029 #include "TClonesArray.h"
00030 #include "TRefArray.h"
00031 #include "TLorentzVector.h"
00032 
00033 
00034 
00035 namespace cms
00036 {
00037 DiJetAnalyzer::DiJetAnalyzer(const edm::ParameterSet& iConfig)
00038 {
00039   jets_=iConfig.getParameter<edm::InputTag>("jetsInput");
00040   ec_=iConfig.getParameter<edm::InputTag>("ecInput");
00041   hbhe_=iConfig.getParameter<edm::InputTag>("hbheInput");
00042   ho_=iConfig.getParameter<edm::InputTag>("hoInput");
00043   hf_=iConfig.getParameter<edm::InputTag>("hfInput");
00044 
00045   // get name of output file with histogramms
00046   fOutputFileName = iConfig.getUntrackedParameter<std::string>("HistOutFile");
00047 
00048    allowMissingInputs_ = true;
00049 }
00050 
00051 
00052 DiJetAnalyzer::~DiJetAnalyzer()
00053 {
00054 
00055 }
00056 
00057 
00058 //
00059 // member functions
00060 //
00061 
00062 // ------------ method called to for each event  ------------
00063 void
00064 DiJetAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00065 {
00066 
00067    using namespace std; 
00068    using namespace edm;
00069    using namespace reco; 
00070 
00071    const double pi = 4.*atan(1.);
00072 
00073   // event number & run number 
00074    eventNumber = iEvent.id().event(); 
00075    runNumber = iEvent.id().run();
00076  
00077 
00078   // read jets
00079    CaloJet jet1, jet2, jet3; 
00080    try {
00081    edm::Handle<CaloJetCollection> jets;
00082    iEvent.getByLabel(jets_,jets);
00083    if(jets->size()>1){ 
00084     jet1 = (*jets)[0]; 
00085     jet2 = (*jets)[1];
00086      if(fabs(jet1.eta())>fabs(jet2.eta())){
00087        CaloJet jet = jet1; 
00088        jet1 = jet2; 
00089        jet2 = jet; 
00090      } 
00091      //     if(fabs(jet1.eta())>eta_1 || (fabs(jet2.eta())-jet_R) < eta_2){ return;}
00092    } else {return;}
00093    tagJetP4->SetPxPyPzE(jet1.px(), jet1.py(), jet1.pz(), jet1.energy());
00094    probeJetP4->SetPxPyPzE(jet2.px(), jet2.py(), jet2.pz(), jet2.energy());
00095    if(jets->size()>2){
00096      jet3 = (*jets)[2];
00097      etVetoJet = jet3.et();
00098    } else { etVetoJet = 0.;}
00099    }catch (cms::Exception& e) { // can't find it!
00100      if (!allowMissingInputs_) { throw e; }  
00101    }
00102 
00103 
00104    double dR = 1000.; 
00105    edm::ESHandle<CaloGeometry> pG;
00106    iSetup.get<CaloGeometryRecord>().get(pG);
00107    const CaloGeometry* geo = pG.product();
00108    vector<DetId> vid = geo->getValidDetIds();
00109    for(vector<DetId>::const_iterator idItr = vid.begin(); idItr != vid.end(); idItr++)
00110      {
00111       if( (*idItr).det() == DetId::Hcal ) {
00112         GlobalPoint pos = geo->getPosition(*idItr); 
00113         double deta = fabs(jet2.eta() - pos.eta());
00114         double dphi = fabs(jet2.phi() - pos.phi()); 
00115         if(dphi>pi){dphi=2*pi-dphi;}
00116         double dR_candidate = sqrt(deta*deta + dphi*dphi); 
00117         int ieta = HcalDetId(*idItr).ieta();
00118         int iphi = HcalDetId(*idItr).iphi();
00119         int idepth = HcalDetId(*idItr).depth();
00120         if(dR_candidate < dR && idepth == 1){
00121           dR = dR_candidate; 
00122           iEtaHit = ieta; 
00123           iPhiHit = iphi; 
00124         }
00125       }
00126      }
00127 
00128    targetE = jet1.et()/sin(jet2.theta()); 
00129 
00130    std::map<DetId,int> mapId; 
00131    vector<CaloTowerPtr> towers_fw = jet2.getCaloConstituents();
00132    vector<CaloTowerPtr>::const_iterator towersItr;
00133    for(towersItr = towers_fw.begin(); towersItr != towers_fw.end(); towersItr++){
00134       size_t tower_size = (*towersItr)->constituentsSize();
00135       for(size_t i=0; i<tower_size; i++){
00136         DetId id = (*towersItr)->constituent(i);
00137         mapId[id] = 1;
00138       }
00139    }
00140 
00141 
00142    //   probeJetEmFrac = 0.;
00143    double emEnergy = 0.; 
00144    try {
00145       Handle<EcalRecHitCollection> ec;
00146       iEvent.getByLabel(ec_,ec);
00147       for(EcalRecHitCollection::const_iterator ecItr = (*ec).begin();
00148                                                 ecItr != (*ec).end(); ++ecItr)
00149       {
00150         DetId id = ecItr->detid();
00151         if(mapId[id]==1){
00152           emEnergy += ecItr->energy(); 
00153         }
00154       }
00155    } catch (cms::Exception& e) { // can't find it!
00156       if (!allowMissingInputs_) throw e;
00157    }
00158 
00159    targetE += -emEnergy;
00160    probeJetEmFrac = emEnergy/jet2.energy(); 
00161 
00162    int nHits = 0; 
00163    try {
00164       Handle<HBHERecHitCollection> hbhe;
00165       iEvent.getByLabel(hbhe_, hbhe);
00166       for(HBHERecHitCollection::const_iterator hbheItr=hbhe->begin(); 
00167                                                  hbheItr!=hbhe->end(); hbheItr++)
00168       {
00169          DetId id = hbheItr->detid();     
00170          if(mapId[id]==1){
00171           TCell* cell = new TCell(id.rawId(),hbheItr->energy()); 
00172           (*cells)[nHits] = cell;
00173           nHits++;  
00174          }       
00175       }
00176    } catch (cms::Exception& e) { // can't find it!
00177       if (!allowMissingInputs_) throw e;
00178    }
00179  
00180 
00181    try {
00182       Handle<HORecHitCollection> ho;
00183       iEvent.getByLabel(ho_, ho);
00184       for(HORecHitCollection::const_iterator hoItr=ho->begin(); 
00185                                                hoItr!=ho->end(); hoItr++)
00186       {
00187          DetId id = hoItr->detid();
00188          if(mapId[id]==1){
00189           TCell* cell = new TCell(id.rawId(),hoItr->energy()); 
00190           (*cells)[nHits] = cell;
00191           nHits++;  
00192          }
00193       }
00194    } catch (cms::Exception& e) { // can't find it!
00195       if (!allowMissingInputs_) throw e;
00196    }
00197 
00198    try {
00199       Handle<HFRecHitCollection> hf;
00200       iEvent.getByLabel(hf_, hf);
00201       for(HFRecHitCollection::const_iterator hfItr=hf->begin(); 
00202                                                hfItr!=hf->end(); hfItr++)
00203       {
00204          DetId id = hfItr->detid();
00205          if(mapId[id]==1){
00206           TCell* cell = new TCell(id.rawId(),hfItr->energy()); 
00207           (*cells)[nHits] = cell; 
00208           nHits++; 
00209          }
00210       }
00211    } catch (cms::Exception& e) { // can't find it!
00212       if (!allowMissingInputs_) throw e;
00213    }
00214  
00215    probeJetEmFrac = emEnergy/jet2.energy();
00216    
00217    tree->Fill(); 
00218  
00219 }
00220 
00221 
00222 // ------------ method called once each job just before starting event loop  ------------
00223 void 
00224 DiJetAnalyzer::beginJob()
00225 {
00226 
00227   hOutputFile   = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
00228 
00229     tree = new TTree("hcalCalibTree", "Tree for IsoTrack Calibration");
00230 
00231     cells = new TClonesArray("TCell");
00232     tagJetP4 = new TLorentzVector();
00233     probeJetP4 = new TLorentzVector();
00234  
00235 
00236     tree->Branch("eventNumber", &eventNumber, "eventNumber/i");
00237     tree->Branch("runNumber", &runNumber, "runNumber/i");  
00238     tree->Branch("iEtaHit", &iEtaHit, "iEtaHit/I");
00239     tree->Branch("iPhiHit", &iPhiHit, "iPhiHit/i");    
00240 
00241     tree->Branch("xTrkEcal", &xTrkEcal, "xTrkEcal/F");
00242     tree->Branch("yTrkEcal", &yTrkEcal, "yTrkEcal/F");
00243     tree->Branch("zTrkEcal", &zTrkEcal, "zTrkEcal/F");
00244     tree->Branch("xTrkHcal", &xTrkHcal, "xTrkHcal/F");
00245     tree->Branch("yTrkHcal", &yTrkHcal, "yTrkHcal/F");
00246     tree->Branch("zTrkHcal", &zTrkHcal, "zTrkHcal/F");
00247 
00248     tree->Branch("cells", &cells, 64000); 
00249     tree->Branch("emEnergy", &emEnergy, "emEnergy/F"); 
00250     tree->Branch("targetE", &targetE, "targetE/F");
00251     tree->Branch("etVetoJet", &etVetoJet, "etVetoJet/F");
00252     tree->Branch("tagJetP4", "TLorentzVector", &tagJetP4);
00253     tree->Branch("probeJetP4", "TLorentzVector", &probeJetP4);
00254     tree->Branch("tagJetEmFrac", &tagJetEmFrac,"tagJetEmFrac/F"); 
00255     tree->Branch("probeJetEmFrac", &probeJetEmFrac,"probeJetEmFrac/F");    
00256    
00257 }
00258 
00259 // ------------ method called once each job just after ending the event loop  ------------
00260 void 
00261 DiJetAnalyzer::endJob() {
00262 
00263    hOutputFile->SetCompressionLevel(2);
00264    hOutputFile->cd();
00265    tree->Write(); 
00266    hOutputFile->Close() ;
00267 
00268 }
00269 }