diff --git a/PWGDQ/Core/HistogramManager.cxx b/PWGDQ/Core/HistogramManager.cxx index 74fa3d21b8c..d2623126911 100644 --- a/PWGDQ/Core/HistogramManager.cxx +++ b/PWGDQ/Core/HistogramManager.cxx @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include //_______________________________________________________________________________ @@ -191,6 +193,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co std::list varList = fVariablesMap[histClass]; varList.push_back(varVector); fVariablesMap[histClass] = varList; + fHistsToFill.clear(); // create and configure histograms according to required options TH1* h = nullptr; @@ -412,6 +415,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co std::list varList = fVariablesMap[histClass]; varList.push_back(varVector); fVariablesMap[histClass] = varList; + fHistsToFill.clear(); TH1* h = nullptr; switch (dimension) { @@ -608,6 +612,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co std::list varList = fVariablesMap[histClass]; varList.push_back(varVector); fVariablesMap[histClass] = varList; + fHistsToFill.clear(); uint32_t nbins = 1; THnBase* h = nullptr; @@ -710,6 +715,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co std::list varList = fVariablesMap[histClass]; varList.push_back(varVector); fVariablesMap[histClass] = varList; + fHistsToFill.clear(); // get the min and max for each axis auto* xmin = new double[nDimensions]; @@ -787,6 +793,56 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co fBinsAllocated += bins; } +//__________________________________________________________________ +std::vector const* HistogramManager::GetHistsToFill(const char* className) +{ + // + // the histograms of a class, cast and paired with their variables on the first fill of the class + // + if (auto it = fHistsToFill.find(std::string_view(className)); it != fHistsToFill.end()) { + return &it->second; + } + auto* hList = dynamic_cast(fMainList->FindObject(className)); + if (!hList) { + return nullptr; + } + std::vector hists; + // the histograms and the std::list of their variables are in the same order + TIter next(hList); + for (auto const& vars : fVariablesMap[className]) { + TObject* h = next(); + if (!h) { + LOG(warn) << "HistogramManager::GetHistsToFill(): class " << className << " has fewer histograms than variable sets, the others are not filled"; + break; + } + bool isProfile = (vars[0] == 1); + if (vars[1] > 0) { + if (dynamic_cast(h) || dynamic_cast(h)) { + hists.push_back({h, &vars, vars[1]}); + } else { + LOG(warn) << "HistogramManager::GetHistsToFill(): " << className << "/" << h->GetName() << " is a " << h->ClassName() << ", not a THn or THnSparse: not filled"; + } + continue; + } + auto* h1 = dynamic_cast(h); + if (!h1) { + LOG(warn) << "HistogramManager::GetHistsToFill(): " << className << "/" << h->GetName() << " is a " << h->ClassName() << ", not a TH1: not filled"; + continue; + } + // the classes FillHistClass casts to + int dimension = h1->GetDimension(); + if ((dimension == 1 && (!isProfile || dynamic_cast(h))) || + (dimension == 2 && (isProfile ? dynamic_cast(h) != nullptr : dynamic_cast(h) != nullptr)) || + (dimension == 3 && (isProfile ? dynamic_cast(h) != nullptr : dynamic_cast(h) != nullptr))) { + hists.push_back({h, &vars, dimension}); + } else { + LOG(warn) << "HistogramManager::GetHistsToFill(): " << className << "/" << h->GetName() << " is a " << h->ClassName() << " of dimension " << dimension + << (isProfile ? ", booked as a profile" : ", booked as a histogram") << ": not filled"; + } + } + return &fHistsToFill.emplace(className, std::move(hists)).first->second; +} + //__________________________________________________________________ void HistogramManager::FillHistClass(const char* className, float* values) { @@ -794,44 +850,28 @@ void HistogramManager::FillHistClass(const char* className, float* values) // fill a class of histograms // - // get the needed histogram list - auto* hList = dynamic_cast(fMainList->FindObject(className)); - if (!hList) { + // get the needed histograms, with the identifiers of the variables to fill them with + auto const* hists = GetHistsToFill(className); + if (!hists) { // TODO: add some meaningfull error message /*LOG(warn) << "HistogramManager::FillHistClass(): Histogram list " << className << " not found!"; LOG(warn) << " Histogram list not filled" << endl; */ return; } - // get the corresponding std::list containng identifiers to the needed variables to be filled - auto const& varList = fVariablesMap[className]; - - TIter next(hList); - - TObject* h = nullptr; bool isProfile = false; bool isTHn = false; - int dimension = 0; bool isFillLabelx = kFALSE; // TODO: At the moment, maximum 20 dimensions are foreseen for the THn histograms. We should make this more dynamic // But maybe its better to have it like to avoid dynamically allocating this array in the histogram loop std::array fillValues{}; int varX = -1, varY = -1, varZ = -1, varT = -1, varW = -1; - // loop over the histogram and std::list - // NOTE: these two should contain the same number of elements and be synchronized, otherwise its a mess - for (auto varIter = varList.begin(); varIter != varList.end(); varIter++) { - h = next(); // get the histogram + // loop over the histograms + for (auto const& [h, varIter, dimension] : *hists) { // decode information from the vector of indices isProfile = ((*varIter)[0] == 1); isTHn = ((*varIter)[1] > 0); - if (isTHn) { - dimension = (*varIter)[1]; - } else if (auto* h1 = dynamic_cast(h)) { - dimension = h1->GetDimension(); - } else { - continue; - } // get the various variable indices varW = (*varIter)[2]; @@ -851,10 +891,7 @@ void HistogramManager::FillHistClass(const char* className, float* values) switch (dimension) { case 1: if (isProfile) { - auto* profile = dynamic_cast(h); - if (!profile) { - break; - } + auto* profile = static_cast(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a histogram of this class if (varW > kNothing) { if (isFillLabelx) { profile->Fill(Form("%d", static_cast(values[varX])), values[varY], values[varW]); @@ -869,10 +906,7 @@ void HistogramManager::FillHistClass(const char* className, float* values) } } } else { - auto* h1 = dynamic_cast(h); - if (!h1) { - break; - } + auto* h1 = static_cast(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a histogram of this class if (varW > kNothing) { if (isFillLabelx) { h1->Fill(Form("%d", static_cast(values[varX])), values[varW]); @@ -890,20 +924,14 @@ void HistogramManager::FillHistClass(const char* className, float* values) break; case 2: if (isProfile) { - auto* profile = dynamic_cast(h); - if (!profile) { - break; - } + auto* profile = static_cast(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a histogram of this class if (varW > kNothing) { profile->Fill(values[varX], values[varY], values[varZ], values[varW]); } else { profile->Fill(values[varX], values[varY], values[varZ]); } } else { - auto* h2 = dynamic_cast(h); - if (!h2) { - break; - } + auto* h2 = static_cast(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a histogram of this class if (varW > kNothing) { if (isFillLabelx) { h2->Fill(Form("%d", static_cast(values[varX])), values[varY], values[varW]); @@ -921,20 +949,14 @@ void HistogramManager::FillHistClass(const char* className, float* values) break; case 3: if (isProfile) { - auto* profile = dynamic_cast(h); - if (!profile) { - break; - } + auto* profile = static_cast(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a histogram of this class if (varW > kNothing) { profile->Fill(values[varX], values[varY], values[varZ], values[varT], values[varW]); } else { profile->Fill(values[varX], values[varY], values[varZ], values[varT]); } } else { - auto* h3 = dynamic_cast(h); - if (!h3) { - break; - } + auto* h3 = static_cast(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a histogram of this class if (varW > kNothing) { h3->Fill(values[varX], values[varY], values[varZ], values[varW]); } else { @@ -948,18 +970,11 @@ void HistogramManager::FillHistClass(const char* className, float* values) } // end switch // end if(!isTHn) } else { + auto* hn = static_cast(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a THn or a THnSparse if (varW > kNothing) { - if (auto* hn = dynamic_cast(h)) { - hn->Fill(fillValues.data(), values[varW]); - } else if (auto* hn = dynamic_cast(h)) { - hn->Fill(fillValues.data(), values[varW]); - } + hn->Fill(fillValues.data(), values[varW]); } else { - if (auto* hn = dynamic_cast(h)) { - hn->Fill(fillValues.data()); - } else if (auto* hn = dynamic_cast(h)) { - hn->Fill(fillValues.data()); - } + hn->Fill(fillValues.data()); } } // end else } // end loop over histograms diff --git a/PWGDQ/Core/HistogramManager.h b/PWGDQ/Core/HistogramManager.h index 2b279bc0ec4..a527bd3d411 100644 --- a/PWGDQ/Core/HistogramManager.h +++ b/PWGDQ/Core/HistogramManager.h @@ -26,10 +26,14 @@ #include #include +#include #include +#include #include #include #include +#include +#include #include class HistogramManager : public TNamed @@ -48,6 +52,7 @@ class HistogramManager : public TNamed { delete fMainList; fMainList = list; + fHistsToFill.clear(); } // Create a new histogram class @@ -109,6 +114,19 @@ class HistogramManager : public TNamed std::vector fVariableNames; //! variable names std::vector fVariableUnits; //! variable units + // the histograms of each class as FillHistClass fills them, made on the first fill of the class + struct HistToFill { + TObject* hist; // of the class FillHistClass casts it to + std::vector const* vars; // its element of fVariablesMap + int dimension; + }; + struct StringHash { + using is_transparent = void; + size_t operator()(std::string_view s) const { return std::hash{}(s); } + }; + std::unordered_map, StringHash, std::equal_to<>> fHistsToFill; //! + std::vector const* GetHistsToFill(const char* className); + void MakeAxisLabels(TAxis* ax, const char* labels); HistogramManager& operator=(const HistogramManager& c);