Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
24 changes: 12 additions & 12 deletions 24 Analysis/Core/src/CorrelationContainer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "TCanvas.h"
#include "TF1.h"
#include "THn.h"
#include "Framework/HistogramRegistry.h"
#include "Framework/HistogramSpec.h"

using namespace o2::framework;

Expand Down Expand Up @@ -710,7 +710,7 @@ TH2* CorrelationContainer::getSumOfRatios(CorrelationContainer* mixed, Correlati
//
// for (Int_t x=1; x<=mixedTwoD->GetNbinsX(); x++)
// for (Int_t y=1; y<=mixedTwoD->GetNbinsY(); y++)
// mixedTwoD->SetBinContent(x, y, histMixedproj->GetBinContent(y));
// mixedTwoD->SetBinContent(x, y, histMixedproj->GetBinContent(y));

// delete histMixedproj;

Expand All @@ -725,8 +725,8 @@ TH2* CorrelationContainer::getSumOfRatios(CorrelationContainer* mixed, Correlati
}
// tracksSame->Scale(tracksMixed->Integral() / tracksSame->Integral());

// new TCanvas; tracksSame->DrawClone("SURF1");
// new TCanvas; tracksMixed->DrawClone("SURF1");
// new TCanvas; tracksSame->DrawClone("SURF1");
// new TCanvas; tracksMixed->DrawClone("SURF1");

// some code to judge the relative contribution of the different correlation functions to the overall uncertainty
Double_t sums[] = {0, 0, 0};
Expand Down Expand Up @@ -759,11 +759,11 @@ TH2* CorrelationContainer::getSumOfRatios(CorrelationContainer* mixed, Correlati
LOGF(info, "The correlation function %d %d has uncertainties %f %f %f (Ratio S/M %f)", multBin, vertexBin, errors[0], errors[1], errors[2], (errors[1] > 0) ? errors[0] / errors[1] : -1);
// code to draw contributions
/*
TH1* proj = tracksSame->ProjectionX("projx", tracksSame->GetYaxis()->FindBin(-1.59), tracksSame->GetYaxis()->FindBin(1.59));
proj->SetTitle(Form("Bin %d", vertexBin));
proj->SetLineColor(vertexBin);
proj->DrawCopy((vertexBin > 1) ? "SAME" : "");
*/
TH1* proj = tracksSame->ProjectionX("projx", tracksSame->GetYaxis()->FindBin(-1.59), tracksSame->GetYaxis()->FindBin(1.59));
proj->SetTitle(Form("Bin %d", vertexBin));
proj->SetLineColor(vertexBin);
proj->DrawCopy((vertexBin > 1) ? "SAME" : "");
*/

if (!totalTracks) {
totalTracks = (TH2*)tracksSame->Clone("totalTracks");
Expand All @@ -773,7 +773,7 @@ TH2* CorrelationContainer::getSumOfRatios(CorrelationContainer* mixed, Correlati

totalEvents += eventSameAll->GetBinContent(vertexBin, multBin);

// new TCanvas; tracksMixed->DrawCopy("SURF1");
// new TCanvas; tracksMixed->DrawCopy("SURF1");
}

delete tracksSame;
Expand Down Expand Up @@ -1676,11 +1676,11 @@ void CorrelationContainer::extendTrackingEfficiency(Bool_t verbose)
void CorrelationContainer::Scale(Double_t factor)
{
// scales all contained histograms by the given factor

for (Int_t i=0; i<4; i++)
if (mTrackHist[i])
mTrackHist[i]->Scale(factor);

mEventHist->Scale(factor);
mTrackHistEfficiency->Scale(factor);
}*/
Expand Down
1 change: 1 addition & 0 deletions 1 Framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ o2_add_library(Framework
src/WSDriverClient.cxx
src/runDataProcessing.cxx
src/ExternalFairMQDeviceProxy.cxx
src/HistogramSpec.cxx
src/HistogramRegistry.cxx
src/StepTHn.cxx
src/Base64.cxx
Expand Down
174 changes: 8 additions & 166 deletions 174 Framework/Core/include/Framework/HistogramRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,177 +20,19 @@
#include "Framework/OutputSpec.h"
#include "Framework/SerializationMethods.h"
#include "Framework/TableBuilder.h"
#include "Framework/RuntimeError.h"

#define HIST(name) CONST_STR(name)

namespace o2::framework
{

//**************************************************************************************************
/**
* Static helper class to generate histograms from the specifications.
* Also provides functions to obtain pointer to the created histogram casted to the correct alternative of the std::variant HistPtr that is used in HistogramRegistry.
*/
//**************************************************************************************************
struct HistFactory {
#include <TDataMember.h>
#include <TDataType.h>

// create histogram of type T with the axes defined in HistogramSpec
template <typename T>
static std::unique_ptr<T> createHist(const HistogramSpec& histSpec)
{
constexpr std::size_t MAX_DIM{10};
const std::size_t nAxes{histSpec.config.axes.size()};
if (nAxes == 0 || nAxes > MAX_DIM) {
LOGF(FATAL, "The histogram specification contains no (or too many) axes.");
return nullptr;
}
#include <deque>

int nBins[MAX_DIM]{0};
double lowerBounds[MAX_DIM]{0.};
double upperBounds[MAX_DIM]{0.};

// first figure out number of bins and dimensions
for (std::size_t i = 0; i < nAxes; i++) {
nBins[i] = (histSpec.config.axes[i].nBins) ? *histSpec.config.axes[i].nBins : histSpec.config.axes[i].binEdges.size() - 1;
lowerBounds[i] = histSpec.config.axes[i].binEdges.front();
upperBounds[i] = histSpec.config.axes[i].binEdges.back();
}

// create histogram
std::unique_ptr<T> hist{generateHist<T>(histSpec.name, histSpec.title, nAxes, nBins, lowerBounds, upperBounds, histSpec.config.nSteps)};
if (!hist) {
LOGF(FATAL, "The number of dimensions specified for histogram %s does not match the type.", histSpec.name);
return nullptr;
}

// set axis properties
for (std::size_t i = 0; i < nAxes; i++) {
TAxis* axis{getAxis(i, hist.get())};
if (axis) {
if (histSpec.config.axes[i].title) {
axis->SetTitle((*histSpec.config.axes[i].title).data());
}
class TList;

// this helps to have axes not only called 0,1,2... in ndim histos
if constexpr (std::is_base_of_v<THnBase, T>) {
if (histSpec.config.axes[i].name) {
axis->SetName((std::string(axis->GetName()) + "-" + *histSpec.config.axes[i].name).data());
}
}

// move the bin edges in case a variable binning was requested
if (!histSpec.config.axes[i].nBins) {
if (!std::is_sorted(std::begin(histSpec.config.axes[i].binEdges), std::end(histSpec.config.axes[i].binEdges))) {
LOGF(FATAL, "The bin edges in histogram %s are not in increasing order!", histSpec.name);
return nullptr;
}
axis->Set(nBins[i], histSpec.config.axes[i].binEdges.data());
}
}
}
if (histSpec.callSumw2) {
hist->Sumw2();
}
return hist;
}

// create histogram and return it casted to the correct alternative held in HistPtr variant
template <typename T>
static HistPtr createHistVariant(const HistogramSpec& histSpec)
{
if (auto hist = castToVariant(createHist<T>(histSpec))) {
return *hist;
} else {
throw runtime_error("Histogram was not created properly.");
}
}

// runtime version of the above
static HistPtr createHistVariant(const HistogramSpec& histSpec)
{
if (histSpec.config.type == HistType::kUndefinedHist) {
throw runtime_error("Histogram type was not specified.");
} else {
return HistogramCreationCallbacks.at(histSpec.config.type)(histSpec);
}
}

// helper function to get the axis via index for any type of root histogram
template <typename T>
static TAxis* getAxis(const int i, T* hist)
{
if constexpr (std::is_base_of_v<THnBase, T> || std::is_base_of_v<StepTHn, T>) {
return hist->GetAxis(i);
} else {
if (i == 0) {
return hist->GetXaxis();
} else if (i == 1) {
return hist->GetYaxis();
} else if (i == 2) {
return hist->GetZaxis();
} else {
return nullptr;
}
}
}

private:
static const std::map<HistType, std::function<HistPtr(const HistogramSpec&)>> HistogramCreationCallbacks;

// helper function to generate the actual histograms
template <typename T>
static T* generateHist(const std::string& name, const std::string& title, const std::size_t nDim,
const int nBins[], const double lowerBounds[], const double upperBounds[], const int nSteps = 1)
{
if constexpr (std::is_base_of_v<StepTHn, T>) {
return new T(name.data(), title.data(), nSteps, nDim, nBins, lowerBounds, upperBounds);
} else if constexpr (std::is_base_of_v<THnBase, T>) {
return new T(name.data(), title.data(), nDim, nBins, lowerBounds, upperBounds);
} else if constexpr (std::is_base_of_v<TH3, T>) {
return (nDim != 3) ? nullptr
: new T(name.data(), title.data(), nBins[0], lowerBounds[0],
upperBounds[0], nBins[1], lowerBounds[1], upperBounds[1],
nBins[2], lowerBounds[2], upperBounds[2]);
} else if constexpr (std::is_base_of_v<TH2, T>) {
return (nDim != 2) ? nullptr
: new T(name.data(), title.data(), nBins[0], lowerBounds[0],
upperBounds[0], nBins[1], lowerBounds[1], upperBounds[1]);
} else if constexpr (std::is_base_of_v<TH1, T>) {
return (nDim != 1)
? nullptr
: new T(name.data(), title.data(), nBins[0], lowerBounds[0], upperBounds[0]);
}
return nullptr;
}

// helper function to cast the actual histogram type (e.g. TH2F) to the correct interface type (e.g. TH2) that is stored in the HistPtr variant
template <typename T>
static std::optional<HistPtr> castToVariant(std::shared_ptr<TObject> obj)
{
if (obj->InheritsFrom(T::Class())) {
return std::static_pointer_cast<T>(obj);
}
return std::nullopt;
}

template <typename T, typename Next, typename... Rest>
static std::optional<HistPtr> castToVariant(std::shared_ptr<TObject> obj)
{
if (auto hist = castToVariant<T>(obj)) {
return hist;
}
return castToVariant<Next, Rest...>(obj);
}
#define HIST(name) CONST_STR(name)

static std::optional<HistPtr> castToVariant(std::shared_ptr<TObject> obj)
{
if (obj) {
// TProfile3D is TH3, TProfile2D is TH2, TH3 is TH1, TH2 is TH1, TProfile is TH1
return castToVariant<THn, THnSparse, TProfile3D, TH3, TProfile2D, TH2, TProfile, TH1, StepTHn>(obj);
}
return std::nullopt;
}
};
namespace o2::framework
{

//**************************************************************************************************
/**
Expand Down
94 changes: 83 additions & 11 deletions 94 Framework/Core/include/Framework/HistogramSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

#include <string>
#include <variant>
#include <deque>
#include <optional>

#include "Framework/StringHelpers.h"
#include "Framework/Configurable.h"

#include "Framework/StepTHn.h"
#include <TH1.h>
#include <TH2.h>
#include <TH3.h>
Expand All @@ -24,14 +27,6 @@
#include <TProfile.h>
#include <TProfile2D.h>
#include <TProfile3D.h>
#include <TList.h>
#include <TDataMember.h>
#include <TDataType.h>

#include "Framework/StepTHn.h"
#include "Framework/Configurable.h"
#include "Framework/StringHelpers.h"
#include "Framework/RuntimeError.h"

namespace o2::framework
{
Expand Down Expand Up @@ -80,8 +75,8 @@ using HistPtr = std::variant<std::shared_ptr<THn>, std::shared_ptr<THnSparse>, s
* Specification of an Axis.
*/
//**************************************************************************************************
// Flag to mark variable bin size in configurable bin edges
constexpr int VARIABLE_WIDTH = 0;
// flag to mark variable bin size in configurable bin edges
constexpr double VARIABLE_WIDTH = 0.;

struct AxisSpec {
AxisSpec(std::vector<double> binEdges_, std::optional<std::string> title_ = std::nullopt, std::optional<std::string> name_ = std::nullopt)
Expand Down Expand Up @@ -201,5 +196,82 @@ struct HistogramSpec {
bool callSumw2{}; // wether or not hist needs heavy error structure produced by Sumw2()
};

//**************************************************************************************************
/**
* Static helper class to generate histograms from the specifications.
* Also provides functions to obtain pointer to the created histogram casted to the correct alternative of the std::variant HistPtr that is used in HistogramRegistry.
*/
//**************************************************************************************************
struct HistFactory {

// create histogram of type T with the axes defined in HistogramSpec
template <typename T>
static std::unique_ptr<T> createHist(const HistogramSpec& histSpec);

// create histogram and return it casted to the correct alternative held in HistPtr variant
template <typename T>
static HistPtr createHistVariant(const HistogramSpec& histSpec);

// runtime version of the above
static HistPtr createHistVariant(const HistogramSpec& histSpec);

// helper function to get the axis via index for any type of root histogram
template <typename T>
static TAxis* getAxis(const int i, T* hist);

private:
static const std::map<HistType, std::function<HistPtr(const HistogramSpec&)>> HistogramCreationCallbacks;

// helper function to generate the actual histograms
template <typename T>
static T* generateHist(const std::string& name, const std::string& title, const std::size_t nDim,
const int nBins[], const double lowerBounds[], const double upperBounds[], const int nSteps = 1);

// helper function to cast the actual histogram type (e.g. TH2F) to the correct interface type (e.g. TH2) that is stored in the HistPtr variant
template <typename T>
static std::optional<HistPtr> castToVariant(std::shared_ptr<TObject> obj);

template <typename T, typename Next, typename... Rest>
static std::optional<HistPtr> castToVariant(std::shared_ptr<TObject> obj);

static std::optional<HistPtr> castToVariant(std::shared_ptr<TObject> obj);
};

#define DECLAREEXT(HType) \
extern template std::unique_ptr<HType> HistFactory::createHist<HType>(const HistogramSpec& histSpec);
DECLAREEXT(TH1D);
DECLAREEXT(TH1F);
DECLAREEXT(TH1I);
DECLAREEXT(TH1C);
DECLAREEXT(TH1S);
DECLAREEXT(TH2D);
DECLAREEXT(TH2F);
DECLAREEXT(TH2I);
DECLAREEXT(TH2C);
DECLAREEXT(TH2S);
DECLAREEXT(TH3D);
DECLAREEXT(TH3F);
DECLAREEXT(TH3I);
DECLAREEXT(TH3C);
DECLAREEXT(TH3S);
DECLAREEXT(THnD);
DECLAREEXT(THnF);
DECLAREEXT(THnI);
DECLAREEXT(THnC);
DECLAREEXT(THnS);
DECLAREEXT(THnL);
DECLAREEXT(THnSparseD);
DECLAREEXT(THnSparseF);
DECLAREEXT(THnSparseI);
DECLAREEXT(THnSparseC);
DECLAREEXT(THnSparseS);
DECLAREEXT(THnSparseL);
DECLAREEXT(TProfile);
DECLAREEXT(TProfile2D);
DECLAREEXT(TProfile3D);
DECLAREEXT(StepTHnF);
DECLAREEXT(StepTHnD)
#undef DECLAREEXT

} // namespace o2::framework
#endif // FRAMEWORK_HISTOGRAMSPEC_H_
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.