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 = tmd->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];
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 = 1e-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);
345 val = sqrt(val * val + valAdd * valAdd);
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);
367 result.Corr[sys] = sysVal;
376 const std::vector<std::vector<HistArray>>& toyMCList =
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);
409 for (
int toy = 0; toy <
m_nToyMC; toy++) {
410 tmpHists.push_back((TH2*)corr.at(0)->Clone());
414 double val = stat->GetBinContent(
bin);
417 if (uncorr !=
nullptr) {
418 double valAdd = uncorr->GetBinContent(
bin);
419 val = sqrt(val * val + valAdd * valAdd);
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) {
460 double val = stat->GetBinContent(
bin);
463 if (uncorr !=
nullptr) {
464 double valAdd = uncorr->GetBinContent(
bin);
465 val = sqrt(val * val + valAdd * valAdd);
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];
475 tmpHist->SetBinContent(
bin, val + sf->GetBinContent(
bin));
477 tmpHist->SetDirectory(
nullptr);
483std::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) {
503 if (!eig.empty() && !uncorr.empty()) {
504 nSys = (eig.at(i))->GetNbinsX() - 1;
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 = (eig.at(i))->GetNbinsX() - 1;
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
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");
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;
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;
696 TIter nextkey(gDirectory->GetListOfKeys());
698 TObject* obj =
nullptr;
699 int seenSystematics = 0;
701 while ((key = (TKey*)nextkey())) {
702 obj = key->ReadObj();
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()) {
726 if (0 ==
setup(objsFull.at(key),
733 <<
" for full sim!");
737 if (!objsFast.at(key).empty()) {
738 if (0 ==
setup(objsFast.at(key),
744 ATH_MSG_ERROR(
"! Could NOT setup histogram " << key <<
" for fast sim");
750 for (
unsigned int sys = 0; sys < sysObjsFast.size(); sys++) {
751 if (0 ==
setup(sysObjsFast.at(sys),
757 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
762 for (
unsigned int sys = 0; sys < sysObjsFull.size(); sys++) {
763 if (0 ==
setup(sysObjsFull.at(sys),
769 ATH_MSG_ERROR(
"! Could NOT setup systematic histograms for fast sim");
779 bool fullToysBooked =
781 bool fastToysBooked =
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())
811 objs.at(key).emplace_back(obj);
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);
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;
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");
903 TH1* tmpHist(
nullptr);
905 for (
const auto& hist : hists) {
906 tmpHist =
static_cast<TH1*
>(hist);
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));
#define ATH_MSG_WARNING(x)
std::vector< size_t > vec
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.