CMS 3D CMS Logo

EcalEleCalibLooper.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include <cmath>
00003 #include <iostream>
00004 #include "FWCore/Framework/interface/Frameworkfwd.h"
00005 #include "FWCore/Framework/interface/MakerMacros.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 #include "Calibration/EcalCalibAlgos/interface/IMACalibBlock.h"
00008 #include "Calibration/EcalCalibAlgos/interface/L3CalibBlock.h"
00009 #include "Calibration/EcalCalibAlgos/interface/EcalEleCalibLooper.h"
00010 #include "DataFormats/Common/interface/Handle.h"
00011 #include "DataFormats/EgammaReco/interface/ClusterShape.h"
00012 //LP includes to read/write the original coeff
00013 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00014 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00015 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00016 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00017 #include "CondFormats/DataRecord/interface/EcalIntercalibConstantsRcd.h"
00018 #include "Calibration/Tools/interface/calibXMLwriter.h"
00019 #include "CalibCalorimetry/CaloMiscalibTools/interface/CaloMiscalibTools.h"
00020 //DS verify all these include
00021 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
00022 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00023 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00024 #include "DataFormats/EgammaReco/interface/ClusterShapeFwd.h"
00025 #include "TH1.h"
00026 #include "TH2.h"
00027 #include "TFile.h"
00028 //DS LogMessages
00029 #include "FWCore/MessageLogger/interface/MessageLogger.h" 
00030 
00031 
00032 
00033 //----------------------------------------------------------------
00034 
00035 
00037 EcalEleCalibLooper::EcalEleCalibLooper (const edm::ParameterSet& iConfig) :
00038       m_barrelAlCa (iConfig.getParameter<edm::InputTag> ("alcaBarrelHitCollection")) ,
00039       m_endcapAlCa (iConfig.getParameter<edm::InputTag> ("alcaEndcapHitCollection")) ,
00040       m_recoWindowSide (iConfig.getParameter<int> ("recoWindowSide")) ,
00041       m_etaWidth (iConfig.getParameter<int> ("etaWidth")) ,
00042  //PG fin dove andare a cercare cristalli da aggiungere a pSubtract
00043  //PG fuori dalla regione da calibrare     
00044       //m_etaBorder (iConfig.getParameter<int> ("etaBorder")) ,
00045       m_phiWidthEB (iConfig.getParameter<int> ("phiWidthEB")) ,
00046  //PG fin dove andare a cercare cristalli da aggiungere a pSubtract
00047  //PG fuori dalla regione da calibrare (ci vuole anche per EE)    
00048       //m_phiBorderEB (iConfig.getParameter<int> ("phiBorderEB")) ,
00049       m_etaStart (etaShifter (iConfig.getParameter<int> ("etaStart"))) , 
00050       m_etaEnd (etaShifter (iConfig.getParameter<int> ("etaEnd"))) ,
00051       m_phiStartEB (iConfig.getParameter<int> ("phiStartEB")) , 
00052       m_phiEndEB (iConfig.getParameter<int> ("phiEndEB")),
00053       m_radStart (iConfig.getParameter<int> ("radStart")) , 
00054       m_radEnd (iConfig.getParameter<int> ("radEnd")) ,
00055       m_radWidth (iConfig.getParameter<int> ("radWidth")) ,
00056  //PG fin dove andare a cercare cristalli da aggiungere a pSubtract
00057  //PG fuori dalla regione da calibrare     
00058       //m_radBorder (iConfig.getParameter<int> ("radBorder")),
00059       m_phiStartEE (iConfig.getParameter<int> ("phiStartEE")) ,
00060       m_phiEndEE (iConfig.getParameter<int> ("phiEndEE")) ,
00061       m_phiWidthEE (iConfig.getParameter<int> ("phiWidthEE")) ,
00062 //PG per applicare tagli al punto di impatto
00063 //PG sulla faccia frontale del cristallo, importante per il testbeam      
00064 //FIXME      m_halfXBand (iConfig.getParameter<double> ("halfXBand")) ,
00065 //FIXME      m_halfYBand (iConfig.getParameter<double> ("halfYBand")) ,
00066       m_maxSelectedNumPerXtal (iConfig.getParameter<int> ("maxSelectedNumPerCrystal")) ,  
00067       m_minEnergyPerCrystal (iConfig.getParameter<double> ("minEnergyPerCrystal")),
00068       m_maxEnergyPerCrystal (iConfig.getParameter<double> ("maxEnergyPerCrystal")) ,
00069       m_minCoeff (iConfig.getParameter<double> ("minCoeff")) ,
00070       m_maxCoeff (iConfig.getParameter<double> ("maxCoeff")) ,
00071       m_usingBlockSolver (iConfig.getParameter<int> ("usingBlockSolver")) ,
00072       //m_minAccept (iConfig.getParameter<double> ("minAccept")) ,
00073       //m_maxAccept (iConfig.getParameter<double> ("maxAccept")) ,
00074       m_loops (iConfig.getParameter<int> ("loops")),
00075       m_ElectronLabel (iConfig.getParameter<edm::InputTag> ("electronLabel"))
00076   //Controls the parameters and their conversions
00077 {
00078    edm::LogInfo ("IML") << "[EcalEleCalibLooper][ctor] asserts" ;
00079    assert ( (m_radEnd - m_radStart)%m_radWidth == 0) ; 
00080    assert ( (m_etaEnd-m_etaStart)%m_etaWidth == 0) ; 
00081    assert (m_etaStart >=0 && m_etaStart < 170);
00082    assert (m_etaEnd >= m_etaStart && m_etaEnd <= 170);
00083 //        assert (m_phiStartEB >=0 && m_phiStartEB < 360);
00084 //        assert (m_phiEndEB >= m_phiStartEB && m_phiEndEB <= 360);
00085 //PG questi due si possono sostituire con 
00086 //PG m_phiStartEE %= 360 ;
00087 //PG if (m_phiStartEE < 0) m_phiStartEE += 360 ;
00088 //PG ed analogo per l'end, credo
00089 //        assert (m_phiStartEE >=0); // || m_phiStartEE < 360);
00090 //        assert (m_phiEndEE >=0); // m_phiStartEE || m_phiEndEE < 360);
00091    assert (m_radStart >=0 && m_radStart <= 50);
00092    assert (m_radEnd >= m_radStart && m_radEnd <= 50);
00093    edm::LogInfo ("IML") << "[EcalEleCalibLooper][ctor] entering " ;
00094    //PG FIXME questi posso farli direttamente con barrelcells
00095    int index;
00096    for (int a=0; a<170; ++a)
00097      for (int b=0; b<360; ++b)
00098        {
00099          index = EBDetId::unhashIndex (a*360+b).rawId ();
00100          m_recalibMap[index] = 1. ;
00101          m_xtalNumOfHits[index] = 0 ;
00102        }
00103    for (int i=0; i<100; ++i)
00104     for (int j=0; j<100; ++j)
00105       {
00106         if (EEDetId::validDetId (i+1, j+1, 1))
00107           {
00108             index = EEDetId (i+1, j+1, 1).rawId ();
00109             m_xtalNumOfHits[index]=0;          
00110             m_recalibMap[index] = 1. ;
00111           }
00112         if (EEDetId::validDetId (i+1, j+1, -1))
00113           {
00114             index = EEDetId (i+1, j+1, -1).rawId ();
00115             m_recalibMap[index]= 1. ;
00116             m_xtalNumOfHits[index]=0;
00117           }
00118       }
00119    edm::LogInfo ("IML") << "[EcalEleCalibLooper][ctor] region definition" ;
00120    EBRegionDefinition () ;
00121    EERegionDefinition () ;
00123    TH2F * EBRegion = new TH2F ("EBRegion","EBRegion",170,0,170,360,0,360) ;
00124    for (int eta = 0; eta<170; ++eta)
00125       for (int phi = 0; phi <360; ++phi){
00126         EBRegion->Fill (eta, phi,m_xtalRegionId[EBDetId::unhashIndex(eta*360+phi).rawId()] );
00127        }
00128    TH2F * EERegion = new TH2F ("EERegion", "EERegion",100,0,100,100,0,100);
00129    for (int x = 0; x<100; ++x)
00130       for (int y = 0; y<100;++y){
00131            if(EEDetId::validDetId(x+1,y+1,1))
00132              EERegion->Fill(x,y,m_xtalRegionId[EEDetId(x+1,y+1,-1).rawId()]);
00133       }
00134           
00135    TFile out ("EBZone.root", "recreate");
00136      EBRegion->Write ();
00137      EERegion->Write ();
00138      out.Close ();
00140 
00141   //PG build the calibration algorithms for the regions
00142   //PG ------------------------------------------------
00143 
00144   edm::LogInfo ("IML") << "[EcalEleCalibLooper][ctor] Calib Block" ;
00145   std::string algorithm = iConfig.getParameter<std::string> ("algorithm") ;
00146   int eventWeight = iConfig.getUntrackedParameter<int> ("L3EventWeight",1) ;
00147 
00148   //PG loop over the regions set
00149   for (int region = 0 ; 
00150        region < EBregionsNum () + 2 * EEregionsNum () ; 
00151        ++region)
00152     {   
00153       if (algorithm == "IMA")
00154         m_EcalCalibBlocks.push_back (
00155             new IMACalibBlock (m_regions.at (region))
00156           ) ; 
00157       else if (algorithm == "L3")
00158         m_EcalCalibBlocks.push_back (
00159             new L3CalibBlock (m_regions.at (region), eventWeight)
00160           ) ; 
00161       else
00162         {
00163           edm::LogError ("building") << algorithm 
00164                           << " is not a valid calibration algorithm" ;
00165           exit (1) ;    
00166         }    
00167     } //PG loop over the regions set
00168  } //end ctor
00169 
00170 
00171 //---------------------------------------------------------------------------
00172 
00173 
00175 EcalEleCalibLooper::~EcalEleCalibLooper ()
00176 {
00177   edm::LogInfo ("IML") << "[EcalEleCalibLooper][dtor]" ;
00178   for (std::vector<VEcalCalibBlock *>::iterator calibBlock = m_EcalCalibBlocks.begin () ;
00179        calibBlock != m_EcalCalibBlocks.end () ;
00180        ++calibBlock) 
00181     delete (*calibBlock) ;
00182 
00183 }
00184 
00185 
00186 //---------------------------------------------------------------------------
00187 
00188 
00190 void 
00191 EcalEleCalibLooper::beginOfJob (const edm::EventSetup & iSetup) 
00192 {
00193   edm::LogInfo ("IML") << "[EcalEleCalibLooper][beginOfJob]" ;
00194   edm::ESHandle<CaloGeometry> geoHandle;
00195   iSetup.get<CaloGeometryRecord> ().get (geoHandle);
00196   const CaloGeometry& geometry = *geoHandle;
00197   m_barrelCells = geometry.getValidDetIds (DetId::Ecal, EcalBarrel);
00198   m_endcapCells = geometry.getValidDetIds (DetId::Ecal, EcalEndcap);
00199 }
00200 
00201 
00202 //----------------------------------------------------------------------
00203 
00204 
00207 void EcalEleCalibLooper::startingNewLoop (unsigned int ciclo) 
00208 {
00209   edm::LogInfo ("IML") << "[InvMatrixCalibLooper][Start] entering loop " << ciclo;
00210   for (std::vector<VEcalCalibBlock *>::iterator calibBlock = m_EcalCalibBlocks.begin () ;
00211        calibBlock != m_EcalCalibBlocks.end () ;
00212        ++calibBlock) 
00213     (*calibBlock)->reset () ;
00214   for (std::map<int,int>::iterator it= m_xtalNumOfHits.begin();
00215        it!=m_xtalNumOfHits.end();
00216        ++it)
00217     it->second = 0 ;
00218  return ;
00219 }
00220 
00221 
00222 //----------------------------------------------------------------------
00223 
00224 
00227 edm::EDLooper::Status
00228 EcalEleCalibLooper::duringLoop (const edm::Event& iEvent,
00229                              const edm::EventSetup&) 
00230 {
00231  const EBRecHitCollection* barrelHitsCollection = 0;
00232  edm::Handle<EBRecHitCollection> barrelRecHitsHandle ;
00233  iEvent.getByLabel (m_barrelAlCa, barrelRecHitsHandle) ;
00234  barrelHitsCollection = barrelRecHitsHandle.product () ;
00235  if (!barrelRecHitsHandle.isValid ()) {
00236      edm::LogError ("reading") << "[EcalEleCalibLooper] barrel rec hits not found" ;
00237      return  kContinue ;//maybe FIXME not with a kContinue but a skip only on the barrel part;
00238     }
00239 
00240  const EERecHitCollection * endcapHitsCollection = 0 ;
00241  edm::Handle<EERecHitCollection> endcapRecHitsHandle ;
00242  iEvent.getByLabel (m_endcapAlCa, endcapRecHitsHandle) ;
00243  endcapHitsCollection = endcapRecHitsHandle.product () ;
00244  if (!endcapRecHitsHandle.isValid ()) {  
00245      edm::LogError ("reading") << "[EcalEleCalibLooper] endcap rec hits not found" ; 
00246      return kContinue;
00247    }
00248 
00249  //Takes the electron collection of the pixel detector
00250  edm::Handle<reco::GsfElectronCollection> pElectrons;
00251  iEvent.getByLabel (m_ElectronLabel,pElectrons);
00252  if (!pElectrons.isValid ()) {
00253      edm::LogError ("reading")<< "[EcalEleCalibLooper] electrons not found" ;
00254      return kContinue;
00255    }
00256 
00257  //Start the loop over the electrons 
00258  for (eleIterator eleIt = pElectrons->begin ();
00259       eleIt != pElectrons->end ();
00260       ++eleIt )
00261    {
00262      double pSubtract = 0 ;
00263      double pTk = 0 ;
00264      DetId Max = findMaxHit (eleIt->superCluster ()->getHitsByDetId (), 
00265                              barrelHitsCollection,  endcapHitsCollection) ;
00266      // Continues if the findMaxHit doesn't find anything
00267      if (Max.det()==0) continue; 
00268 
00269      if (m_maxSelectedNumPerXtal > 0 && 
00270         m_xtalNumOfHits[Max.rawId ()] > m_maxSelectedNumPerXtal ) continue;
00271      ++m_xtalNumOfHits[Max.rawId()];
00272      std::map<int , double> xtlMap;
00273      int blockIndex =  m_xtalRegionId[Max.rawId ()] ;
00274      pTk = eleIt->trackMomentumAtVtx ().R ();
00275      if  ( Max.subdetId () == EcalBarrel  )
00276        {
00277          EBDetId EBmax = Max;
00278          if (EBregionCheck (etaShifter (EBmax.ieta ()), EBmax.iphi ()-1)) continue;//IN the future FIXME
00279          fillEBMap (EBmax, barrelHitsCollection, xtlMap,
00280                     blockIndex, pSubtract );
00281        }
00282      else 
00283        {
00284          EEDetId EEmax = Max;
00285                if (EEregionCheck (EEmax.ix ()-1, EEmax.iy ()-1)) continue ;
00286          fillEEMap (EEmax, endcapHitsCollection, xtlMap,
00287                     blockIndex, pSubtract ) ;
00288          pSubtract += eleIt->superCluster ()->preshowerEnergy () ;          
00289        }
00290      m_EcalCalibBlocks.at (blockIndex)->Fill (xtlMap.begin (), xtlMap.end (),pTk,pSubtract) ;
00291    } //End of the loop over the electron collection
00292 
00293   return  kContinue;
00294 } //end of duringLoop
00295 
00296 
00297 //----------------------------------------------------------------------
00298 
00299 
00303 edm::EDLooper::Status EcalEleCalibLooper::endOfLoop (const edm::EventSetup& dumb,unsigned int iCounter)
00304 {
00305  edm::LogInfo ("IML") << "[InvMatrixCalibLooper][endOfLoop] entering..." ;
00306  for (std::vector<VEcalCalibBlock *>::iterator calibBlock = m_EcalCalibBlocks.begin ();
00307        calibBlock!=m_EcalCalibBlocks.end ();
00308        ++calibBlock) 
00309    (*calibBlock)->solve (m_usingBlockSolver, m_minCoeff,m_maxCoeff);
00310 
00311   TH1F * EBcoeffEnd = new TH1F ("EBRegion","EBRegion",100,0.5,2.1) ;
00312   TH2F * EBcoeffMap = new TH2F ("EBcoeff","EBcoeff",171,-85,85,360,1,361);
00313   TH1F * EEPcoeffEnd = new TH1F ("EEPRegion", "EEPRegion",100,0.5,2.1);
00314   TH1F * EEMcoeffEnd = new TH1F ("EEMRegion", "EEMRegion",100,0.5,2.1);
00315   TH2F * EEPcoeffMap = new TH2F ("EEPcoeffMap","EEPcoeffMap",101,1,101,101,0,101);
00316   TH2F * EEMcoeffMap = new TH2F ("EEMcoeffMap","EEMcoeffMap",101,1,101,101,0,101);
00317  //loop over the barrel xtals to get the coeffs
00318  for (std::vector<DetId>::const_iterator barrelIt=m_barrelCells.begin();
00319        barrelIt!=m_barrelCells.end();++barrelIt)
00320         {
00321           EBDetId ee (*barrelIt);
00322           int index= barrelIt->rawId();
00323           if(m_xtalRegionId[index]==-1)continue;
00324           m_recalibMap[index] *= 
00325               m_EcalCalibBlocks.at(m_xtalRegionId[index])->at(m_xtalPositionInRegion[index]);
00326           EBcoeffEnd->Fill(m_recalibMap[index]);
00327           EBcoeffMap->Fill(ee.ieta(),ee.iphi(),m_recalibMap[index]);
00328         } //PG loop over phi
00329 
00330   // loop over the EndCap to get the recalib coefficients
00331     for(std::vector<DetId>::const_iterator endcapIt=m_endcapCells.begin();
00332          endcapIt!=m_endcapCells.end();++endcapIt)
00333     {
00334      EEDetId ee (*endcapIt);
00335      int index =endcapIt->rawId(); 
00336      if (ee.zside()>0) 
00337         { 
00338           if (m_xtalRegionId[index]==-1) continue ;
00339           m_recalibMap[index] *= 
00340              m_EcalCalibBlocks.at (m_xtalRegionId[index])->at (m_xtalPositionInRegion[index]);
00341           EEPcoeffEnd->Fill (m_recalibMap[index]) ;
00342           EEPcoeffMap->Fill (ee.ix(),ee.iy(),m_recalibMap[index]) ;
00343         }
00344       else
00345         {
00346           m_recalibMap[index] *= 
00347             m_EcalCalibBlocks.at (m_xtalRegionId[index])->at (m_xtalPositionInRegion[index]);
00348           EEMcoeffEnd->Fill (m_recalibMap[index]) ;
00349           EEMcoeffMap->Fill (ee.ix(),ee.iy(),m_recalibMap[index]) ;
00350         }
00351     } // loop over the EndCap to get the recalib coefficients
00352 
00353   edm::LogInfo ("IML") << "[InvMatrixCalibLooper][endOfLoop] End of endOfLoop" ;
00354 
00355   char filename[80];
00356   sprintf(filename,"coeffs%d.root",iCounter);
00357   TFile zout (filename, "recreate");
00358   EBcoeffEnd->Write () ;
00359   EBcoeffMap->Write () ;
00360   EEPcoeffEnd->Write () ;
00361   EEPcoeffMap->Write () ;
00362   EEMcoeffEnd->Write () ;
00363   EEMcoeffMap->Write () ;
00364   zout.Close () ;
00365   if (iCounter < m_loops-1 ) return kContinue ;
00366   else return kStop; 
00367 }
00368 
00369 
00370 //-------------------------------------------------------------------
00371 
00372 
00375 void 
00376 EcalEleCalibLooper::endOfJob ()
00377 {
00378  edm::LogInfo ("IML") << "[InvMatrixCalibLooper][endOfJob] saving calib coeffs" ;
00379 
00380 //Writes the coeffs 
00381  calibXMLwriter barrelWriter (EcalBarrel);
00382  calibXMLwriter endcapWriter (EcalEndcap);
00383  for (std::vector<DetId>::const_iterator barrelIt = m_barrelCells.begin (); 
00384        barrelIt!=m_barrelCells.end (); 
00385        ++barrelIt) 
00386    {
00387      EBDetId eb (*barrelIt);
00388      barrelWriter.writeLine (eb,m_recalibMap[barrelIt->rawId()]);
00389    }
00390  for (std::vector<DetId>::const_iterator endcapIt = m_endcapCells.begin ();
00391       endcapIt!=m_endcapCells.end ();
00392       ++endcapIt) 
00393    {
00394      EEDetId ee (*endcapIt);
00395      endcapWriter.writeLine (ee,m_recalibMap[endcapIt->rawId()]);
00396    }
00397 
00398  edm::LogInfo ("IML") << "[InvMatrixCalibLooper][endOfJob] Exiting" ;    
00399 }
00400 
00401 
00402 //------------------------------------//
00403 //      definition of functions       //
00404 //------------------------------------//
00405 
00406 
00408 int 
00409 EcalEleCalibLooper::EBregionCheck (const int eta, const int phi) const 
00410  {
00411    if (eta < m_etaStart) return 1 ;
00412    if (eta >= m_etaEnd)   return 2 ;
00413    if (phi < m_phiStartEB) return 3 ;
00414    if (phi >= m_phiEndEB)   return 4 ;
00415    return 0 ;
00416  }
00417 
00418 
00419 //--------------------------------------------
00420 
00421 
00423 inline double degrees (double radiants)
00424  {
00425   return radiants * 180 * (1/M_PI) ;
00426  }
00427 
00428 
00429 //--------------------------------------------
00430 
00431 
00433 inline double radiants (int degrees)
00434  {
00435    return degrees * M_PI * (1 / 180) ;  
00436  }    
00437 
00438 
00439 //--------------------------------------------
00440 
00441 
00443 double EcalEleCalibLooper::giveLimit (int degrees)
00444   {
00445     //PG 200 > atan (50/0.5)
00446     if (degrees == 90) return 90 ; 
00447     return tan (radiants (degrees)) ;      
00448   } 
00449 
00450 
00451 //--------------------------------------------
00452 
00453 
00455 inline double Mod (double phi)
00456  {
00457   if (phi>=360 && phi<720) return phi-360;
00458   if (phi>=720) return phi-720;
00459   return phi;
00460  } 
00461 
00462 
00463 //----------------------------------------
00464 
00465 
00467 int EcalEleCalibLooper::EBRegionId (const int etaXtl,const int phiXtl) const 
00468 {
00469  if (EBregionCheck(etaXtl,phiXtl)) return -1;
00470  int phifake = m_phiStartEB;
00471  if (m_phiStartEB>m_phiEndEB) phifake = m_phiStartEB - 360;
00472  int Nphi = (m_phiEndEB-phifake)/m_phiWidthEB ;
00473  int etaI = (etaXtl-m_etaStart) / m_etaWidth ;  
00474  int phiI = (phiXtl-m_phiStartEB) / m_phiWidthEB ; 
00475  int regionNumEB = phiI + Nphi*etaI ;
00476  return (int) regionNumEB;
00477 }
00478 
00479 
00480 //----------------------------------------
00481 
00482 
00484 int EcalEleCalibLooper::EERegionId (const int ics, const int ips) const
00485 {
00486  if (EEregionCheck(ics,ips)) return -1;
00487  int phifake = m_phiStartEE;
00488  if (m_phiStartEE>m_phiEndEE) phifake = m_phiStartEE - 360;
00489  double radius = (ics-50) * (ics-50) + (ips-50) * (ips-50) ;
00490  radius = sqrt (radius) ;
00491  int Nphi = (m_phiEndEE - phifake)/m_phiWidthEE ;
00492  double phi = atan2 (static_cast<double> (ips-50), 
00493                      static_cast<double> (ics-50)) ;
00494  phi = degrees (phi);
00495  if (phi < 0) phi += 360; 
00496  int radI = static_cast<int> ((radius-m_radStart) / m_radWidth) ;
00497  int phiI = static_cast<int> ((m_phiEndEE-phi) / m_phiWidthEE) ;
00498  int regionNumEE = phiI + Nphi*radI ;
00499  return  regionNumEE ;
00500 }
00501 
00502 
00503 //----------------------------------------
00504 
00505 
00507 inline int EcalEleCalibLooper::EEregionsNum () const 
00508 {
00509   int phifake = m_phiStartEE;
00510   if (m_phiStartEE>m_phiEndEE) phifake = m_phiStartEE - 360;
00511   return ( (m_radEnd - m_radStart)/m_radWidth) * ( (m_phiEndEE - phifake)/m_phiWidthEE) ;
00512 }
00513 
00514 
00515 //----------------------------------------
00516 
00517 
00519 inline int EcalEleCalibLooper::EBregionsNum () const 
00520 {
00521   int phi = m_phiStartEB;
00522   if (m_phiStartEB>m_phiEndEB) phi = m_phiStartEB - 360;
00523   return ( (m_etaEnd - m_etaStart)/m_etaWidth) * ( (m_phiEndEB - phi)/m_phiWidthEB) ; 
00524 }
00525 
00526 
00527 //----------------------------------------
00528 
00529 
00531 void EcalEleCalibLooper::EBRegionDefinition ()
00532 {
00533  int reg=-1;
00534  for (int it = 0 ; it < EBregionsNum () ; ++it) m_regions.push_back (0) ;   
00535  for (int eta = 0 ; eta < 170  ; ++eta)
00536    for (int phi = 0 ; phi < 360 ; ++phi)
00537       {
00538         reg = EBRegionId (eta,phi) ;
00539         m_xtalRegionId[EBDetId::unhashIndex (eta*360+phi).rawId ()] = reg ; 
00540         if (reg==-1) continue;
00541         m_xtalPositionInRegion[EBDetId::unhashIndex (eta*360+phi).rawId ()] = m_regions.at (reg) ;
00542         ++m_regions.at (reg);
00543       }
00544 }
00545 
00546 
00547 //----------------------------------------
00548 
00549 
00550 //DS EE Region Definition 
00551 void EcalEleCalibLooper::EERegionDefinition ()
00552 {
00553  // reset
00554  int EBnum=EBregionsNum();
00555  int EEnum=EEregionsNum();
00556  for (int it = 0 ; it < 2* EEnum ; ++it) m_regions.push_back (0) ;   
00557  // loop sui xtl 
00558  int reg=-1;
00559  for (int ics = 0 ; ics < 100 ; ++ics)
00560   for (int ips = 0 ; ips < 100 ; ++ips)
00561     {
00562      int ireg = EERegionId(ics, ips);
00563      if (ireg==-1) reg =-1;
00564      else reg = EBnum + ireg;
00565      if (EEDetId::validDetId (ics+1, ips+1, 1))
00566       {
00567         m_xtalRegionId[EEDetId (ics+1, ips+1, 1).rawId ()] = reg ; 
00568         if (reg==-1) continue;
00569         m_xtalPositionInRegion[EEDetId (ics+1, ips+1, 1).rawId ()] = m_regions.at (reg) ;
00570         ++m_regions.at(reg);
00571       }
00572      if (reg!=-1) reg += EEnum; 
00573      if (EEDetId::validDetId (ics+1, ips+1, -1))
00574       {
00575         m_xtalRegionId[EEDetId (ics+1, ips+1, -1).rawId ()] = reg ; 
00576         if (reg==-1) continue;
00577         m_xtalPositionInRegion[EEDetId (ics+1, ips+1, -1).rawId ()] = m_regions.at (reg) ;
00578         ++m_regions.at (reg) ;
00579        }
00580     }
00581 }
00582 
00583 
00584 //-----------------------------------------
00585 
00586 
00588 int EcalEleCalibLooper::EEregionCheck (const int ics, const int ips)  const
00589 {
00590   int x = ics-50;
00591   int y = ips-50;
00592   double radius2 = x*x + y*y ;
00593   if (radius2 < 10*10) return 1;  //center of the donut
00594   if (radius2 > 50*50) return 1;  //outer part of the donut
00595   if (radius2 < m_radStart * m_radStart) return 2 ;
00596   if (radius2 >= m_radEnd * m_radEnd) return 2 ;
00597   double phi = atan2 (static_cast<double> (y),static_cast<double> (x));
00598   phi = degrees (phi);
00599   if (phi < 0) phi += 360; 
00600   if (m_phiStartEE < m_phiEndEE 
00601      && phi > m_phiStartEE && phi < m_phiEndEE ) return 0; 
00602   if (m_phiStartEE > m_phiEndEE 
00603       && (phi > m_phiStartEE || phi < m_phiEndEE )) return 0; 
00604    return 3;
00605 }
00606 
00607 
00608 //--------------------------------------------
00609 
00610 
00611 //Shifts eta in other coordinates (from 0 to 170)
00612 inline int EcalEleCalibLooper::etaShifter (const int etaOld) const
00613    {
00614      if (etaOld < 0) return etaOld + 85;
00615      else if (etaOld > 0) return etaOld + 84;
00616    }
00617 
00618 
00619 //--------------------------------------------
00620 
00621 
00623 DetId  EcalEleCalibLooper::findMaxHit (const std::vector<DetId> & v1,
00624                                     const EBRecHitCollection* EBhits,
00625                                     const EERecHitCollection* EEhits) 
00626 {
00627  double currEnergy = 0.;
00628  DetId maxHit;
00629  for (std::vector<DetId>::const_iterator idsIt = v1.begin () ; 
00630       idsIt != v1.end () ; ++idsIt)
00631    {
00632     if (idsIt->subdetId () == EcalBarrel) 
00633        {              
00634          EBRecHitCollection::const_iterator itrechit;
00635          itrechit = EBhits->find (*idsIt) ;
00636          if (itrechit == EBhits->end () )
00637            {
00638             edm::LogWarning("IML") <<"max hit not found";
00639             continue;
00640            }
00641          if (itrechit->energy () > currEnergy)
00642            {
00643              currEnergy = itrechit->energy () ;
00644              maxHit= *idsIt ;
00645            }
00646        } //barrel part ends
00647     else 
00648        {     
00649          EERecHitCollection::const_iterator itrechit;
00650          itrechit = EEhits->find (*idsIt) ;
00651          if (itrechit == EEhits->end () )
00652            {
00653              edm::LogWarning("IML") <<"max hit not found";
00654              continue;
00655            }
00656               
00657          if (itrechit->energy () > currEnergy)
00658            {
00659             currEnergy=itrechit->energy ();
00660             maxHit= *idsIt;
00661            }
00662        } //ends the barrel part
00663     } //end of the loop over the detId
00664  return maxHit;
00665 }
00666 
00667 
00668 //---------------------------------------------------
00669 
00670 
00672 void EcalEleCalibLooper::fillEBMap (EBDetId EBmax,
00673                 const EcalRecHitCollection * barrelHitsCollection,
00674                 std::map<int,double> & EBxtlMap,
00675                 int EBNumberOfRegion, double & pSubtract )
00676 {
00677  int curr_eta;
00678  int curr_phi;
00679  double dummy;
00680  pSubtract=0;
00681 //Maybe updated differently for eta and phi
00682  for (int ii = 0 ; ii < m_recoWindowSide ; ++ii)
00683   for (int ij = 0 ; ij < m_recoWindowSide ; ++ij)
00684     {
00685      //PG CMS official reference system
00686      curr_eta = EBmax.ieta () - m_recoWindowSide / 2 + ii ;
00687      curr_phi = EBmax.iphi () - m_recoWindowSide / 2 + ij ;
00688      if (abs (curr_eta) > 85) continue;
00689      //Skpis over the zero between EB+ and EB-
00690     if (curr_eta * EBmax.ieta () <= 0) 
00691        {
00692          if (EBmax.ieta () > 0) --curr_eta ; 
00693          else curr_eta++; 
00694        }
00695      if (curr_phi < 1) curr_phi += 360;
00696      if (curr_phi >= 360) curr_phi -= 360;
00697      if (EBDetId::validDetId (curr_eta,curr_phi))
00698       {
00699        EBDetId det = EBDetId (curr_eta,curr_phi,EBDetId::ETAPHIMODE);
00700        EcalRecHitCollection::const_iterator curr_recHit = barrelHitsCollection->find (det) ;
00701        if (curr_recHit == barrelHitsCollection->end ()) continue;
00702        dummy = curr_recHit->energy () ;
00703        if ( dummy > m_minEnergyPerCrystal && dummy < m_maxEnergyPerCrystal)
00704             dummy *= m_recalibMap[det.rawId ()] ;     
00705        else continue;
00706        if (m_xtalRegionId[det.rawId ()] == EBNumberOfRegion)
00707             EBxtlMap[m_xtalPositionInRegion[det.rawId ()]] = dummy ;
00708        else pSubtract += dummy;
00709 //PG FIXME qui bisognera' inserire il ctrl per non essere troppo lontano, il bordo insomma
00710       }
00711     }
00712 }
00713 
00714 //---------------------------------------------------
00715 
00716 
00718 void EcalEleCalibLooper::fillEEMap (EEDetId EEmax,
00719                 const EcalRecHitCollection * endcapHitsCollection,
00720                 std::map<int,double> & EExtlMap,
00721                 int EENumberOfRegion, double & pSubtract )
00722   
00723 {
00724  int curr_x=0;
00725  int curr_y=0;
00726  double dummy=0.;
00727  pSubtract=0;
00728  int ecalZone=EEmax.zside();
00729  //Loop on the energy reconstruction window
00730  for (int ii=0;ii<m_recoWindowSide;++ii)
00731  for (int ij=0;ij<m_recoWindowSide;++ij)
00732   {
00733      curr_x=EEmax.ix ()-m_recoWindowSide/2+ii;
00734      curr_y=EEmax.iy ()-m_recoWindowSide/2+ij;
00735      if (EEDetId::validDetId (curr_x,curr_y,EEmax.zside ()))
00736      {
00737       EEDetId det = EEDetId (curr_x,curr_y,ecalZone,EEDetId::XYMODE);
00738       EcalRecHitCollection::const_iterator curr_recHit = endcapHitsCollection->find (det) ;
00739       if (curr_recHit == endcapHitsCollection->end ()) continue;
00740       dummy = curr_recHit->energy () ;
00741       if ( dummy < m_minEnergyPerCrystal || dummy > m_maxEnergyPerCrystal ) continue ; 
00742       dummy *= m_recalibMap[det.rawId ()] ;     
00743        if (m_xtalRegionId[det.rawId ()] == EENumberOfRegion)
00744             EExtlMap[m_xtalPositionInRegion[det.rawId ()]] = dummy ;
00745       else pSubtract += dummy;
00746       //PG FIXME qui bisognera' inserire il ctrl per non essere troppo lontano, il bordo insomma
00747      } 
00748    } //PG loop on the energy reconstruction window
00749  } 
00750    
00751 
00752 

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