CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hcalCalib.cc
Go to the documentation of this file.
1 // TSelector-based code for getting the HCAL resp. correction
2 // from physics events. Works for DiJet and IsoTrack calibration.
3 //
4 // Anton Anastassov (Northwestern)
5 // Email: aa@fnal.gov
6 //
7 //
8 
12 
13 #include <iostream>
14 #include <fstream>
15 #include <sstream>
16 #include <string>
17 
18 #include <map>
19 #include <numeric>
20 #include <algorithm>
21 #include <set>
22 
24 
27 
28 void hcalCalib::Begin(TTree* /*tree*/) {
29  TString option = GetOption();
30 
31  nEvents = 0;
32 
34  edm::LogError("HcalCalib") << "\nERROR: Failed to read the phi symmetry corrections.\n"
35  << "Check if the filename is correct. If the corrections are not needed, set the "
36  "corresponding flag to \"false\"\n"
37  << "\nThe program will be terminated\n";
38 
39  exit(1);
40  }
41 
42  // cellEnergies.reserve(1000000);
43  // cellIds.reserve(1000000);
44  // targetEnergies.reserve(1000000);
45 
46  histoFile = new TFile(HISTO_FILENAME.Data(), "RECREATE");
47 
48  h1_trkP = new TH1F("h1_trkP", "Track momenta; p_{trk} (GeV); Number of tracks", 100, 0, 200);
49  h1_allTrkP = new TH1F("h1_allTrkP", "Track momenta - all tracks; p_{trk} (GeV); Number of tracks", 100, 0, 200);
50 
51  h1_selTrkP_iEta10 = new TH1F(
52  "h1_selTrkP_iEta10", "Track momenta - tracks with |iEta|<10; p_{trk} (GeV); Number of tracks", 100, 0, 200);
53 
54  if (CALIB_TYPE == "ISO_TRACK")
55  h1_rawSumE = new TH1F("h1_rawSumE", "Cluster Energy; E_{cl} (GeV); Number of tracks", 100, 0, 200);
56  else
57  h1_rawSumE = new TH1F("h1_rawSumE", "Cluster Energy; E_{cl} (GeV); Number of tracks", 1000, 0, 2000);
58 
59  h1_rawResp = new TH1F("h1_rawResp", "Uncorrected response: |iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
60  h1_corResp = new TH1F("h1_corResp", "Corrected response: |iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
61 
63  new TH1F("h1_rawRespBarrel", "Uncorrected response: |iEta|<15; E_{had}/p; Number of tracks", 300, 0, 3);
65  new TH1F("h1_corRespBarrel", "Corrected response: |iEta|<15; E_{had}/p; Number of tracks", 300, 0, 3);
66 
68  new TH1F("h1_rawRespEndcap", "Uncorrected response: 17<|iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
70  new TH1F("h1_corRespEndcap", "Corrected response: 17<|iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
71 
72  h1_numEventsTwrIEta = new TH1F("h1_numEventsTwrIEta", "h1_numEventsTwrIEta", 80, -40, 40);
73 
74  h2_dHitRefBarrel = new TH2F("h2_dHitRefBarrel",
75  "{#Delta}i{#phi} vs {#Delta}i{#eta} of hit and most energetic "
76  "tower(|i{#eta}|<16);{#Delta}i{#eta}; {#Delta}i{#phi}",
77  10,
78  -5,
79  5,
80  10,
81  -5,
82  5);
83  h2_dHitRefEndcap = new TH2F("h2_dHitRefEndcap",
84  "{#Delta}i{#phi} vs {#Delta}i{#eta} of hit and most energetic tower (16<|i{#eta}|<25) "
85  ";{#Delta}i{#eta}; {#Delta}i{#phi}",
86  10,
87  -5,
88  5,
89  10,
90  -5,
91  5);
92 
93  TString histoName = "isoTrack_";
94 
95  for (Int_t i = 0; i < 48; ++i) {
96  Long_t iEta;
97  if (i < 24)
98  iEta = i - 24;
99  else
100  iEta = i - 23;
101  TString hn = histoName + iEta;
102  h1_corRespIEta[i] = new TH1F(hn, hn, 300, 0, 3.0);
103  }
104 
105 } // end of Begin()
106 
107 //void hcalCalib::SlaveBegin(TTree * /*tree*/) {
108 // TString option = GetOption();
109 //}
110 
111 Bool_t hcalCalib::Process(Long64_t entry) {
112  // fChain->GetTree()->GetEntry(entry);
113  GetEntry(entry);
114 
115  std::set<UInt_t> uniqueIds; // for testing: check if there are duplicate cells (AA)
116 
117  Bool_t acceptEvent = kTRUE;
118 
119  ++nEvents;
120 
121  if (!(nEvents % 100000))
122  edm::LogVerbatim("HcalCalib") << "event: " << nEvents;
123 
124  h1_allTrkP->Fill(targetE);
125 
126  if (targetE < MIN_TARGET_E || targetE > MAX_TARGET_E)
127  return kFALSE;
128  ;
129 
130  // make local copy as the cells may be modified due to phi/depth sum, phi corrections etc
131  std::vector<TCell> selectCells;
132 
133  if (cells->GetSize() == 0)
134  return kFALSE;
135 
136  if (CALIB_TYPE == "DI_JET" && probeJetEmFrac > 0.999)
137  return kTRUE;
138 
139  for (Int_t i = 0; i < cells->GetSize(); ++i) {
140  TCell* thisCell = (TCell*)cells->At(i);
141 
142  if (HcalDetId(thisCell->id()).subdet() == HcalOuter)
143  continue; // reject HO, make a switch!
144 
145  if (HcalDetId(thisCell->id()).subdet() != HcalBarrel && HcalDetId(thisCell->id()).subdet() != HcalEndcap &&
146  HcalDetId(thisCell->id()).subdet() != HcalForward) {
147  edm::LogWarning("HcalCalib") << "Unknown or wrong hcal subdetector: " << HcalDetId(thisCell->id()).subdet();
148  }
149 
150  // Apply phi symmetry corrections if the flag is set
152  thisCell->SetE(phiSymCor[thisCell->id()] * thisCell->e());
153 
154  if (thisCell->e() > MIN_CELL_E)
155  selectCells.push_back(*thisCell);
156  }
157 
158  if (selectCells.empty()) {
159  edm::LogWarning("HcalCalib") << "NO CELLS ABOVE THRESHOLD FOUND FOR TARGET!!!";
160  }
161 
162  if (SUM_DEPTHS)
163  sumDepths(selectCells);
164  else if (SUM_SMALL_DEPTHS)
165  sumSmallDepths(selectCells); // depth 1,2 in twrs 15,16
166 
167  // most energetic tower (IsoTracks) or centroid of probe jet (DiJets)
168  std::pair<Int_t, UInt_t> refPos;
169 
170  Int_t dEtaHitRef = 999;
171  Int_t dPhiHitRef = 999;
172 
173  if (CALIB_TYPE == "ISO_TRACK") {
174  Int_t iEtaMaxE; // filled by reference in getIEtaIPhiForHighestE
175  UInt_t iPhiMaxE; //
176 
177  getIEtaIPhiForHighestE(selectCells, iEtaMaxE, iPhiMaxE);
178 
179  dEtaHitRef = iEtaMaxE - iEtaHit;
180  dPhiHitRef = iPhiMaxE - iPhiHit;
181 
182  if (dPhiHitRef < -36)
183  dPhiHitRef += 72;
184  if (dPhiHitRef > 36)
185  dPhiHitRef -= 72;
186 
187  if (iEtaHit * iEtaMaxE < 0) {
188  if (dEtaHitRef < 0)
189  dEtaHitRef += 1;
190  if (dEtaHitRef > 0)
191  dEtaHitRef -= 1;
192  }
193 
194  if (abs(iEtaHit) < 16)
195  h2_dHitRefBarrel->Fill(dEtaHitRef, dPhiHitRef);
196  if (abs(iEtaHit) > 16 && abs(iEtaHit) < 25)
197  h2_dHitRefEndcap->Fill(dEtaHitRef, dPhiHitRef);
198 
199  // --------------------------------------------------
200  // Choice of cluster definition
201  //
202  // fixed size NxN clusters as specified in to config file
203  if (!USE_CONE_CLUSTERING) {
204  if (abs(iEtaMaxE) < 16 && HB_CLUSTER_SIZE == 3)
205  filterCells3x3(selectCells, iEtaMaxE, iPhiMaxE);
206  if (abs(iEtaMaxE) > 15 && HE_CLUSTER_SIZE == 3)
207  filterCells3x3(selectCells, iEtaMaxE, iPhiMaxE);
208 
209  if (abs(iEtaMaxE) < 16 && HB_CLUSTER_SIZE == 5)
210  filterCells5x5(selectCells, iEtaMaxE, iPhiMaxE);
211  if (abs(iEtaMaxE) > 15 && HE_CLUSTER_SIZE == 5)
212  filterCells5x5(selectCells, iEtaMaxE, iPhiMaxE);
213  } else {
214  // calculate distance at hcal surface
215  const GlobalPoint hitPositionHcal(xTrkHcal, yTrkHcal, zTrkHcal);
216  filterCellsInCone(selectCells, hitPositionHcal, MAX_CONE_DIST, theCaloGeometry);
217  }
218 
219  refPos.first = iEtaMaxE;
220  refPos.second = iPhiMaxE;
221 
222  } else if (CALIB_TYPE == "DI_JET") { // Apply selection cuts on DiJet events here
223 
225  acceptEvent = kFALSE;
226 
227  Float_t jetsDPhi = probeJetP4->DeltaPhi(*tagJetP4);
228  if (fabs(jetsDPhi * 180.0 / M_PI) < MIN_DPHI_DIJETS)
229  acceptEvent = kFALSE;
230 
232  acceptEvent = kFALSE;
233  if (fabs(probeJetP4->Eta()) < MIN_PROBEJET_ABSETA)
234  acceptEvent = kFALSE;
235  if (fabs(tagJetP4->Eta()) > MAX_TAGJET_ABSETA)
236  acceptEvent = kFALSE;
237  if (fabs(tagJetP4->Et()) < MIN_TAGJET_ET)
238  acceptEvent = kFALSE;
239 
240  if (acceptEvent) {
241  Int_t iEtaMaxE; // filled by reference in getIEtaIPhiForHighestE
242  UInt_t iPhiMaxE; //
243 
244  getIEtaIPhiForHighestE(selectCells, iEtaMaxE, iPhiMaxE);
245 
246  // The ref position for the jet is not used in the minimization at this time.
247  // It will be needed if we attempt to do matrix inversion: then the question is
248  // which value is better suited: the centroid of the jet or the hottest tower...
249 
250  // refPos.first = iEtaHit;
251  // refPos.second = iPhiHit;
252 
253  refPos.first = iEtaMaxE;
254  refPos.second = iPhiMaxE;
255 
256  if (abs(iEtaMaxE) > 40)
257  acceptEvent = kFALSE; // for testing : set as parameter (AA)
258  }
259  }
260 
261  if (COMBINE_PHI)
262  combinePhi(selectCells);
263 
264  // fill the containers for the minimization prcedures
265  std::vector<Float_t> energies;
266  std::vector<UInt_t> ids;
267 
268  for (std::vector<TCell>::iterator i_it = selectCells.begin(); i_it != selectCells.end(); ++i_it) {
269  // for testing : fill only unique id's
270 
271  if (uniqueIds.insert(i_it->id()).second) {
272  energies.push_back(i_it->e());
273  ids.push_back(i_it->id());
274  }
275  }
276 
277  if (CALIB_TYPE == "ISO_TRACK") {
278  if (accumulate(energies.begin(), energies.end(), 0.0) / targetE < MIN_EOVERP)
279  acceptEvent = kFALSE;
280  if (accumulate(energies.begin(), energies.end(), 0.0) / targetE > MAX_EOVERP)
281  acceptEvent = kFALSE;
282 
283  if (emEnergy > MAX_TRK_EME)
284  acceptEvent = kFALSE;
285 
286  if (abs(dEtaHitRef) > 1 || abs(dPhiHitRef) > 1)
287  acceptEvent = kFALSE;
288 
289  // Have to check if for |iEta|>20 (and neighboring region) the dPhiHitRef
290  // should be relaxed to 2. The neighboring towers have dPhi=2...
291  }
292 
293  h1_rawSumE->Fill(accumulate(energies.begin(), energies.end(), 0.0));
294 
295  // here we fill the information for the minimization procedure
296  if (acceptEvent) {
297  cellEnergies.push_back(energies);
298  cellIds.push_back(ids);
299  targetEnergies.push_back(targetE);
300  refIEtaIPhi.push_back(refPos);
301 
302  if (abs(refPos.first) <= 10)
303  h1_selTrkP_iEta10->Fill(targetE);
304  }
305 
306  // Clean up
307  energies.clear();
308  ids.clear();
309  selectCells.clear();
310 
311  return kTRUE;
312 }
313 
314 //void hcalCalib::SlaveTerminate() {}
315 
317  edm::LogVerbatim("HcalCalib") << "\n\nFinished reading the events.\n"
318  << "Number of input objects: " << cellIds.size()
319  << "\nPerforming minimization: depending on selected method can take some time...\n\n";
320 
321  for (std::vector<std::pair<Int_t, UInt_t> >::iterator it_rp = refIEtaIPhi.begin(); it_rp != refIEtaIPhi.end();
322  ++it_rp) {
323  Float_t weight = (abs(it_rp->first) < 21) ? 1.0 / 72.0 : 1.0 / 36.0;
324  h1_numEventsTwrIEta->Fill(it_rp->first, weight);
325  }
326 
327  if (CALIB_METHOD == "MATRIX_INV_OF_ETA_AVE") {
329  } else if (CALIB_METHOD == "L3" || CALIB_METHOD == "L3_AND_MTRX_INV") {
330  int eventWeight = 2; // 2 is the default (try at some point 0,1,2,3)
331  MinL3AlgoUniv<UInt_t>* thisL3Algo = new MinL3AlgoUniv<UInt_t>(eventWeight);
332  int numIterations = 10; // default 10
333 
334  solution = thisL3Algo->iterate(cellEnergies, cellIds, targetEnergies, numIterations);
335 
336  // in order to handle the case where sumDepths="false", but the flag to sum depths 1,2 in HB towers 15, 16
337  // is set (sumSmallDepths) we create entries in "solution" to create equal correction here
338  // for each encountered coef in depth one.
339 
340  if (!SUM_DEPTHS && SUM_SMALL_DEPTHS) {
341  std::vector<UInt_t> idForSummedCells;
342 
343  for (std::map<UInt_t, Float_t>::iterator m_it = solution.begin(); m_it != solution.end(); ++m_it) {
344  if (HcalDetId(m_it->first).ietaAbs() != 15 && HcalDetId(m_it->first).ietaAbs() != 16)
345  continue;
346  if (HcalDetId(m_it->first).subdet() != HcalBarrel)
347  continue;
348  if (HcalDetId(m_it->first).depth() == 1)
349  idForSummedCells.push_back(HcalDetId(m_it->first));
350  }
351 
352  for (std::vector<UInt_t>::iterator v_it = idForSummedCells.begin(); v_it != idForSummedCells.end(); ++v_it) {
353  UInt_t addCoefId = HcalDetId(HcalBarrel, HcalDetId(*v_it).ieta(), HcalDetId(*v_it).iphi(), 2);
354  solution[addCoefId] = solution[*v_it];
355  }
356 
357  } // end of special treatment for "sumSmallDepths" mode
358 
359  if (CALIB_METHOD == "L3_AND_MTRX_INV") {
361 
362  // loop over the solution from L3 and multiply by the additional factor from
363  // the matrix inversion. Set coef outside of the valid calibration region =1.
364  for (std::map<UInt_t, Float_t>::iterator it_s = solution.begin(); it_s != solution.end(); ++it_s) {
365  Int_t iEtaSol = HcalDetId(it_s->first).ieta();
366  if (abs(iEtaSol) < CALIB_ABS_IETA_MIN || abs(iEtaSol) > CALIB_ABS_IETA_MAX)
367  it_s->second = 1.0;
368  else
369  it_s->second *= iEtaCoefMap[iEtaSol];
370  }
371 
372  } // if CALIB_METHOD=="L3_AND_MTRX_INV"
373 
374  } // end of L3 or L3 + mtrx inversion minimization
375 
376  // done getting the constants -> write the formatted file
377  makeTextFile();
378 
379  // fill some histograms
380  Float_t rawResp = 0;
381  Float_t corResp = 0;
382  Int_t maxIEta = 0;
383  Int_t minIEta = 999;
384 
385  for (unsigned int i = 0; i < cellEnergies.size(); ++i) {
386  Int_t iEta;
387  for (unsigned int j = 0; j < (cellEnergies[i]).size(); ++j) {
388  iEta = HcalDetId(cellIds[i][j]).ieta();
389  rawResp += (cellEnergies[i])[j];
390 
391  if (CALIB_METHOD == "L3_AND_MTRX_INV") {
392  corResp += (solution[cellIds[i][j]] * cellEnergies[i][j]);
393  } else if (CALIB_METHOD == "L3") {
394  corResp += (solution[cellIds[i][j]] * (cellEnergies[i][j]));
395  } else if (CALIB_METHOD == "MATRIX_INV_OF_ETA_AVE") {
396  corResp += (iEtaCoefMap[iEta] * cellEnergies[i][j]);
397  }
398 
399  if (maxIEta < abs(iEta))
400  maxIEta = abs(iEta);
401  if (minIEta > abs(iEta))
402  minIEta = abs(iEta);
403  }
404 
405  rawResp /= targetEnergies[i];
406  corResp /= targetEnergies[i];
407 
408  // fill histograms based on iEta on ref point of the cluster (for now: hottest tower)
409  // expect range |iEta|<=24 (to do: add flexibility for arbitrary range)
410 
411  if (CALIB_TYPE == "ISO_TRACK") {
412  Int_t ind = refIEtaIPhi[i].first;
413  (ind < 0) ? (ind += 24) : (ind += 23);
414  if (ind >= 0 && ind < 48) {
415  h1_corRespIEta[ind]->Fill(corResp);
416  }
417 
418  // fill histograms for cases where all towers are in the barrel or endcap
419  if (maxIEta < 25) {
420  h1_rawResp->Fill(rawResp);
421  h1_corResp->Fill(corResp);
422  }
423  if (maxIEta < 15) {
424  h1_rawRespBarrel->Fill(rawResp);
425  h1_corRespBarrel->Fill(corResp);
426  } else if (maxIEta < 25 && minIEta > 16) {
427  h1_rawRespEndcap->Fill(rawResp);
428  h1_corRespEndcap->Fill(corResp);
429  }
430  } // histograms for isotrack calibration
431 
432  else {
433  // put jet plots here
434  }
435 
436  rawResp = 0;
437  corResp = 0;
438  maxIEta = 0;
439  minIEta = 999;
440  }
441 
442  // save the histograms
443  h1_trkP->Write();
444  h1_allTrkP->Write();
445 
446  h1_selTrkP_iEta10->Write();
447 
448  h1_rawSumE->Write();
449  h1_rawResp->Write();
450  h1_corResp->Write();
451  h1_rawRespBarrel->Write();
452  h1_corRespBarrel->Write();
453  h1_rawRespEndcap->Write();
454  h1_corRespEndcap->Write();
455  h1_numEventsTwrIEta->Write();
456  h2_dHitRefBarrel->Write();
457  h2_dHitRefEndcap->Write();
458  for (Int_t i = 0; i < 48; ++i) {
459  h1_corRespIEta[i]->Write();
460  }
461 
462  histoFile->Write();
463  histoFile->Close();
464 
465  edm::LogVerbatim("HcalCalib") << "\n Finished calibration.\n ";
466 
467 } // end of Terminate()
468 
469 //**************************************************************************************************************
470 
472  // these maps are keyed by iEta
473  std::map<Int_t, Float_t> aveTargetE;
474  std::map<Int_t, Int_t> nEntries; // count hits
475 
476  // iEtaRef iEtaCell, energy
477  std::map<Int_t, std::map<Int_t, Float_t> > aveHitE; // add energies in the loop, normalize after that
478 
479  for (unsigned int i = 0; i < cellEnergies.size(); ++i) {
480  Int_t iEtaRef = refIEtaIPhi[i].first;
481  aveTargetE[iEtaRef] += targetEnergies[i];
482  nEntries[iEtaRef]++;
483 
484  // for hybrid method: matrix inv of averages preceeded by L3
485  if (CALIB_METHOD == "L3_AND_MTRX_INV") {
486  for (unsigned int j = 0; j < (cellEnergies[i]).size(); ++j) {
487  aveHitE[iEtaRef][HcalDetId(cellIds[i][j]).ieta()] += (solution[cellIds[i][j]] * cellEnergies[i][j]);
488  }
489  } else if (CALIB_METHOD == "MATRIX_INV_OF_ETA_AVE") {
490  for (unsigned int j = 0; j < (cellEnergies[i]).size(); ++j) {
491  aveHitE[iEtaRef][HcalDetId(cellIds[i][j]).ieta()] += cellEnergies[i][j];
492  }
493  }
494 
495  } // end of loop of entries
496 
497  // scale by number of entries to get the averages
498  Float_t norm = 1.0;
499  for (std::map<Int_t, Float_t>::iterator m_it = aveTargetE.begin(); m_it != aveTargetE.end(); ++m_it) {
500  Int_t iEta = m_it->first;
501  norm = (nEntries[iEta] > 0) ? 1.0 / (nEntries[iEta]) : 1.0;
502  aveTargetE[iEta] *= norm;
503 
504  std::map<Int_t, Float_t>::iterator n_it = (aveHitE[iEta]).begin();
505 
506  Float_t sumRawE = 0;
507  for (; n_it != (aveHitE[iEta]).end(); ++n_it) {
508  (n_it->second) *= norm;
509  sumRawE += (n_it->second);
510  }
511 
512  } // end of scaling by number of entries
513 
514  Int_t ONE_SIDE_IETA_RANGE = CALIB_ABS_IETA_MAX - CALIB_ABS_IETA_MIN + 1;
515 
516  // conversion from iEta to index for the linear system
517  // contains elements only in the valid range for *matrix inversion*
518  std::vector<Int_t> iEtaList;
519 
520  for (Int_t i = -CALIB_ABS_IETA_MAX; i <= CALIB_ABS_IETA_MAX; ++i) {
521  if (abs(i) < CALIB_ABS_IETA_MIN)
522  continue;
523  iEtaList.push_back(i);
524  }
525 
526  TMatrixD A(2 * ONE_SIDE_IETA_RANGE, 2 * ONE_SIDE_IETA_RANGE);
527  TMatrixD b(2 * ONE_SIDE_IETA_RANGE, 1);
528  TMatrixD x(2 * ONE_SIDE_IETA_RANGE, 1);
529 
530  for (Int_t i = 0; i < 2 * ONE_SIDE_IETA_RANGE; ++i) {
531  for (Int_t j = 0; j < 2 * ONE_SIDE_IETA_RANGE; ++j) {
532  A(i, j) = 0.0;
533  }
534  }
535 
536  for (UInt_t i = 0; i < iEtaList.size(); ++i) {
537  b(i, 0) = aveTargetE[iEtaList[i]];
538 
539  std::map<Int_t, Float_t>::iterator n_it = aveHitE[iEtaList[i]].begin();
540  for (; n_it != aveHitE[iEtaList[i]].end(); ++n_it) {
541  if (fabs(n_it->first) > CALIB_ABS_IETA_MAX || fabs(n_it->first) < CALIB_ABS_IETA_MIN)
542  continue;
543  Int_t j = Int_t(find(iEtaList.begin(), iEtaList.end(), n_it->first) - iEtaList.begin());
544  A(i, j) = n_it->second;
545  }
546  }
547 
548  TMatrixD coef = b;
549  TDecompQRH qrh(A);
550  Bool_t hasSolution = qrh.MultiSolve(coef);
551 
552  if (hasSolution && (CALIB_METHOD == "L3_AND_MTRX_INV" || CALIB_METHOD == "MATRIX_INV_OF_ETA_AVE")) {
553  for (UInt_t i = 0; i < iEtaList.size(); ++i) {
554  iEtaCoefMap[iEtaList[i]] = coef(i, 0);
555  }
556  }
557 }
558 
560  std::ifstream phiSymFile(PHI_SYM_COR_FILENAME.Data());
561 
562  if (!phiSymFile) {
563  edm::LogWarning("HcalCalib") << "\nERROR: Can not find file with phi symmetry constants \""
564  << PHI_SYM_COR_FILENAME.Data() << "\"";
565  return kFALSE;
566  }
567 
568  // assumes the format used in CSA08, first line is a comment
569 
570  Int_t iEta;
571  UInt_t iPhi;
572  UInt_t depth;
573  TString sdName;
574  UInt_t detId;
575 
576  Float_t value;
578 
580 
581  while (getline(phiSymFile, line)) {
582  if (line.empty() || line[0] == '#')
583  continue;
584 
585  std::istringstream linestream(line);
586  linestream >> iEta >> iPhi >> depth >> sdName >> value >> std::hex >> detId;
587 
588  if (sdName == "HB")
589  sd = HcalBarrel;
590  else if (sdName == "HE")
591  sd = HcalEndcap;
592  else if (sdName == "HO")
593  sd = HcalOuter;
594  else if (sdName == "HF")
595  sd = HcalForward;
596  else {
597  edm::LogWarning("HcalCalib") << "\nInvalid detector name in phi symmetry constants file: " << sdName.Data()
598  << "\nCheck file and rerun!\n";
599  return kFALSE;
600  }
601 
602  // check if the data is consistent
603 
604  if (HcalDetId(sd, iEta, iPhi, depth) != HcalDetId(detId)) {
605  edm::LogWarning("HcalCalib")
606  << "\nInconsistent info in phi symmetry file: subdet, iEta, iPhi, depth do not match rawId!\n";
607  return kFALSE;
608  }
609  HcalDetId hId(detId);
610  if (!topo_->valid(hId)) {
611  edm::LogWarning("HcalCalib") << "\nInvalid DetId from: iEta=" << iEta << " iPhi=" << iPhi << " depth=" << depth
612  << " subdet=" << sdName.Data() << " detId=" << detId << "\n";
613  return kFALSE;
614  }
615 
616  phiSymCor[HcalDetId(sd, iEta, iPhi, depth)] = value;
617  }
618 
619  return kTRUE;
620 }
621 
623  //{ HcalEmpty=0, HcalBarrel=1, HcalEndcap=2, HcalOuter=3, HcalForward=4, HcalTriggerTower=5, HcalOther=7 };
624 
625  TString sdName[8] = {"EMPTY", "HB", "HE", "HO", "HF", "TRITWR", "UNDEF", "OTHER"};
626 
627  FILE* constFile = fopen(OUTPUT_COR_COEF_FILENAME.Data(), "w+");
628 
629  // header of the constants file
630  fprintf(constFile, "%1s%16s%16s%16s%16s%9s%11s\n", "#", "eta", "phi", "depth", "det", "value", "DetId");
631 
632  // Order loops to produce sequence of constants as for phi symmetry
633 
634  for (Int_t sd = 1; sd <= 4; sd++) {
635  for (Int_t e = 1; e <= 50; e++) {
636  Int_t eta;
637 
638  for (Int_t side = 0; side < 2; side++) {
639  eta = (side == 0) ? -e : e; //ta *= (-1);
640 
641  for (Int_t phi = 1; phi <= 72; phi++) {
642  for (Int_t d = 1; d < 5; d++) {
643  if (!topo_->valid(HcalDetId(HcalSubdetector(sd), eta, phi, d)))
644  continue;
645  HcalDetId id(HcalSubdetector(sd), eta, phi, d);
646  Float_t corrFactor = 1.0;
647 
648  if (abs(eta) >= CALIB_ABS_IETA_MIN && abs(eta) <= CALIB_ABS_IETA_MAX && HcalSubdetector(sd) != HcalOuter) {
649  // if (abs(eta)>=CALIB_ABS_IETA_MIN && abs(eta)<=22 && HcalSubdetector(sd)!=HcalOuter) {
650 
651  // need some care when depths were summed for iEta=16 =>
652  // the coeficients are saved in depth 1 of HB: affects
653  Int_t subdetInd = sd;
654 
655  if (abs(eta) == 16 && HcalSubdetector(sd) == HcalEndcap && SUM_DEPTHS) {
656  subdetInd = 1;
657  }
658 
659  if (CALIB_METHOD == "L3" || CALIB_METHOD == "L3_AND_MTRX_INV") {
660  if (SUM_DEPTHS && COMBINE_PHI)
661  corrFactor = solution[HcalDetId(HcalSubdetector(subdetInd), eta, 1, 1)];
662  else if (SUM_DEPTHS)
663  corrFactor = solution[HcalDetId(HcalSubdetector(subdetInd), eta, phi, 1)];
664  else if (COMBINE_PHI)
665  corrFactor = solution[HcalDetId(HcalSubdetector(sd), eta, 1, d)];
666  else
667  corrFactor = solution[HcalDetId(HcalSubdetector(sd), eta, phi, d)];
668 
669  // Remark: a new case was added (sumSmallDepths) where the first two depths in towers 15,16
670  // are summed and stored in depth 1.
671  // For now we create the correction coef for depth 2 (set equal to depth 1)
672  // after the call to the L3 minimizer so that this case is also handled without modifying the
673  // logic above. Probably it is better to move it here?
674 
675  } // L3
676 
677  else if (CALIB_METHOD == "MATRIX_INV_OF_ETA_AVE") {
678  corrFactor = iEtaCoefMap[eta];
679  }
680 
681  else if (CALIB_METHOD == "ISO_TRK_PHI_SYM") {
682  corrFactor = solution[HcalDetId(HcalSubdetector(sd), eta, phi, d)];
683  }
684 
685  } // if within the calibration range
686 
687  fprintf(constFile, "%17i%16i%16i%16s%9.5f%11X\n", eta, phi, d, sdName[sd].Data(), corrFactor, id.rawId());
688  }
689  }
690  }
691  }
692  }
693 
694  return;
695 }
696 
697 inline void hcalCalib::Init(TTree* tree) {
698  // The Init() function is called when the selector needs to initialize
699  // a new tree or chain. Typically here the branch addresses and branch
700  // pointers of the tree will be set.
701  // It is normaly not necessary to make changes to the generated
702  // code, but the routine can be extended by the user if needed.
703  // Init() will be called many times when running on PROOF
704  // (once per file to be processed).
705 
706  // Set object pointer
707  cells = nullptr;
708  tagJetP4 = nullptr;
709  probeJetP4 = nullptr;
710 
711  // Set branch addresses and branch pointers
712  if (!tree)
713  return;
714  fChain = tree;
715 
716  // fChain->SetMakeClass(1);
717 
718  fChain->SetBranchAddress("eventNumber", &eventNumber, &b_eventNumber);
719  fChain->SetBranchAddress("runNumber", &runNumber, &b_runNumber);
720  fChain->SetBranchAddress("iEtaHit", &iEtaHit, &b_iEtaHit);
721  fChain->SetBranchAddress("iPhiHit", &iPhiHit, &b_iPhiHit);
722  fChain->SetBranchAddress("cells", &cells, &b_cells);
723  fChain->SetBranchAddress("emEnergy", &emEnergy, &b_emEnergy);
724  fChain->SetBranchAddress("targetE", &targetE, &b_targetE);
725  fChain->SetBranchAddress("etVetoJet", &etVetoJet, &b_etVetoJet);
726 
727  fChain->SetBranchAddress("xTrkHcal", &xTrkHcal, &b_xTrkHcal);
728  fChain->SetBranchAddress("yTrkHcal", &yTrkHcal, &b_yTrkHcal);
729  fChain->SetBranchAddress("zTrkHcal", &zTrkHcal, &b_zTrkHcal);
730  fChain->SetBranchAddress("xTrkEcal", &xTrkEcal, &b_xTrkEcal);
731  fChain->SetBranchAddress("yTrkEcal", &yTrkEcal, &b_yTrkEcal);
732  fChain->SetBranchAddress("zTrkEcal", &zTrkEcal, &b_zTrkEcal);
733 
734  fChain->SetBranchAddress("tagJetEmFrac", &tagJetEmFrac, &b_tagJetEmFrac);
735  fChain->SetBranchAddress("probeJetEmFrac", &probeJetEmFrac, &b_probeJetEmFrac);
736 
737  fChain->SetBranchAddress("tagJetP4", &tagJetP4, &b_tagJetP4);
738  fChain->SetBranchAddress("probeJetP4", &probeJetP4, &b_probeJetP4);
739 }
740 
741 inline Bool_t hcalCalib::Notify() {
742  // The Notify() function is called when a new file is opened. This
743  // can be either for a new TTree in a TChain or when when a new TTree
744  // is started when using PROOF. It is normaly not necessary to make changes
745  // to the generated code, but the routine can be extended by the
746  // user if needed. The return value is currently not used.
747 
748  return kTRUE;
749 }
void filterCellsInCone(std::vector< TCell > &selectCells, const GlobalPoint hitPositionHcal, Float_t maxConeDist, const CaloGeometry *theCaloGeometry)
std::vector< std::pair< Int_t, UInt_t > > refIEtaIPhi
Definition: hcalCalib.h:228
Log< level::Info, true > LogVerbatim
std::vector< Float_t > targetEnergies
Definition: hcalCalib.h:229
TH1F * h1_corRespBarrel
Definition: hcalCalib.h:109
TH1F * h1_trkP
Definition: hcalCalib.h:100
std::map< Int_t, Float_t > iEtaCoefMap
Definition: hcalCalib.h:235
constexpr int ietaAbs() const
get the absolute value of the cell ieta
Definition: HcalDetId.h:148
TBranch * b_iEtaHit
Definition: hcalCalib.h:75
TTree * fChain
Definition: hcalCalib.h:48
UInt_t eventNumber
pointer to the analyzed TTree or TChain
Definition: hcalCalib.h:50
Float_t MAX_TARGET_E
Definition: hcalCalib.h:141
TBranch * b_etVetoJet
Definition: hcalCalib.h:80
std::vector< std::vector< UInt_t > > cellIds
Definition: hcalCalib.h:227
TString PHI_SYM_COR_FILENAME
Definition: hcalCalib.h:174
uint16_t *__restrict__ id
tuple sumSmallDepths
Definition: diJetCalib.py:58
TBranch * b_yTrkEcal
Definition: hcalCalib.h:86
void GetCoefFromMtrxInvOfAve()
Definition: hcalCalib.cc:471
void Begin(TTree *tree) override
Definition: hcalCalib.cc:28
TBranch * b_probeJetEmFrac
Definition: hcalCalib.h:90
bool valid(const DetId &id) const override
TBranch * b_iPhiHit
Definition: hcalCalib.h:76
Float_t xTrkHcal
Definition: hcalCalib.h:59
TString CALIB_TYPE
Definition: hcalCalib.h:171
TLorentzVector * tagJetP4
Definition: hcalCalib.h:66
Float_t tagJetEmFrac
Definition: hcalCalib.h:69
Float_t zTrkHcal
Definition: hcalCalib.h:61
std::map< UInt_t, Float_t > phiSymCor
Definition: hcalCalib.h:231
Float_t xTrkEcal
Definition: hcalCalib.h:62
Float_t e()
Definition: TCell.h:31
Float_t MAX_EOVERP
Definition: hcalCalib.h:145
Float_t targetE
Definition: hcalCalib.h:56
Bool_t SUM_DEPTHS
Definition: hcalCalib.h:151
Definition: TCell.h:15
Float_t MIN_CELL_E
Definition: hcalCalib.h:143
TFile * histoFile
Definition: hcalCalib.h:97
TLorentzVector * probeJetP4
Definition: hcalCalib.h:67
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
UInt_t nEvents
Definition: hcalCalib.h:95
Float_t yTrkEcal
Definition: hcalCalib.h:63
Int_t CALIB_ABS_IETA_MIN
Definition: hcalCalib.h:162
TH1F * h1_rawSumE
Definition: hcalCalib.h:105
TH1F * h1_corRespEndcap
Definition: hcalCalib.h:111
TBranch * b_zTrkHcal
Definition: hcalCalib.h:84
U second(std::pair< T, U > const &p)
tuple d
Definition: ztail.py:151
constexpr HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:138
TH2F * h2_dHitRefEndcap
Definition: hcalCalib.h:115
TBranch * b_tagJetP4
Definition: hcalCalib.h:92
Int_t HE_CLUSTER_SIZE
Definition: hcalCalib.h:156
TBranch * b_runNumber
Definition: hcalCalib.h:74
TH1F * h1_rawResp
Definition: hcalCalib.h:106
UInt_t id()
Definition: TCell.h:32
constexpr int iphi() const
get the cell iphi
Definition: HcalDetId.h:157
Int_t iEtaHit
Definition: hcalCalib.h:52
Float_t MAX_TRK_EME
Definition: hcalCalib.h:146
TBranch * b_zTrkEcal
Definition: hcalCalib.h:87
Float_t MIN_DPHI_DIJETS
Definition: hcalCalib.h:149
Int_t HB_CLUSTER_SIZE
Definition: hcalCalib.h:155
TH1F * h1_corResp
Definition: hcalCalib.h:107
Float_t MIN_TAGJET_ET
Definition: hcalCalib.h:167
void Terminate() override
Definition: hcalCalib.cc:316
TBranch * b_eventNumber
Definition: hcalCalib.h:73
Bool_t APPLY_PHI_SYM_COR_FLAG
Definition: hcalCalib.h:175
TH1F * h1_rawRespBarrel
Definition: hcalCalib.h:108
tuple combinePhi
Definition: diJetCalib.py:54
HcalSubdetector
Definition: HcalAssistant.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Float_t zTrkEcal
Definition: hcalCalib.h:64
TH1F * h1_corRespIEta[48]
Definition: hcalCalib.h:119
constexpr int ieta() const
get the cell ieta
Definition: HcalDetId.h:155
TH1F * h1_numEventsTwrIEta
Definition: hcalCalib.h:112
TBranch * b_tagJetEmFrac
Definition: hcalCalib.h:89
const HcalTopology * topo_
Definition: hcalCalib.h:181
std::map< UInt_t, Float_t > solution
Definition: hcalCalib.h:233
Float_t emEnergy
Definition: hcalCalib.h:55
std::vector< std::vector< Float_t > > cellEnergies
Definition: hcalCalib.h:226
Float_t MIN_EOVERP
Definition: hcalCalib.h:144
TBranch * b_cells
Definition: hcalCalib.h:77
void SetE(Float_t e)
Definition: TCell.h:34
void filterCells3x3(std::vector< TCell > &selectCells, Int_t iEta, UInt_t iPhi)
Float_t MIN_PROBEJET_ABSETA
Definition: hcalCalib.h:169
#define M_PI
Bool_t COMBINE_PHI
Definition: hcalCalib.h:153
tuple sumDepths
Definition: diJetCalib.py:51
Float_t MAX_ET_THIRD_JET
Definition: hcalCalib.h:148
UInt_t iPhiHit
Definition: hcalCalib.h:53
Bool_t USE_CONE_CLUSTERING
Definition: hcalCalib.h:158
TString CALIB_METHOD
Definition: hcalCalib.h:172
TString OUTPUT_COR_COEF_FILENAME
Definition: hcalCalib.h:177
double sd
TH1F * h1_rawRespEndcap
Definition: hcalCalib.h:110
double b
Definition: hdecay.h:118
Int_t CALIB_ABS_IETA_MAX
Definition: hcalCalib.h:161
void Init(TTree *tree) override
Definition: hcalCalib.cc:697
TBranch * b_yTrkHcal
Definition: hcalCalib.h:83
Float_t etVetoJet
Definition: hcalCalib.h:57
TBranch * b_emEnergy
Definition: hcalCalib.h:78
void makeTextFile()
Definition: hcalCalib.cc:622
TBranch * b_xTrkEcal
Definition: hcalCalib.h:85
Bool_t Process(Long64_t entry) override
Definition: hcalCalib.cc:111
Bool_t ReadPhiSymCor()
Definition: hcalCalib.cc:559
TClonesArray * cells
Definition: hcalCalib.h:54
void filterCells5x5(std::vector< TCell > &selectCells, Int_t iEta, UInt_t iPhi)
Bool_t SUM_SMALL_DEPTHS
Definition: hcalCalib.h:152
string end
Definition: dataset.py:937
list entry
Definition: mps_splice.py:68
Int_t GetEntry(Long64_t entry, Int_t getall=0) override
Definition: hcalCalib.h:129
TString HISTO_FILENAME
Definition: hcalCalib.h:178
TH1F * h1_allTrkP
Definition: hcalCalib.h:101
Definition: APVGainStruct.h:7
IDmap iterate(const std::vector< std::vector< float > > &eventMatrix, const std::vector< std::vector< IDdet > > &idMatrix, const std::vector< float > &energyVector, const int &nIter, const bool &normalizeFlag=false)
Definition: MinL3AlgoUniv.h:76
Float_t MAX_TAGJET_ABSETA
Definition: hcalCalib.h:166
constexpr int depth() const
get the tower depth
Definition: HcalDetId.h:164
Float_t MAX_PROBEJET_EMFRAC
Definition: hcalCalib.h:164
const CaloGeometry * theCaloGeometry
Definition: hcalCalib.h:180
Log< level::Warning, false > LogWarning
int weight
Definition: histoStyle.py:51
UInt_t runNumber
Definition: hcalCalib.h:51
TBranch * b_xTrkHcal
Definition: hcalCalib.h:82
TBranch * b_targetE
Definition: hcalCalib.h:79
void getIEtaIPhiForHighestE(std::vector< TCell > &selectCells, Int_t &iEta, UInt_t &iPhi)
Float_t MAX_CONE_DIST
Definition: hcalCalib.h:159
Float_t yTrkHcal
Definition: hcalCalib.h:60
tuple size
Write out results.
Bool_t Notify() override
Definition: hcalCalib.cc:741
Float_t probeJetEmFrac
Definition: hcalCalib.h:70
TH1F * h1_selTrkP_iEta10
Definition: hcalCalib.h:103
TH2F * h2_dHitRefBarrel
Definition: hcalCalib.h:114
TBranch * b_probeJetP4
Definition: hcalCalib.h:93