CMS 3D CMS Logo

PhotonMIPHaloTagger.cc
Go to the documentation of this file.
1 
24 
27 
33 
40 
43 
44 #include <string>
45 #include <TMath.h>
46 
47 
49 
50 
51  EBecalCollection_ = iC.consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("barrelEcalRecHitCollection"));
52  EEecalCollection_ = iC.consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("endcapEcalRecHitCollection"));
53 
54 
55  yRangeFit_ = conf.getParameter<double>("YRangeFit");
56  xRangeFit_ = conf.getParameter<double>("XRangeFit");
57  residualWidthEnergy_ = conf.getParameter<double>("ResidualWidth");
58  haloDiscThreshold_ = conf.getParameter<double>("HaloDiscThreshold");
59 
60 
61 }
62 
63 
64 
65 
66 
68  const edm::Event& e,
69  const edm::EventSetup& es,
71 
72  //get the predefined variables
77 
78 
79 
80  //First store in local variables
81  mipFitResults_.clear();
82 
83  nhitCone_ = -99; //hit inside the cone
84  ismipHalo_ = false; // halo?
85 
86 
87 
88  // Get EcalRecHits
89  bool validEcalRecHits=true;
90 
91  edm::Handle<EcalRecHitCollection> barrelHitHandle;
92  e.getByToken(EBecalCollection_, barrelHitHandle);
93 
94  if (!barrelHitHandle.isValid()) {
95  edm::LogError("MIPcalculate") << "Error! Can't get the barrel hits product ";//<<EBecalCollection_.label(); (cant be bothered tracking the input tag just for this error message)
96  validEcalRecHits=false;
97  }
98 
99 
100  if(validEcalRecHits){
101  // GetMIPTrailFit
102  mipFitResults_ = GetMipTrailFit(pho, e, es,
103  barrelHitHandle,
105  nhitCone_,
106  ismipHalo_ );
107 
108 
109 
110  //Now set the variable in "MIPVaraible"
111  mipId.mipChi2 = mipFitResults_[0];
112  mipId.mipTotEnergy = mipFitResults_[1];
113  mipId.mipSlope = mipFitResults_[2];
114  mipId.mipIntercept = mipFitResults_[3];
115  mipId.mipNhitCone = nhitCone_;
116  mipId.mipIsHalo = ismipHalo_;
117  }
118  else{ std::cout<<"MIPCalculate::Ecal Collection is not there hence set some default values"<<std::endl;
119  mipId.mipChi2 = -999.;
120  mipId.mipTotEnergy = -999.;
121  mipId.mipSlope = -999.;
122  mipId.mipIntercept = -999.;
123  mipId.mipNhitCone = 0;
124  mipId.mipIsHalo = false;
125  }
126 
127  // std::cout<<"Setting: isHalo = "<<ismipHalo_<<" nhitcone= "<<nhitCone_<<std::endl;
128  // std::cout<<"Setting: Chi2 = "<<mipFitResults_[0]<<" eT= "<<mipFitResults_[1]<<" slope = "<<mipFitResults_[2]<<" Intercept ="<<mipFitResults_[3]<<std::endl;
129 }
130 
131 
132 
133 
134 
135 // Get the MIP Variable NCone, Energy etc here
137  const edm::Event& iEvent,
138  const edm::EventSetup& iSetup,
139  edm::Handle<EcalRecHitCollection> ecalhitsCollEB,
140  double inputRangeY,
141  double inputRangeX,
142  double inputResWidth,
143  double inputHaloDiscCut,
144  int &NhitCone_,
145  bool &ismipHalo_)
146 {
147  bool debug_= false;
148 
149  if(debug_)std::cout<<" inside MipFitTrail "<<std::endl;
150  //getting them from cfi config
151  double yrange = inputRangeY;
152  double xrange = inputRangeX;
153  double res_width = inputResWidth; //width of the residual distribution
154  double halo_disc_cut = inputHaloDiscCut; //cut based on Shower,Angle and MIP
155 
156 
157  //initilize them here
158  double m = 0.;
159  m = yrange/xrange; //slope of the lines which form the cone around the trail
160 
161 
162 
163 
164  //first get the seed cell index
165  int seedIEta = -999;
166  int seedIPhi = -999;
167  double seedE = -999.;
168 
169 
170  //get seed propert.
171  GetSeedHighestE(photon,
172  iEvent,
173  iSetup,
174  ecalhitsCollEB,
175  seedIEta,
176  seedIPhi,
177  seedE);
178 
179  if(debug_)std::cout<<"Seed E ="<<seedE<<" Seed ieta = "<<seedIEta<<" Seed iphi ="<< seedIPhi<<std::endl;
180 
181  //to store results
182  std::vector<double> FitResults_;
183  FitResults_.clear();
184 
185 
186  //create some vector and clear them
187  std::vector<int> ieta_cell;
188  std::vector<int> iphi_cell;
189  std::vector<double> energy_cell;
190 
191  ieta_cell.clear();
192  iphi_cell.clear();
193  energy_cell.clear();
194 
195 
196  int ietacell = 0;
197  int iphicell = 0;
198  int kArray = 0;
199 
200  double energy_total = 0.;
201  int delt_ieta =0;
202  int delt_iphi =0;
203 
204 
205  for(EcalRecHitCollection::const_iterator it = ecalhitsCollEB->begin();it!=ecalhitsCollEB->end();++it)
206  {
207  EBDetId dit = it->detid();
208 
209 
210  iphicell = dit.iphi();
211  ietacell = dit.ieta();
212 
213  if(ietacell < 0)ietacell++;
214 
215  //Exclude all cells within +/- 5 ieta of seed cell
216  if(TMath::Abs(ietacell - seedIEta) >= 5 && it->energy() > 0.)
217  {
218 
219  delt_ieta = ietacell - seedIEta;
220  delt_iphi = iphicell - seedIPhi;
221 
222  //Phi wrapping inclusion
223  if(delt_iphi > 180){ delt_iphi = delt_iphi - 360; }
224  if(delt_iphi < -180){ delt_iphi = delt_iphi + 360; }
225 
226  //Condition to be within the cones
227  if( ( (delt_iphi >= (m*delt_ieta)) && (delt_iphi <= (-m*delt_ieta)) ) ||
228  ( (delt_iphi <= (m*delt_ieta)) && (delt_iphi >= (-m*delt_ieta)) )
229  )
230  {
231  ieta_cell.push_back(delt_ieta);
232  iphi_cell.push_back(delt_iphi);
233  energy_cell.push_back(it->energy());
234  energy_total += it->energy();
235  kArray++;
236  }
237 
238  }//within cerntain range of seed cell
239 
240  }//loop voer hits
241 
242 
243 
244 //Iterations for imporovements
245 
246  int Npoints = 0;
247  int throwaway_index = -1;
248  double chi2;
249  double eT = 0.;
250  double hres = 99999.;
251 
252  //some tmp local variale
253  double Roundness_ =999.;
254  double Angle_ =999.;
255  double halo_disc_ =0.;
256 
257  Npoints = kArray;
258 
259  if(debug_)std::cout<<" starting npoing = "<<Npoints<<std::endl;
260 
261  //defined some variable for iterative fitting the mip trail line
262  double sx = 0.0;
263  double sy = 0.0;
264  double ss = 0.0;
265  double sxx = 0.0;
266  double sxy = 0.0;
267  double a1 = 0.0;
268  double b1 = 0.0;
269  double m_chi2 = 0.0;
270  double etot_cell=0.0;
271 
272 
273  //start Iterations
274  for(int it=0; it < 200 && hres >(5.0*res_width); it++)
275  { //Max iter. is 200
276 
277 
278  //Throw away previous highest residual, if not first loop
279  if (throwaway_index!=-1)
280  {
281  ieta_cell.erase(ieta_cell.begin()+throwaway_index);
282  iphi_cell.erase(iphi_cell.begin()+throwaway_index);
283  energy_cell.erase(energy_cell.begin()+throwaway_index);
284  Npoints--;
285  }
286 
287 
288  //Lets Initialize them first for each iteration
289  sx = 0.0;
290  sy = 0.0;
291  ss = 0.0;
292  sxx = 0.0;
293  sxy = 0.0;
294  m_chi2 = 0.0;
295  etot_cell=0.0;
296 
297 
298  //Fit the line to trail
299  for(int j=0; j<Npoints; j++)
300  {
301  double wt = 1.0;
302  ss += wt;
303  sx += ieta_cell[j]*wt;
304  sy += iphi_cell[j];
305  sxx += ieta_cell[j]*ieta_cell[j]*wt;
306  sxy += ieta_cell[j]*iphi_cell[j]*wt;
307  }
308 
309  double delt = ss*sxx - (sx*sx);
310  a1 = ((sxx*sy)-(sx*sxy))/delt; // INTERCEPT
311  b1 = ((ss*sxy)-(sx*sy))/delt; // SLOPE
312 
313 
314  double highest_res = 0.;
315  int highres_index = 0;
316 
317 
318  for(int j=0; j<Npoints; j++)
319  {
320  double res = 1.0*iphi_cell[j] - a1 - b1*ieta_cell[j];
321  double res_sq = res*res;
322 
323  if(TMath::Abs(res) > highest_res)
324  {
325  highest_res = TMath::Abs(res);
326  highres_index = j;
327  }
328 
329  m_chi2 += res_sq;
330  etot_cell += energy_cell[j];
331 
332  }
333 
334 
335  throwaway_index = highres_index;
336  hres = highest_res;
337 
338  chi2 = m_chi2 /((Npoints-2));
339  chi2 = chi2/(res_width*res_width);
340  eT = etot_cell;
341 
342  }//for loop for iterations
343 
344  if(debug_)std::cout<<"hres = "<<hres<<std::endl;
345 
346  //get roundness and angle for this photon candidate form EcalClusterTool
347  std::vector<float> showershapes_barrel = EcalClusterTools::roundnessBarrelSuperClusters(*(photon->superCluster()), (*ecalhitsCollEB.product()));
348 
349  Roundness_ = showershapes_barrel[0];
350  Angle_ = showershapes_barrel[1];
351 
352  if(debug_)std::cout<<" eTot ="<<eT<<" Rounness = "<<Roundness_<<" Angle_ "<<Angle_ <<std::endl;
353 
354  //get the halo disc variable
355  halo_disc_ = eT/(Roundness_* Angle_);
356 
357 
359  FitResults_.push_back(chi2); //chi2
360  FitResults_.push_back(eT); //total energy
361  FitResults_.push_back(b1); //slope
362  FitResults_.push_back(a1); //intercept
363  NhitCone_ = Npoints; //nhit in cone
364  if(halo_disc_ > halo_disc_cut)ismipHalo_ = true; //is halo?, yes if halo_disc > 70 by default
365  // based on 2010 study
366 
367 
368  if(debug_)std::cout<<"Endof MIP Trail: halo_dic= "<<halo_disc_<<" nhitcone ="<<NhitCone_<<" isHalo= "<<ismipHalo_<<std::endl;
369  if(debug_)std::cout<<"Endof MIP Trail: Chi2 = "<<chi2<<" eT= "<<eT<<" slope = "<<b1<<" Intercept ="<<a1<<std::endl;
370 
371  return FitResults_;
372 
373 }
374 
375 
376 
377 
378 
379 //get the seed crystal index
381  const edm::Event& iEvent,
382  const edm::EventSetup& iSetup,
384  int &seedIeta,
385  int &seedIphi,
386  double &seedEnergy)
387 {
388 
389  bool debug_= false;
390 
391  if(debug_)std::cout<<"Inside GetSeed"<<std::endl;
392  //Get the Seed
393  double SeedE = -999.;
394 
395  //initilaze them here
396  seedIeta = -999;
397  seedIphi = -999;
398  seedEnergy = -999.;
399 
400 
401 
402 
403  std::vector< std::pair<DetId, float> > PhotonHit_DetIds = photon->superCluster()->hitsAndFractions();
404  int ncrys = 0;
405 
406  std::vector< std::pair<DetId, float> >::const_iterator detitr;
407  for(detitr = PhotonHit_DetIds.begin(); detitr != PhotonHit_DetIds.end(); ++detitr)
408  {
409 
410  if (((*detitr).first).det() == DetId::Ecal && ((*detitr).first).subdetId() == EcalBarrel)
411  {
412  EcalRecHitCollection::const_iterator j= Brechit->find(((*detitr).first));
414 
415  if ( j!= Brechit->end()) thishit = j;
416  if ( j== Brechit->end()){ continue;}
417 
418 
419  EBDetId detId = (EBDetId)((*detitr).first);
420 
421  double crysE = thishit->energy();
422 
423  if (crysE > SeedE)
424  {SeedE = crysE;
425  seedIeta = detId.ieta();
426  seedIphi = (detId.iphi());
427  seedEnergy = SeedE;
428 
429  if(debug_)std::cout<<"Current max Seed = "<<SeedE<<" seedIphi = "<<seedIphi<<" ieta= "<<seedIeta<<std::endl;
430 
431  }
432 
433  ncrys++;
434 
435 
436  }//check if in Barrel
437 
438  }//loop over EBrechits cells
439 
440 
441 
442  if(debug_)std::cout<<"End of GetSeed"<<std::endl;
443 
444 }
445 
446 
447 
448 
T getParameter(std::string const &) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void MIPcalculate(const reco::Photon *, const edm::Event &, const edm::EventSetup &es, reco::Photon::MIPVariables &mipId)
std::vector< EcalRecHit >::const_iterator const_iterator
reco::SuperClusterRef superCluster() const override
Ref to SuperCluster.
Definition: Electron.h:6
void GetSeedHighestE(const reco::Photon *photon, const edm::Event &iEvent, const edm::EventSetup &iSetup, edm::Handle< EcalRecHitCollection > Brechit, int &seedIEta, int &seedIPhi, double &seedE)
int iphi() const
get the crystal iphi
Definition: EBDetId.h:51
int iEvent
Definition: GenABIO.cc:224
edm::EDGetToken EBecalCollection_
T Abs(T a)
Definition: MathUtil.h:49
std::vector< double > mipFitResults_
int ieta() const
get the crystal ieta
Definition: EBDetId.h:49
void setup(const edm::ParameterSet &conf, edm::ConsumesCollector &&iC)
bool isValid() const
Definition: HandleBase.h:74
std::vector< double > GetMipTrailFit(const reco::Photon *photon, const edm::Event &iEvent, const edm::EventSetup &iSetup, edm::Handle< EcalRecHitCollection > ecalhitsCollEB, double inputRangeY, double inputRangeX, double inputResWidth, double inputHaloDiscCut, int &NhitCone_, bool &ismipHalo_)
const_iterator end() const
T const * product() const
Definition: Handle.h:74
iterator find(key_type k)
edm::EDGetToken EEecalCollection_
const_iterator begin() const