27 #include "TObjString.h"
65 char const *
const LowPt_string =
"LowPt" ;
74 :
asg::AsgMessaging(std::string(
name))
76 , m_doCombToyMC(false)
92 if (m_corrFileNameList.empty()) {
96 ATH_MSG_DEBUG(
"Initializing tool with " << m_corrFileNameList.size()
97 <<
" configuration file(s)");
99 if (m_doToyMC && m_doCombToyMC) {
101 <<
" Only use one!");
108 if (m_doToyMC || m_doCombToyMC) {
112 const std::unique_ptr<char[]>
fname(
113 gSystem->ExpandPathName(m_corrFileNameList[0].c_str()));
114 std::unique_ptr<TMD5> tmd = std::make_unique<TMD5>();
115 const char* tmd_as_string = tmd->FileChecksum(
fname.get())->AsString();
116 m_seed = *(
reinterpret_cast<const unsigned long int*
>(tmd_as_string));
121 m_Rndm = TRandom3(m_seed);
126 if (0 == getHistograms()) {
127 ATH_MSG_ERROR(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
128 <<
"! Problem when calling getHistograms()");
131 const unsigned int nRunNumbersFull = m_begRunNumberList.size();
132 const unsigned int nRunNumbersFast = m_begRunNumberListFastSim.size();
134 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
135 <<
"Found " << nRunNumbersFast
136 <<
" run number ranges for fast sim with a total of "
138 <<
" scale factor histograms.");
140 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
141 <<
"Found " << nRunNumbersFull
142 <<
" run number ranges for full sim with a total of "
144 <<
" scale factor histograms.");
155 const double cluster_eta,
158 const bool onlyTotal)
const
167 int runnumberIndex = -1;
169 for (
unsigned int i = 0;
i < m_begRunNumberListFastSim.size(); ++
i) {
170 if (m_begRunNumberListFastSim[
i] <=
runnumber &&
177 for (
unsigned int i = 0;
i < m_begRunNumberList.size(); ++
i) {
185 if (runnumberIndex < 0) {
196 const auto& currentmap = (
isFastSim) ? m_fastHistList : m_histList;
197 const std::vector<HistArray>& sfVector = currentmap.at(
mapkey::sf);
203 if (sfVector.empty() || runnumberIndex >=
static_cast<int>(sfVector.size())) {
210 const HistArray& sfObjectArray = sfVector[runnumberIndex];
211 const auto& edges = (
isFastSim) ? m_fastHistEdges[runnumberIndex]
212 : m_histEdges[runnumberIndex];
213 const int entries = edges.size();
219 double yValue(cluster_eta);
223 bool invalid =
false;
224 bool changedEt =
false;
236 if (std::abs(yValue) >= histEdge.
etaMax) {
241 if (std::abs(yValue) < histEdge.
etaMin) {
255 xValue = histEdge.
etMax - 1000 ;
279 << yValue <<
" , et = " <<
et <<
" , run number = "
280 <<
runnumber <<
". Please check your input files!");
293 constexpr
double epsilon = 1
e-6;
294 if (currentEdge.
etaMin >= (0 - epsilon)) {
295 yValue = std::abs(yValue);
298 const TH2* currentHist =
static_cast<TH2*
>(sfObjectArray[
index].get()) ;
299 const int globalBinNumber = currentHist->FindFixBin(xValue, yValue);
300 const double scaleFactor = currentHist->GetBinContent(globalBinNumber);
301 const double scaleFactorErr = currentHist->GetBinError(globalBinNumber);
306 result.Total = scaleFactorErr;
308 result.histBinNum = globalBinNumber;
318 double statErr = -999;
319 const std::vector<HistArray>& statVector = currentmap.at(
mapkey::stat);
320 if (!statVector.empty()) {
321 if (!statVector[runnumberIndex].
empty()) {
322 statErr =
static_cast<TH1*
>(statVector[runnumberIndex][
index].get())
323 ->GetBinContent(globalBinNumber);
330 double val = statErr;
331 const std::vector<HistArray>& uncorrVector = currentmap.at(
mapkey::uncorr);
332 if (!uncorrVector.empty()) {
333 if (!uncorrVector.at(runnumberIndex).empty()) {
334 const double valAdd =
335 static_cast<TH1*
>(uncorrVector[runnumberIndex][
index].get())
336 ->GetBinContent(globalBinNumber);
349 result.Corr.resize(m_nSysMax);
350 const std::vector<std::vector<HistArray>>& sysList =
352 if (sysList.size() >
static_cast<unsigned int>(
index)) {
353 if (sysList.at(
index).size() >
static_cast<unsigned int>(runnumberIndex)) {
354 const int sys_entries = sysList.at(
index).at(runnumberIndex).size();
355 for (
int sys = 0;
sys < sys_entries; ++
sys) {
357 static_cast<TH2*
>(sysList[
index][runnumberIndex][
sys].get())
358 ->GetBinContent(globalBinNumber);
366 if (m_doToyMC || m_doCombToyMC) {
367 result.toys.resize(
static_cast<size_t>(m_nToyMC));
368 const std::vector<std::vector<HistArray>>& toyMCList =
369 ((
isFastSim) ? m_uncorrToyMCSystFast : m_uncorrToyMCSystFull);
370 if (toyMCList.size() > (
unsigned int)runnumberIndex) {
371 for (
int toy = 0; toy < m_nToyMC; ++toy) {
372 if (toyMCList[runnumberIndex][toy].
size() >
373 static_cast<unsigned int>(
index)) {
375 static_cast<TH2*
>(toyMCList[runnumberIndex][toy][
index].get())
376 ->GetBinContent(globalBinNumber);
393 const std::vector<TH1*>& corr,
396 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")! "
397 <<
"Entering function buildSingleToyMC");
398 std::vector<TH2*> tmpHists;
399 int nBins = (
stat->GetNbinsX() + 2) * (
stat->GetNbinsY() + 2);
400 tmpHists.reserve(m_nToyMC);
401 for (
int toy = 0; toy < m_nToyMC; toy++) {
402 tmpHists.push_back((
TH2*)corr.at(0)->Clone());
410 double valAdd =
uncorr->GetBinContent(
bin);
413 for (
int toy = 0; toy < m_nToyMC; toy++) {
414 tmpHists.at(toy)->SetBinContent(
415 bin, (
val * m_Rndm.Gaus(0, 1)) +
sf->GetBinContent(
bin));
417 tmpHists.at(toy)->SetDirectory(
nullptr);
433 const std::vector<TH1*>& corr,
438 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
439 <<
"Entering function buildSingleCombToyMC");
442 const int nBins = (
stat->GetNbinsX() + 2) * (
stat->GetNbinsY() + 2);
445 std::vector<double> rnd(nSys, 0);
446 for (
int s = 0;
s < nSys; ++
s) {
447 rnd[
s] = m_Rndm.Gaus(0, 1);
456 double valAdd =
uncorr->GetBinContent(
bin);
459 val =
val * m_Rndm.Gaus(0, 1);
462 for (
int s = 0;
s < nSys; ++
s) {
463 if (corr.at(
s) !=
nullptr) {
464 val +=
static_cast<TH2*
>(corr.at(
s))->GetBinContent(
bin) * rnd[
s];
469 tmpHist->SetDirectory(
nullptr);
475 std::vector<Root::TElectronEfficiencyCorrectionTool::HistArray>
477 const std::vector<TH1*>&
sf,
478 const std::vector<TH1*>&
eig,
479 const std::vector<TH1*>&
stat,
480 const std::vector<TH1*>&
uncorr,
481 const std::vector<std::vector<TH1*>>& corr)
484 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
485 <<
"Entering function buildToyMCTable");
488 int randomCounter(0);
489 std::vector<HistArray> tmpVec;
490 const int stat_entries =
stat.size();
492 for (
int toyMC = 0; toyMC < m_nToyMC; toyMC++) {
494 for (
int i = 0;
i < stat_entries; ++
i) {
496 nSys = ((
TH1*)
eig.at(
i))->GetNbinsX() - 1;
497 tmpArray.emplace_back(buildSingleCombToyMC((
TH2*)
sf.at(
i),
504 tmpArray.emplace_back(buildSingleCombToyMC((
TH2*)
sf.at(
i),
512 tmpVec.emplace_back(std::move(tmpArray));
515 std::vector<std::vector<TH2*>> tmpVec2;
516 for (
int i = 0;
i < stat_entries; ++
i) {
517 nSys = ((
TH1*)
eig.at(
i))->GetNbinsX() - 1;
518 tmpVec2.push_back(buildSingleToyMC((
TH2*)
sf.at(
i),
524 for (
int toy = 0; toy < m_nToyMC; toy++) {
526 for (
auto&
i : tmpVec2) {
527 tmpArray.emplace_back(
i.at(toy));
529 tmpVec.emplace_back(std::move(tmpArray));
539 std::map<
float, std::vector<float>>& pt_eta1)
const
542 const std::vector<HistArray>& tmpVec = (!m_histList[
mapkey::sf].empty())
548 std::vector<float>
eta1;
552 for (
const auto& ikey : tmpVec) {
554 for (
const auto&
entries : ikey) {
558 int nbinsX = h_tmp->GetNbinsX();
559 int nbinsY = h_tmp->GetNbinsY();
561 for (
int biny = 1; biny <= nbinsY; ++biny) {
562 eta1.push_back(h_tmp->GetYaxis()->GetBinLowEdge(biny));
565 for (
int binx = 1; binx <= nbinsX; ++binx) {
566 pt_eta1[h_tmp->GetXaxis()->GetBinLowEdge(binx)] =
eta1;
570 for (
auto&
i : pt_eta1) {
571 nbinsTotal +=
i.second.size();
582 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
583 <<
"Entering function getHistograms");
591 if (!m_corrFileNameList.empty()) {
592 TString firstFileNameAndPath = m_corrFileNameList[0].c_str();
593 std::unique_ptr<TObjArray> myStringList(firstFileNameAndPath.Tokenize(
"/"));
594 int lastIdx = myStringList->GetLast();
595 TString
fileName = ((TObjString*)myStringList->At(lastIdx))->GetString();
596 std::unique_ptr<TObjArray> myFileNameTokensList(
fileName.Tokenize(
"."));
598 if (myFileNameTokensList->GetLast() < 3) {
606 for (
auto&
ifile : m_corrFileNameList) {
608 const std::unique_ptr<char[]>
fname(gSystem->ExpandPathName(
ifile.c_str()));
609 std::unique_ptr<TFile>
rootFile(TFile::Open(
fname.get(),
"READ"));
616 TIter nextdir(
rootFile->GetListOfKeys());
618 TObject*
obj =
nullptr;
619 while ((
dir = (TKey*)nextdir())) {
621 if (
obj->IsA()->InheritsFrom(
"TDirectory")) {
623 std::unique_ptr<TObjArray> dirNameArray(
624 TString(
obj->GetName()).Tokenize(
"_"));
627 int lastIdx = dirNameArray->GetLast();
630 "The folder name seems to have the wrong format! Directory name:"
635 if (0 == this->setupHistogramsInFolder(*dirNameArray, lastIdx)) {
637 <<
dir->GetName() <<
"in file " <<
ifile);
641 ATH_MSG_ERROR(
"Wrong file content! Expected only Directories "
656 const TObjArray& dirNameArray,
660 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
661 <<
"Entering funtion setupHistogramsInFolder");
664 TString myBegRunNumString =
665 ((TObjString*)dirNameArray.At(lastIdx - 1))->GetString();
666 if (myBegRunNumString.IsDigit()) {
667 runNumBegin = myBegRunNumString.Atoi();
670 TString myEndRunNumString =
671 ((TObjString*)dirNameArray.At(lastIdx))->GetString();
672 if (myEndRunNumString.IsDigit()) {
673 runNumEnd = myEndRunNumString.Atoi();
675 if (runNumBegin < 0 || runNumEnd < 0 || runNumEnd < runNumBegin) {
676 ATH_MSG_ERROR(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
677 <<
"Could NOT interpret the run number range: "
678 << runNumBegin <<
" - " << runNumEnd);
683 std::vector<std::vector<TH1*>> objsFull(
mapkey::end);
684 std::vector<std::vector<TH1*>> objsFast(
mapkey::end);
686 std::vector<std::vector<TH1*>> sysObjsFull;
687 std::vector<std::vector<TH1*>> sysObjsFast;
690 TObject*
obj =
nullptr;
691 int seenSystematics = 0;
693 while ((
key = (TKey*)nextkey())) {
695 if (
obj->IsA()->InheritsFrom(
"TH1")) {
698 if (std::strstr(
obj->GetName(),
"FullSim") !=
nullptr) {
700 static_cast<TH1*
>(
obj), objsFull, sysObjsFull, seenSystematics);
701 }
else if (std::strstr(
obj->GetName(),
"AtlFast") !=
nullptr) {
703 static_cast<TH1*
>(
obj), objsFast, sysObjsFast, seenSystematics);
706 <<
obj->GetName() <<
" is full or fast simulation!");
711 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
712 <<
"Setting up histograms for Run range "
716 for (
int key : s_keys) {
717 if (!objsFull.at(
key).empty()) {
725 <<
" for full sim!");
729 if (!objsFast.at(
key).empty()) {
732 m_begRunNumberListFastSim,
733 m_endRunNumberListFastSim,
741 m_fastSysList.resize(sysObjsFast.size());
742 for (
unsigned int sys = 0;
sys < sysObjsFast.size();
sys++) {
745 m_begRunNumberListFastSim,
746 m_endRunNumberListFastSim,
749 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
753 m_sysList.resize(sysObjsFull.size());
754 for (
unsigned int sys = 0;
sys < sysObjsFull.size();
sys++) {
761 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
766 fillHistEdges(m_histList.at(
mapkey::sf), m_histEdges);
767 fillHistEdges(m_fastHistList.at(
mapkey::sf), m_fastHistEdges);
770 if (m_doToyMC || m_doCombToyMC) {
771 bool fullToysBooked =
772 setupUncorrToySyst(objsFull, sysObjsFull, m_uncorrToyMCSystFull);
773 bool fastToysBooked =
774 setupUncorrToySyst(objsFast, sysObjsFast, m_uncorrToyMCSystFast);
775 if (fullToysBooked || fastToysBooked) {
778 <<
" ToyMC systematics ");
782 <<
" combined ToyMC systematics ");
795 std::vector<std::vector<TH1*>>&
objs,
796 std::vector<std::vector<TH1*>>& sysObjs,
797 int& seenSystematics)
800 for (
int key : s_keys) {
801 if (TString(
obj->GetName())
807 const TString tmpName(
obj->GetName());
809 if (tmpName.EndsWith(
"_sys")) {
811 std::vector<TH1*> tmpArray;
813 tmpArray.emplace_back(
static_cast<TH1*
>(
obj->Clone()));
814 sysObjs.emplace_back(tmpArray);
818 if (tmpName.Contains(
"_corr")) {
826 if (tmpName.EndsWith(
"corr0")) {
828 std::vector<TH1*> tmpArray;
830 sysObjs.emplace_back(tmpArray);
838 sysObjs.back().emplace_back(
obj);
843 if (seenSystematics > m_nSysMax) {
844 m_nSysMax = seenSystematics;
852 std::vector<std::vector<TH1*>>&
objs,
853 std::vector<std::vector<TH1*>>& sysObjs,
854 std::vector<std::vector<HistArray>>& uncorrToyMCSyst)
856 bool toysBooked =
false;
863 uncorrToyMCSyst.push_back(buildToyMCTable(
884 const std::vector<TH1*>&
hists,
885 std::vector<HistArray>& histList,
886 std::vector<unsigned int>& beginRunNumberList,
887 std::vector<unsigned int>& endRunNumberList,
888 const int runNumBegin,
889 const int runNumEnd)
const
892 ATH_MSG_ERROR(
"! Could NOT find histogram with name *_sf in folder");
899 tmpHist->SetDirectory(
nullptr);
900 tmpArray.emplace_back(
tmpHist);
902 histList.emplace_back(std::move(tmpArray));
904 if (!beginRunNumberList.empty()) {
905 if (runNumBegin != (
int)beginRunNumberList.back()) {
906 beginRunNumberList.push_back(runNumBegin);
909 beginRunNumberList.push_back(runNumBegin);
911 if (!endRunNumberList.empty()) {
912 if (runNumEnd != (
int)endRunNumberList.back()) {
913 endRunNumberList.push_back(runNumEnd);
916 endRunNumberList.push_back(runNumEnd);
923 const std::vector<HistArray>& sfPerPeriodHist,
924 std::vector<std::vector<HistEdge>>& sfPerPeriodEdges) {
926 for (
const auto&
vec : sfPerPeriodHist) {
927 const size_t vecSize =
vec.size();
928 std::vector<HistEdge> periodVec;
929 periodVec.reserve(vecSize);
930 for (
size_t i = 0;
i < vecSize; ++
i) {
932 const auto*
const xAxis =
tmpHist->GetXaxis();
933 const auto* yAxis =
tmpHist->GetYaxis();
935 histEdge.
etaMax = yAxis->GetXmax();
936 histEdge.
etaMin = yAxis->GetXmin();
937 histEdge.
etMax = xAxis->GetXmax();
938 histEdge.
etMin = xAxis->GetXmin();
940 (std::strstr(
tmpHist->GetName(), LowPt_string) !=
nullptr);
942 periodVec.emplace_back(histEdge);
944 sfPerPeriodEdges.emplace_back(std::move(periodVec));