CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/HLTrigger/HLTanalyzers/src/HLTAlCa.cc

Go to the documentation of this file.
00001 #include <iostream> 
00002 #include <sstream> 
00003 #include <istream> 
00004 #include <fstream> 
00005 #include <iomanip> 
00006 #include <string> 
00007 #include <cmath> 
00008 #include <functional> 
00009 #include <stdlib.h> 
00010 #include <string.h> 
00011 
00012 #include "HLTrigger/HLTanalyzers/interface/HLTAlCa.h"
00013 
00014 HLTAlCa::HLTAlCa() {
00015   evtCounter=0;
00016 
00017   //set parameter defaults 
00018   _Monte=false;
00019   _Debug=false;
00020 
00021   TheMapping = new EcalElectronicsMapping(); 
00022   first_ = true;
00023 }
00024 
00025 /*  Setup the analysis to put the branch-variables into the tree. */
00026 void HLTAlCa::setup(const edm::ParameterSet& pSet, TTree* HltTree) {
00027   
00028   edm::ParameterSet myEmParams = pSet.getParameter<edm::ParameterSet>("RunParameters") ;
00029   std::vector<std::string> parameterNames = myEmParams.getParameterNames() ;
00030 
00031   for ( std::vector<std::string>::iterator iParam = parameterNames.begin();
00032         iParam != parameterNames.end(); iParam++ ){
00033     if  ( (*iParam) == "Monte" ) _Monte =  myEmParams.getParameter<bool>( *iParam );
00034     else if ( (*iParam) == "Debug" ) _Debug =  myEmParams.getParameter<bool>( *iParam );
00035   }
00036 
00037   // AlCa-specific parameters
00038   clusSeedThr_ = pSet.getParameter<double> ("clusSeedThr"); 
00039   clusSeedThrEndCap_ = pSet.getParameter<double> ("clusSeedThrEndCap"); 
00040   clusEtaSize_ = pSet.getParameter<int> ("clusEtaSize"); 
00041   clusPhiSize_ = pSet.getParameter<int> ("clusPhiSize"); 
00042   seleXtalMinEnergy_ = pSet.getParameter<double> ("seleXtalMinEnergy"); 
00043   RegionalMatch_ = pSet.getUntrackedParameter<bool>("RegionalMatch",true); 
00044   ptMinEMObj_ = pSet.getParameter<double>("ptMinEMObj"); 
00045   EMregionEtaMargin_ = pSet.getParameter<double>("EMregionEtaMargin"); 
00046   EMregionPhiMargin_ = pSet.getParameter<double>("EMregionPhiMargin"); 
00047   Jets_ = pSet.getUntrackedParameter<bool>("Jets",false); 
00048   JETSdoCentral_ = pSet.getUntrackedParameter<bool>("JETSdoCentral",true); 
00049   JETSdoForward_ = pSet.getUntrackedParameter<bool>("JETSdoForward",true); 
00050   JETSdoTau_ = pSet.getUntrackedParameter<bool>("JETSdoTau",true); 
00051   JETSregionEtaMargin_ = pSet.getUntrackedParameter<double>("JETS_regionEtaMargin",1.0); 
00052   JETSregionPhiMargin_ = pSet.getUntrackedParameter<double>("JETS_regionPhiMargin",1.0); 
00053   Ptmin_jets_ = pSet.getUntrackedParameter<double>("Ptmin_jets",0.); 
00054 
00055    
00056   // Parameters for the position calculation:
00057   edm::ParameterSet posCalcParameters = 
00058     pSet.getParameter<edm::ParameterSet>("posCalcParameters");
00059   posCalculator_ = PositionCalc(posCalcParameters);
00060   
00061 
00062   const int kMaxClus = 2000; 
00063   ptClusAll = new float[kMaxClus]; 
00064   etaClusAll = new float[kMaxClus]; 
00065   phiClusAll = new float[kMaxClus]; 
00066   s4s9ClusAll = new float[kMaxClus]; 
00067 
00068   // AlCa-specific branches of the tree 
00069   HltTree->Branch("ohHighestEnergyEERecHit",&ohHighestEnergyEERecHit,"ohHighestEnergyEERecHit/F");
00070   HltTree->Branch("ohHighestEnergyEBRecHit",&ohHighestEnergyEBRecHit,"ohHighestEnergyEBRecHit/F"); 
00071   HltTree->Branch("ohHighestEnergyHBHERecHit",&ohHighestEnergyHBHERecHit,"ohHighestEnergyHBHERecHit/F");  
00072   HltTree->Branch("ohHighestEnergyHORecHit",&ohHighestEnergyHORecHit,"ohHighestEnergyHORecHit/F");   
00073   HltTree->Branch("ohHighestEnergyHFRecHit",&ohHighestEnergyHFRecHit,"ohHighestEnergyHFRecHit/F");   
00074   HltTree->Branch("Nalcapi0clusters",&Nalcapi0clusters,"Nalcapi0clusters/I");
00075   HltTree->Branch("ohAlcapi0ptClusAll",ptClusAll,"ohAlcapi0ptClusAll[Nalcapi0clusters]/F");
00076   HltTree->Branch("ohAlcapi0etaClusAll",etaClusAll,"ohAlcapi0etaClusAll[Nalcapi0clusters]/F"); 
00077   HltTree->Branch("ohAlcapi0phiClusAll",phiClusAll,"ohAlcapi0phiClusAll[Nalcapi0clusters]/F"); 
00078   HltTree->Branch("ohAlcapi0s4s9ClusAll",s4s9ClusAll,"ohAlcapi0s4s9ClusAll[Nalcapi0clusters]/F"); 
00079 }
00080 
00081 /* **Analyze the event** */
00082 void HLTAlCa::analyze(const edm::Handle<EBRecHitCollection>               & ebrechits, 
00083                       const edm::Handle<EERecHitCollection>               & eerechits, 
00084                       const edm::Handle<HBHERecHitCollection>             & hbherechits,
00085                       const edm::Handle<HORecHitCollection>               & horechits,
00086                       const edm::Handle<HFRecHitCollection>               & hfrechits,
00087                       const edm::Handle<EBRecHitCollection>               & pi0ebrechits,  
00088                       const edm::Handle<EERecHitCollection>               & pi0eerechits,  
00089                       const edm::Handle<l1extra::L1EmParticleCollection>  & l1EGIso,   
00090                       const edm::Handle<l1extra::L1EmParticleCollection>  & l1EGNonIso,   
00091                       const edm::Handle<l1extra::L1JetParticleCollection> & l1extjetc,  
00092                       const edm::Handle<l1extra::L1JetParticleCollection> & l1extjetf,
00093                       const edm::Handle<l1extra::L1JetParticleCollection> & l1exttaujet,
00094                       const edm::ESHandle< EcalElectronicsMapping >       & ecalmapping,  
00095                       const edm::ESHandle<CaloGeometry>                   & geoHandle,  
00096                       const edm::ESHandle<CaloTopology>                   & pTopology,   
00097                       const edm::ESHandle<L1CaloGeometry>                 & l1CaloGeom, 
00098                       TTree* HltTree) {
00099 
00100   //  std::cout << " Beginning HLTAlCa " << std::endl;
00101 
00102   ohHighestEnergyEERecHit = -1.0;
00103   ohHighestEnergyEBRecHit = -1.0;
00104   ohHighestEnergyHBHERecHit = -1.0; 
00105   ohHighestEnergyHORecHit = -1.0; 
00106   ohHighestEnergyHFRecHit = -1.0; 
00107 
00108   if (ebrechits.isValid()) {
00109     EBRecHitCollection myebrechits;
00110     myebrechits = * ebrechits;
00111     
00112     float ebrechitenergy = -1.0;
00113 
00114     typedef EBRecHitCollection::const_iterator ebrechititer;
00115 
00116     for (ebrechititer i=myebrechits.begin(); i!=myebrechits.end(); i++) {
00117       ebrechitenergy = i->energy();
00118       if(ebrechitenergy > ohHighestEnergyEBRecHit)
00119         ohHighestEnergyEBRecHit = ebrechitenergy;
00120     }
00121   }
00122 
00123   if (eerechits.isValid()) { 
00124     EERecHitCollection myeerechits; 
00125     myeerechits = * eerechits; 
00126  
00127     float eerechitenergy = -1.0; 
00128 
00129     typedef EERecHitCollection::const_iterator eerechititer; 
00130  
00131     for (eerechititer i=myeerechits.begin(); i!=myeerechits.end(); i++) { 
00132       eerechitenergy = i->energy(); 
00133       if(eerechitenergy > ohHighestEnergyEERecHit) 
00134         ohHighestEnergyEERecHit = eerechitenergy; 
00135     } 
00136   } 
00137 
00138   if (hbherechits.isValid()) {  
00139     HBHERecHitCollection myhbherechits;  
00140     myhbherechits = * hbherechits;  
00141   
00142     float hbherechitenergy = -1.0;  
00143  
00144     typedef HBHERecHitCollection::const_iterator hbherechititer;  
00145   
00146     for (hbherechititer i=myhbherechits.begin(); i!=myhbherechits.end(); i++) {  
00147       hbherechitenergy = i->energy();  
00148       if(hbherechitenergy > ohHighestEnergyHBHERecHit)  
00149         ohHighestEnergyHBHERecHit = hbherechitenergy;  
00150     }  
00151   }  
00152 
00153   if (horechits.isValid()) {   
00154     HORecHitCollection myhorechits;   
00155     myhorechits = * horechits;   
00156    
00157     float horechitenergy = -1.0;   
00158   
00159     typedef HORecHitCollection::const_iterator horechititer;   
00160    
00161     for (horechititer i=myhorechits.begin(); i!=myhorechits.end(); i++) {   
00162       horechitenergy = i->energy();   
00163       if(horechitenergy > ohHighestEnergyHORecHit)   
00164         ohHighestEnergyHORecHit = horechitenergy;   
00165     }   
00166   }   
00167 
00168   if (hfrechits.isValid()) {   
00169     HFRecHitCollection myhfrechits;   
00170     myhfrechits = * hfrechits;   
00171    
00172     float hfrechitenergy = -1.0;   
00173   
00174     typedef HFRecHitCollection::const_iterator hfrechititer;   
00175    
00176     for (hfrechititer i=myhfrechits.begin(); i!=myhfrechits.end(); i++) {   
00177       hfrechitenergy = i->energy();   
00178       if(hfrechitenergy > ohHighestEnergyHFRecHit)   
00179         ohHighestEnergyHFRecHit = hfrechitenergy;   
00180     }   
00181   }   
00182 
00184   // Start of AlCa pi0 trigger variables here
00186 
00187   if (first_) { 
00188 
00189   const EcalElectronicsMapping* TheMapping_ = ecalmapping.product(); 
00190   *TheMapping = *TheMapping_; 
00191   first_ = false; 
00192 
00193   geometry_eb = geoHandle->getSubdetectorGeometry(DetId::Ecal,EcalBarrel); 
00194   geometry_ee = geoHandle->getSubdetectorGeometry(DetId::Ecal,EcalEndcap); 
00195   geometry_es = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalPreshower); 
00196    
00197   topology_eb = pTopology->getSubdetectorTopology(DetId::Ecal,EcalBarrel); 
00198   topology_ee = pTopology->getSubdetectorTopology(DetId::Ecal,EcalEndcap); 
00199   }
00200 
00201   // End pi0 setup 
00202     
00203   //first get all the FEDs around EM objects with PT > defined value. 
00204   FEDListUsed.clear();
00205   std::vector<int>::iterator it; 
00206   if( RegionalMatch_){
00207     
00208     for( l1extra::L1EmParticleCollection::const_iterator emItr = l1EGIso->begin();
00209          emItr != l1EGIso->end() ;++emItr ){
00210       
00211       float pt = emItr -> pt();
00212       
00213       if( pt< ptMinEMObj_ ) continue; 
00214         
00215       int etaIndex = emItr->gctEmCand()->etaIndex() ;
00216       int phiIndex = emItr->gctEmCand()->phiIndex() ;
00217       double etaLow  = l1CaloGeom->etaBinLowEdge( etaIndex ) ;
00218       double etaHigh = l1CaloGeom->etaBinHighEdge( etaIndex ) ;
00219       double phiLow  = l1CaloGeom->emJetPhiBinLowEdge( phiIndex ) ;
00220       double phiHigh = l1CaloGeom->emJetPhiBinHighEdge( phiIndex ) ;
00221       
00222       std::vector<int> feds = ListOfFEDS(etaLow, etaHigh, phiLow, phiHigh, EMregionEtaMargin_, EMregionPhiMargin_);
00223       for (int n=0; n < (int)feds.size(); n++) {
00224         int fed = feds[n];
00225         it = find(FEDListUsed.begin(),FEDListUsed.end(),fed);
00226         if( it == FEDListUsed.end()){
00227           FEDListUsed.push_back(fed);
00228         }
00229       }
00230     }
00231     
00232     for( l1extra::L1EmParticleCollection::const_iterator emItr = l1EGNonIso->begin();
00233          emItr != l1EGNonIso->end() ;++emItr ){
00234       
00235       float pt = emItr -> pt();
00236 
00237       if( pt< ptMinEMObj_ ) continue; 
00238 
00239       int etaIndex = emItr->gctEmCand()->etaIndex() ;
00240       int phiIndex = emItr->gctEmCand()->phiIndex() ;
00241       double etaLow  = l1CaloGeom->etaBinLowEdge( etaIndex ) ;
00242       double etaHigh = l1CaloGeom->etaBinHighEdge( etaIndex ) ;
00243       double phiLow  = l1CaloGeom->emJetPhiBinLowEdge( phiIndex ) ;
00244       double phiHigh = l1CaloGeom->emJetPhiBinHighEdge( phiIndex ) ;
00245       
00246       std::vector<int> feds = ListOfFEDS(etaLow, etaHigh, phiLow, phiHigh, EMregionEtaMargin_, EMregionPhiMargin_);
00247       for (int n=0; n < (int)feds.size(); n++) {
00248         int fed = feds[n];
00249         it = find(FEDListUsed.begin(),FEDListUsed.end(),fed);
00250         if( it == FEDListUsed.end()){
00251           FEDListUsed.push_back(fed);
00252         }
00253       }
00254     }
00255 
00256     if( Jets_ ){
00257       
00258       double epsilon = 0.01;
00259       
00260       if (JETSdoCentral_) {
00261         
00262         for (l1extra::L1JetParticleCollection::const_iterator jetItr=l1extjetc->begin(); jetItr != l1extjetc->end(); jetItr++) {
00263           
00264           double pt    =   jetItr-> pt();
00265           double eta   =   jetItr-> eta();
00266           double phi   =   jetItr-> phi();
00267           
00268           if (pt < Ptmin_jets_ ) continue;
00269           
00270           std::vector<int> feds = ListOfFEDS(eta, eta, phi-epsilon, phi+epsilon, JETSregionEtaMargin_, JETSregionPhiMargin_);
00271           for (int n=0; n < (int)feds.size(); n++) {
00272             int fed = feds[n];
00273             it = find(FEDListUsed.begin(),FEDListUsed.end(),fed);
00274             if( it == FEDListUsed.end()){
00275               FEDListUsed.push_back(fed);
00276             }
00277           }
00278         }
00279         
00280       }
00281       
00282       if (JETSdoForward_) {
00283         
00284         for (l1extra::L1JetParticleCollection::const_iterator jetItr=l1extjetf->begin(); jetItr != l1extjetf->end(); jetItr++) {
00285           
00286           double pt    =  jetItr -> pt();
00287           double eta   =  jetItr -> eta();
00288           double phi   =  jetItr -> phi();
00289           
00290           if (pt < Ptmin_jets_ ) continue;
00291           
00292           std::vector<int> feds = ListOfFEDS(eta, eta, phi-epsilon, phi+epsilon, JETSregionEtaMargin_, JETSregionPhiMargin_);
00293           
00294           for (int n=0; n < (int)feds.size(); n++) {
00295             int fed = feds[n];
00296             it = find(FEDListUsed.begin(),FEDListUsed.end(),fed);
00297             if( it == FEDListUsed.end()){
00298               FEDListUsed.push_back(fed);
00299             }
00300           }
00301           
00302         }
00303       }
00304       
00305       if (JETSdoTau_) {
00306         
00307         for (l1extra::L1JetParticleCollection::const_iterator jetItr=l1exttaujet->begin(); jetItr != l1exttaujet->end(); jetItr++) {
00308           
00309           double pt    =  jetItr -> pt();
00310           double eta   =  jetItr -> eta();
00311           double phi   =  jetItr -> phi();
00312           
00313           if (pt < Ptmin_jets_ ) continue;
00314           
00315           std::vector<int> feds = ListOfFEDS(eta, eta, phi-epsilon, phi+epsilon, JETSregionEtaMargin_, JETSregionPhiMargin_);
00316           for (int n=0; n < (int)feds.size(); n++) {
00317             int fed = feds[n];
00318             it = find(FEDListUsed.begin(),FEDListUsed.end(),fed);
00319             if( it == FEDListUsed.end()){
00320               FEDListUsed.push_back(fed);
00321             }
00322           }
00323           
00324         }
00325       }
00326     }
00327   } 
00328 
00330   //separate into barrel and endcap to speed up when checking 
00331   FEDListUsedBarrel.clear(); 
00332   FEDListUsedEndcap.clear(); 
00333   for(  int j=0; j< int(FEDListUsed.size());j++){ 
00334     int fed = FEDListUsed[j]; 
00335     
00336     if( fed >= 10 && fed <= 45){ 
00337       FEDListUsedBarrel.push_back(fed); 
00338     }else FEDListUsedEndcap.push_back(fed); 
00339   } 
00340   
00341   //==============Start to process barrel part==================/// 
00342 
00343   if (pi0ebrechits.isValid()) { 
00344 
00345     const EcalRecHitCollection *hitCollection_p = pi0ebrechits.product(); 
00346     
00347     std::vector<EcalRecHit> seeds; 
00348     seeds.clear(); 
00349     
00350     std::vector<EBDetId> usedXtals; 
00351     usedXtals.clear(); 
00352     
00353     detIdEBRecHits.clear(); 
00354     EBRecHits.clear();  // EcalRecHit 
00355   
00356     detIdEBRecHits.clear(); 
00357     EBRecHits.clear();  // EcalRecHit 
00358     
00360     EBRecHitCollection::const_iterator itb; 
00361     
00362     for (itb=pi0ebrechits->begin(); itb!=pi0ebrechits->end(); itb++) { 
00363       double energy = itb->energy(); 
00364       if( energy < seleXtalMinEnergy_) continue;  
00365       
00366       EBDetId det = itb->id(); 
00367       
00368       if (RegionalMatch_){ 
00369         int fed = TheMapping->DCCid(det); 
00370         it = find(FEDListUsedBarrel.begin(),FEDListUsedBarrel.end(),fed); 
00371         if(it == FEDListUsedBarrel.end()) continue;  
00372       } 
00373       
00374       detIdEBRecHits.push_back(det); 
00375       EBRecHits.push_back(*itb); 
00376       
00377       if (energy > clusSeedThr_) seeds.push_back(*itb);       
00378     } 
00379     
00380     int nClus;
00381     std::vector<float> eClus;
00382     std::vector<float> etClus;
00383     std::vector<float> etaClus;
00384     std::vector<float> phiClus;
00385     std::vector<EBDetId> max_hit;
00386     std::vector< std::vector<EcalRecHit> > RecHitsCluster;
00387     std::vector<float> s4s9Clus;
00388     
00389     nClus=0;
00390     
00391     // Make own simple clusters (3x3, 5x5 or clusPhiSize_ x clusEtaSize_)
00392     sort(seeds.begin(), seeds.end(), eecalRecHitLess());
00393     
00394     Nalcapi0clusters = 0;
00395     nClusAll = 0; 
00396     
00397     for (std::vector<EcalRecHit>::iterator itseed=seeds.begin(); itseed!=seeds.end(); itseed++) {
00398       EBDetId seed_id = itseed->id();
00399       std::vector<EBDetId>::const_iterator usedIds;
00400       
00401       bool seedAlreadyUsed=false;
00402       for(usedIds=usedXtals.begin(); usedIds!=usedXtals.end(); usedIds++){
00403         if(*usedIds==seed_id){
00404           seedAlreadyUsed=true;
00405           break; 
00406         }
00407       }
00408       
00409       if(seedAlreadyUsed)continue;
00410       
00411       std::vector<DetId> clus_v = topology_eb->getWindow(seed_id,clusEtaSize_,clusPhiSize_);
00412       std::vector< std::pair<DetId, float> > clus_used;
00413       
00414       std::vector<EcalRecHit> RecHitsInWindow;
00415       
00416       double simple_energy = 0; 
00417       
00418       for (std::vector<DetId>::iterator det=clus_v.begin(); det!=clus_v.end(); det++) {
00419         EBDetId EBdet = *det;
00420         
00421         bool  HitAlreadyUsed=false;
00422         for(usedIds=usedXtals.begin(); usedIds!=usedXtals.end(); usedIds++){
00423           if(*usedIds==*det){
00424             HitAlreadyUsed=true;
00425             break;
00426           }
00427         }
00428         
00429         if(HitAlreadyUsed)continue;
00430       
00432           if (RegionalMatch_){
00433             int fed = TheMapping->DCCid(EBdet);
00434             it = find(FEDListUsedBarrel.begin(),FEDListUsedBarrel.end(),fed);
00435             if(it == FEDListUsedBarrel.end()) continue; 
00436           }
00437           
00438           
00439           std::vector<EBDetId>::iterator itdet = find( detIdEBRecHits.begin(),detIdEBRecHits.end(),EBdet);
00440           if(itdet == detIdEBRecHits.end()) continue; 
00441           
00442           int nn = int(itdet - detIdEBRecHits.begin());
00443           usedXtals.push_back(*det);
00444           RecHitsInWindow.push_back(EBRecHits[nn]);
00445           clus_used.push_back( std::pair<DetId, float>(*det, 1) );
00446           simple_energy = simple_energy + EBRecHits[nn].energy();
00447       }
00448       
00449       math::XYZPoint clus_pos = posCalculator_.Calculate_Location(clus_used,hitCollection_p,geometry_eb,geometry_es);
00450       
00451       float theta_s = 2. * atan(exp(-clus_pos.eta()));
00452       float p0x_s = simple_energy * sin(theta_s) * cos(clus_pos.phi());
00453       float p0y_s = simple_energy * sin(theta_s) * sin(clus_pos.phi());
00454       float et_s = sqrt( p0x_s*p0x_s + p0y_s*p0y_s);
00455       
00456       eClus.push_back(simple_energy);
00457       etClus.push_back(et_s);
00458       etaClus.push_back(clus_pos.eta());
00459       phiClus.push_back(clus_pos.phi());
00460       max_hit.push_back(seed_id);
00461       RecHitsCluster.push_back(RecHitsInWindow);
00462       
00463       //Compute S4/S9 variable
00464       //We are not sure to have 9 RecHits so need to check eta and phi:
00465       
00466       //check s4s9
00467       float s4s9_tmp[4];
00468       for(int i=0;i<4;i++)s4s9_tmp[i]= 0;
00469       
00470       int seed_ieta = seed_id.ieta();
00471       int seed_iphi = seed_id.iphi();
00472       
00473       convxtalid( seed_iphi,seed_ieta);
00474       
00475       for(unsigned int j=0; j<RecHitsInWindow.size();j++){
00476         EBDetId det = (EBDetId)RecHitsInWindow[j].id(); 
00477         
00478         int ieta = det.ieta();
00479         int iphi = det.iphi();
00480         
00481         convxtalid(iphi,ieta);
00482         
00483         float en = RecHitsInWindow[j].energy(); 
00484         
00485         int dx = diff_neta_s(seed_ieta,ieta);
00486         int dy = diff_nphi_s(seed_iphi,iphi);
00487         
00488         if(dx <= 0 && dy <=0) s4s9_tmp[0] += en; 
00489         if(dx >= 0 && dy <=0) s4s9_tmp[1] += en; 
00490         if(dx <= 0 && dy >=0) s4s9_tmp[2] += en; 
00491         if(dx >= 0 && dy >=0) s4s9_tmp[3] += en; 
00492       }
00493       
00494       float s4s9_max = *std::max_element( s4s9_tmp,s4s9_tmp+4)/simple_energy; 
00495       s4s9Clus.push_back(s4s9_max);
00496       
00497       if(nClusAll<MAXCLUS){
00498         ptClusAll[nClusAll] = et_s;
00499         etaClusAll[nClusAll] = clus_pos.eta();
00500         phiClusAll[nClusAll] = clus_pos.phi();
00501         s4s9ClusAll[nClusAll] = s4s9_max;
00502         nClusAll++; 
00503         Nalcapi0clusters++;
00504       }
00505       
00506       nClus++;
00507     }
00508   }
00509 
00510   //==============Start of  Endcap ==================//
00511 
00512   if (pi0eerechits.isValid()) {  
00513     
00514     const EcalRecHitCollection *hitCollection_e = pi0eerechits.product();  
00515     
00516     detIdEERecHits.clear(); 
00517     EERecHits.clear();  // EcalRecHit
00518     
00519     std::vector<EcalRecHit> seedsEndCap;
00520     seedsEndCap.clear();
00521     
00522     std::vector<EEDetId> usedXtalsEndCap;
00523     usedXtalsEndCap.clear();
00524     
00526     EERecHitCollection::const_iterator ite;
00527     for (ite=pi0eerechits->begin(); ite!=pi0eerechits->end(); ite++) {
00528       double energy = ite->energy();
00529       if( energy < seleXtalMinEnergy_) continue; 
00530       
00531       EEDetId det = ite->id();
00532       if (RegionalMatch_){
00533         EcalElectronicsId elid = TheMapping->getElectronicsId(det);
00534         int fed = elid.dccId();
00535         it = find(FEDListUsedEndcap.begin(),FEDListUsedEndcap.end(),fed);
00536         if(it == FEDListUsedEndcap.end()) continue; 
00537       }
00538       
00539       detIdEERecHits.push_back(det);
00540       EERecHits.push_back(*ite);
00541       
00542     
00543       if (energy > clusSeedThrEndCap_) seedsEndCap.push_back(*ite);
00544       
00545     }
00546     
00547     //Create empty output collections
00548     std::auto_ptr< EERecHitCollection > pi0EERecHitCollection( new EERecHitCollection );
00549     
00550     int nClusEndCap;
00551     std::vector<float> eClusEndCap;
00552     std::vector<float> etClusEndCap;
00553     std::vector<float> etaClusEndCap;
00554     std::vector<float> phiClusEndCap;
00555     std::vector< std::vector<EcalRecHit> > RecHitsClusterEndCap;
00556     std::vector<float> s4s9ClusEndCap;
00557     nClusEndCap=0;
00558     
00559     // Make own simple clusters (3x3, 5x5 or clusPhiSize_ x clusEtaSize_)
00560     sort(seedsEndCap.begin(), seedsEndCap.end(), eecalRecHitLess());
00561     
00562     for (std::vector<EcalRecHit>::iterator itseed=seedsEndCap.begin(); itseed!=seedsEndCap.end(); itseed++) {
00563       EEDetId seed_id = itseed->id();
00564       std::vector<EEDetId>::const_iterator usedIds;
00565       
00566       bool seedAlreadyUsed=false;
00567       for(usedIds=usedXtalsEndCap.begin(); usedIds!=usedXtalsEndCap.end(); usedIds++){
00568         if(*usedIds==seed_id){
00569           seedAlreadyUsed=true;
00570           break; 
00571         }
00572       }
00573       
00574       if(seedAlreadyUsed)continue;
00575       
00576       std::vector<DetId> clus_v = topology_ee->getWindow(seed_id,clusEtaSize_,clusPhiSize_);
00577       std::vector< std::pair<DetId, float> > clus_used;
00578       
00579       std::vector<EcalRecHit> RecHitsInWindow;
00580       
00581       double simple_energy = 0; 
00582       
00583       for (std::vector<DetId>::iterator det=clus_v.begin(); det!=clus_v.end(); det++) {
00584         EEDetId EEdet = *det;
00585         
00586         bool  HitAlreadyUsed=false;
00587         for(usedIds=usedXtalsEndCap.begin(); usedIds!=usedXtalsEndCap.end(); usedIds++){
00588           if(*usedIds==*det){
00589             HitAlreadyUsed=true;
00590             break;
00591           }
00592         }
00593         
00594         if(HitAlreadyUsed)continue;
00595         
00596         //once again. check FED of this det.
00597         if (RegionalMatch_){
00598           EcalElectronicsId elid = TheMapping->getElectronicsId(EEdet);
00599           int fed = elid.dccId();
00600           it = find(FEDListUsedEndcap.begin(),FEDListUsedEndcap.end(),fed);
00601           if(it == FEDListUsedEndcap.end()) continue; 
00602         }
00603         
00604         std::vector<EEDetId>::iterator itdet = find( detIdEERecHits.begin(),detIdEERecHits.end(),EEdet);
00605         if(itdet == detIdEERecHits.end()) continue; 
00606         
00607         int nn = int(itdet - detIdEERecHits.begin());
00608         usedXtalsEndCap.push_back(*det);
00609         RecHitsInWindow.push_back(EERecHits[nn]);
00610         clus_used.push_back( std::pair<DetId, float>(*det, 1) );
00611         simple_energy = simple_energy + EERecHits[nn].energy();
00612       }
00613       
00614       math::XYZPoint clus_pos = posCalculator_.Calculate_Location(clus_used,hitCollection_e,geometry_ee,geometry_es);
00615       
00616       float theta_s = 2. * atan(exp(-clus_pos.eta()));
00617       float p0x_s = simple_energy * sin(theta_s) * cos(clus_pos.phi());
00618       float p0y_s = simple_energy * sin(theta_s) * sin(clus_pos.phi());
00619       float et_s = sqrt( p0x_s*p0x_s + p0y_s*p0y_s);
00620       
00621       eClusEndCap.push_back(simple_energy);
00622       etClusEndCap.push_back(et_s);
00623       etaClusEndCap.push_back(clus_pos.eta());
00624       phiClusEndCap.push_back(clus_pos.phi());
00625       RecHitsClusterEndCap.push_back(RecHitsInWindow);
00626       
00627       //Compute S4/S9 variable
00628       //We are not sure to have 9 RecHits so need to check eta and phi:
00629       float s4s9_tmp[4];
00630       for(int i=0;i<4;i++) s4s9_tmp[i]= 0; 
00631       
00632       int ixSeed = seed_id.ix();
00633       int iySeed = seed_id.iy();
00634       for(unsigned int j=0; j<RecHitsInWindow.size();j++){
00635         EEDetId det_this = (EEDetId)RecHitsInWindow[j].id(); 
00636         int dx = ixSeed - det_this.ix();
00637         int dy = iySeed - det_this.iy();
00638         
00639         float en = RecHitsInWindow[j].energy(); 
00640         
00641         if(dx <= 0 && dy <=0) s4s9_tmp[0] += en; 
00642         if(dx >= 0 && dy <=0) s4s9_tmp[1] += en; 
00643         if(dx <= 0 && dy >=0) s4s9_tmp[2] += en; 
00644         if(dx >= 0 && dy >=0) s4s9_tmp[3] += en; 
00645       }
00646       
00647       float s4s9_max = *std::max_element( s4s9_tmp,s4s9_tmp+4)/simple_energy;
00648       s4s9ClusEndCap.push_back(s4s9_max);
00649       
00650       if(nClusAll<MAXCLUS){
00651         ptClusAll[nClusAll] = et_s;
00652         etaClusAll[nClusAll] = clus_pos.eta();
00653         phiClusAll[nClusAll] = clus_pos.phi();
00654         s4s9ClusAll[nClusAll] = s4s9_max;
00655         nClusAll++;
00656         Nalcapi0clusters++;
00657       }
00658       
00659       nClusEndCap++;
00660     }
00661   }
00662 
00663   // If no barrel OR endcap rechits, set everything to 0
00664   if(!(pi0eerechits.isValid()) && !(pi0ebrechits.isValid()))
00665     Nalcapi0clusters = 0;
00666 }
00667 
00668 
00670 // Below here are helper functions for the pi0 variables
00672 
00673 
00675 std::vector<int> HLTAlCa::ListOfFEDS(double etaLow, double etaHigh, double phiLow, 
00676                                     double phiHigh, double etamargin, double phimargin)
00677 {
00678 
00679   std::vector<int> FEDs;
00680 
00681   if (phimargin > Geom::pi()) phimargin =  Geom::pi() ;
00682 
00683   etaLow -= etamargin;
00684   etaHigh += etamargin;
00685   double phiMinus = phiLow - phimargin;
00686   double phiPlus = phiHigh + phimargin;
00687 
00688   bool all = false;
00689   double dd = fabs(phiPlus-phiMinus);
00690 
00691   if (dd > 2.*Geom::pi() ) all = true;
00692 
00693   while (phiPlus > Geom::pi()) { phiPlus -= 2.*Geom::pi() ; }
00694   while (phiMinus < 0) { phiMinus += 2.*Geom::pi() ; }
00695   if ( phiMinus > Geom::pi()) phiMinus -= 2.*Geom::pi() ;
00696 
00697   double dphi = phiPlus - phiMinus;
00698   if (dphi < 0) dphi += 2.*Geom::pi() ;
00699 
00700   if (dphi > Geom::pi()) {
00701     int fed_low1 = TheMapping->GetFED(etaLow,phiMinus*180./Geom::pi());
00702     int fed_low2 = TheMapping->GetFED(etaLow,phiPlus*180./Geom::pi());
00703 
00704     if (fed_low1 == fed_low2) all = true;
00705     int fed_hi1 = TheMapping->GetFED(etaHigh,phiMinus*180./Geom::pi());
00706     int fed_hi2 = TheMapping->GetFED(etaHigh,phiPlus*180./Geom::pi());
00707     
00708     if (fed_hi1 == fed_hi2) all = true;
00709   }
00710 
00711   if (all) {
00712 
00713     phiMinus = -20 * Geom::pi() / 180.;  // -20 deg
00714     phiPlus = -40 * Geom::pi() / 180.;  // -20 deg
00715   }
00716 
00717   const EcalEtaPhiRegion ecalregion(etaLow,etaHigh,phiMinus,phiPlus);
00718 
00719   FEDs = TheMapping->GetListofFEDs(ecalregion);
00720 
00721   return FEDs;
00722 
00723 }
00724 
00725 
00727 int HLTAlCa::convertSmToFedNumbBarrel(int ieta, int smId){
00728     
00729   if( ieta<=-1) return smId - 9; 
00730   else return smId + 27; 
00731   
00732   
00733 }
00734 
00735 
00736 void HLTAlCa::convxtalid(Int_t &nphi,Int_t &neta)
00737 {
00738   // Barrel only
00739   // Output nphi 0...359; neta 0...84; nside=+1 (for eta>0), or 0 (for eta<0).
00740   // neta will be [-85,-1] , or [0,84], the minus sign indicates the z<0 side.
00741   
00742   if(neta > 0) neta -= 1;
00743   if(nphi > 359) nphi=nphi-360;
00744   
00745   // final check
00746   if(nphi >359 || nphi <0 || neta< -85 || neta > 84)
00747     {
00748       std::cout <<" unexpected fatal error in HLTPi0::convxtalid "<<  nphi <<  " " << neta <<  " " <<std::endl;
00749       //exit(1);
00750     }
00751 } //end of convxtalid
00752 
00753 
00754 
00755 
00756 int HLTAlCa::diff_neta_s(Int_t neta1, Int_t neta2){
00757   Int_t mdiff;
00758   mdiff=(neta1-neta2);
00759   return mdiff;
00760 }
00761 
00762 
00763 // Calculate the distance in xtals taking into account the periodicity of the Barrel 
00764 int HLTAlCa::diff_nphi_s(Int_t nphi1,Int_t nphi2) { 
00765   Int_t mdiff; 
00766   if(std::abs(nphi1-nphi2) < (360-std::abs(nphi1-nphi2))) { 
00767     mdiff=nphi1-nphi2; 
00768   } 
00769   else { 
00770     mdiff=360-std::abs(nphi1-nphi2); 
00771     if(nphi1>nphi2) mdiff=-mdiff; 
00772   } 
00773   return mdiff; 
00774 } 
00775