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 if (!eig.empty() and !uncorr.empty())[[
likely]]{
526 for (
int i = 0; i < stat_entries; ++i) {
527 nSys = (eig.at(i))->GetNbinsX() - 1;
535 for (
int toy = 0; toy <
m_nToyMC; toy++) {
537 for (
auto& i : tmpVec2) {
538 tmpArray.emplace_back(i.at(toy));
540 tmpVec.emplace_back(std::move(tmpArray));
550 std::map<
float, std::vector<float>>& pt_eta1)
const
559 std::vector<float> eta1;
563 for (
const auto& ikey : tmpVec) {
565 for (
const auto&
entries : ikey) {
568 TH2* h_tmp = ((TH2*)
entries.get());
569 int nbinsX = h_tmp->GetNbinsX();
570 int nbinsY = h_tmp->GetNbinsY();
572 for (
int biny = 1; biny <= nbinsY; ++biny) {
573 eta1.push_back(h_tmp->GetYaxis()->GetBinLowEdge(biny));
576 for (
int binx = 1; binx <= nbinsX; ++binx) {
577 pt_eta1[h_tmp->GetXaxis()->GetBinLowEdge(binx)] = eta1;
581 for (
auto& i : pt_eta1) {
582 nbinsTotal += i.second.size();
592 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
593 <<
"Entering function getHistograms");
595 TDirectory* origDir = gDirectory;
603 std::unique_ptr<TObjArray> myStringList(firstFileNameAndPath.Tokenize(
"/"));
604 int lastIdx = myStringList->GetLast();
605 TString fileName = ((TObjString*)myStringList->At(lastIdx))->GetString();
606 std::unique_ptr<TObjArray> myFileNameTokensList(fileName.Tokenize(
"."));
608 if (myFileNameTokensList->GetLast() < 3) {
618 const std::unique_ptr<char[]> fname(gSystem->ExpandPathName(ifile.c_str()));
619 std::unique_ptr<TFile> rootFile(TFile::Open(fname.get(),
"READ"));
626 TIter nextdir(rootFile->GetListOfKeys());
628 TObject* obj =
nullptr;
629 while ((dir = (TKey*)nextdir())) {
630 obj = dir->ReadObj();
631 if (obj->IsA()->InheritsFrom(
"TDirectory")) {
633 std::unique_ptr<TObjArray> dirNameArray(
634 TString(obj->GetName()).Tokenize(
"_"));
637 int lastIdx = dirNameArray->GetLast();
640 "The folder name seems to have the wrong format! Directory name:"
644 rootFile->cd(obj->GetName());
647 << dir->GetName() <<
"in file " << ifile);
651 ATH_MSG_ERROR(
"Wrong file content! Expected only Directories "
652 << gDirectory->cd());
656 gDirectory = origDir;
664 if (histListSize != edgeListSize) {
665 ATH_MSG_ERROR(
"Histo List and Edge List differ in Run period entries "
666 << histListSize <<
" vs " << edgeListSize);
669 for (
size_t i = 0; i < histListSize; ++i) {
672 "Histo List and Edge List differ for the Run period in entry" << i);
681 if (histListSize != edgeListSize) {
682 ATH_MSG_ERROR(
"Fast Histo List and Edge List differ in Run period entries "
683 << histListSize <<
" vs " << edgeListSize);
686 for (
size_t i = 0; i < histListSize; ++i) {
690 "Fast Histo List and Edge List differ for the Run period in entry"
703 const TObjArray& dirNameArray,
707 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
708 <<
"Entering funtion setupHistogramsInFolder");
711 TString myBegRunNumString =
712 ((TObjString*)dirNameArray.At(lastIdx - 1))->GetString();
713 if (myBegRunNumString.IsDigit()) {
714 runNumBegin = myBegRunNumString.Atoi();
717 TString myEndRunNumString =
718 ((TObjString*)dirNameArray.At(lastIdx))->GetString();
719 if (myEndRunNumString.IsDigit()) {
720 runNumEnd = myEndRunNumString.Atoi();
722 if (runNumBegin < 0 || runNumEnd < 0 || runNumEnd < runNumBegin) {
723 ATH_MSG_ERROR(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
724 <<
"Could NOT interpret the run number range: "
725 << runNumBegin <<
" - " << runNumEnd);
730 std::vector<std::vector<TH1*>> objsFull(
mapkey::end);
731 std::vector<std::vector<TH1*>> objsFast(
mapkey::end);
733 std::vector<std::vector<TH1*>> sysObjsFull;
734 std::vector<std::vector<TH1*>> sysObjsFast;
735 TIter nextkey(gDirectory->GetListOfKeys());
737 TObject* obj =
nullptr;
738 int seenSystematics = 0;
740 while ((key = (TKey*)nextkey())) {
741 obj = key->ReadObj();
742 if (obj->IsA()->InheritsFrom(
"TH1")) {
745 if (std::strstr(obj->GetName(),
"FullSim") !=
nullptr) {
747 static_cast<TH1*
>(obj), objsFull, sysObjsFull, seenSystematics);
748 }
else if (std::strstr(obj->GetName(),
"AtlFast") !=
nullptr) {
750 static_cast<TH1*
>(obj), objsFast, sysObjsFast, seenSystematics);
753 << obj->GetName() <<
" is full or fast simulation!");
758 ATH_MSG_DEBUG(
" (file: " << __FILE__ <<
", line: " << __LINE__ <<
")\n"
759 <<
"Setting up histograms for Run range "
763 for (
int key : s_keys) {
764 if (!objsFull.at(key).empty()) {
765 if (0 ==
setup(objsFull.at(key),
772 <<
" for full sim!");
776 if (!objsFast.at(key).empty()) {
777 if (0 ==
setup(objsFast.at(key),
783 ATH_MSG_ERROR(
"! Could NOT setup histogram " << key <<
" for fast sim");
789 for (
unsigned int sys = 0; sys < sysObjsFast.size(); sys++) {
790 if (0 ==
setup(sysObjsFast.at(sys),
796 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
801 for (
unsigned int sys = 0; sys < sysObjsFull.size(); sys++) {
802 if (0 ==
setup(sysObjsFull.at(sys),
808 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
815 bool fullToysBooked =
817 bool fastToysBooked =
819 if (fullToysBooked || fastToysBooked) {
822 <<
" ToyMC systematics ");
826 <<
" combined ToyMC systematics ");
839 std::vector<std::vector<TH1*>>& objs,
840 std::vector<std::vector<TH1*>>& sysObjs,
841 int& seenSystematics)
844 for (
int key : s_keys) {
845 if (TString(obj->GetName())
847 objs.at(key).emplace_back(obj);
851 const TString tmpName(obj->GetName());
853 if (tmpName.EndsWith(
"_sys")) {
855 std::vector<TH1*> tmpArray;
857 tmpArray.emplace_back(
static_cast<TH1*
>(obj->Clone()));
858 sysObjs.emplace_back(tmpArray);
862 if (tmpName.Contains(
"_corr")) {
870 if (tmpName.EndsWith(
"corr0")) {
872 std::vector<TH1*> tmpArray;
874 sysObjs.emplace_back(tmpArray);
882 sysObjs.back().emplace_back(obj);
896 std::vector<std::vector<TH1*>>& objs,
897 std::vector<std::vector<TH1*>>& sysObjs,
898 std::vector<std::vector<HistArray>>& uncorrToyMCSyst)
900 bool toysBooked =
false;
928 const std::vector<TH1*>& hists,
929 std::vector<HistArray>& histList,
930 std::vector<unsigned int>& beginRunNumberList,
931 std::vector<unsigned int>& endRunNumberList,
932 const int runNumBegin,
933 const int runNumEnd)
const
936 ATH_MSG_ERROR(
"! Could NOT find histogram with name *_sf in folder");
939 TH1* tmpHist(
nullptr);
941 for (
const auto& hist : hists) {
942 tmpHist =
static_cast<TH1*
>(hist);
943 tmpHist->SetDirectory(
nullptr);
944 tmpArray.emplace_back(tmpHist);
946 histList.emplace_back(std::move(tmpArray));
948 if (!beginRunNumberList.empty()) {
949 if (runNumBegin != (
int)beginRunNumberList.back()) {
950 beginRunNumberList.push_back(runNumBegin);
953 beginRunNumberList.push_back(runNumBegin);
955 if (!endRunNumberList.empty()) {
956 if (runNumEnd != (
int)endRunNumberList.back()) {
957 endRunNumberList.push_back(runNumEnd);
960 endRunNumberList.push_back(runNumEnd);
967 const std::vector<HistArray>& sfPerPeriodHist,
968 std::vector<std::vector<HistEdge>>& sfPerPeriodEdges) {
970 for (
const auto&
vec : sfPerPeriodHist) {
971 const size_t vecSize =
vec.size();
972 std::vector<HistEdge> periodVec;
973 periodVec.reserve(vecSize);
974 for (
size_t i = 0; i < vecSize; ++i) {
975 const auto* tmpHist =
static_cast<TH2*
>(
vec[i].get());
976 const auto*
const xAxis = tmpHist->GetXaxis();
977 const auto* yAxis = tmpHist->GetYaxis();
979 histEdge.
etaMax = yAxis->GetXmax();
980 histEdge.
etaMin = yAxis->GetXmin();
981 histEdge.
etMax = xAxis->GetXmax();
982 histEdge.
etMin = xAxis->GetXmin();
984 (std::strstr(tmpHist->GetName(), LowPt_string) !=
nullptr);
986 periodVec.emplace_back(histEdge);
988 sfPerPeriodEdges.emplace_back(std::move(periodVec));
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#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.