Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 70 additions & 55 deletions PWGDQ/Core/HistogramManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <list>
#include <memory>
#include <span>
#include <string_view>
#include <utility>
#include <vector>

//_______________________________________________________________________________
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -787,51 +793,85 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
fBinsAllocated += bins;
}

//__________________________________________________________________
std::vector<HistogramManager::HistToFill> 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()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add header for string_view in HistogramManager.cxx

return &it->second;
}
auto* hList = dynamic_cast<TList*>(fMainList->FindObject(className));
if (!hList) {
return nullptr;
}
std::vector<HistToFill> 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<THnSparse*>(h) || dynamic_cast<THn*>(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";
}
Comment thread
axti98 marked this conversation as resolved.
Resolved
continue;
}
auto* h1 = dynamic_cast<TH1*>(h);
if (!h1) {
LOG(warn) << "HistogramManager::GetHistsToFill(): " << className << "/" << h->GetName() << " is a " << h->ClassName() << ", not a TH1: not filled";
continue;
Comment thread
axti98 marked this conversation as resolved.
Resolved
}
// the classes FillHistClass casts to
int dimension = h1->GetDimension();
if ((dimension == 1 && (!isProfile || dynamic_cast<TProfile*>(h))) ||
(dimension == 2 && (isProfile ? dynamic_cast<TProfile2D*>(h) != nullptr : dynamic_cast<TH2*>(h) != nullptr)) ||
(dimension == 3 && (isProfile ? dynamic_cast<TProfile3D*>(h) != nullptr : dynamic_cast<TH3*>(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";
}
Comment thread
axti98 marked this conversation as resolved.
Resolved
}
return &fHistsToFill.emplace(className, std::move(hists)).first->second;
Comment thread
axti98 marked this conversation as resolved.
Resolved
}

//__________________________________________________________________
void HistogramManager::FillHistClass(const char* className, float* values)
{
//
// fill a class of histograms
//

// get the needed histogram list
auto* hList = dynamic_cast<TList*>(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<double, 20> 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<TH1*>(h)) {
dimension = h1->GetDimension();
} else {
continue;
}

// get the various variable indices
varW = (*varIter)[2];
Expand All @@ -851,10 +891,7 @@ void HistogramManager::FillHistClass(const char* className, float* values)
switch (dimension) {
case 1:
if (isProfile) {
auto* profile = dynamic_cast<TProfile*>(h);
if (!profile) {
break;
}
auto* profile = static_cast<TProfile*>(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<int>(values[varX])), values[varY], values[varW]);
Expand All @@ -869,10 +906,7 @@ void HistogramManager::FillHistClass(const char* className, float* values)
}
}
} else {
auto* h1 = dynamic_cast<TH1*>(h);
if (!h1) {
break;
}
auto* h1 = static_cast<TH1*>(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<int>(values[varX])), values[varW]);
Expand All @@ -890,20 +924,14 @@ void HistogramManager::FillHistClass(const char* className, float* values)
break;
case 2:
if (isProfile) {
auto* profile = dynamic_cast<TProfile2D*>(h);
if (!profile) {
break;
}
auto* profile = static_cast<TProfile2D*>(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<TH2*>(h);
if (!h2) {
break;
}
auto* h2 = static_cast<TH2*>(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<int>(values[varX])), values[varY], values[varW]);
Expand All @@ -921,20 +949,14 @@ void HistogramManager::FillHistClass(const char* className, float* values)
break;
case 3:
if (isProfile) {
auto* profile = dynamic_cast<TProfile3D*>(h);
if (!profile) {
break;
}
auto* profile = static_cast<TProfile3D*>(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<TH3*>(h);
if (!h3) {
break;
}
auto* h3 = static_cast<TH3*>(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 {
Expand All @@ -948,18 +970,11 @@ void HistogramManager::FillHistClass(const char* className, float* values)
} // end switch
// end if(!isTHn)
} else {
auto* hn = static_cast<THnBase*>(h); // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast): GetHistsToFill only keeps a THn or a THnSparse
if (varW > kNothing) {
if (auto* hn = dynamic_cast<THnSparse*>(h)) {
hn->Fill(fillValues.data(), values[varW]);
} else if (auto* hn = dynamic_cast<THn*>(h)) {
hn->Fill(fillValues.data(), values[varW]);
}
hn->Fill(fillValues.data(), values[varW]);
} else {
if (auto* hn = dynamic_cast<THnSparse*>(h)) {
hn->Fill(fillValues.data());
} else if (auto* hn = dynamic_cast<THn*>(h)) {
hn->Fill(fillValues.data());
}
hn->Fill(fillValues.data());
}
} // end else
} // end loop over histograms
Expand Down
18 changes: 18 additions & 0 deletions PWGDQ/Core/HistogramManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
#include <Rtypes.h>
#include <RtypesCore.h>

#include <cstddef>
#include <cstdint>
#include <functional>
#include <list>
#include <map>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

class HistogramManager : public TNamed
Expand All @@ -48,6 +52,7 @@ class HistogramManager : public TNamed
{
delete fMainList;
fMainList = list;
fHistsToFill.clear();
}

// Create a new histogram class
Expand Down Expand Up @@ -109,6 +114,19 @@ class HistogramManager : public TNamed
std::vector<TString> fVariableNames; //! variable names
std::vector<TString> 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<int> 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<std::string_view>{}(s); }
Comment thread
axti98 marked this conversation as resolved.
Resolved
};
std::unordered_map<std::string, std::vector<HistToFill>, StringHash, std::equal_to<>> fHistsToFill; //!
std::vector<HistToFill> const* GetHistsToFill(const char* className);

void MakeAxisLabels(TAxis* ax, const char* labels);

HistogramManager& operator=(const HistogramManager& c);
Expand Down
Loading