33 #include "TDecompSVD.h" 34 #include "TDecompQRH.h" 70 TString
option = GetOption();
74 if (APPLY_PHI_SYM_COR_FLAG && !ReadPhiSymCor()) {
75 cout <<
"\nERROR: Failed to read the phi symmetry corrections." << endl;
76 cout <<
"Check if the filename is correct. If the corrections are not needed, set the corresponding flag to \"false\"\n" << endl;
78 cout <<
"\nThe program will be terminated\n" << endl;
89 histoFile =
new TFile(HISTO_FILENAME.Data(),
"RECREATE");
92 h1_trkP =
new TH1F(
"h1_trkP",
"Track momenta; p_{trk} (GeV); Number of tracks", 100, 0, 200);
93 h1_allTrkP =
new TH1F(
"h1_allTrkP",
"Track momenta - all tracks; p_{trk} (GeV); Number of tracks", 100, 0, 200);
95 h1_selTrkP_iEta10 =
new TH1F(
"h1_selTrkP_iEta10",
"Track momenta - tracks with |iEta|<10; p_{trk} (GeV); Number of tracks", 100, 0, 200);
99 if (CALIB_TYPE==
"ISO_TRACK")
100 h1_rawSumE =
new TH1F(
"h1_rawSumE",
"Cluster Energy; E_{cl} (GeV); Number of tracks", 100, 0, 200);
102 h1_rawSumE =
new TH1F(
"h1_rawSumE",
"Cluster Energy; E_{cl} (GeV); Number of tracks", 1000, 0, 2000);
106 h1_rawResp =
new TH1F(
"h1_rawResp",
"Uncorrected response: |iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
107 h1_corResp =
new TH1F(
"h1_corResp",
"Corrected response: |iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
109 h1_rawRespBarrel =
new TH1F(
"h1_rawRespBarrel",
"Uncorrected response: |iEta|<15; E_{had}/p; Number of tracks", 300, 0, 3);
110 h1_corRespBarrel =
new TH1F(
"h1_corRespBarrel",
"Corrected response: |iEta|<15; E_{had}/p; Number of tracks", 300, 0, 3);
112 h1_rawRespEndcap =
new TH1F(
"h1_rawRespEndcap",
"Uncorrected response: 17<|iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
113 h1_corRespEndcap =
new TH1F(
"h1_corRespEndcap",
"Corrected response: 17<|iEta|<24; E_{had}/p; Number of tracks", 300, 0, 3);
117 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);
118 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);
122 TString histoName =
"isoTrack_";
124 for (Int_t
i=0;
i<48; ++
i) {
126 if (
i<24) iEta =
i-24;
128 TString hn = histoName + iEta;
148 set<UInt_t> uniqueIds;
151 Bool_t acceptEvent = kTRUE;
160 if (targetE < MIN_TARGET_E || targetE > MAX_TARGET_E)
return kFALSE;;
164 vector<TCell> selectCells;
167 if (cells->GetSize()==0)
return kFALSE;
169 if (CALIB_TYPE==
"DI_JET" && probeJetEmFrac > 0.999)
return kTRUE;
172 for (Int_t
i=0;
i<cells->GetSize(); ++
i) {
181 cout <<
"Unknown or wrong hcal subdetector: " <<
HcalDetId(thisCell->
id()).subdet() << endl;
187 if (APPLY_PHI_SYM_COR_FLAG) thisCell->
SetE(phiSymCor[thisCell->
id()] * thisCell->
e());
189 if (thisCell->
e() > MIN_CELL_E) selectCells.push_back(*thisCell);
193 if (selectCells.empty()) {
194 cout <<
"NO CELLS ABOVE THRESHOLD FOUND FOR TARGET!!!" << endl;
204 pair<Int_t, UInt_t> refPos;
206 Int_t dEtaHitRef = 999;
207 Int_t dPhiHitRef = 999;
209 if (CALIB_TYPE==
"ISO_TRACK") {
216 dEtaHitRef = iEtaMaxE - iEtaHit;
217 dPhiHitRef = iPhiMaxE - iPhiHit;
219 if (dPhiHitRef < -36) dPhiHitRef += 72;
220 if (dPhiHitRef > 36) dPhiHitRef -= 72;
222 if (iEtaHit*iEtaMaxE < 0) {
223 if (dEtaHitRef<0) dEtaHitRef += 1;
224 if (dEtaHitRef>0) dEtaHitRef -= 1;
235 if (!USE_CONE_CLUSTERING) {
236 if (
abs(iEtaMaxE)<16 && HB_CLUSTER_SIZE==3)
filterCells3x3(selectCells, iEtaMaxE, iPhiMaxE);
237 if (
abs(iEtaMaxE)>15 && HE_CLUSTER_SIZE==3)
filterCells3x3(selectCells, iEtaMaxE, iPhiMaxE);
239 if (
abs(iEtaMaxE)<16 && HB_CLUSTER_SIZE==5)
filterCells5x5(selectCells, iEtaMaxE, iPhiMaxE);
240 if (
abs(iEtaMaxE)>15 && HE_CLUSTER_SIZE==5)
filterCells5x5(selectCells, iEtaMaxE, iPhiMaxE);
244 const GlobalPoint hitPositionHcal(xTrkHcal, yTrkHcal, zTrkHcal);
250 refPos.first = iEtaMaxE;
251 refPos.second = iPhiMaxE;
254 else if (CALIB_TYPE==
"DI_JET") {
256 if (etVetoJet>MAX_ET_THIRD_JET) acceptEvent = kFALSE;
258 Float_t jetsDPhi = probeJetP4->DeltaPhi(*tagJetP4);
259 if (fabs(jetsDPhi * 180.0/
M_PI) < MIN_DPHI_DIJETS) acceptEvent = kFALSE;
261 if (probeJetEmFrac>MAX_PROBEJET_EMFRAC) acceptEvent = kFALSE;
262 if (fabs(probeJetP4->Eta())<MIN_PROBEJET_ABSETA) acceptEvent = kFALSE;
263 if (fabs(tagJetP4->Eta())>MAX_TAGJET_ABSETA) acceptEvent = kFALSE;
264 if (fabs(tagJetP4->Et())< MIN_TAGJET_ET) acceptEvent = kFALSE;
280 refPos.first = iEtaMaxE;
281 refPos.second = iPhiMaxE;
283 if (
abs(iEtaMaxE)>40) acceptEvent = kFALSE;
293 vector<Float_t> energies;
296 for (vector<TCell>::iterator i_it = selectCells.begin(); i_it!=selectCells.end(); ++i_it) {
300 if (uniqueIds.insert(i_it->id()).
second) {
301 energies.push_back(i_it->e());
302 ids.push_back(i_it->id());
307 if (CALIB_TYPE==
"ISO_TRACK") {
308 if (accumulate(energies.begin(), energies.end(), 0.0) / targetE < MIN_EOVERP) acceptEvent=kFALSE;
309 if (accumulate(energies.begin(), energies.end(), 0.0) / targetE > MAX_EOVERP) acceptEvent=kFALSE;
311 if (emEnergy > MAX_TRK_EME) acceptEvent=kFALSE;
313 if (
abs(dEtaHitRef)>1 ||
abs(dPhiHitRef)>1) acceptEvent=kFALSE;
322 h1_rawSumE->Fill(accumulate(energies.begin(), energies.end(), 0.0));
327 cellEnergies.push_back(energies);
328 cellIds.push_back(ids);
329 targetEnergies.push_back(targetE);
330 refIEtaIPhi.push_back(refPos);
332 if (
abs(refPos.first)<=10)
353 cout <<
"\n\nFinished reading the events.\n";
354 cout <<
"Number of input objects: " << cellIds.size() << endl;
355 cout <<
"Performing minimization: depending on selected method can take some time...\n\n";
357 for (vector<pair<Int_t, UInt_t> >::iterator it_rp=refIEtaIPhi.begin(); it_rp!=refIEtaIPhi.end(); ++it_rp ) {
358 Float_t
weight = (
abs(it_rp->first)<21)? 1.0/72.0 : 1.0/36.0;
362 if (CALIB_METHOD==
"MATRIX_INV_OF_ETA_AVE") {
363 GetCoefFromMtrxInvOfAve();
365 else if (CALIB_METHOD==
"L3" || CALIB_METHOD==
"L3_AND_MTRX_INV") {
368 int numIterations = 10;
370 solution = thisL3Algo->
iterate(cellEnergies, cellIds, targetEnergies, numIterations);
376 if (!SUM_DEPTHS && SUM_SMALL_DEPTHS) {
377 vector<UInt_t> idForSummedCells;
379 for (map<UInt_t, Float_t>::iterator m_it = solution.begin(); m_it != solution.end(); ++m_it) {
383 idForSummedCells.push_back(
HcalDetId(m_it->first));
386 for (vector<UInt_t>::iterator v_it=idForSummedCells.begin(); v_it!=idForSummedCells.end(); ++v_it) {
388 solution[addCoefId] = solution[*v_it];
394 if (CALIB_METHOD==
"L3_AND_MTRX_INV") {
395 GetCoefFromMtrxInvOfAve();
399 for (map<UInt_t, Float_t>::iterator it_s=solution.begin(); it_s!=solution.end(); ++it_s) {
401 if (
abs(iEtaSol)<CALIB_ABS_IETA_MIN ||
abs(iEtaSol)> CALIB_ABS_IETA_MAX) it_s->second = 1.0;
402 else it_s->second *= iEtaCoefMap[iEtaSol];
419 for (
unsigned int i=0;
i<cellEnergies.size(); ++
i) {
421 for (
unsigned int j=0; j<(cellEnergies[
i]).
size(); ++j) {
423 rawResp += (cellEnergies[
i])[j];
425 if (CALIB_METHOD==
"L3_AND_MTRX_INV") {
426 corResp += (solution[cellIds[
i][j]] * cellEnergies[
i][j]);
428 else if (CALIB_METHOD==
"L3") {
429 corResp += (solution[cellIds[
i][j]] * (cellEnergies[
i][j]));
431 else if (CALIB_METHOD==
"MATRIX_INV_OF_ETA_AVE") {
432 corResp += (iEtaCoefMap[iEta]* cellEnergies[
i][j]);
435 if (maxIEta <
abs(iEta) ) maxIEta =
abs(iEta);
436 if (minIEta >
abs(iEta) ) minIEta =
abs(iEta);
439 rawResp /= targetEnergies[
i];
440 corResp /= targetEnergies[
i];
446 if (CALIB_TYPE==
"ISO_TRACK") {
447 Int_t ind = refIEtaIPhi[
i].first;
448 (ind<0) ? (ind +=24) : (ind +=23);
449 if (ind>=0 && ind<48) {
462 else if (maxIEta<25 && minIEta>16) {
500 for (Int_t
i=0;
i<48; ++
i) {
511 cout <<
"\n Finished calibration.\n " << endl;
524 map<Int_t, Float_t> aveTargetE;
525 map<Int_t, Int_t> nEntries;
528 map<Int_t, map<Int_t, Float_t> > aveHitE;
530 for (
unsigned int i=0;
i<cellEnergies.size(); ++
i) {
531 Int_t iEtaRef = refIEtaIPhi[
i].first;
532 aveTargetE[iEtaRef] += targetEnergies[
i];
536 if (CALIB_METHOD==
"L3_AND_MTRX_INV") {
537 for (
unsigned int j=0; j<(cellEnergies[
i]).
size(); ++j) {
538 aveHitE[iEtaRef][
HcalDetId(cellIds[
i][j]).
ieta()] += (solution[cellIds[
i][j]] * cellEnergies[
i][j]);
541 else if (CALIB_METHOD==
"MATRIX_INV_OF_ETA_AVE") {
542 for (
unsigned int j=0; j<(cellEnergies[
i]).
size(); ++j) {
543 aveHitE[iEtaRef][
HcalDetId(cellIds[
i][j]).
ieta()] += cellEnergies[
i][j];
552 for (map<Int_t, Float_t>::iterator m_it = aveTargetE.begin(); m_it!=aveTargetE.end(); ++m_it){
553 Int_t iEta = m_it->first;
554 norm = (nEntries[iEta] > 0)? 1.0/(nEntries[iEta]) : 1.0;
555 aveTargetE[iEta] *= norm;
557 map<Int_t, Float_t>::iterator n_it = (aveHitE[iEta]).
begin();
560 for (; n_it!=(aveHitE[iEta]).
end(); ++n_it) {
561 (n_it->second) *= norm;
562 sumRawE += (n_it->second);
567 Int_t ONE_SIDE_IETA_RANGE = CALIB_ABS_IETA_MAX - CALIB_ABS_IETA_MIN +1;
571 vector<Int_t> iEtaList;
573 for (Int_t
i=-CALIB_ABS_IETA_MAX;
i<=CALIB_ABS_IETA_MAX; ++
i) {
574 if (
abs(
i)<CALIB_ABS_IETA_MIN)
continue;
575 iEtaList.push_back(
i);
578 TMatrixD
A(2*ONE_SIDE_IETA_RANGE, 2*ONE_SIDE_IETA_RANGE);
579 TMatrixD
b(2*ONE_SIDE_IETA_RANGE, 1);
580 TMatrixD x(2*ONE_SIDE_IETA_RANGE, 1);
582 for (Int_t
i=0;
i<2*ONE_SIDE_IETA_RANGE; ++
i) {
583 for (Int_t j=0; j<2*ONE_SIDE_IETA_RANGE; ++j) {
588 for (UInt_t
i=0;
i<iEtaList.size(); ++
i) {
589 b(
i, 0) = aveTargetE[iEtaList[
i]];
591 map<Int_t, Float_t>::iterator n_it = aveHitE[iEtaList[
i]].begin();
592 for (; n_it!=aveHitE[iEtaList[
i]].end(); ++n_it){
594 if (fabs(n_it->first)>CALIB_ABS_IETA_MAX || fabs(n_it->first)<CALIB_ABS_IETA_MIN)
continue;
595 Int_t j = Int_t(
find(iEtaList.begin(),iEtaList.end(), n_it->first) - iEtaList.begin());
596 A(
i, j) = n_it->second;
604 Bool_t hasSolution = qrh.MultiSolve(coef);
606 if (hasSolution && (CALIB_METHOD==
"L3_AND_MTRX_INV" || CALIB_METHOD==
"MATRIX_INV_OF_ETA_AVE")) {
607 for (UInt_t
i=0;
i<iEtaList.size(); ++
i) {
608 iEtaCoefMap[iEtaList[
i]] = coef(
i, 0);
616 std::ifstream phiSymFile(PHI_SYM_COR_FILENAME.Data());
619 cout <<
"\nERROR: Can not find file with phi symmetry constants \"" << PHI_SYM_COR_FILENAME.Data() <<
"\"" << endl;
636 while (getline(phiSymFile, line)) {
638 if(line.empty() || line[0]==
'#')
continue;
640 std::istringstream linestream(line);
641 linestream >> iEta >> iPhi >> depth >> sdName >> value >> hex >> detId;
648 cout <<
"\nInvalid detector name in phi symmetry constants file: " << sdName.Data() << endl;
649 cout <<
"Check file and rerun!\n" << endl;
656 cout <<
"\nInconsistent info in phi symmetry file: subdet, iEta, iPhi, depth do not match rawId!\n" << endl;
660 if (!topo_->valid(hId)) {
661 cout <<
"\nInvalid DetId from: iEta=" << iEta <<
" iPhi=" << iPhi <<
" depth=" << depth
662 <<
" subdet=" << sdName.Data() <<
" detId=" << detId << endl << endl;
677 TString sdName[8] = {
"EMPTY",
"HB",
"HE",
"HO",
"HF",
"TRITWR",
"UNDEF",
"OTHER"};
679 FILE *constFile = fopen(OUTPUT_COR_COEF_FILENAME.Data(),
"w+");
682 fprintf(constFile,
"%1s%16s%16s%16s%16s%9s%11s\n",
683 "#",
"eta",
"phi",
"depth",
"det",
"value",
"DetId");
687 for(Int_t
sd=1;
sd<=4;
sd++) {
689 for(Int_t
e=1;
e<=50;
e++) {
692 for(Int_t side=0; side<2; side++) {
693 eta = (side==0)? -
e :
e;
695 for(Int_t phi=1; phi<=72; phi++) {
697 for(Int_t
d=1;
d<5;
d++) {
701 Float_t corrFactor = 1.0;
710 Int_t subdetInd =
sd;
716 if (CALIB_METHOD==
"L3" || CALIB_METHOD==
"L3_AND_MTRX_INV") {
734 else if (CALIB_METHOD==
"MATRIX_INV_OF_ETA_AVE") {
735 corrFactor = iEtaCoefMap[
eta];
738 else if (CALIB_METHOD==
"ISO_TRK_PHI_SYM") {
745 fprintf(constFile,
"%17i%16i%16i%16s%9.5f%11X\n",
746 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)