CMS 3D CMS Logo

HFClusterAlgo.cc

Go to the documentation of this file.
00001 #include "RecoEgamma/EgammaHFProducers/interface/HFClusterAlgo.h"
00002 #include <sstream>
00003 #include <iostream>
00004 #include <algorithm>
00005 #include <list> 
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 using namespace std;
00008 using namespace reco;
00018 HFClusterAlgo::HFClusterAlgo() {
00019 }
00020 
00021 class CompareHFCompleteHitET {
00022 public:
00023   bool operator()(const HFClusterAlgo::HFCompleteHit& h1,const HFClusterAlgo::HFCompleteHit& h2) const {
00024     return h1.et>h2.et;
00025   }
00026 };
00027 
00028 class CompareHFCore {
00029 public:
00030   bool operator()(const double c1,const double c2) const {
00031     return c1>c2;
00032   }
00033 };
00034 
00035 void HFClusterAlgo::setup(double minTowerEnergy) {
00036   
00037   m_minTowerEnergy=minTowerEnergy;
00038 }
00039 
00041 void HFClusterAlgo::clusterize(const HFRecHitCollection& hf, 
00042                                const CaloGeometry& geom,
00043                                HFEMClusterShapeCollection& clusterShapes,
00044                                BasicClusterCollection& BasicClusters,
00045                                SuperClusterCollection& SuperClusters) {
00046   
00047   std::vector<HFCompleteHit> hits, seeds;
00048   HFRecHitCollection::const_iterator j;
00049   std::vector<HFCompleteHit>::iterator i;
00050   std::vector<HFCompleteHit>::iterator k;
00051   int dP, dE, PWrap;
00052   bool isok=true;
00053   HFEMClusterShape clusShp;
00054   BasicCluster Bclus;
00055   SuperCluster Sclus;
00056    bool doCluster=false;
00057   for (j=hf.begin(); j!= hf.end(); j++)  {
00058     HFCompleteHit ahit;
00059     ahit.id=j->id();
00060     ahit.energy=j->energy();
00061     double eta=geom.getPosition(j->id()).eta();
00062     ahit.et=ahit.energy/cosh(eta);
00063     
00064     hits.push_back(ahit);
00065   }
00066   
00067   std::sort(hits.begin(), hits.end(), CompareHFCompleteHitET());
00068   for (i=hits.begin(); i!= hits.end(); i++)  {
00069     
00070     if (i->et > 10) {
00071       if ((i->id.ietaAbs()==40)||(i->id.ietaAbs()==41)||(i->id.ietaAbs()==29)||(i->id.depth()!=1)) 
00072         isok = false; 
00073       
00074       if ( (i==hits.begin()) && (isok) ) {
00075         doCluster=true;
00076 //      seeds.push_back(*i);
00077 //      makeCluster( i->id(),hf, geom,clusShp,Bclus,Sclus);
00078 //      clusterShapes.push_back(clusShp); 
00079 //      BasicClusters.push_back(Bclus);
00080 //      SuperClusters.push_back(Sclus);         
00081         //clusterAsoc.insert(SuperClusters,clusterShapes);
00082   
00083       }
00084       else {
00085         for (k=seeds.begin(); isok && k!=seeds.end(); k++) { //i->hits, k->seeds
00086           
00087           for (dE=-2; dE<=2; dE++)
00088             for (dP=-4;dP<=4; dP+=2) {
00089               PWrap=k->id.iphi()+dP;    
00090               if (PWrap<0) 
00091                 PWrap+=72;
00092               if (PWrap>72)
00093                 PWrap-=72;
00094               
00095               if ( (i->id.iphi()==PWrap) && (i->id.ieta()==k->id.ieta()+dE))
00096                 isok = false;
00097             }
00098         } 
00099         if (isok) {
00100           doCluster=true;
00101         }
00102       }
00103       if (doCluster) { 
00104         seeds.push_back(*i);
00105         makeCluster( i->id(),hf, geom,clusShp,Bclus,Sclus);
00106         
00107         clusterShapes.push_back(clusShp);
00108         BasicClusters.push_back(Bclus);
00109         SuperClusters.push_back(Sclus);
00110         
00111 //      clusterAssoc.insert(edm::Ref<SuperClusterCollection>(SuperClusters,where),
00112 //                          edm::Ref<HFEMClusterShapeCollection>(clusterShapes,where));
00113         
00114       }
00115       
00116     }  
00117     // clusterAsoc.insert(SuperClusters,clusters);
00118   }
00119 }
00120 
00121 void HFClusterAlgo::makeCluster(const HcalDetId& seedid,
00122                                 const HFRecHitCollection& hf, 
00123                                 const CaloGeometry& geom,
00124                                 HFEMClusterShape& clusShp,
00125                                 BasicCluster& Bclus,
00126                                 SuperCluster& Sclus)  {
00127                         
00128 
00129   double w=0;//sum over all log E's
00130   double wgt=0;
00131   double w_e=0;//sum over ieat*energy
00132   double w_x=0;
00133   double w_y=0;
00134   double w_z=0;
00135   double wp_e=0;//sum over iphi*energy
00136   double e_e=0;//nonwieghted eta sum
00137   double e_ep=0; //nonweighted phi sum
00138   double sum_energy=0;
00139   double l_3=0;//sum for enenergy in 3x3 long fibers etc.
00140   double s_3=0;
00141   double l_5=0;
00142   double s_5=0;
00143   double l_1=0;
00144   double s_1=0;
00145   int de, dp, ls, phiWrap;
00146   double l_1e=0;
00147   GlobalPoint sp=geom.getPosition(seedid);
00148   std::vector<double> coreCanid;
00149   std::vector<double>::const_iterator ci;
00150   HFRecHitCollection::const_iterator i;
00151   std::vector<DetId> usedHits; 
00152  
00153   HFRecHitCollection::const_iterator si;
00154   HcalDetId sid(HcalForward,seedid.ieta(),seedid.iphi(),1);
00155   si=hf.find(sid);  
00156 
00157   // lots happens here
00158   // edge type 1 has 40/41 in 3x3 and 5x5
00159   bool edge_type1=seedid.ietaAbs()==39 && (seedid.iphi()%4)==3;
00160   
00161   for (de=-2; de<=2; de++)
00162     for (dp=-4;dp<=4; dp+=2) {
00163       phiWrap=seedid.iphi()+dp; 
00164       if (phiWrap<0) 
00165         phiWrap+=72;
00166       if (phiWrap>72)
00167         phiWrap-=72;
00168       
00169       for (ls=1;ls<=2; ls++){
00170         
00171         /* Handling of phi-width change problems */
00172         if (edge_type1 && de==seedid.zside())
00173           if (dp==-2) { // we want it in the 3x3
00174             phiWrap-=2;
00175             if (phiWrap<0) phiWrap+=72;
00176           } else if (dp==-4) continue; // but not double counted in 5x5
00177         
00178         HcalDetId id(HcalForward,seedid.ieta()+de,phiWrap,ls);
00179         i=hf.find(id);
00180         
00181         DetId Did(id.rawId());
00182         usedHits.push_back(Did);
00183         
00184         if (i==hf.end()) continue;
00185         if (i->energy()> m_minTowerEnergy){
00186           if (ls==1) {
00187             l_5+=i->energy();
00188           }
00189           
00190           if ((ls==1)&&(de>-2)&&(de<2)&&(dp>-4)&&(dp<4)) {
00191             l_3+=i->energy();
00192           }
00193           if ((ls==1)&&(dp==0)&&(de==0)) {
00194             l_1=i->energy();
00195           }
00196           if (ls==2) {
00197             s_5+=i->energy();
00198           }       
00199           if ((ls==2)&&(de>-2)&&(de<2)&&(dp>-4)&&(dp<4)) {
00200             s_3+=i->energy();
00201           }
00202           if ((ls==2)&&(dp==0)&&(de==0)) {
00203             s_1=i->energy();
00204           }
00205           if ((ls==1)&&(de>-2)&&(de<2)&&(dp>-4)&&(dp<4)&&(i->energy()>(.5*si->energy()))) {
00206             coreCanid.push_back(i->energy());
00207           }
00208           
00209           
00210           GlobalPoint p=geom.getPosition(id);
00211           
00212           double d_p = p.phi()-sp.phi();
00213           while (d_p < -M_PI)
00214             d_p+=2*M_PI;
00215           while (d_p > M_PI)
00216             d_p-=2*M_PI;
00217           double d_e = p.eta()-sp.eta();
00218           if((de>-2)&&(de<2)&&(dp>-4)&&(dp<4)/*&&(ls==1)*/ && i->energy()>0) {//long only
00219             wgt=log((i->energy()));
00220             if (wgt>0){
00221               w+=wgt;
00222               w_e+=(d_e)*wgt;
00223               wp_e+=(d_p)*wgt;
00224               e_e+=d_e;
00225               e_ep+=d_p;
00226               sum_energy+=i->energy();
00227               w_x+=(p.x())*wgt;//(p.x()-sp.x())*wgt;
00228               w_y+=(p.y())*wgt;
00229               w_z+=(p.z())*wgt;
00230             }
00231           }
00232         }       
00233       }
00234     }
00235   //Core sorting done here
00236   std::sort(coreCanid.begin(), coreCanid.end(), CompareHFCore());
00237   for (ci=coreCanid.begin();ci!=coreCanid.end();ci++){
00238     if(ci==coreCanid.begin()){
00239       l_1e=*ci;
00240     }else if (*ci>.5*l_1e){
00241       l_1e+=*ci;
00242     }
00243   }//core sorting end 
00244   
00245   double z_=w_z/w;    //w_z/w+sp.z(); if changed to delta z style
00246   double x_=w_x/w;
00247   double y_=w_y/w;
00248   
00249   double eta=w_e/w+sp.eta();
00250   
00251   double phi=(wp_e/w)+sp.phi();
00252   
00253   while (phi < -M_PI)
00254     phi+=2*M_PI;
00255   while (phi > M_PI)
00256     phi-=2*M_PI;
00257   
00258   double HFEtaBounds[14] = {2.853, 2.964, 3.139, 3.314, 3.489, 3.664, 3.839, 4.013, 4.191, 4.363, 4.538, 4.716, 4.889, 5.191};
00259   double RcellEta = eta;
00260   double Cphi = (phi>0.)?(fmod((phi),0.087*2)/(0.087*2)):((fmod((phi),0.087*2)/(0.087*2))+1.0);
00261   double Rbin = -1.0;
00262   for (int icell = 0; icell < 12; icell++ ){
00263     if ( (RcellEta>HFEtaBounds[icell]) && (RcellEta<HFEtaBounds[icell+1]) )
00264       Rbin = (RcellEta - HFEtaBounds[icell])/(HFEtaBounds[icell+1] - HFEtaBounds[icell]);
00265   }
00266   double Ceta=Rbin;
00267   
00268   while (phi< -M_PI)
00269     phi+=2*M_PI;
00270   while (phi > M_PI)
00271     phi-=2*M_PI;
00272   
00273   
00274   math::XYZPoint xyzclus(x_,y_,z_);
00275   
00276   double chi2=-1;
00277   AlgoId algoID = hybrid;
00278   //return  HFEMClusterShape, BasicCluster, SuperCluster
00279   
00280   HFEMClusterShape myClusShp(l_1, s_1, l_3, s_3, l_5,s_5, l_1e,Ceta, Cphi,seedid);
00281   
00282   clusShp = myClusShp;
00283   
00284   BasicCluster MyBclus(l_3,xyzclus,chi2,usedHits,algoID);
00285   Bclus=MyBclus;
00286   
00287   
00288   SuperCluster MySclus(l_3,xyzclus);
00289   Sclus=MySclus;
00290   
00291 }

Generated on Tue Jun 9 17:43:22 2009 for CMSSW by  doxygen 1.5.4