33 #include "TDecompSVD.h" 34 #include "TDecompQRH.h" 68 TString
option = GetOption();
72 if (APPLY_PHI_SYM_COR_FLAG && !ReadPhiSymCor()) {
73 cout <<
"\nERROR: Failed to read the phi symmetry corrections." << endl;
74 cout <<
"Check if the filename is correct. If the corrections are not needed, set the corresponding flag to " 78 cout <<
"\nThe program will be terminated\n" << endl;
87 histoFile =
new TFile(HISTO_FILENAME.Data(),
"RECREATE");
89 h1_trkP =
new TH1F(
"h1_trkP",
"Track momenta; p_{trk} (GeV); Number of tracks", 100, 0, 200);
90 h1_allTrkP =
new TH1F(
"h1_allTrkP",
"Track momenta - all tracks; p_{trk} (GeV); Number of tracks", 100, 0, 200);
93 "h1_selTrkP_iEta10",
"Track momenta - tracks with |iEta|<10; p_{trk} (GeV); Number of tracks", 100, 0, 200);
95 if (CALIB_TYPE ==
"ISO_TRACK")
96 h1_rawSumE =
new TH1F(
"h1_rawSumE",
"Cluster Energy; E_{cl} (GeV); Number of tracks", 100, 0, 200);
98 h1_rawSumE =
new TH1F(
"h1_rawSumE",
"Cluster Energy; E_{cl} (GeV); Number of tracks", 1000, 0, 2000);
100 h1_rawResp =
new TH1F(
"h1_rawResp",
"Uncorrected response: |iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
101 h1_corResp =
new TH1F(
"h1_corResp",
"Corrected response: |iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
104 new TH1F(
"h1_rawRespBarrel",
"Uncorrected response: |iEta|<15; E_{had}/p; Number of tracks", 300, 0, 3);
106 new TH1F(
"h1_corRespBarrel",
"Corrected response: |iEta|<15; E_{had}/p; Number of tracks", 300, 0, 3);
109 new TH1F(
"h1_rawRespEndcap",
"Uncorrected response: 17<|iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
111 new TH1F(
"h1_corRespEndcap",
"Corrected response: 17<|iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
116 "{#Delta}i{#phi} vs {#Delta}i{#eta} of hit and most energetic " 117 "tower(|i{#eta}|<16);{#Delta}i{#eta}; {#Delta}i{#phi}",
125 "{#Delta}i{#phi} vs {#Delta}i{#eta} of hit and most energetic tower (16<|i{#eta}|<25) " 126 ";{#Delta}i{#eta}; {#Delta}i{#phi}",
134 TString histoName =
"isoTrack_";
136 for (Int_t
i = 0;
i < 48; ++
i) {
142 TString hn = histoName + iEta;
156 set<UInt_t> uniqueIds;
158 Bool_t acceptEvent = kTRUE;
167 if (targetE < MIN_TARGET_E || targetE > MAX_TARGET_E)
172 vector<TCell> selectCells;
174 if (
cells->GetSize() == 0)
177 if (CALIB_TYPE ==
"DI_JET" && probeJetEmFrac > 0.999)
180 for (Int_t
i = 0;
i <
cells->GetSize(); ++
i) {
188 cout <<
"Unknown or wrong hcal subdetector: " <<
HcalDetId(thisCell->
id()).subdet() << endl;
192 if (APPLY_PHI_SYM_COR_FLAG)
193 thisCell->
SetE(phiSymCor[thisCell->
id()] * thisCell->
e());
195 if (thisCell->
e() > MIN_CELL_E)
196 selectCells.push_back(*thisCell);
199 if (selectCells.empty()) {
200 cout <<
"NO CELLS ABOVE THRESHOLD FOUND FOR TARGET!!!" << endl;
205 else if (SUM_SMALL_DEPTHS)
209 pair<Int_t, UInt_t> refPos;
211 Int_t dEtaHitRef = 999;
212 Int_t dPhiHitRef = 999;
214 if (CALIB_TYPE ==
"ISO_TRACK") {
220 dEtaHitRef = iEtaMaxE - iEtaHit;
221 dPhiHitRef = iPhiMaxE - iPhiHit;
223 if (dPhiHitRef < -36)
228 if (iEtaHit * iEtaMaxE < 0) {
235 if (
abs(iEtaHit) < 16)
237 if (
abs(iEtaHit) > 16 &&
abs(iEtaHit) < 25)
244 if (!USE_CONE_CLUSTERING) {
245 if (
abs(iEtaMaxE) < 16 && HB_CLUSTER_SIZE == 3)
247 if (
abs(iEtaMaxE) > 15 && HE_CLUSTER_SIZE == 3)
250 if (
abs(iEtaMaxE) < 16 && HB_CLUSTER_SIZE == 5)
252 if (
abs(iEtaMaxE) > 15 && HE_CLUSTER_SIZE == 5)
256 const GlobalPoint hitPositionHcal(xTrkHcal, yTrkHcal, zTrkHcal);
260 refPos.first = iEtaMaxE;
261 refPos.second = iPhiMaxE;
263 }
else if (CALIB_TYPE ==
"DI_JET") {
265 if (etVetoJet > MAX_ET_THIRD_JET)
266 acceptEvent = kFALSE;
268 Float_t jetsDPhi = probeJetP4->DeltaPhi(*tagJetP4);
269 if (fabs(jetsDPhi * 180.0 /
M_PI) < MIN_DPHI_DIJETS)
270 acceptEvent = kFALSE;
272 if (probeJetEmFrac > MAX_PROBEJET_EMFRAC)
273 acceptEvent = kFALSE;
274 if (fabs(probeJetP4->Eta()) < MIN_PROBEJET_ABSETA)
275 acceptEvent = kFALSE;
276 if (fabs(tagJetP4->Eta()) > MAX_TAGJET_ABSETA)
277 acceptEvent = kFALSE;
278 if (fabs(tagJetP4->Et()) < MIN_TAGJET_ET)
279 acceptEvent = kFALSE;
294 refPos.first = iEtaMaxE;
295 refPos.second = iPhiMaxE;
297 if (
abs(iEtaMaxE) > 40)
298 acceptEvent = kFALSE;
306 vector<Float_t> energies;
309 for (vector<TCell>::iterator i_it = selectCells.begin(); i_it != selectCells.end(); ++i_it) {
312 if (uniqueIds.insert(i_it->id()).
second) {
313 energies.push_back(i_it->e());
314 ids.push_back(i_it->id());
318 if (CALIB_TYPE ==
"ISO_TRACK") {
319 if (accumulate(energies.begin(), energies.end(), 0.0) / targetE < MIN_EOVERP)
320 acceptEvent = kFALSE;
321 if (accumulate(energies.begin(), energies.end(), 0.0) / targetE > MAX_EOVERP)
322 acceptEvent = kFALSE;
324 if (emEnergy > MAX_TRK_EME)
325 acceptEvent = kFALSE;
327 if (
abs(dEtaHitRef) > 1 ||
abs(dPhiHitRef) > 1)
328 acceptEvent = kFALSE;
334 h1_rawSumE->Fill(accumulate(energies.begin(), energies.end(), 0.0));
338 cellEnergies.push_back(energies);
339 cellIds.push_back(ids);
340 targetEnergies.push_back(targetE);
341 refIEtaIPhi.push_back(refPos);
343 if (
abs(refPos.first) <= 10)
358 cout <<
"\n\nFinished reading the events.\n";
359 cout <<
"Number of input objects: " << cellIds.size() << endl;
360 cout <<
"Performing minimization: depending on selected method can take some time...\n\n";
362 for (vector<pair<Int_t, UInt_t> >::iterator it_rp = refIEtaIPhi.begin(); it_rp != refIEtaIPhi.end(); ++it_rp) {
363 Float_t
weight = (
abs(it_rp->first) < 21) ? 1.0 / 72.0 : 1.0 / 36.0;
367 if (CALIB_METHOD ==
"MATRIX_INV_OF_ETA_AVE") {
368 GetCoefFromMtrxInvOfAve();
369 }
else if (CALIB_METHOD ==
"L3" || CALIB_METHOD ==
"L3_AND_MTRX_INV") {
372 int numIterations = 10;
374 solution = thisL3Algo->
iterate(cellEnergies, cellIds, targetEnergies, numIterations);
380 if (!SUM_DEPTHS && SUM_SMALL_DEPTHS) {
381 vector<UInt_t> idForSummedCells;
383 for (map<UInt_t, Float_t>::iterator m_it = solution.begin(); m_it != solution.end(); ++m_it) {
389 idForSummedCells.push_back(
HcalDetId(m_it->first));
392 for (vector<UInt_t>::iterator v_it = idForSummedCells.begin(); v_it != idForSummedCells.end(); ++v_it) {
394 solution[addCoefId] = solution[*v_it];
399 if (CALIB_METHOD ==
"L3_AND_MTRX_INV") {
400 GetCoefFromMtrxInvOfAve();
404 for (map<UInt_t, Float_t>::iterator it_s = solution.begin(); it_s != solution.end(); ++it_s) {
406 if (
abs(iEtaSol) < CALIB_ABS_IETA_MIN ||
abs(iEtaSol) > CALIB_ABS_IETA_MAX)
409 it_s->second *= iEtaCoefMap[iEtaSol];
425 for (
unsigned int i = 0;
i < cellEnergies.size(); ++
i) {
427 for (
unsigned int j = 0; j < (cellEnergies[
i]).
size(); ++j) {
429 rawResp += (cellEnergies[
i])[j];
431 if (CALIB_METHOD ==
"L3_AND_MTRX_INV") {
432 corResp += (solution[cellIds[
i][j]] * cellEnergies[
i][j]);
433 }
else if (CALIB_METHOD ==
"L3") {
434 corResp += (solution[cellIds[
i][j]] * (cellEnergies[
i][j]));
435 }
else if (CALIB_METHOD ==
"MATRIX_INV_OF_ETA_AVE") {
436 corResp += (iEtaCoefMap[iEta] * cellEnergies[
i][j]);
439 if (maxIEta <
abs(iEta))
441 if (minIEta >
abs(iEta))
445 rawResp /= targetEnergies[
i];
446 corResp /= targetEnergies[
i];
451 if (CALIB_TYPE ==
"ISO_TRACK") {
452 Int_t ind = refIEtaIPhi[
i].first;
453 (ind < 0) ? (ind += 24) : (ind += 23);
454 if (ind >= 0 && ind < 48) {
466 }
else if (maxIEta < 25 && minIEta > 16) {
498 for (Int_t
i = 0;
i < 48; ++
i) {
505 cout <<
"\n Finished calibration.\n " << endl;
513 map<Int_t, Float_t> aveTargetE;
514 map<Int_t, Int_t> nEntries;
517 map<Int_t, map<Int_t, Float_t> > aveHitE;
519 for (
unsigned int i = 0;
i < cellEnergies.size(); ++
i) {
520 Int_t iEtaRef = refIEtaIPhi[
i].first;
521 aveTargetE[iEtaRef] += targetEnergies[
i];
525 if (CALIB_METHOD ==
"L3_AND_MTRX_INV") {
526 for (
unsigned int j = 0; j < (cellEnergies[
i]).
size(); ++j) {
527 aveHitE[iEtaRef][
HcalDetId(cellIds[
i][j]).
ieta()] += (solution[cellIds[
i][j]] * cellEnergies[
i][j]);
529 }
else if (CALIB_METHOD ==
"MATRIX_INV_OF_ETA_AVE") {
530 for (
unsigned int j = 0; j < (cellEnergies[
i]).
size(); ++j) {
531 aveHitE[iEtaRef][
HcalDetId(cellIds[
i][j]).
ieta()] += cellEnergies[
i][j];
539 for (map<Int_t, Float_t>::iterator m_it = aveTargetE.begin(); m_it != aveTargetE.end(); ++m_it) {
540 Int_t iEta = m_it->first;
541 norm = (nEntries[iEta] > 0) ? 1.0 / (nEntries[iEta]) : 1.0;
542 aveTargetE[iEta] *= norm;
544 map<Int_t, Float_t>::iterator n_it = (aveHitE[iEta]).
begin();
547 for (; n_it != (aveHitE[iEta]).
end(); ++n_it) {
548 (n_it->second) *= norm;
549 sumRawE += (n_it->second);
554 Int_t ONE_SIDE_IETA_RANGE = CALIB_ABS_IETA_MAX - CALIB_ABS_IETA_MIN + 1;
558 vector<Int_t> iEtaList;
560 for (Int_t
i = -CALIB_ABS_IETA_MAX;
i <= CALIB_ABS_IETA_MAX; ++
i) {
561 if (
abs(
i) < CALIB_ABS_IETA_MIN)
563 iEtaList.push_back(
i);
566 TMatrixD
A(2 * ONE_SIDE_IETA_RANGE, 2 * ONE_SIDE_IETA_RANGE);
567 TMatrixD
b(2 * ONE_SIDE_IETA_RANGE, 1);
568 TMatrixD x(2 * ONE_SIDE_IETA_RANGE, 1);
570 for (Int_t
i = 0;
i < 2 * ONE_SIDE_IETA_RANGE; ++
i) {
571 for (Int_t j = 0; j < 2 * ONE_SIDE_IETA_RANGE; ++j) {
576 for (UInt_t
i = 0;
i < iEtaList.size(); ++
i) {
577 b(
i, 0) = aveTargetE[iEtaList[
i]];
579 map<Int_t, Float_t>::iterator n_it = aveHitE[iEtaList[
i]].begin();
580 for (; n_it != aveHitE[iEtaList[
i]].end(); ++n_it) {
581 if (fabs(n_it->first) > CALIB_ABS_IETA_MAX || fabs(n_it->first) < CALIB_ABS_IETA_MIN)
583 Int_t j = Int_t(
find(iEtaList.begin(), iEtaList.end(), n_it->first) - iEtaList.begin());
584 A(
i, j) = n_it->second;
590 Bool_t hasSolution = qrh.MultiSolve(coef);
592 if (hasSolution && (CALIB_METHOD ==
"L3_AND_MTRX_INV" || CALIB_METHOD ==
"MATRIX_INV_OF_ETA_AVE")) {
593 for (UInt_t
i = 0;
i < iEtaList.size(); ++
i) {
594 iEtaCoefMap[iEtaList[
i]] = coef(
i, 0);
600 std::ifstream phiSymFile(PHI_SYM_COR_FILENAME.Data());
603 cout <<
"\nERROR: Can not find file with phi symmetry constants \"" << PHI_SYM_COR_FILENAME.Data() <<
"\"" << endl;
620 while (getline(phiSymFile, line)) {
621 if (line.empty() || line[0] ==
'#')
624 std::istringstream linestream(line);
625 linestream >> iEta >> iPhi >> depth >> sdName >> value >> hex >> detId;
629 else if (sdName ==
"HE")
631 else if (sdName ==
"HO")
633 else if (sdName ==
"HF")
636 cout <<
"\nInvalid detector name in phi symmetry constants file: " << sdName.Data() << endl;
637 cout <<
"Check file and rerun!\n" << endl;
644 cout <<
"\nInconsistent info in phi symmetry file: subdet, iEta, iPhi, depth do not match rawId!\n" << endl;
648 if (!topo_->valid(hId)) {
649 cout <<
"\nInvalid DetId from: iEta=" << iEta <<
" iPhi=" << iPhi <<
" depth=" << depth
650 <<
" subdet=" << sdName.Data() <<
" detId=" << detId << endl
664 TString sdName[8] = {
"EMPTY",
"HB",
"HE",
"HO",
"HF",
"TRITWR",
"UNDEF",
"OTHER"};
666 FILE* constFile = fopen(OUTPUT_COR_COEF_FILENAME.Data(),
"w+");
669 fprintf(constFile,
"%1s%16s%16s%16s%16s%9s%11s\n",
"#",
"eta",
"phi",
"depth",
"det",
"value",
"DetId");
673 for (Int_t
sd = 1;
sd <= 4;
sd++) {
674 for (Int_t
e = 1;
e <= 50;
e++) {
677 for (Int_t side = 0; side < 2; side++) {
678 eta = (side == 0) ? -
e :
e;
680 for (Int_t phi = 1; phi <= 72; phi++) {
681 for (Int_t
d = 1;
d < 5;
d++) {
685 Float_t corrFactor = 1.0;
692 Int_t subdetInd =
sd;
698 if (CALIB_METHOD ==
"L3" || CALIB_METHOD ==
"L3_AND_MTRX_INV") {
699 if (SUM_DEPTHS && COMBINE_PHI)
703 else if (COMBINE_PHI)
716 else if (CALIB_METHOD ==
"MATRIX_INV_OF_ETA_AVE") {
717 corrFactor = iEtaCoefMap[
eta];
720 else if (CALIB_METHOD ==
"ISO_TRK_PHI_SYM") {
726 fprintf(constFile,
"%17i%16i%16i%16s%9.5f%11X\n", eta, phi,
d, sdName[
sd].Data(), corrFactor,
id.rawId());
void filterCellsInCone(std::vector< TCell > &selectCells, const GlobalPoint hitPositionHcal, Float_t maxConeDist, const CaloGeometry *theCaloGeometry)
TH1F * h1_corRespIEta[48]
HcalSubdetector subdet() const
get the subdetector
void GetCoefFromMtrxInvOfAve()
void Begin(TTree *tree) override
TH1F * h1_numEventsTwrIEta
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
U second(std::pair< T, U > const &p)
void combinePhi(std::vector< TCell > &selectCells)
int depth() const
get the tower depth
int ieta() const
get the cell ieta
void Terminate() override
Abs< T >::type abs(const T &t)
void filterCells3x3(std::vector< TCell > &selectCells, Int_t iEta, UInt_t iPhi)
int ietaAbs() const
get the absolute value of the cell ieta
int iphi() const
get the cell iphi
void sumSmallDepths(std::vector< TCell > &selectCells)
Bool_t Process(Long64_t entry) override
void filterCells5x5(std::vector< TCell > &selectCells, Int_t iEta, UInt_t iPhi)
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)
void sumDepths(std::vector< TCell > &selectCells)
void getIEtaIPhiForHighestE(std::vector< TCell > &selectCells, Int_t &iEta, UInt_t &iPhi)