27#include "TObjString.h"
65char const *
const LowPt_string =
"LowPt" ;
97 <<
" configuration file(s)");
101 <<
" Only use one!");
112 const std::unique_ptr<char[]> fname(
114 std::unique_ptr<TMD5> tmd = std::make_unique<TMD5>();
115 const char* tmd_as_string = TMD5::FileChecksum(fname.get())->AsString();
116 m_seed = *(
reinterpret_cast<const unsigned long int*
>(tmd_as_string));
127 ATH_MSG_ERROR(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
128 <<
"! Problem when calling getHistograms()");
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.");
163 const double cluster_eta,
166 const bool onlyTotal)
const
175 int runnumberIndex = -1;
193 if (runnumberIndex < 0) {
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];
221 const int entries = edges.size();
227 double yValue(cluster_eta);
231 bool invalid =
false;
232 bool changedEt =
false;
235 for (
int i = 0; i <
entries; ++i) {
237 const HistEdge& histEdge = edges[i];
245 if (std::abs(yValue) >= histEdge.
etaMax) {
250 if (std::abs(yValue) < histEdge.
etaMin) {
264 xValue = histEdge.
etMax - 1000 ;
288 << yValue <<
" , et = " <<
et <<
" , run number = "
289 <<
runnumber <<
". Please check your input files!");
302 constexpr double epsilon = 1e-6;
303 if (currentEdge.
etaMin >= (0 - epsilon)) {
304 yValue = std::abs(yValue);
307 const TH2* currentHist =
static_cast<TH2*
>(sfObjectArray[
index].get()) ;
308 const int globalBinNumber = currentHist->FindFixBin(xValue, yValue);
309 const double scaleFactor = currentHist->GetBinContent(globalBinNumber);
310 const double scaleFactorErr = currentHist->GetBinError(globalBinNumber);
314 result.SF= scaleFactor;
315 result.Total = scaleFactorErr;
316 result.histIndex =
index;
317 result.histBinNum = globalBinNumber;
327 double statErr = -999;
328 const std::vector<HistArray>& statVector = currentmap.at(
mapkey::stat);
329 if (runnumberIndex <
static_cast<int> (statVector.size())) {
330 if (!statVector[runnumberIndex].
empty()) {
331 statErr =
static_cast<TH1*
>(statVector[runnumberIndex][
index].get())
332 ->GetBinContent(globalBinNumber);
333 result.Stat = statErr;
339 double val = statErr;
340 const std::vector<HistArray>& uncorrVector = currentmap.at(
mapkey::uncorr);
341 if (runnumberIndex <
static_cast<int>(uncorrVector.size())) {
342 if (!uncorrVector.at(runnumberIndex).empty()) {
343 const double valAdd =
344 static_cast<TH1*
>(uncorrVector[runnumberIndex][
index].get())
345 ->GetBinContent(globalBinNumber);
346 val = sqrt(val * val + valAdd * valAdd);
359 const std::vector<std::vector<HistArray>>& sysList =
361 if (sysList.size() >
static_cast<unsigned int>(
index)) {
362 if (sysList.at(
index).size() >
static_cast<unsigned int>(runnumberIndex)) {
363 const int sys_entries = sysList.at(
index).at(runnumberIndex).size();
364 for (
int sys = 0; sys < sys_entries; ++sys) {
366 static_cast<TH2*
>(sysList[
index][runnumberIndex][sys].get())
367 ->GetBinContent(globalBinNumber);
368 result.Corr[sys] = sysVal;
376 result.toys.resize(
static_cast<size_t>(
m_nToyMC));
377 const std::vector<std::vector<HistArray>>& toyMCList =
379 if (toyMCList.size() > (
unsigned int)runnumberIndex) {
380 for (
int toy = 0; toy <
m_nToyMC; ++toy) {
381 if (toyMCList[runnumberIndex][toy].
size() >
382 static_cast<unsigned int>(
index)) {
384 static_cast<TH2*
>(toyMCList[runnumberIndex][toy][
index].get())
385 ->GetBinContent(globalBinNumber);
402 const std::vector<TH1*>& corr,
405 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")! "
406 <<
"Entering function buildSingleToyMC");
407 std::vector<TH2*> tmpHists;
408 int nBins = (stat->GetNbinsX() + 2) * (stat->GetNbinsY() + 2);
410 for (
int toy = 0; toy <
m_nToyMC; toy++) {
411 tmpHists.push_back((TH2*)corr.at(0)->Clone());
415 double val = stat->GetBinContent(
bin);
418 if (uncorr !=
nullptr) {
419 double valAdd = uncorr->GetBinContent(
bin);
420 val = sqrt(val * val + valAdd * valAdd);
422 for (
int toy = 0; toy <
m_nToyMC; toy++) {
423 tmpHists.at(toy)->SetBinContent(
424 bin, (val *
m_Rndm.Gaus(0, 1)) + sf->GetBinContent(
bin));
426 tmpHists.at(toy)->SetDirectory(
nullptr);
442 const std::vector<TH1*>& corr,
447 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
448 <<
"Entering function buildSingleCombToyMC");
451 const int nBins = (stat->GetNbinsX() + 2) * (stat->GetNbinsY() + 2);
452 tmpHist = (TH2*)corr.at(0)->Clone();
454 std::vector<double>
rnd(nSys, 0);
455 for (
int s = 0; s < nSys; ++s) {
461 double val = stat->GetBinContent(
bin);
464 if (uncorr !=
nullptr) {
465 double valAdd = uncorr->GetBinContent(
bin);
466 val = sqrt(val * val + valAdd * valAdd);
468 val = val *
m_Rndm.Gaus(0, 1);
471 for (
int s = 0; s < nSys; ++s) {
472 if (corr.at(s) !=
nullptr) {
473 val +=
static_cast<TH2*
>(corr.at(s))->GetBinContent(
bin) *
rnd[s];
476 tmpHist->SetBinContent(
bin, val + sf->GetBinContent(
bin));
478 tmpHist->SetDirectory(
nullptr);
484std::vector<Root::TElectronEfficiencyCorrectionTool::HistArray>
486 const std::vector<TH1*>& sf,
487 const std::vector<TH1*>& eig,
488 const std::vector<TH1*>& stat,
489 const std::vector<TH1*>& uncorr,
490 const std::vector<std::vector<TH1*>>& corr)
493 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
494 <<
"Entering function buildToyMCTable");
497 int randomCounter(0);
498 std::vector<HistArray> tmpVec;
499 const int stat_entries = stat.size();
501 for (
int toyMC = 0; toyMC <
m_nToyMC; toyMC++) {
503 for (
int i = 0; i < stat_entries; ++i) {
504 if (!eig.empty() && !uncorr.empty()) {
505 nSys = (eig.at(i))->GetNbinsX() - 1;
521 tmpVec.emplace_back(std::move(tmpArray));
524 std::vector<std::vector<TH2*>> tmpVec2;
525 for (
int i = 0; i < stat_entries; ++i) {
526 nSys = (eig.at(i))->GetNbinsX() - 1;
533 for (
int toy = 0; toy <
m_nToyMC; toy++) {
535 for (
auto& i : tmpVec2) {
536 tmpArray.emplace_back(i.at(toy));
538 tmpVec.emplace_back(std::move(tmpArray));
548 std::map<
float, std::vector<float>>& pt_eta1)
const
557 std::vector<float> eta1;
561 for (
const auto& ikey : tmpVec) {
563 for (
const auto&
entries : ikey) {
566 TH2* h_tmp = ((TH2*)
entries.get());
567 int nbinsX = h_tmp->GetNbinsX();
568 int nbinsY = h_tmp->GetNbinsY();
570 for (
int biny = 1; biny <= nbinsY; ++biny) {
571 eta1.push_back(h_tmp->GetYaxis()->GetBinLowEdge(biny));
574 for (
int binx = 1; binx <= nbinsX; ++binx) {
575 pt_eta1[h_tmp->GetXaxis()->GetBinLowEdge(binx)] = eta1;
579 for (
auto& i : pt_eta1) {
580 nbinsTotal += i.second.size();
590 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
591 <<
"Entering function getHistograms");
593 TDirectory* origDir = gDirectory;
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) {
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())) {
628 obj = dir->ReadObj();
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:"
642 rootFile->cd(obj->GetName());
645 << dir->GetName() <<
"in file " << ifile);
649 ATH_MSG_ERROR(
"Wrong file content! Expected only Directories "
650 << gDirectory->cd());
654 gDirectory = origDir;
662 if (histListSize != edgeListSize) {
663 ATH_MSG_ERROR(
"Histo List and Edge List differ in Run period entries "
664 << histListSize <<
" vs " << edgeListSize);
667 for (
size_t i = 0; i < histListSize; ++i) {
670 "Histo List and Edge List differ for the Run period in entry" << i);
679 if (histListSize != edgeListSize) {
680 ATH_MSG_ERROR(
"Fast Histo List and Edge List differ in Run period entries "
681 << histListSize <<
" vs " << edgeListSize);
684 for (
size_t i = 0; i < histListSize; ++i) {
688 "Fast Histo List and Edge List differ for the Run period in entry"
701 const TObjArray& dirNameArray,
705 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
706 <<
"Entering funtion setupHistogramsInFolder");
709 TString myBegRunNumString =
710 ((TObjString*)dirNameArray.At(lastIdx - 1))->GetString();
711 if (myBegRunNumString.IsDigit()) {
712 runNumBegin = myBegRunNumString.Atoi();
715 TString myEndRunNumString =
716 ((TObjString*)dirNameArray.At(lastIdx))->GetString();
717 if (myEndRunNumString.IsDigit()) {
718 runNumEnd = myEndRunNumString.Atoi();
720 if (runNumBegin < 0 || runNumEnd < 0 || runNumEnd < runNumBegin) {
721 ATH_MSG_ERROR(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
722 <<
"Could NOT interpret the run number range: "
723 << runNumBegin <<
" - " << runNumEnd);
728 std::vector<std::vector<TH1*>> objsFull(
mapkey::end);
729 std::vector<std::vector<TH1*>> objsFast(
mapkey::end);
731 std::vector<std::vector<TH1*>> sysObjsFull;
732 std::vector<std::vector<TH1*>> sysObjsFast;
733 TIter nextkey(gDirectory->GetListOfKeys());
735 TObject* obj =
nullptr;
736 int seenSystematics = 0;
738 while ((key = (TKey*)nextkey())) {
739 obj = key->ReadObj();
740 if (obj->IsA()->InheritsFrom(
"TH1")) {
743 if (std::strstr(obj->GetName(),
"FullSim") !=
nullptr) {
745 static_cast<TH1*
>(obj), objsFull, sysObjsFull, seenSystematics);
746 }
else if (std::strstr(obj->GetName(),
"AtlFast") !=
nullptr) {
748 static_cast<TH1*
>(obj), objsFast, sysObjsFast, seenSystematics);
751 << obj->GetName() <<
" is full or fast simulation!");
756 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
757 <<
"Setting up histograms for Run range "
761 for (
int key : s_keys) {
762 if (!objsFull.at(key).empty()) {
763 if (0 ==
setup(objsFull.at(key),
770 <<
" for full sim!");
774 if (!objsFast.at(key).empty()) {
775 if (0 ==
setup(objsFast.at(key),
781 ATH_MSG_ERROR(
"! Could NOT setup histogram " << key <<
" for fast sim");
787 for (
unsigned int sys = 0; sys < sysObjsFast.size(); sys++) {
788 if (0 ==
setup(sysObjsFast.at(sys),
794 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
799 for (
unsigned int sys = 0; sys < sysObjsFull.size(); sys++) {
800 if (0 ==
setup(sysObjsFull.at(sys),
806 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
813 bool fullToysBooked =
815 bool fastToysBooked =
817 if (fullToysBooked || fastToysBooked) {
820 <<
" ToyMC systematics ");
824 <<
" combined ToyMC systematics ");
837 std::vector<std::vector<TH1*>>& objs,
838 std::vector<std::vector<TH1*>>& sysObjs,
839 int& seenSystematics)
842 for (
int key : s_keys) {
843 if (TString(obj->GetName())
845 objs.at(key).emplace_back(obj);
849 const TString tmpName(obj->GetName());
851 if (tmpName.EndsWith(
"_sys")) {
853 std::vector<TH1*> tmpArray;
855 tmpArray.emplace_back(
static_cast<TH1*
>(obj->Clone()));
856 sysObjs.emplace_back(tmpArray);
860 if (tmpName.Contains(
"_corr")) {
868 if (tmpName.EndsWith(
"corr0")) {
870 std::vector<TH1*> tmpArray;
872 sysObjs.emplace_back(tmpArray);
880 sysObjs.back().emplace_back(obj);
894 std::vector<std::vector<TH1*>>& objs,
895 std::vector<std::vector<TH1*>>& sysObjs,
896 std::vector<std::vector<HistArray>>& uncorrToyMCSyst)
898 bool toysBooked =
false;
926 const std::vector<TH1*>& hists,
927 std::vector<HistArray>& histList,
928 std::vector<unsigned int>& beginRunNumberList,
929 std::vector<unsigned int>& endRunNumberList,
930 const int runNumBegin,
931 const int runNumEnd)
const
934 ATH_MSG_ERROR(
"! Could NOT find histogram with name *_sf in folder");
937 TH1* tmpHist(
nullptr);
939 for (
const auto& hist : hists) {
940 tmpHist =
static_cast<TH1*
>(hist);
941 tmpHist->SetDirectory(
nullptr);
942 tmpArray.emplace_back(tmpHist);
944 histList.emplace_back(std::move(tmpArray));
946 if (!beginRunNumberList.empty()) {
947 if (runNumBegin != (
int)beginRunNumberList.back()) {
948 beginRunNumberList.push_back(runNumBegin);
951 beginRunNumberList.push_back(runNumBegin);
953 if (!endRunNumberList.empty()) {
954 if (runNumEnd != (
int)endRunNumberList.back()) {
955 endRunNumberList.push_back(runNumEnd);
958 endRunNumberList.push_back(runNumEnd);
965 const std::vector<HistArray>& sfPerPeriodHist,
966 std::vector<std::vector<HistEdge>>& sfPerPeriodEdges) {
968 for (
const auto&
vec : sfPerPeriodHist) {
969 const size_t vecSize =
vec.size();
970 std::vector<HistEdge> periodVec;
971 periodVec.reserve(vecSize);
972 for (
size_t i = 0; i < vecSize; ++i) {
973 const auto* tmpHist =
static_cast<TH2*
>(
vec[i].get());
974 const auto*
const xAxis = tmpHist->GetXaxis();
975 const auto* yAxis = tmpHist->GetYaxis();
977 histEdge.
etaMax = yAxis->GetXmax();
978 histEdge.
etaMin = yAxis->GetXmin();
979 histEdge.
etMax = xAxis->GetXmax();
980 histEdge.
etMin = xAxis->GetXmin();
982 (std::strstr(tmpHist->GetName(), LowPt_string) !=
nullptr);
984 periodVec.emplace_back(histEdge);
986 sfPerPeriodEdges.emplace_back(std::move(periodVec));
#define ATH_MSG_WARNING(x)
std::vector< size_t > vec
size_t size() const
Number of registered mappings.
static const Attributes_t empty
AsgMessaging(const std::string &name)
Constructor with a name.
bool setup(asg::AnaToolHandle< Interface > &tool, const std::string &type, const std::vector< std::string > &config, const std::string &progressFile="")
mostly useful for athena, which will otherwise re-use the previous tool
static std::vector< uint32_t > runnumber
const char * keytostring(int input)
Extra patterns decribing particle interation process.