CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/Calibration/HcalCalibAlgos/plugins/ValidIsoTrkCalib.cc

Go to the documentation of this file.
00001 //
00002 // Package:    ValidIsoTrkCalib
00003 // Class:      ValidIsoTrkCalib
00004 // 
00005 
00006 /*
00007  Description: Validation for Isolated tracks Calibration
00008  
00009  Implementation:
00010 See the twiki page for details:
00011 https://twiki.cern.ch/twiki/bin/view/CMS/ValidIsoTrkCalib
00012 
00013 */
00014 
00015 //
00016 // Original Author:  Andrey Pozdnyakov
00017 //         Created:  Tue Nov  4 01:16:05 CET 2008
00018 // $Id: ValidIsoTrkCalib.cc,v 1.11 2010/01/29 19:58:02 andrey Exp $
00019 //
00020 
00021 // system include files
00022 
00023 #include <memory>
00024 
00025 // user include files
00026 #include "FWCore/Framework/interface/Frameworkfwd.h"
00027 #include "FWCore/Framework/interface/EDAnalyzer.h"
00028 #include "FWCore/Framework/interface/ESHandle.h"
00029 
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Framework/interface/MakerMacros.h"
00032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00033 
00034 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00035 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00036 #include "TrackingTools/TrackAssociator/interface/TrackDetectorAssociator.h"
00037 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00038 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00039 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00040 #include "DataFormats/HcalIsolatedTrack/interface/IsolatedPixelTrackCandidate.h"
00041 #include "DataFormats/HcalIsolatedTrack/interface/IsolatedPixelTrackCandidateFwd.h"
00042 
00043 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00044 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00045 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00046 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00047 #include "Geometry/CaloEventSetup/interface/CaloTopologyRecord.h"
00048 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00049 #include "CondFormats/HcalObjects/interface/HcalRespCorrs.h"
00050 #include "CondFormats/DataRecord/interface/HcalRespCorrsRcd.h"
00051 //TFile Service
00052 #include "FWCore/ServiceRegistry/interface/Service.h"
00053 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00054 #include "Calibration/HcalCalibAlgos/src/MaxHit_struct.h"
00055 
00056 #include "TROOT.h"
00057 #include "TFile.h"
00058 #include "TTree.h"
00059 #include "TH1F.h"
00060 
00061 
00062 #include <fstream>
00063 #include <map>
00064 
00065 using namespace edm;
00066 using namespace std;
00067 using namespace reco;
00068 
00069 class ValidIsoTrkCalib : public edm::EDAnalyzer {
00070 public:
00071   explicit ValidIsoTrkCalib(const edm::ParameterSet&);
00072   ~ValidIsoTrkCalib();
00073 
00074   double getDistInPlaneSimple(const GlobalPoint caloPoint, const GlobalPoint rechitPoint);
00075 
00076 private:
00077 
00078   virtual void beginJob() ;
00079   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00080   virtual void endJob() ;
00081 
00082 
00083     
00084   // ----------member data ---------------------------
00085   
00086   //Variables from HcalIsotrackAnalyzer
00087 
00088   TrackDetectorAssociator trackAssociator_;
00089   TrackAssociatorParameters parameters_;
00090   double taECALCone_;
00091   double taHCALCone_;
00092 
00093   const CaloGeometry* geo;
00094   InputTag genTracksLabel_;
00095   InputTag genhbheLabel_;
00096   InputTag genhoLabel_;
00097   std::vector<edm::InputTag> genecalLabel_;
00098   InputTag hbheLabel_;
00099   InputTag hoLabel_;
00100   InputTag trackLabel_;
00101   InputTag trackLabel1_;
00102   
00103   //std::string m_inputTrackLabel;
00104   //std::string m_hcalLabel;
00105 
00106   double associationConeSize_;
00107   string AxB_;
00108   double calibrationConeSize_;
00109 
00110   bool allowMissingInputs_;
00111   string outputFileName_;
00112   //string calibFactorsFileName_;
00113   //  string corrfile;
00114   
00115   bool takeGenTracks_;
00116   //bool takeAllRecHits_, takeGenTracks_;
00117   int gen, iso, pix;
00118   float genPt[500], genPhi[500], genEta[500];
00119   float isoPt[500], isoPhi[500], isoEta[500];
00120   float pixPt[500], pixPhi[500], pixEta[500];
00121   //int  hbheiEta[5000],hbheiPhi[5000],hbheDepth[5000];  
00122   //float hbheEnergy[5000];  
00123 
00124 
00125   int NisoTrk;
00126   float trackPt, trackE, trackEta, trackPhi;
00127   float ptNear;
00128   float ptrack, rvert;
00129   //float eecal, ehcal;
00130 
00131   int MinNTrackHitsBarrel, MinNTECHitsEndcap;
00132   double energyECALmip, maxPNear;
00133   double energyMinIso, energyMaxIso;
00134 
00135 
00136   Float_t emEnergy;
00137   Float_t targetE;
00138 
00139   //TFile* rootFile;
00140   //  TTree* tree;
00141   TTree *tTree, *fTree;
00142 
00143   Float_t xTrkEcal;
00144   Float_t yTrkEcal;
00145   Float_t zTrkEcal;
00146 
00147   Float_t xTrkHcal;
00148   Float_t yTrkHcal;
00149   Float_t zTrkHcal;
00150 
00151   int Nhits;
00152   float eClustBefore;  //Calo energy before calibration
00153   float eClustAfter;   //After calibration
00154   float eTrack;      //Track energy
00155   float etaTrack;     
00156   float phiTrack;      
00157   float eECAL;      // Energy deposited in ECAL
00158   int numHits;       //number of rechits
00159   //float e3x3;
00160 
00161   float eBeforeDepth1;
00162   float eAfterDepth1;
00163   float eBeforeDepth2;
00164   float eAfterDepth2;
00165   float eCentHitBefore;
00166   float eCentHitAfter;
00167   float CentHitFactor;
00168   int iEta;
00169   int iPhi;
00170   int iEtaTr;
00171   int iPhiTr;
00172   float iDr, delR;
00173   int dietatr;
00174   int diphitr;
00175 
00176   float iTime;
00177   float HTime[100];
00178   float e3x3Before;
00179   float e3x3After;
00180   float e5x5Before;
00181   float e5x5After;
00182   int eventNumber;
00183   int runNumber;
00184   float PtNearBy;
00185   float numVH, numVS, numValidTrkHits, numValidTrkStrips;
00186 
00187   const HcalRespCorrs* respRecalib;
00188  //  map<UInt_t, Float_t> CalibFactors; 
00189   //  Bool_t ReadCalibFactors(string);
00190 
00191   TH1F *nTracks;
00192 
00193   edm::Service<TFileService> fs;
00194   // int Lumi_n;
00195 
00196   
00197 };
00198 
00199 double ValidIsoTrkCalib::getDistInPlaneSimple(const GlobalPoint caloPoint,
00200                             const GlobalPoint rechitPoint)
00201 {
00202 
00203   // Simplified version of getDistInPlane
00204   // Assume track direction is origin -> point of hcal intersection
00205 
00206   const GlobalVector caloIntersectVector(caloPoint.x(),
00207                                          caloPoint.y(),
00208                                          caloPoint.z());
00209 
00210   const GlobalVector caloIntersectUnitVector = caloIntersectVector.unit();
00211 
00212   const GlobalVector rechitVector(rechitPoint.x(),
00213                                   rechitPoint.y(),
00214                                   rechitPoint.z());
00215 
00216   const GlobalVector rechitUnitVector = rechitVector.unit();
00217   double dotprod = caloIntersectUnitVector.dot(rechitUnitVector);
00218   double rechitdist = caloIntersectVector.mag()/dotprod;
00219 
00220 
00221   const GlobalVector effectiveRechitVector = rechitdist*rechitUnitVector;
00222   const GlobalPoint effectiveRechitPoint(effectiveRechitVector.x(),
00223                                          effectiveRechitVector.y(),
00224                                          effectiveRechitVector.z());
00225 
00226 
00227   GlobalVector distance_vector = effectiveRechitPoint-caloPoint;
00228 
00229   if (dotprod > 0.)
00230   {
00231     return distance_vector.mag();
00232   }
00233   else
00234   {
00235     return 999999.;
00236 
00237   }
00238 
00239 
00240 }
00241 
00242 
00243 ValidIsoTrkCalib::ValidIsoTrkCalib(const edm::ParameterSet& iConfig)
00244   
00245 {
00246 
00247   //takeAllRecHits_=iConfig.getUntrackedParameter<bool>("takeAllRecHits");
00248   takeGenTracks_=iConfig.getUntrackedParameter<bool>("takeGenTracks");
00249 
00250   genTracksLabel_ = iConfig.getParameter<edm::InputTag>("genTracksLabel");
00251   genhbheLabel_= iConfig.getParameter<edm::InputTag>("genHBHE");
00252   //genhoLabel_=iConfig.getParameter<edm::InputTag>("genHO");
00253   //genecalLabel_=iConfig.getParameter<std::vector<edm::InputTag> >("genECAL");
00254 
00255   // m_hcalLabel = iConfig.getUntrackedParameter<std::string> ("hcalRecHitsLabel","hbhereco");
00256 
00257   hbheLabel_= iConfig.getParameter<edm::InputTag>("hbheInput");
00258   hoLabel_=iConfig.getParameter<edm::InputTag>("hoInput");
00259   //eLabel_=iConfig.getParameter<edm::InputTag>("eInput");
00260   trackLabel_ = iConfig.getParameter<edm::InputTag>("HcalIsolTrackInput");
00261   trackLabel1_ = iConfig.getParameter<edm::InputTag>("trackInput");
00262 
00263   associationConeSize_=iConfig.getParameter<double>("associationConeSize");
00264   allowMissingInputs_=iConfig.getUntrackedParameter<bool>("allowMissingInputs", true);
00265 //  outputFileName_=iConfig.getParameter<std::string>("outputFileName");
00266   //  calibFactorsFileName_=iConfig.getParameter<std::string>("calibFactorsFileName");
00267 
00268 
00269   AxB_=iConfig.getParameter<std::string>("AxB");
00270   calibrationConeSize_=iConfig.getParameter<double>("calibrationConeSize");
00271 
00272   MinNTrackHitsBarrel =  iConfig.getParameter<int>("MinNTrackHitsBarrel");
00273   MinNTECHitsEndcap =  iConfig.getParameter<int>("MinNTECHitsEndcap");
00274   energyECALmip = iConfig.getParameter<double>("energyECALmip");
00275   energyMinIso = iConfig.getParameter<double>("energyMinIso");
00276   energyMaxIso = iConfig.getParameter<double>("energyMaxIso");
00277   maxPNear = iConfig.getParameter<double>("maxPNear");
00278 
00279   edm::ParameterSet parameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
00280   parameters_.loadParameters( parameters );
00281   trackAssociator_.useDefaultPropagator();
00282 
00283   taECALCone_=iConfig.getUntrackedParameter<double>("TrackAssociatorECALCone",0.5);
00284   taHCALCone_=iConfig.getUntrackedParameter<double>("TrackAssociatorHCALCone",0.6);
00285 
00286 }
00287 
00288 
00289 ValidIsoTrkCalib::~ValidIsoTrkCalib()
00290 {
00291  
00292    // do anything here that needs to be done at desctruction time
00293    // (e.g. close files, deallocate resources etc.)
00294 
00295 }
00296 
00297 
00298 // ------------ method called to for each event  ------------
00299 void
00300 ValidIsoTrkCalib::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00301 {
00302    using namespace edm;
00303    
00304  try{
00305    edm::ESHandle <HcalRespCorrs> recalibCorrs;
00306    iSetup.get<HcalRespCorrsRcd>().get("recalibrate",recalibCorrs);
00307    respRecalib = recalibCorrs.product();
00308 
00309    LogInfo("CalibConstants")<<"  Loaded:  OK ";
00310 
00311  }catch(const cms::Exception & e) {
00312    LogWarning("CalibConstants")<<"   Not Found!! ";
00313  }
00314 
00315    edm::Handle<reco::TrackCollection> generalTracks;
00316    iEvent.getByLabel(genTracksLabel_, generalTracks);
00317 
00318   edm::Handle<reco::TrackCollection> isoProdTracks;
00319   iEvent.getByLabel(trackLabel1_,isoProdTracks);
00320 
00321 
00322   edm::Handle<reco::IsolatedPixelTrackCandidateCollection> pixelTracks;
00323   //edm::Handle<reco::TrackCollection> pixelTracks;
00324   iEvent.getByLabel(trackLabel_,pixelTracks);
00325   
00326   /*
00327   edm::Handle<EcalRecHitCollection> ecal;
00328   iEvent.getByLabel(eLabel_,ecal);
00329   const EcalRecHitCollection Hitecal = *(ecal.product());
00330   */
00331 
00332   edm::Handle<HBHERecHitCollection> hbhe;
00333   iEvent.getByLabel(hbheLabel_,hbhe);
00334   const HBHERecHitCollection Hithbhe = *(hbhe.product());
00335 
00336   edm::ESHandle<CaloGeometry> pG;
00337   iSetup.get<CaloGeometryRecord>().get(pG);
00338   geo = pG.product();
00339 
00340   // Lumi_n=iEvent.luminosityBlock();
00341   parameters_.useEcal = true;
00342   parameters_.useHcal = true;
00343   parameters_.useCalo = false;
00344   parameters_.useMuon = false;
00345   parameters_.dREcal = taECALCone_;
00346   parameters_.dRHcal = taHCALCone_;
00347 
00348 
00349   //cout<<"Hello World. TrackCollectionSize: "<< pixelTracks->size()<<endl;
00350 
00351   //cout<<" generalTracks Size: "<< generalTracks->size()<<endl;
00352   int n = generalTracks->size();
00353   nTracks->Fill(n);
00354 
00355   if(takeGenTracks_ && iEvent.id().event()%10==1)
00356     {
00357       gen = generalTracks->size();
00358       iso = isoProdTracks->size();
00359       pix = pixelTracks->size();
00360       
00361       genPt[0] = -33;             
00362       genPhi[0] = -33;            
00363       genEta[0] = -33;            
00364       
00365       isoPt[0] = -33;             
00366       isoPhi[0] = -33;            
00367       isoEta[0] = -33;            
00368       
00369       pixPt[0] = -33;             
00370       pixPhi[0] = -33;            
00371       pixEta[0] = -33;            
00372       
00373       Int_t gencount=0, isocount=0, pixcount=0;
00374       for (reco::TrackCollection::const_iterator gentr=generalTracks->begin(); gentr!=generalTracks->end(); gentr++)
00375         {
00376           genPt[gencount] = gentr->pt();          
00377           genPhi[gencount] = gentr->phi();        
00378           genEta[gencount] = gentr->eta();        
00379           gencount++;
00380         }
00381       
00382       for (reco::TrackCollection::const_iterator isotr=isoProdTracks->begin(); isotr!=isoProdTracks->end(); isotr++)
00383         {
00384           isoPt[isocount] = isotr->pt();          
00385           isoPhi[isocount] = isotr->phi();        
00386           isoEta[isocount] = isotr->eta();        
00387           isocount++;
00388         }
00389       
00390       for (reco::IsolatedPixelTrackCandidateCollection::const_iterator pixtr=pixelTracks->begin(); pixtr!=pixelTracks->end(); pixtr++)
00391         {
00392           pixPt[pixcount] = pixtr->pt();          
00393           pixPhi[pixcount] = pixtr->phi();        
00394           pixEta[pixcount] = pixtr->eta();        
00395           pixcount++;
00396         }
00397     }
00398 
00399   tTree -> Fill();
00400 
00401 if (pixelTracks->size()==0) return;
00402   
00403 
00404 for (reco::TrackCollection::const_iterator trit=isoProdTracks->begin(); trit!=isoProdTracks->end(); trit++)
00405     {
00406       
00407       reco::IsolatedPixelTrackCandidateCollection::const_iterator isoMatched=pixelTracks->begin();
00408       //reco::TrackCollection::const_iterator isoMatched=pixelTracks->begin();
00409       bool matched=false;
00410  
00411    //for (reco::IsolatedPixelTrackCandidateCollection::const_iterator trit = pixelTracks->begin(); trit!=pixelTracks->end(); trit++)
00412    for (reco::IsolatedPixelTrackCandidateCollection::const_iterator it = pixelTracks->begin(); it!=pixelTracks->end(); it++)
00413    //for (reco::TrackCollection::const_iterator it = pixelTracks->begin(); it!=pixelTracks->end(); it++)
00414    { 
00415 
00416           if (abs((trit->pt() - it->pt())/it->pt()) < 0.005 && abs(trit->eta() - it->eta()) < 0.01) 
00417            {
00418              isoMatched=it;
00419              matched=true;
00420              break;
00421            }
00422         }
00423       // CUT
00424 
00425    if (!matched) continue;
00426    if(isoMatched->maxPtPxl() > maxPNear) continue;
00427     
00428    ptNear = isoMatched->maxPtPxl();      
00429    //cout<<"Point 0.1  isoMatch. ptnear: "<<ptNear<<endl;
00430 
00431       
00432       // CUT
00433       if (trit->hitPattern().numberOfValidHits()<MinNTrackHitsBarrel) continue;
00434       if (fabs(trit->eta())>1.47&&trit->hitPattern().numberOfValidStripTECHits()<MinNTECHitsEndcap) continue;
00435 
00436       //cout<<"Point 0.2.1 after numofvalidhits HB: "<<trit->hitPattern().numberOfValidHits()<<endl;
00437       //cout<<"Point 0.2.2 after numofvalidstrips HE: "<<trit->hitPattern().numberOfValidStripTECHits()<<endl;
00438 
00439   numVH = trit->hitPattern().numberOfValidHits();
00440   numVS = trit->hitPattern().numberOfValidStripTECHits();
00441       
00442     
00443       
00444       trackE = sqrt(trit->px()*trit->px()+trit->py()*trit->py()+trit->pz()*trit->pz()+0.14*0.14);
00445       trackPt = trit->pt();
00446       trackEta = trit->eta();
00447       trackPhi = trit->phi();
00448 
00449       emEnergy = isoMatched->energyIn();
00450 
00451       //cout<<"Point 0.3.  Matched :: pt: "<<trit->pt()<<" wholeEnergy: "<<trackE<<"  emEnergy: "<<emEnergy<<"  eta: "<<etahcal<<" phi: "<<phihcal<<endl;
00452       //cout<<"Point 0.4.  EM energy in cone: "<<emEnergy<<"  EtaHcal: "<<etahcal<<"  PhiHcal: "<<phihcal<<endl;
00453 
00454       TrackDetMatchInfo info = trackAssociator_.associate(iEvent, iSetup,trackAssociator_.getFreeTrajectoryState(iSetup, *trit), parameters_);
00455       
00456       //float etaecal=info.trkGlobPosAtEcal.eta();
00457       //float phiecal=info.trkGlobPosAtEcal.phi();
00458       
00459 
00460       float etahcal=info.trkGlobPosAtHcal.eta();
00461       float phihcal=info.trkGlobPosAtHcal.phi();
00462 
00463 
00464         xTrkEcal=info.trkGlobPosAtEcal.x();
00465         yTrkEcal=info.trkGlobPosAtEcal.y();
00466         zTrkEcal=info.trkGlobPosAtEcal.z();
00467 
00468         xTrkHcal=info.trkGlobPosAtHcal.x();
00469         yTrkHcal=info.trkGlobPosAtHcal.y();
00470         zTrkHcal=info.trkGlobPosAtHcal.z();
00471 
00472         GlobalPoint gP(xTrkHcal,yTrkHcal,zTrkHcal);
00473 
00474 
00475         int iphitrue = -10;
00476         int ietatrue = 100;
00477 
00478         if (abs(etahcal)<1.392) 
00479           {
00480             const CaloSubdetectorGeometry* gHB = geo->getSubdetectorGeometry(DetId::Hcal,HcalBarrel);
00481             const HcalDetId tempId = gHB->getClosestCell(gP);
00482             ietatrue = tempId.ieta();
00483             iphitrue = tempId.iphi();
00484           }
00485 
00486         if (abs(etahcal)>1.392 &&  abs(etahcal)<3.0) 
00487           {
00488             const CaloSubdetectorGeometry* gHE = geo->getSubdetectorGeometry(DetId::Hcal,HcalEndcap);
00489             const HcalDetId tempId = gHE->getClosestCell(gP);
00490             ietatrue = tempId.ieta();
00491             iphitrue = tempId.iphi();
00492           }
00493 
00494       /*
00495         float dphi = fabs(info.trkGlobPosAtHcal.phi() - phihit); 
00496         if(dphi > 4.*atan(1.)) dphi = 8.*atan(1.) - dphi;
00497         float deta = fabs(info.trkGlobPosAtHcal.eta() - etahit); 
00498         float dr = sqrt(dphi*dphi + deta*deta);
00499       */
00500 
00501 
00502        MaxHit_struct MaxHit;
00503        
00504        MaxHit.hitenergy=-100.;
00505 
00506       //container for used recHits
00507       std::vector<int> usedHits; 
00508       //  
00509       usedHits.clear();
00510       //cout <<"Point 1. Entrance to HBHECollection"<<endl;
00511 
00512       //float dddeta = 1000.;
00513       //float dddphi = 1000.;
00514       //int iphitrue = 1234;
00515       //int ietatrue = 1234;
00516 
00517       GlobalPoint gPhot;
00518 
00519 
00520       for (HBHERecHitCollection::const_iterator hhit=Hithbhe.begin(); hhit!=Hithbhe.end(); hhit++) 
00521         {
00522           
00523           //check that this hit was not considered before and push it into usedHits
00524           bool hitIsUsed=false;
00525           int hitHashedIndex=hhit->id().hashed_index();
00526           for (uint32_t i=0; i<usedHits.size(); i++)
00527             {
00528               if (usedHits[i]==hitHashedIndex) hitIsUsed=true;
00529             }
00530           if (hitIsUsed) continue;
00531           usedHits.push_back(hitHashedIndex);
00532           //
00533 
00534           // rof 16.05.2008 start: include the possibility for recalibration
00535           float recal = 1;
00536           // rof end
00537 
00538           GlobalPoint pos = geo->getPosition(hhit->detid());
00539           float phihit = pos.phi();
00540           float etahit = pos.eta();
00541           
00542           int iphihitm  = (hhit->id()).iphi();
00543           int ietahitm  = (hhit->id()).ieta();
00544           int depthhit = (hhit->id()).depth();
00545           float enehit = hhit->energy() * recal;
00546           
00547           if (depthhit!=1) continue;
00548            
00549           float dphi = fabs(phihcal - phihit); 
00550           if(dphi > 4.*atan(1.)) dphi = 8.*atan(1.) - dphi;
00551           float deta = fabs(etahcal - etahit); 
00552           float dr = sqrt(dphi*dphi + deta*deta);
00553           
00554           /*
00555           if (deta<dddeta) {
00556            ietatrue = ietahitm;
00557            dddeta=deta;
00558           }
00559           
00560           if (dphi<dddphi) {
00561           iphitrue = iphihitm;
00562           dddphi=dphi;
00563           }
00564           */
00565           
00566           if(dr<associationConeSize_) 
00567             {
00568               
00569               for (HBHERecHitCollection::const_iterator hhit2=Hithbhe.begin(); hhit2!=Hithbhe.end(); hhit2++) 
00570                 {
00571                   int iphihitm2  = (hhit2->id()).iphi();
00572                   int ietahitm2  = (hhit2->id()).ieta();
00573                   int depthhit2 = (hhit2->id()).depth();
00574                   float enehit2 = hhit2->energy() * recal;
00575                   
00576                   if (iphihitm==iphihitm2 && ietahitm==ietahitm2  && depthhit!=depthhit2)  enehit = enehit+enehit2;
00577                 
00578                 }                 
00579               
00580 
00581               //cout<<"IN CONE ieta: "<<ietahitm<<"  iphi: "<<iphihitm<<" depthhit: "<<depthhit<<"  dr: "<<dr<<" energy: "<<enehit<<endl;        
00582 
00583               //Find a Hit with Maximum Energy
00584               
00585               if(enehit > MaxHit.hitenergy) 
00586                 {
00587                   MaxHit.hitenergy =  enehit;
00588                   MaxHit.ietahitm   = (hhit->id()).ieta();
00589                   MaxHit.iphihitm   = (hhit->id()).iphi();
00590                   MaxHit.dr   = dr;
00591                   //MaxHit.depthhit  = (hhit->id()).depth();
00592                   MaxHit.depthhit  = 1;
00593 
00594                   //gPhot = geo->getPosition(hhit->detid());
00595                 }
00596 
00597             }
00598         } //end of all HBHE hits cycle
00599       
00600       usedHits.clear();
00601 
00602       //cout<<"Hottest ieta: "<<MaxHit.ietahitm<<"  iphi: "<<MaxHit.iphihitm<<"  dr: "<<MaxHit.dr<<endl;    
00603       //cout<<"Track   ieta: "<<ietatrue<<"  iphi: "<<iphitrue<<endl;    
00604       
00605       //cout<<"Point 3.  MaxHit :::En "<<MaxHit.hitenergy<<"  ieta: "<<MaxHit.ietahitm<<"  iphi: "<<MaxHit.iphihitm<<endl;
00606       
00607       Bool_t passCuts = kFALSE;
00608       if(trackE > energyMinIso && trackE < energyMaxIso && emEnergy < energyECALmip && MaxHit.hitenergy > 0. && abs(MaxHit.ietahitm)<29) passCuts = kTRUE;
00609       
00610       //cout<<"Pont 0.1.1.  trackE:"<<trackE<<"  emEn: "<<emEnergy<<endl;
00611       
00612 
00613       numHits=0;
00614       
00615       eClustBefore = 0.0;
00616       eClustAfter = 0.0;
00617       eBeforeDepth1 = 0.0;
00618       eAfterDepth1 = 0.0;
00619       eBeforeDepth2 = 0.0;    
00620       eAfterDepth2 = 0.0;
00621       CentHitFactor = 0.0; 
00622       e3x3After = 0.0;
00623       e3x3Before = 0.0;
00624       e5x5After = 0.0;
00625       e5x5Before = 0.0;
00626 
00627                   
00628       for (HBHERecHitCollection::const_iterator hhit=Hithbhe.begin(); hhit!=Hithbhe.end(); hhit++) 
00629         {
00630           
00631           //check that this hit was not considered before and push it into usedHits
00632           bool hitIsUsed=false;
00633           int hitHashedIndex=hhit->id().hashed_index();
00634           for (uint32_t i=0; i<usedHits.size(); i++)
00635             {
00636               if (usedHits[i]==hitHashedIndex) hitIsUsed=true;
00637             }
00638           if (hitIsUsed) continue;
00639           usedHits.push_back(hitHashedIndex);
00640           
00641           int DIETA = 100;
00642           if(MaxHit.ietahitm*(hhit->id()).ieta()>0)
00643             {
00644               DIETA = MaxHit.ietahitm - (hhit->id()).ieta();
00645             }
00646           if(MaxHit.ietahitm*(hhit->id()).ieta()<0)
00647             {
00648               DIETA = MaxHit.ietahitm - (hhit->id()).ieta();
00649               DIETA = DIETA>0 ? DIETA-1 : DIETA+1; 
00650             }
00651           
00652           int DIPHI = abs(MaxHit.iphihitm - (hhit->id()).iphi());
00653           DIPHI = DIPHI>36 ? 72-DIPHI : DIPHI;
00654           
00655 
00656 
00657           int numbercell=100; //always collect Wide clastor!
00658 
00659           //if(AxB_=="5x5" || AxB_=="3x3" || AxB_=="7x7"|| AxB_=="Cone")
00660 
00661           //if(AxB_=="3x3") numbercell = 1;
00662           //if(AxB_=="5x5") numbercell = 2;
00663           //if(AxB_=="Cone") numbercell = 1000;
00664 
00665           if( abs(DIETA)<=numbercell && (abs(DIPHI)<=numbercell || ( abs(MaxHit.ietahitm)>=20 && abs(DIPHI)<=numbercell+1)) )
00666             {
00667               const GlobalPoint pos2 = geo->getPosition(hhit->detid());
00668 
00669               if(passCuts && hhit->energy()>0)
00670                 {
00671                   
00672                   
00673                   float factor = 0.0;     
00674                   // factor = CalibFactors[hhit->id()];
00675                   factor = respRecalib -> getValues(hhit->id())->getValue();
00676 
00677                   
00678                   
00679                   //if(i<5){cout<<" calib factors: "<<factor<<"  ij "<<100*i+j<<endl;}
00680                   
00681                   if (hhit->id().ieta() == MaxHit.ietahitm && hhit->id().iphi() == MaxHit.iphihitm) CentHitFactor=factor;         
00682                   
00683                   if (hhit->id().ieta() == MaxHit.ietahitm && hhit->id().iphi() == MaxHit.iphihitm) iTime = hhit->time();         
00684                   
00685                   if(AxB_!="3x3" && AxB_!="5x5" && AxB_!="Cone") LogWarning(" AxB ")<<"   Not supported: "<< AxB_;
00686                   
00687                   
00688                   if (abs(DIETA)<=2 && (abs(DIPHI)<=2 || ((abs(MaxHit.ietahitm)>20 && abs(DIPHI)<=4) && !( (abs(MaxHit.ietahitm)==21 || abs(MaxHit.ietahitm)==22) && abs((hhit->id()).ieta())<=20 && abs(DIPHI)>2) )) )
00689                     {
00690 
00691                       e5x5Before += hhit->energy();
00692                       e5x5After += hhit->energy()*factor;
00693                     }
00694                   
00695                   
00696                   if (abs(DIETA)<=1 && (abs(DIPHI)<=1 || ((abs(MaxHit.ietahitm)>20 && abs(DIPHI)<=2) && !(abs(MaxHit.ietahitm)==21 && abs((hhit->id()).ieta())<=20 && abs(DIPHI)>1) )) )
00697                     {
00698 
00699                       e3x3Before += hhit->energy();
00700                       e3x3After += hhit->energy()*factor;
00701                     }
00702                       
00703                       
00704                   if(AxB_=="5x5") 
00705                     {
00706                       
00707                       if (abs(DIETA)<=2 && (abs(DIPHI)<=2 || ( abs(MaxHit.ietahitm)>20 && abs(DIPHI)<=4) ) )
00708                         {
00709                           if (abs(MaxHit.ietahitm)==21 && abs((hhit->id()).ieta())<=20 && abs(DIPHI)>3) continue;
00710                           
00711                           HTime[numHits]=  hhit->time();
00712                           numHits++;
00713                                                   
00714                           eClustBefore += hhit->energy();
00715                           eClustAfter += hhit->energy()*factor;
00716                                                   
00717                           if ((hhit->id().depth() == 1) && (abs(hhit->id().ieta()) > 17) && (abs(hhit->id().ieta()) < 29))
00718                             {   
00719                               eBeforeDepth1 += hhit->energy();
00720                               eAfterDepth1 += hhit->energy()*factor;
00721                             }
00722                           else if((hhit->id().depth() == 2) && (abs(hhit->id().ieta()) > 17) && (abs(hhit->id().ieta()) < 29))
00723                             {
00724                               eBeforeDepth2 += hhit->energy();
00725                               eAfterDepth2 += hhit->energy()*factor;
00726                             }
00727                         }
00728                     }//end of 5x5
00729                   
00730                   
00731                   
00732                   if(AxB_=="3x3") 
00733                     {
00734                       if (abs(DIETA)<=1 && (abs(DIPHI)<=1 || ( abs(MaxHit.ietahitm)>20 && abs(DIPHI)<=2) ) )
00735                         {
00736                           if (abs(MaxHit.ietahitm)==21 && abs((hhit->id()).ieta())<=20 && abs(DIPHI)>2) continue;
00737                                 
00738                           HTime[numHits]=  hhit->time();
00739                           numHits++;                          
00740                                                   
00741                           eClustBefore += hhit->energy();
00742                           eClustAfter += hhit->energy() * factor;
00743                           
00744                           if ((hhit->id().depth() == 1) && (abs(hhit->id().ieta()) > 17) && (abs(hhit->id().ieta()) < 29))
00745                             {   
00746                               eBeforeDepth1 += hhit->energy();
00747                               eAfterDepth1 += hhit->energy() * factor;
00748                             }
00749                           else if((hhit->id().depth() == 2) && (abs(hhit->id().ieta()) > 17) && (abs(hhit->id().ieta()) < 29))
00750                             {
00751                               eBeforeDepth2 += hhit->energy();
00752                               eAfterDepth2 += hhit->energy() * factor;
00753                             }
00754                           
00755                         }
00756                     }//end of 3x3
00757                   
00758                                   
00759                   if (AxB_=="Cone" && getDistInPlaneSimple(gP, pos2) < calibrationConeSize_) {
00760                     
00761                     HTime[numHits]=  hhit->time();
00762                     numHits++;                        
00763                                     
00764                     eClustBefore += hhit->energy();
00765                     eClustAfter += hhit->energy() * factor;
00766                     
00767                     if ((hhit->id().depth() == 1) && (abs(hhit->id().ieta()) > 17) && (abs(hhit->id().ieta()) < 29))
00768                       { 
00769                         eBeforeDepth1 += hhit->energy();
00770                         eAfterDepth1 += hhit->energy() * factor;
00771                       }
00772                     else if((hhit->id().depth() == 2) && (abs(hhit->id().ieta()) > 17) && (abs(hhit->id().ieta()) < 29))
00773                       {
00774                         eBeforeDepth2 += hhit->energy();
00775                         eAfterDepth2 += hhit->energy() * factor;
00776                       }
00777                     
00778                     
00779                   }//end of Cone
00780                   
00781                 }//end of passCuts
00782               
00783             }//end of DIETA DIPHI
00784           
00785         } //end of associatedcone HBHE hits cycle
00786       
00787       
00788       int dieta_M_P = 100;
00789       int diphi_M_P = 100;
00790       if(MaxHit.ietahitm*ietatrue>0) {dieta_M_P = abs (MaxHit.ietahitm-ietatrue);}
00791       if(MaxHit.ietahitm*ietatrue<0) {dieta_M_P = abs(MaxHit.ietahitm-ietatrue)-1;}
00792       diphi_M_P = abs(MaxHit.iphihitm-iphitrue);
00793       diphi_M_P =  diphi_M_P>36 ? 72-diphi_M_P : diphi_M_P; 
00794       
00795       
00796       if(passCuts)
00797         
00798         {
00799           eventNumber = iEvent.id().event();
00800           runNumber = iEvent.id().run();
00801           
00802           eCentHitBefore = MaxHit.hitenergy;
00803           eCentHitAfter = MaxHit.hitenergy*CentHitFactor;
00804           eECAL = emEnergy;
00805           numValidTrkHits = numVH;
00806           numValidTrkStrips = numVS;
00807           PtNearBy = ptNear;
00808           
00809           
00810           eTrack = trackE;
00811           phiTrack = trackPhi;
00812           etaTrack = trackEta;
00813           
00814           iEta = MaxHit.ietahitm;
00815           iPhi = MaxHit.iphihitm;
00816           
00817           iEtaTr = ietatrue;
00818           iPhiTr = iphitrue;
00819           iDr = sqrt(diphi_M_P*diphi_M_P+dieta_M_P*dieta_M_P);
00820           delR = MaxHit.dr;
00821           dietatr = dieta_M_P;
00822           diphitr = diphi_M_P;
00823           
00824           fTree -> Fill();
00825         }
00826 
00827     } //end of isoProdTracks cycle
00828 
00829 }
00830 
00831 // ------------ method called once each job just before starting event loop  ------------
00832 void 
00833 ValidIsoTrkCalib::beginJob()
00834 {
00835 
00836   // if(!ReadCalibFactors(calibFactorsFileName_.c_str() )) {cout<<"Cant read file with cailib coefficients!! ---"<<endl;}
00837 
00838 // try{   
00839 //   edm::ESHandle <HcalRespCorrs> recalibCorrs;
00840 //   iSetup.get<HcalRespCorrsRcd>().get("recalibrate",recalibCorrs);
00841 //   respRecalib = recalibCorrs.product();
00842 //
00843 //   LogInfo("CalibConstants")<<"  Loaded:  OK ";
00844 //   
00845 // }catch(const cms::Exception & e) {
00846 //   LogWarning("CalibConstants")<<"   Not Found!! ";
00847 // }
00848  
00849  
00850  //  rootFile = new TFile(outputFileName_.c_str(),"RECREATE");
00851  
00852  //@@@@@@@@@@@@@
00853   TFileDirectory ValDir = fs->mkdir("Validation");
00854  
00855  nTracks = fs->make<TH1F>("nTracks","general;number of general tracks",11,-0.5,10.5);
00856 
00857   
00858  tTree = new TTree("tTree", "Tree for gen info"); 
00859 
00860   if(takeGenTracks_) { 
00861     tTree->Branch("gen", &gen, "gen/I");
00862     tTree->Branch("iso", &iso, "iso/I");
00863     tTree->Branch("pix", &pix, "pix/I");
00864     tTree->Branch("genPt", genPt, "genPt[gen]/F");
00865     tTree->Branch("genPhi", genPhi, "genPhi[gen]/F");
00866     tTree->Branch("genEta", genEta, "genEta[gen]/F");
00867     
00868     tTree->Branch("isoPt", isoPt, "isoPt[iso]/F");
00869     tTree->Branch("isoPhi", isoPhi, "isoPhi[iso]/F");
00870     tTree->Branch("isoEta", isoEta, "isoEta[iso]/F");
00871     
00872     tTree->Branch("pixPt", pixPt, "pixPt[pix]/F");
00873     tTree->Branch("pixPhi", pixPhi, "pixPhi[pix]/F");
00874     tTree->Branch("pixEta", pixEta, "pixEta[pix]/F");
00875   }
00876 
00877   fTree = new TTree("fTree", "Tree for IsoTrack Calibration"); 
00878    
00879   fTree->Branch("eventNumber", &eventNumber, "eventNumber/I");
00880   fTree->Branch("runNumber", &runNumber, "runNumber/I");
00881 
00882   fTree->Branch("eClustBefore", &eClustBefore,"eClustBefore/F");
00883   fTree->Branch("eClustAfter", &eClustAfter,"eClustAfter/F");
00884   fTree->Branch("eTrack", &eTrack, "eTrack/F");
00885   fTree->Branch("etaTrack", &etaTrack, "etaTrack/F");
00886   fTree->Branch("phiTrack", &phiTrack, "phiTrack/F");
00887     
00888   fTree->Branch("numHits", &numHits, "numHits/I");
00889   fTree->Branch("eECAL", &eECAL, "eECAL/F");
00890   fTree->Branch("PtNearBy", &PtNearBy, "PtNearBy/F");
00891   fTree->Branch("numValidTrkHits", &numValidTrkHits, "numValidTrkHits/F");
00892   fTree->Branch("numValidTrkStrips", &numValidTrkStrips, "numValidTrkStrips/F");
00893   
00894   fTree->Branch("eBeforeDepth1", &eBeforeDepth1, "eBeforeDepth1/F");
00895   fTree->Branch("eBeforeDepth2", &eBeforeDepth2, "eBeforeDepth2/F");
00896   fTree->Branch("eAfterDepth1", &eAfterDepth1, "eAfterDepth1/F");
00897   fTree->Branch("eAfterDepth2", &eAfterDepth2, "eAfterDepth2/F");
00898   
00899   fTree->Branch("e3x3Before", &e3x3Before, "e3x3Before/F");
00900   fTree->Branch("e3x3After", &e3x3After, "e3x3After/F");
00901   fTree->Branch("e5x5Before", &e5x5Before, "e5x5Before/F");
00902   fTree->Branch("e5x5After", &e5x5After, "e5x5After/F");
00903   
00904   fTree->Branch("eCentHitAfter", &eCentHitAfter, "eCentHitAfter/F");
00905   fTree->Branch("eCentHitBefore", &eCentHitBefore, "eCentHitBefore/F");
00906   fTree->Branch("iEta", &iEta, "iEta/I");
00907   fTree->Branch("iPhi", &iPhi, "iPhi/I");
00908 
00909 
00910   fTree->Branch("iEtaTr", &iEtaTr, "iEtaTr/I");
00911   fTree->Branch("iPhiTr", &iPhiTr, "iPhiTr/I");
00912   fTree->Branch("dietatr", &dietatr, "dietatr/I");
00913   fTree->Branch("diphitr", &diphitr, "diphitr/I");
00914   fTree->Branch("iDr", &iDr, "iDr/F");
00915   fTree->Branch("delR", &delR, "delR/F");
00916 
00917   fTree->Branch("iTime", &iTime, "iTime/F");
00918   fTree->Branch("HTime", HTime, "HTime[numHits]/F");
00919 
00920     fTree->Branch("xTrkEcal", &xTrkEcal, "xTrkEcal/F");
00921     fTree->Branch("yTrkEcal", &yTrkEcal, "yTrkEcal/F");
00922     fTree->Branch("zTrkEcal", &zTrkEcal, "zTrkEcal/F");
00923     fTree->Branch("xTrkHcal", &xTrkHcal, "xTrkHcal/F");
00924     fTree->Branch("yTrkHcal", &yTrkHcal, "yTrkHcal/F");
00925     fTree->Branch("zTrkHcal", &zTrkHcal, "zTrkHcal/F");
00926 
00927 }
00928 
00929 
00930 
00931 // ------------ method called once each job just after ending the event loop  ------------
00932 void 
00933 ValidIsoTrkCalib::endJob() 
00934 {
00935   //  rootFile->Write();
00936   //rootFile->Close();
00937 }
00938 
00939 //define this as a plug-in
00940 DEFINE_FWK_MODULE(ValidIsoTrkCalib);