CMS 3D CMS Logo

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