CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Calibration/EcalCalibAlgos/src/InvRingCalib.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include <math.h>
00003 #include "FWCore/Framework/interface/Frameworkfwd.h"
00004 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/Framework/interface/MakerMacros.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "Calibration/EcalCalibAlgos/interface/InvRingCalib.h"
00009 #include "Calibration/EcalCalibAlgos/interface/IMACalibBlock.h"
00010 #include "Calibration/EcalCalibAlgos/interface/L3CalibBlock.h"
00011 #include "DataFormats/Common/interface/Handle.h"
00012 #include "Calibration/Tools/interface/calibXMLwriter.h"
00013 #include "CalibCalorimetry/CaloMiscalibTools/interface/MiscalibReaderFromXMLEcalBarrel.h"
00014 #include "CalibCalorimetry/CaloMiscalibTools/interface/MiscalibReaderFromXMLEcalEndcap.h"
00015 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
00016 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00017 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00018 #include "RecoEcal/EgammaCoreTools/interface/EcalClusterTools.h"
00019 
00020 #include "Calibration/EcalCalibAlgos/interface/MatrixFillMap.h"
00021 #include "Calibration/EcalCalibAlgos/interface/ClusterFillMap.h"
00022 
00023 //Not to remain in the final version
00024 #include "TH2.h"
00025 #include "TFile.h"
00026 #include <iostream>
00027 //----------------------------------------------------------------
00028 //ctor
00029 
00030 InvRingCalib::InvRingCalib (const edm::ParameterSet& iConfig) :
00031   m_barrelAlCa (iConfig.getParameter<edm::InputTag>("barrelAlca")),
00032   m_endcapAlCa (iConfig.getParameter<edm::InputTag>("endcapAlca")) ,
00033   m_ElectronLabel (iConfig.getParameter<edm::InputTag>("ElectronLabel")),
00034   m_recoWindowSidex (iConfig.getParameter<int>("recoWindowSidex")),
00035   m_recoWindowSidey (iConfig.getParameter<int>("recoWindowSidey")),
00036   m_minEnergyPerCrystal (iConfig.getParameter<double>("minEnergyPerCrystal")) ,
00037   m_maxEnergyPerCrystal (iConfig.getParameter<double>("maxEnergyPerCrystal")) ,
00038   m_etaStart (iConfig.getParameter<int>("etaStart")),
00039   m_etaEnd (iConfig.getParameter<int>("etaEnd")),
00040   m_etaWidth (iConfig.getParameter<int>("etaWidth")),
00041   m_maxSelectedNumPerRing (iConfig.getParameter<int>("maxNumPerRing")),
00042   m_minCoeff (iConfig.getParameter<double>("minCoeff")),
00043   m_maxCoeff (iConfig.getParameter<double>("maxCoeff")),
00044   m_usingBlockSolver(iConfig.getParameter<int>("usingBlockSolver")),
00045   m_startRing (iConfig.getParameter<int>("startRing")),
00046   m_endRing (iConfig.getParameter<int>("endRing")),
00047   m_EBcoeffFile (iConfig.getParameter<std::string>("EBcoeffs")),
00048   m_EEcoeffFile (iConfig.getParameter<std::string>("EEcoeffs")),
00049   m_EEZone (iConfig.getParameter<int>("EEZone"))
00050 {
00051   //controls if the parameters inputed are correct
00052   if ((m_etaEnd*m_etaStart)>0)
00053     assert (!((m_etaEnd - m_etaStart )%m_etaWidth)); 
00054   if ((m_etaEnd*m_etaStart)<0)
00055     assert (!((m_etaEnd - m_etaStart-1 )%m_etaWidth)); 
00056 
00057   assert (m_etaStart >=-85 && m_etaStart <= 86);
00058   assert (m_etaEnd >= m_etaStart && m_etaEnd <= 86);
00059   assert (m_startRing>-1 && m_startRing<= 40);
00060   assert (m_endRing>=m_startRing && m_endRing<=40);
00061 
00062   assert (!((m_endRing - m_startRing)%m_etaWidth));
00063   assert (( abs(m_EEZone)<=1));
00064   
00065   m_loops = (unsigned int) iConfig.getParameter<int>("loops")- 1;
00066   //LP CalibBlock vector instantiation
00067   edm::LogInfo ("IML") << "[InvRingCalib][ctor] Calib Block" ;
00068   std::string algorithm = iConfig.getParameter<std::string> ("algorithm") ;
00069   m_mapFillerType = iConfig.getParameter<std::string> ("FillType");
00070   int eventWeight = iConfig.getUntrackedParameter<int> ("L3EventWeight",1) ;
00071   
00072   for (int i = 0 ; i < EBRegionNum () ; ++i)
00073    {
00074   if (algorithm == "IMA")
00075         m_IMACalibBlocks.push_back (
00076             new IMACalibBlock (m_etaWidth)
00077           ) ; 
00078       else if (algorithm == "L3")
00079         m_IMACalibBlocks.push_back (
00080             new L3CalibBlock (m_etaWidth, eventWeight)
00081           ) ; 
00082       else
00083         {
00084           edm::LogError ("building") << algorithm 
00085                           << " is not a valid calibration algorithm" ;
00086           exit (1) ;    
00087         }      
00088    }   
00089   int EEBlocks = 0 ;
00090   if (m_EEZone == 0) EEBlocks = 2 * EERegionNum () ;
00091   if (m_EEZone == 1 || m_EEZone == -1) EEBlocks = EERegionNum () ;
00092 
00093   for (int i = 0; i < EEBlocks ; ++i)
00094    {
00095   
00096   if (algorithm == "IMA")
00097         m_IMACalibBlocks.push_back (
00098             new IMACalibBlock (m_etaWidth)
00099           ) ; 
00100       else if (algorithm == "L3")
00101         m_IMACalibBlocks.push_back (
00102             new L3CalibBlock (m_etaWidth, eventWeight)
00103           ) ; 
00104       else
00105         {
00106           edm::LogError ("building") << algorithm 
00107                           << " is not a valid calibration algorithm" ;
00108           exit (1) ;    
00109         }      
00110    }   
00111   edm::LogInfo ("IML") <<" [InvRingCalib][ctor] end of creator";
00112 }
00113 
00114 
00115 //-------------------------------------------------------------- end ctor
00117 
00118 
00119 InvRingCalib::~InvRingCalib ()
00120 {
00121 }
00122 
00123 
00124 //---------------------------------------------------
00125 
00126 
00127 
00129 void 
00130 InvRingCalib::beginOfJob () 
00131 {
00132   isfirstcall_=true;
00133 
00134  
00135 }
00136 
00137 
00138 //--------------------------------------------------------
00139 
00140 
00142 void InvRingCalib::startingNewLoop (unsigned int ciclo) 
00143 {
00144     edm::LogInfo ("IML") << "[InvMatrixCalibLooper][Start] entering loop " << ciclo;
00145     for (std::vector<VEcalCalibBlock *>::iterator calibBlock = m_IMACalibBlocks.begin () ;
00146          calibBlock != m_IMACalibBlocks.end () ;
00147          ++calibBlock)
00148       {
00149         //LP empties the energies vector, to fill DuringLoop.
00150         (*calibBlock)->reset () ;
00151       }
00152    for (std::map<int,int>::const_iterator ring=m_xtalRing.begin();
00153         ring!=m_xtalRing.end();
00154         ++ring)
00155            m_RingNumOfHits[ring->second]=0;
00156    return ;
00157 }
00158 
00159 
00160 //--------------------------------------------------------
00161 
00162 
00164 edm::EDLooper::Status 
00165 InvRingCalib::duringLoop (const edm::Event& iEvent,
00166                           const edm::EventSetup& iSetup) 
00167 {
00168 
00169 
00170    if (isfirstcall_){
00171     edm::LogInfo ("IML") << "[InvRingCalib][beginOfJob]" ;
00172     //gets the geometry from the event setup
00173     edm::ESHandle<CaloGeometry> geoHandle;
00174     iSetup.get<CaloGeometryRecord>().get(geoHandle);
00175     const CaloGeometry& geometry = *geoHandle;
00176     edm::LogInfo ("IML") <<"[InvRingCalib] Event Setup read";
00177     //fills a vector with all the cells
00178     m_barrelCells = geometry.getValidDetIds(DetId::Ecal, EcalBarrel);
00179     m_endcapCells = geometry.getValidDetIds(DetId::Ecal, EcalEndcap);
00180     //Defines the EB regions
00181     edm::LogInfo ("IML") <<"[InvRingCalib] Defining Barrel Regions";
00182     EBRegionDef();
00183     //Defines what is a ring in the EE
00184     edm::LogInfo ("IML") <<"[InvRingCalib] Defining endcap Rings";
00185     EERingDef(iSetup);
00186     //Defines the regions in the EE
00187     edm::LogInfo ("IML") <<"[InvRingCalib] Defining endcap Regions";
00188     EERegionDef();
00189     if (m_mapFillerType == "Cluster") m_MapFiller= new ClusterFillMap (
00190                                                                        m_recoWindowSidex ,m_recoWindowSidey ,
00191                                                                        m_xtalRegionId ,m_minEnergyPerCrystal ,
00192                                                                        m_maxEnergyPerCrystal , m_RinginRegion ,
00193                                                                        & m_barrelMap ,
00194                                                                        & m_endcapMap ); 
00195     if (m_mapFillerType == "Matrix") m_MapFiller = new MatrixFillMap (
00196                                                                       m_recoWindowSidex ,m_recoWindowSidey ,
00197                                                                       m_xtalRegionId , m_minEnergyPerCrystal ,
00198                                                                       m_maxEnergyPerCrystal , m_RinginRegion ,
00199                                                                       & m_barrelMap ,
00200                                                                       & m_endcapMap); 
00201     edm::LogInfo ("IML") <<"[InvRingCalib] Initializing the coeffs";
00202     //Sets the initial coefficients to 1.
00203     //Graphs to check ring, regions and so on, not needed in the final version
00204     TH2F EBRegion ("EBRegion","EBRegion",171,-85,86,360,1,361);
00205     TH2F EBRing ("EBRing","EBRing",171,-85,86,360,1,361);
00206     for (std::vector<DetId>::const_iterator it= m_barrelCells.begin();
00207          it!= m_barrelCells.end(); 
00208          ++it )
00209       {
00210         EBDetId eb (*it);
00211         EBRing.Fill(eb.ieta(),eb.iphi(),m_RinginRegion[it->rawId()]);
00212         EBRegion.Fill(eb.ieta(),eb.iphi(),m_xtalRegionId[it->rawId()]);
00213       }
00214 
00215     TH2F EEPRegion ("EEPRegion", "EEPRegion",100,1,101,100,1,101);
00216     TH2F EEPRing ("EEPRing", "EEPRing",100,1,101,100,1,101);
00217     TH2F EEPRingReg ("EEPRingReg", "EEPRingReg",100,1,101,100,1,101);
00218     TH2F EEMRegion ("EEMRegion", "EEMRegion",100,1,101,100,1,101);
00219     TH2F EEMRing ("EEMRing", "EEMRing",100,1,101,100,1,101);
00220     TH2F EEMRingReg ("EEMRingReg", "EEMRingReg",100,1,101,100,1,101);
00221     //  TH1F eta ("eta","eta",250,-85,165);
00222     for (std::vector<DetId>::const_iterator it = m_endcapCells.begin();
00223          it!= m_endcapCells.end();
00224          ++it)
00225       {
00226         EEDetId ee (*it);
00227         if (ee.zside()>0)
00228           {
00229             EEPRegion.Fill(ee.ix(),ee.iy(),m_xtalRegionId[ee.rawId()]);
00230             EEPRing.Fill(ee.ix(),ee.iy(),m_xtalRing[ee.rawId()]);
00231             EEPRingReg.Fill(ee.ix(),ee.iy(),m_RinginRegion[ee.rawId()]);
00232           }
00233         if (ee.zside()<0)
00234           {
00235             EEMRegion.Fill(ee.ix(),ee.iy(),m_xtalRegionId[ee.rawId()]);
00236             EEMRing.Fill(ee.ix(),ee.iy(),m_xtalRing[ee.rawId()]);
00237             EEMRingReg.Fill(ee.ix(),ee.iy(),m_RinginRegion[ee.rawId()]);
00238           }    
00239       } 
00240 
00241     //  for (std::map<int,float>::iterator it=m_eta.begin();
00242     //        it!=m_eta.end();++it)
00243     //             eta.Fill(it->first,it->second);
00244     TFile out ("EBZone.root", "recreate");
00245     EBRegion.Write();
00246     EBRing.Write();
00247     EEPRegion.Write();
00248     EEPRing.Write();
00249     EEPRingReg.Write();
00250     EEMRegion.Write();
00251     EEMRing.Write();
00252     //  eta.Write();
00253     EEMRingReg.Write();
00254     out.Close();
00255     edm::LogInfo ("IML") <<"[InvRingCalib] Start to acquire the coeffs";
00256     CaloMiscalibMapEcal EBmap;
00257     EBmap.prefillMap ();
00258     MiscalibReaderFromXMLEcalBarrel barrelreader (EBmap);
00259     if (!m_EBcoeffFile.empty()) barrelreader.parseXMLMiscalibFile (m_EBcoeffFile);
00260     EcalIntercalibConstants costants (EBmap.get());
00261     m_barrelMap = costants.getMap();
00262     CaloMiscalibMapEcal EEmap ;   
00263     EEmap.prefillMap ();
00264     MiscalibReaderFromXMLEcalEndcap endcapreader (EEmap);
00265     if (!m_EEcoeffFile.empty()) endcapreader.parseXMLMiscalibFile (m_EEcoeffFile) ;
00266     EcalIntercalibConstants EEcostants (EEmap.get());
00267     m_endcapMap = EEcostants.getMap();
00268 
00269     isfirstcall_=false;
00270   } // if isfirstcall
00271 
00272 
00273 
00274 
00275 
00276 
00277   //gets the barrel recHits
00278   double pSubtract = 0.;
00279   double pTk = 0.;
00280   const EcalRecHitCollection* barrelHitsCollection = 0;
00281   edm::Handle<EBRecHitCollection> barrelRecHitsHandle ;
00282   iEvent.getByLabel (m_barrelAlCa, barrelRecHitsHandle) ;
00283   barrelHitsCollection = barrelRecHitsHandle.product () ;
00284 
00285  if (!barrelRecHitsHandle.isValid ()) {
00286      edm::LogError ("IML") << "[EcalEleCalibLooper] barrel rec hits not found" ;
00287      return  kContinue ;
00288     }
00289   //gets the endcap recHits
00290   const EcalRecHitCollection* endcapHitsCollection = 0;
00291   edm::Handle<EERecHitCollection> endcapRecHitsHandle ;
00292   iEvent.getByLabel (m_endcapAlCa, endcapRecHitsHandle) ;
00293   endcapHitsCollection = endcapRecHitsHandle.product () ;
00294 
00295  if (!endcapRecHitsHandle.isValid ()) {  
00296      edm::LogError ("IML") << "[EcalEleCalibLooper] endcap rec hits not found" ; 
00297      return kContinue;
00298    }
00299 
00300   //gets the electrons
00301   edm::Handle<reco::GsfElectronCollection> pElectrons;
00302   iEvent.getByLabel(m_ElectronLabel,pElectrons);
00303 
00304  if (!pElectrons.isValid ()) {
00305      edm::LogError ("IML")<< "[EcalEleCalibLooper] electrons not found" ;
00306      return kContinue;
00307    }
00308 
00309   //loops over the electrons in the event
00310   for (reco::GsfElectronCollection::const_iterator eleIt = pElectrons->begin();
00311        eleIt != pElectrons->end();
00312        ++eleIt )
00313     {
00314       pSubtract =0;
00315       pTk=eleIt->trackMomentumAtVtx().R();
00316       std::map<int , double> xtlMap;
00317       DetId Max=0; 
00318       if (fabs(eleIt->eta()<1.49))
00319              Max = EcalClusterTools::getMaximum(eleIt->superCluster()->hitsAndFractions(),barrelHitsCollection).first;
00320       else 
00321              Max = EcalClusterTools::getMaximum(eleIt->superCluster()->hitsAndFractions(),endcapHitsCollection).first;
00322       if (Max.det()==0) continue;
00323        m_MapFiller->fillMap(eleIt->superCluster ()->hitsAndFractions (),Max, 
00324                            barrelHitsCollection,endcapHitsCollection, xtlMap,pSubtract);
00325       if (m_xtalRegionId[Max.rawId()]==-1) continue;
00326       pSubtract += eleIt->superCluster()->preshowerEnergy() ;
00327       ++m_RingNumOfHits[m_xtalRing[Max.rawId()]];
00328       //fills the calibBlock 
00329       m_IMACalibBlocks.at(m_xtalRegionId[Max.rawId()])->Fill (
00330           xtlMap.begin(), xtlMap.end(),pTk,pSubtract
00331         ) ;
00332     }
00333   return  kContinue;
00334 } //end of duringLoop
00335 
00336 
00337 //-------------------------------------
00338 
00339 
00340 //EndOfLoop
00341 edm::EDLooper::Status 
00342 InvRingCalib::endOfLoop (const edm::EventSetup& dumb, 
00343                          unsigned int iCounter)
00344 {
00345    std::map<int,double> InterRings;
00346   edm::LogInfo ("IML") << "[InvMatrixCalibLooper][endOfLoop] Start to invert the matrixes" ;
00347   //call the autoexplaining "solve" method for every calibBlock
00348   for (std::vector<VEcalCalibBlock *>::iterator calibBlock=m_IMACalibBlocks.begin();
00349        calibBlock!=m_IMACalibBlocks.end();
00350        ++calibBlock)
00351     (*calibBlock)->solve(m_usingBlockSolver,m_minCoeff,m_maxCoeff);
00352 
00353   edm::LogInfo("IML") << "[InvRingLooper][endOfLoop] Starting to write the coeffs";
00354   TH1F *coeffDistr = new TH1F("coeffdistr","coeffdistr",100 ,0.7,1.4);
00355   TH1F *coeffMap = new TH1F("coeffRingMap","coeffRingMap",250,-85,165);
00356   TH1F *ringDistr = new TH1F("ringDistr","ringDistr",250,-85,165);
00357   TH1F *RingFill = new TH1F("RingFill","RingFill",250,-85,165);
00358   for(std::map<int,int>::const_iterator it=m_xtalRing.begin();
00359       it!=m_xtalRing.end();
00360       ++it)
00361     ringDistr->Fill(it->second+0.1);
00362 
00363   int ID;
00364   std::map<int,int> flag;
00365   for(std::map<int,int>::const_iterator it=m_xtalRing.begin();
00366       it!=m_xtalRing.end();
00367       ++it)
00368     flag[it->second]=0;
00369   
00370   for (std::vector<DetId>::const_iterator it=m_barrelCells.begin();
00371        it!=m_barrelCells.end();
00372        ++it)
00373     { 
00374       ID= it->rawId();
00375       if (m_xtalRegionId[ID]==-1) continue;
00376       if (flag[m_xtalRing[ID]]) continue;
00377       flag[m_xtalRing[ID]] =1;
00378       RingFill->Fill(m_xtalRing[ID],m_RingNumOfHits[m_xtalRing[ID]]);
00379       InterRings[m_xtalRing[ID]] = m_IMACalibBlocks.at(m_xtalRegionId[ID])->at(m_RinginRegion[ID]);
00380       coeffMap->Fill (m_xtalRing[ID]+0.1,InterRings[m_xtalRing[ID]]);
00381       coeffDistr->Fill(InterRings[m_xtalRing[ID]]);
00382     }
00383 
00384   for (std::vector<DetId>::const_iterator it=m_endcapCells.begin();
00385        it!=m_endcapCells.end();
00386        ++it)
00387     { 
00388       ID= it->rawId();
00389       if (m_xtalRegionId[ID]==-1) continue;
00390       if (flag[m_xtalRing[ID]]) continue;
00391       flag[m_xtalRing[ID]]= 1;
00392       InterRings[m_xtalRing[ID]] = m_IMACalibBlocks.at(m_xtalRegionId[ID])->at(m_RinginRegion[ID]);
00393       RingFill->Fill(m_xtalRing[ID],m_RingNumOfHits[m_xtalRing[ID]]);
00394       coeffMap->Fill (m_xtalRing[ID],InterRings[m_xtalRing[ID]]);
00395       coeffDistr->Fill(InterRings[m_xtalRing[ID]]);
00396                 
00397     } 
00398    
00399   char filename[80];
00400   sprintf(filename,"coeff%d.root",iCounter);
00401   TFile out(filename,"recreate");    
00402   coeffDistr->Write();
00403   coeffMap->Write();
00404   ringDistr->Write();
00405   RingFill->Write();
00406   out.Close();
00407   for (std::vector<DetId>::const_iterator it=m_barrelCells.begin();
00408        it!=m_barrelCells.end();
00409        ++it){
00410          m_barrelMap[*it]*=InterRings[m_xtalRing[it->rawId()]];
00411   }
00412   for (std::vector<DetId>::const_iterator it=m_endcapCells.begin();
00413        it!=m_endcapCells.end();
00414        ++it)
00415           m_endcapMap[*it]*=InterRings[m_xtalRing[it->rawId()]];
00416   if (iCounter < m_loops-1 ) return kContinue ;
00417   else return kStop; 
00418 }
00419 
00420 
00421 //---------------------------------------
00422 
00423 
00424 //LP endOfJob
00425 void 
00426 InvRingCalib::endOfJob ()
00427 {
00428 
00429  edm::LogInfo ("IML") << "[InvMatrixCalibLooper][endOfJob] saving calib coeffs" ;
00430  calibXMLwriter barrelWriter(EcalBarrel);
00431  calibXMLwriter endcapWriter(EcalEndcap);
00432  for (std::vector<DetId>::const_iterator barrelIt =m_barrelCells.begin(); 
00433        barrelIt!=m_barrelCells.end(); 
00434        ++barrelIt) {
00435             EBDetId eb (*barrelIt);
00436             barrelWriter.writeLine(eb,m_barrelMap[eb]);
00437           }
00438  for (std::vector<DetId>::const_iterator endcapIt = m_endcapCells.begin();
00439      endcapIt!=m_endcapCells.end();
00440      ++endcapIt) {
00441           EEDetId ee (*endcapIt);
00442           endcapWriter.writeLine(ee,m_endcapMap[ee]);
00443         }
00444 }
00445 
00446 
00447 //------------------------------------//
00448 //      definition of functions       //
00449 //------------------------------------//
00450 
00451 //------------------------------------------------------------
00452 
00453 
00455 void InvRingCalib::EERingDef(const edm::EventSetup& iSetup)  
00456 {
00457  //Gets the Handle for the geometry from the eventSetup
00458  edm::ESHandle<CaloGeometry> geoHandle;
00459  iSetup.get<CaloGeometryRecord>().get(geoHandle);
00460  //Gets the geometry of the endcap
00461  const CaloGeometry& geometry = *geoHandle;
00462  const CaloSubdetectorGeometry *endcapGeometry = geometry.getSubdetectorGeometry(DetId::Ecal, EcalEndcap);
00463 // const CaloSubdetectorGeometry *barrelGeometry = geometry.getSubdetectorGeometry(DetId::Ecal, EcalBarrel);
00464  //for every xtal gets the position Vector and the phi position
00465  
00466 // for (std::vector<DetId>::const_iterator barrelIt = m_barrelCells.begin();
00467 //    barrelIt!=m_barrelCells.end();
00468 //    ++barrelIt) {
00469 //     const CaloCellGeometry *cellGeometry = barrelGeometry->getGeometry(*barrelIt);
00470 //     GlobalPoint point;
00471 //     EBDetId eb (*barrelIt);
00472 //     point=cellGeometry->getPosition();
00473 //     m_eta[eb.ieta()]=point.eta() ;    //cellGeometry->getPosition().eta();
00474 //    }
00475  for (std::vector<DetId>::const_iterator endcapIt = m_endcapCells.begin();
00476     endcapIt!=m_endcapCells.end();
00477     ++endcapIt) {
00478      const CaloCellGeometry *cellGeometry = endcapGeometry->getGeometry(*endcapIt);
00479      m_cellPos[endcapIt->rawId()] = cellGeometry->getPosition();
00480      m_cellPhi[endcapIt->rawId()] = cellGeometry->getPosition().phi();
00481   }
00482  //takes the first 39 xtals at a fixed y varying the x coordinate and saves their eta coordinate 
00483  float eta_ring[39];
00484  for (int ring=0; ring<39; ring++) 
00485         if (EEDetId::validDetId(ring,50,1)){  
00486           EEDetId det = EEDetId (ring,50,1,EEDetId::XYMODE);
00487           eta_ring[ring]=m_cellPos[det.rawId()].eta();
00488           }
00489  //defines the bonduary of the rings as the average eta of a xtal
00490  double etaBonduary[40];
00491  etaBonduary[0]=1.49;
00492  etaBonduary[39]=4.0;
00493  for (int ring=1; ring<39; ++ring)
00494        etaBonduary[ring]=(eta_ring[ring]+eta_ring[ring-1])/2.;
00495  //assign to each xtal a ring number
00496  int CRing;
00497  for (int ring=0; ring<39; ring++) 
00498    for (std::vector<DetId>::const_iterator endcapIt=m_endcapCells.begin();
00499         endcapIt!=m_endcapCells.end();++endcapIt){
00500      if (fabs(m_cellPos[endcapIt->rawId()].eta())>etaBonduary[ring] &&
00501          fabs(m_cellPos[endcapIt->rawId()].eta())<etaBonduary[ring+1])
00502           {
00503               EEDetId ee(*endcapIt);
00504               if (ee.zside()>0) CRing=ring + 86; 
00505               else CRing = ring + 125;
00506               m_xtalRing[endcapIt->rawId()]=CRing;
00507 //              m_eta[CRing]=m_cellPos[endcapIt->rawId()].eta();
00508           }    
00509       }
00510  return;
00511 }
00512 
00513 
00514 //------------------------------------------------------------
00515 
00516 
00518 int InvRingCalib::EERegId( int id) 
00519 {
00520    int reg;
00521    int ring;
00522    EEDetId ee (id);
00523   //sets the reg to -1 if the ring doesn't exist or is outside the region of interest 
00524    if (m_xtalRing[id] == -1) return -1;
00525   //avoid the calibration in the wrong zside
00526    if (m_EEZone == 1 ){
00527    if (ee.zside()<0) return -1;
00528    ring = m_xtalRing[id]-86;
00529    if(ring >=m_endRing) return -1;
00530    if (ring<m_startRing) return -1;
00531    reg = (ring -m_startRing) / m_etaWidth;
00532    m_RinginRegion[id]=(ring -m_startRing) % m_etaWidth;
00533    return reg;
00534    }
00535    if (m_EEZone == -1){
00536    if (ee.zside()>0) return -1;
00537    ring = m_xtalRing[id] -125;
00538    if(ring >=m_endRing) return -1;
00539    if (ring<m_startRing) return -1;
00540    reg = (ring -m_startRing) / m_etaWidth;
00541    m_RinginRegion[id]=(ring -m_startRing) % m_etaWidth;
00542    return reg;
00543    }
00544    if (ee.zside()>0) ring=m_xtalRing[id]-86;
00545      else ring = m_xtalRing[id]-125;
00546    if(ring >=m_endRing) return -1;
00547    if (ring<m_startRing) return -1;
00548    reg = (ring -m_startRing) / m_etaWidth;
00549    m_RinginRegion[id]=(ring -m_startRing) % m_etaWidth;
00550    return reg;
00551 }
00552 //----------------------------------------
00555 void InvRingCalib::EERegionDef ()
00556 {
00557 int reg;
00558 for (std::vector<DetId>::const_iterator endcapIt=m_endcapCells.begin();
00559      endcapIt!=m_endcapCells.end();++endcapIt){
00560       EEDetId ee(*endcapIt);
00561       reg = EERegId(endcapIt->rawId());
00562       //If the ring is not of interest saves only the region Id(-1)
00563       if(reg==-1) 
00564          m_xtalRegionId[endcapIt->rawId()]=reg;
00565       //sums the number of region in EB or EB+EE to have different regionsId in different regions 
00566       else {
00567       if (ee.zside()>0)reg += EBRegionNum();
00568       else reg += EBRegionNum()+EERegionNum();
00569       m_xtalRegionId[endcapIt->rawId()]=reg;
00570    }
00571   }
00572 }
00573 
00574 
00575 //------------------------------------------------------------
00576 
00577 
00579 inline int InvRingCalib::EERegionNum () const 
00580 {
00581   return ((m_endRing - m_startRing)/m_etaWidth);
00582 }
00583 
00584 
00586 int InvRingCalib::EBRegionNum () const 
00587 {
00588   if ((m_etaEnd*m_etaStart)>0)
00589    return ((m_etaEnd - m_etaStart )/m_etaWidth); 
00590   
00591   if ((m_etaEnd*m_etaStart)<0)
00592    return ((m_etaEnd - m_etaStart-1 )/m_etaWidth); 
00593   
00594   return 0;
00595 }
00598 void InvRingCalib::RegPrepare()
00599 {
00600  int k=0;
00601  for (int i = m_etaStart;i<m_etaEnd;++i)
00602  {
00603   if (i==0) continue;
00604   m_Reg[i]=k/m_etaWidth;
00605   ++k;
00606  }
00607 }
00609 int InvRingCalib::EBRegId(const int ieta) 
00610 {
00611  if (ieta<m_etaStart || ieta>=m_etaEnd) return -1;
00612  else return (m_Reg[ieta]);
00613 }
00614 
00615 
00616 //------------------------------------------------------------
00617 
00618 
00619 //EB Region Definition
00620 void InvRingCalib::EBRegionDef()
00621 {
00622   RegPrepare();
00623   for (std::vector<DetId>::const_iterator it=m_barrelCells.begin();
00624         it!=m_barrelCells.end();++it)
00625   {
00626     EBDetId eb (it->rawId());
00627     m_xtalRing[eb.rawId()] = eb.ieta() ;
00628     m_xtalRegionId[eb.rawId()] = EBRegId (eb.ieta()); 
00629     if (m_xtalRegionId[eb.rawId()]==-1) continue;
00630     m_RinginRegion[eb.rawId()] = (eb.ieta() - m_etaStart)% m_etaWidth; 
00631   }
00632 }
00633 //------------------------------------------------------------