CMS 3D CMS Logo

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

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