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.");
154 const auto& currentmap = (
isFastSim) ? m_fastHistList : m_histList;
163 const double cluster_eta,
166 const bool onlyTotal)
const
175 int runnumberIndex = -1;
177 for (
unsigned int i = 0;
i < m_begRunNumberListFastSim.size(); ++
i) {
178 if (m_begRunNumberListFastSim[
i] <=
runnumber &&
185 for (
unsigned int i = 0;
i < m_begRunNumberList.size(); ++
i) {
193 if (runnumberIndex < 0) {
204 const auto& currentmap = (
isFastSim) ? m_fastHistList : m_histList;
205 const std::vector<HistArray>& sfVector = currentmap.at(
mapkey::sf);
211 if (sfVector.empty() || runnumberIndex >=
static_cast<int>(sfVector.size())) {
218 const HistArray& sfObjectArray = sfVector[runnumberIndex];
219 const auto& edges = (
isFastSim) ? m_fastHistEdges[runnumberIndex]
220 : m_histEdges[runnumberIndex];
221 const int entries = edges.size();
227 double yValue(cluster_eta);
231 bool invalid =
false;
232 bool changedEt =
false;
244 if (std::abs(yValue) >= histEdge.
etaMax) {
249 if (std::abs(yValue) < histEdge.
etaMin) {
263 xValue = histEdge.
etMax - 1000 ;
287 << yValue <<
" , et = " <<
et <<
" , run number = "
288 <<
runnumber <<
". Please check your input files!");
301 constexpr
double epsilon = 1
e-6;
302 if (currentEdge.
etaMin >= (0 - epsilon)) {
303 yValue = std::abs(yValue);
306 const TH2* currentHist =
static_cast<TH2*
>(sfObjectArray[
index].get()) ;
307 const int globalBinNumber = currentHist->FindFixBin(xValue, yValue);
308 const double scaleFactor = currentHist->GetBinContent(globalBinNumber);
309 const double scaleFactorErr = currentHist->GetBinError(globalBinNumber);
314 result.Total = scaleFactorErr;
316 result.histBinNum = globalBinNumber;
326 double statErr = -999;
327 const std::vector<HistArray>& statVector = currentmap.at(
mapkey::stat);
328 if (!statVector.empty()) {
329 if (!statVector[runnumberIndex].
empty()) {
330 statErr =
static_cast<TH1*
>(statVector[runnumberIndex][
index].get())
331 ->GetBinContent(globalBinNumber);
338 double val = statErr;
339 const std::vector<HistArray>& uncorrVector = currentmap.at(
mapkey::uncorr);
340 if (!uncorrVector.empty()) {
341 if (!uncorrVector.at(runnumberIndex).empty()) {
342 const double valAdd =
343 static_cast<TH1*
>(uncorrVector[runnumberIndex][
index].get())
344 ->GetBinContent(globalBinNumber);
357 result.Corr.resize(m_nSysMax);
358 const std::vector<std::vector<HistArray>>& sysList =
360 if (sysList.size() >
static_cast<unsigned int>(
index)) {
361 if (sysList.at(
index).size() >
static_cast<unsigned int>(runnumberIndex)) {
362 const int sys_entries = sysList.at(
index).at(runnumberIndex).size();
363 for (
int sys = 0;
sys < sys_entries; ++
sys) {
365 static_cast<TH2*
>(sysList[
index][runnumberIndex][
sys].get())
366 ->GetBinContent(globalBinNumber);
374 if (m_doToyMC || m_doCombToyMC) {
375 result.toys.resize(
static_cast<size_t>(m_nToyMC));
376 const std::vector<std::vector<HistArray>>& toyMCList =
377 ((
isFastSim) ? m_uncorrToyMCSystFast : m_uncorrToyMCSystFull);
378 if (toyMCList.size() > (
unsigned int)runnumberIndex) {
379 for (
int toy = 0; toy < m_nToyMC; ++toy) {
380 if (toyMCList[runnumberIndex][toy].
size() >
381 static_cast<unsigned int>(
index)) {
383 static_cast<TH2*
>(toyMCList[runnumberIndex][toy][
index].get())
384 ->GetBinContent(globalBinNumber);
401 const std::vector<TH1*>& corr,
404 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")! "
405 <<
"Entering function buildSingleToyMC");
406 std::vector<TH2*> tmpHists;
407 int nBins = (
stat->GetNbinsX() + 2) * (
stat->GetNbinsY() + 2);
408 tmpHists.reserve(m_nToyMC);
409 for (
int toy = 0; toy < m_nToyMC; toy++) {
410 tmpHists.push_back((TH2*)corr.at(0)->Clone());
418 double valAdd =
uncorr->GetBinContent(
bin);
421 for (
int toy = 0; toy < m_nToyMC; toy++) {
422 tmpHists.at(toy)->SetBinContent(
423 bin, (
val * m_Rndm.Gaus(0, 1)) +
sf->GetBinContent(
bin));
425 tmpHists.at(toy)->SetDirectory(
nullptr);
441 const std::vector<TH1*>& corr,
446 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
447 <<
"Entering function buildSingleCombToyMC");
450 const int nBins = (
stat->GetNbinsX() + 2) * (
stat->GetNbinsY() + 2);
451 tmpHist = (TH2*)corr.at(0)->Clone();
453 std::vector<double> rnd(nSys, 0);
454 for (
int s = 0;
s < nSys; ++
s) {
455 rnd[
s] = m_Rndm.Gaus(0, 1);
464 double valAdd =
uncorr->GetBinContent(
bin);
467 val =
val * m_Rndm.Gaus(0, 1);
470 for (
int s = 0;
s < nSys; ++
s) {
471 if (corr.at(
s) !=
nullptr) {
472 val +=
static_cast<TH2*
>(corr.at(
s))->GetBinContent(
bin) * rnd[
s];
477 tmpHist->SetDirectory(
nullptr);
483 std::vector<Root::TElectronEfficiencyCorrectionTool::HistArray>
485 const std::vector<TH1*>&
sf,
486 const std::vector<TH1*>&
eig,
487 const std::vector<TH1*>&
stat,
488 const std::vector<TH1*>&
uncorr,
489 const std::vector<std::vector<TH1*>>& corr)
492 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
493 <<
"Entering function buildToyMCTable");
496 int randomCounter(0);
497 std::vector<HistArray> tmpVec;
498 const int stat_entries =
stat.size();
500 for (
int toyMC = 0; toyMC < m_nToyMC; toyMC++) {
502 for (
int i = 0;
i < stat_entries; ++
i) {
504 nSys = ((TH1*)
eig.at(
i))->GetNbinsX() - 1;
505 tmpArray.emplace_back(buildSingleCombToyMC((TH2*)
sf.at(
i),
512 tmpArray.emplace_back(buildSingleCombToyMC((TH2*)
sf.at(
i),
520 tmpVec.emplace_back(std::move(tmpArray));
523 std::vector<std::vector<TH2*>> tmpVec2;
524 for (
int i = 0;
i < stat_entries; ++
i) {
525 nSys = ((TH1*)
eig.at(
i))->GetNbinsX() - 1;
526 tmpVec2.push_back(buildSingleToyMC((TH2*)
sf.at(
i),
532 for (
int toy = 0; toy < m_nToyMC; toy++) {
534 for (
auto&
i : tmpVec2) {
535 tmpArray.emplace_back(
i.at(toy));
537 tmpVec.emplace_back(std::move(tmpArray));
547 std::map<
float, std::vector<float>>& pt_eta1)
const
550 const std::vector<HistArray>& tmpVec = (!m_histList[
mapkey::sf].empty())
556 std::vector<float>
eta1;
560 for (
const auto& ikey : tmpVec) {
562 for (
const auto&
entries : ikey) {
565 TH2* h_tmp = ((TH2*)
entries.get());
566 int nbinsX = h_tmp->GetNbinsX();
567 int nbinsY = h_tmp->GetNbinsY();
569 for (
int biny = 1; biny <= nbinsY; ++biny) {
570 eta1.push_back(h_tmp->GetYaxis()->GetBinLowEdge(biny));
573 for (
int binx = 1; binx <= nbinsX; ++binx) {
574 pt_eta1[h_tmp->GetXaxis()->GetBinLowEdge(binx)] =
eta1;
578 for (
auto&
i : pt_eta1) {
579 nbinsTotal +=
i.second.size();
590 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
591 <<
"Entering function getHistograms");
599 if (!m_corrFileNameList.empty()) {
600 TString firstFileNameAndPath = m_corrFileNameList[0].c_str();
601 std::unique_ptr<TObjArray> myStringList(firstFileNameAndPath.Tokenize(
"/"));
602 int lastIdx = myStringList->GetLast();
603 TString
fileName = ((TObjString*)myStringList->At(lastIdx))->GetString();
604 std::unique_ptr<TObjArray> myFileNameTokensList(
fileName.Tokenize(
"."));
606 if (myFileNameTokensList->GetLast() < 3) {
614 for (
auto&
ifile : m_corrFileNameList) {
616 const std::unique_ptr<char[]>
fname(gSystem->ExpandPathName(
ifile.c_str()));
617 std::unique_ptr<TFile>
rootFile(TFile::Open(
fname.get(),
"READ"));
624 TIter nextdir(
rootFile->GetListOfKeys());
626 TObject*
obj =
nullptr;
627 while ((
dir = (TKey*)nextdir())) {
629 if (
obj->IsA()->InheritsFrom(
"TDirectory")) {
631 std::unique_ptr<TObjArray> dirNameArray(
632 TString(
obj->GetName()).Tokenize(
"_"));
635 int lastIdx = dirNameArray->GetLast();
638 "The folder name seems to have the wrong format! Directory name:"
643 if (0 == this->setupHistogramsInFolder(*dirNameArray, lastIdx)) {
645 <<
dir->GetName() <<
"in file " <<
ifile);
649 ATH_MSG_ERROR(
"Wrong file content! Expected only Directories "
664 const TObjArray& dirNameArray,
668 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
669 <<
"Entering funtion setupHistogramsInFolder");
672 TString myBegRunNumString =
673 ((TObjString*)dirNameArray.At(lastIdx - 1))->GetString();
674 if (myBegRunNumString.IsDigit()) {
675 runNumBegin = myBegRunNumString.Atoi();
678 TString myEndRunNumString =
679 ((TObjString*)dirNameArray.At(lastIdx))->GetString();
680 if (myEndRunNumString.IsDigit()) {
681 runNumEnd = myEndRunNumString.Atoi();
683 if (runNumBegin < 0 || runNumEnd < 0 || runNumEnd < runNumBegin) {
684 ATH_MSG_ERROR(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
685 <<
"Could NOT interpret the run number range: "
686 << runNumBegin <<
" - " << runNumEnd);
691 std::vector<std::vector<TH1*>> objsFull(
mapkey::end);
692 std::vector<std::vector<TH1*>> objsFast(
mapkey::end);
694 std::vector<std::vector<TH1*>> sysObjsFull;
695 std::vector<std::vector<TH1*>> sysObjsFast;
698 TObject*
obj =
nullptr;
699 int seenSystematics = 0;
701 while ((
key = (TKey*)nextkey())) {
703 if (
obj->IsA()->InheritsFrom(
"TH1")) {
706 if (std::strstr(
obj->GetName(),
"FullSim") !=
nullptr) {
708 static_cast<TH1*
>(
obj), objsFull, sysObjsFull, seenSystematics);
709 }
else if (std::strstr(
obj->GetName(),
"AtlFast") !=
nullptr) {
711 static_cast<TH1*
>(
obj), objsFast, sysObjsFast, seenSystematics);
714 <<
obj->GetName() <<
" is full or fast simulation!");
719 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
720 <<
"Setting up histograms for Run range "
724 for (
int key : s_keys) {
725 if (!objsFull.at(
key).empty()) {
733 <<
" for full sim!");
737 if (!objsFast.at(
key).empty()) {
740 m_begRunNumberListFastSim,
741 m_endRunNumberListFastSim,
749 m_fastSysList.resize(sysObjsFast.size());
750 for (
unsigned int sys = 0;
sys < sysObjsFast.size();
sys++) {
753 m_begRunNumberListFastSim,
754 m_endRunNumberListFastSim,
757 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
761 m_sysList.resize(sysObjsFull.size());
762 for (
unsigned int sys = 0;
sys < sysObjsFull.size();
sys++) {
769 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
774 fillHistEdges(m_histList.at(
mapkey::sf), m_histEdges);
775 fillHistEdges(m_fastHistList.at(
mapkey::sf), m_fastHistEdges);
778 if (m_doToyMC || m_doCombToyMC) {
779 bool fullToysBooked =
780 setupUncorrToySyst(objsFull, sysObjsFull, m_uncorrToyMCSystFull);
781 bool fastToysBooked =
782 setupUncorrToySyst(objsFast, sysObjsFast, m_uncorrToyMCSystFast);
783 if (fullToysBooked || fastToysBooked) {
786 <<
" ToyMC systematics ");
790 <<
" combined ToyMC systematics ");
803 std::vector<std::vector<TH1*>>&
objs,
804 std::vector<std::vector<TH1*>>& sysObjs,
805 int& seenSystematics)
808 for (
int key : s_keys) {
809 if (TString(
obj->GetName())
815 const TString tmpName(
obj->GetName());
817 if (tmpName.EndsWith(
"_sys")) {
819 std::vector<TH1*> tmpArray;
821 tmpArray.emplace_back(
static_cast<TH1*
>(
obj->Clone()));
822 sysObjs.emplace_back(tmpArray);
826 if (tmpName.Contains(
"_corr")) {
834 if (tmpName.EndsWith(
"corr0")) {
836 std::vector<TH1*> tmpArray;
838 sysObjs.emplace_back(tmpArray);
846 sysObjs.back().emplace_back(
obj);
851 if (seenSystematics > m_nSysMax) {
852 m_nSysMax = seenSystematics;
860 std::vector<std::vector<TH1*>>&
objs,
861 std::vector<std::vector<TH1*>>& sysObjs,
862 std::vector<std::vector<HistArray>>& uncorrToyMCSyst)
864 bool toysBooked =
false;
871 uncorrToyMCSyst.push_back(buildToyMCTable(
892 const std::vector<TH1*>&
hists,
893 std::vector<HistArray>& histList,
894 std::vector<unsigned int>& beginRunNumberList,
895 std::vector<unsigned int>& endRunNumberList,
896 const int runNumBegin,
897 const int runNumEnd)
const
900 ATH_MSG_ERROR(
"! Could NOT find histogram with name *_sf in folder");
907 tmpHist->SetDirectory(
nullptr);
908 tmpArray.emplace_back(
tmpHist);
910 histList.emplace_back(std::move(tmpArray));
912 if (!beginRunNumberList.empty()) {
913 if (runNumBegin != (
int)beginRunNumberList.back()) {
914 beginRunNumberList.push_back(runNumBegin);
917 beginRunNumberList.push_back(runNumBegin);
919 if (!endRunNumberList.empty()) {
920 if (runNumEnd != (
int)endRunNumberList.back()) {
921 endRunNumberList.push_back(runNumEnd);
924 endRunNumberList.push_back(runNumEnd);
931 const std::vector<HistArray>& sfPerPeriodHist,
932 std::vector<std::vector<HistEdge>>& sfPerPeriodEdges) {
934 for (
const auto&
vec : sfPerPeriodHist) {
935 const size_t vecSize =
vec.size();
936 std::vector<HistEdge> periodVec;
937 periodVec.reserve(vecSize);
938 for (
size_t i = 0;
i < vecSize; ++
i) {
939 const auto*
tmpHist =
static_cast<TH2*
>(
vec[
i].get());
940 const auto*
const xAxis =
tmpHist->GetXaxis();
941 const auto* yAxis =
tmpHist->GetYaxis();
943 histEdge.
etaMax = yAxis->GetXmax();
944 histEdge.
etaMin = yAxis->GetXmin();
945 histEdge.
etMax = xAxis->GetXmax();
946 histEdge.
etMin = xAxis->GetXmin();
948 (std::strstr(
tmpHist->GetName(), LowPt_string) !=
nullptr);
950 periodVec.emplace_back(histEdge);
952 sfPerPeriodEdges.emplace_back(std::move(periodVec));