12#include <TDirectory.h>
22#include <TPaveStats.h>
31HistoManager::HistoManager(
const std::string& dirname,
const std::string& fname,
bool load,
const std::string& prefix) : mDirName(dirname)
34 if (
load && !mDefName.empty()) {
35 int nh = this->
load(fname, dirname);
36 LOGP(info,
"HistoManager::load was requested: got {} histos from {}/{}", nh, fname, dirname);
37 if (!prefix.empty()) {
47 for (
int i = 0;
i < GetLast() + 1; ++
i) {
52 if (
auto* histo =
dynamic_cast<TH1*
>(obj)) {
53 histo->SetDirectory(
nullptr);
56 hm->mNHistos = mNHistos;
57 hm->setFileName(mDefName);
58 hm->setDirName(mDirName);
70 AddAtAndExpand(histo, at);
71 histo->SetDirectory(
nullptr);
72 histo->SetUniqueID(at + 1);
78 return id <= GetLast() ? dynamic_cast<TGraph*>(UncheckedAt(
id)) :
nullptr;
83 return id <= GetLast() ? dynamic_cast<TH1*>(UncheckedAt(
id)) :
nullptr;
88 return dynamic_cast<TH1*
>(FindObject(
name.c_str()));
93 return dynamic_cast<TH1F*
>(UncheckedAt(
id));
98 return dynamic_cast<TH2F*
>(UncheckedAt(
id));
103 return dynamic_cast<TProfile*
>(UncheckedAt(
id));
114 AddAtAndExpand(gr, at);
115 gr->SetUniqueID(at + 1);
121 TObjArray::Compress();
122 for (
int i = 0;
i < GetLast() + 1; ++
i) {
124 histo->SetUniqueID(
i + 1);
135 bool localFile = kFALSE;
136 TFile* lfile =
nullptr;
137 const char* dirName =
nullptr;
141 auto* tmpF =
static_cast<TFile*
>(gROOT->GetListOfFiles()->FindObject(mDefName.c_str()));
142 if (tmpF && tmpF->IsOpen()) {
143 TString opt = tmpF->GetOption();
145 if (!opt.Contains(
"read")) {
152 TString pwd = gDirectory->GetPath();
154 std::string originalName = mDefName;
155 if (mDefName.empty() || mDefName[0] ==
' ') {
156 mDefName =
"histoman";
158 TString rootName = mDefName.c_str();
159 if (!rootName.Contains(
".root")) {
162 lfile = TFile::Open(mDefName.c_str(),
"UPDATE");
163 mDefName = originalName;
168 dirName = mDirName.c_str();
169 if (dirName && dirName[0] && dirName[0] !=
' ') {
170 if (!lfile->Get(dirName)) {
171 lfile->mkdir(dirName);
175 LOGP(info,
"Writing histograms to: {}/{}", lfile->GetPath(), dirName);
177 for (
int i = 0;
i < GetLast() + 1; ++
i) {
182 auto* histo =
dynamic_cast<TH1*
>(obj);
183 TDirectory* dr =
nullptr;
185 dr = histo->GetDirectory();
186 histo->SetDirectory(
nullptr);
188 obj->Write(
nullptr, TObject::kOverwrite);
190 histo->SetDirectory(dr);
198 auto* oldDir =
static_cast<TDirectory*
>(gROOT->GetListOfFiles()->FindObject(pwd.Data()));
206 int nent = GetLast() + 1;
207 for (
int i = 0;
i < nent; ++
i) {
220 int nent = GetLast() + 1;
221 for (
int i = 0;
i < nent; ++
i) {
234 int nent = GetLast() + 1;
235 for (
int i = 0;
i < nent; ++
i) {
240 LOGP(info,
"At position #{}",
i);
243 LOGP(info,
"Total number of defined histograms: %d", mNHistos);
244 LOGP(info,
"Current output path: {}/{}", mDefName, mDirName);
252 int nent = GetLast() + 1;
253 for (
int i = 0;
i < nent; ++
i) {
258 if (hh->InheritsFrom(
"TNamed")) {
259 auto name = pref + hh->GetName();
260 static_cast<TNamed*
>(hh)->SetName(
name.c_str());
270 int nent = GetLast() + 1;
271 int nent1 = hm->GetLast() + 1;
273 Error(
"addHistos",
"HistoManagers have different content: %d vs %d", nent, nent1);
276 for (
int i = 0;
i < nent; ++
i) {
291 int nent = GetLast() + 1;
292 int nent1 = hm->GetLast() + 1;
294 Error(
"divideHistos",
"HistoManagers have different content: %d vs %d", nent, nent1);
297 for (
int i = 0;
i < nent; ++
i) {
313 int nent = GetLast() + 1;
314 int nent1 = hm->GetLast() + 1;
316 Error(
"multiplyHistos",
"HistoManagers have different content: %d vs %d", nent, nent1);
319 for (
int i = 0;
i < nent; ++
i) {
331 int nent = GetLast() + 1;
332 for (
int i = 0;
i < nent; ++
i) {
342 int nent = GetLast() + 1;
343 for (
int i = 0;
i < nent; ++
i) {
344 auto* hh1 =
dynamic_cast<TH1*
>(UncheckedAt(
i));
354 mDefName = file->GetName();
370 int last = GetLast() + 1;
372 for (
int i = 0;
i < last; ++
i) {
377 if (hist->GetEntries() < 1) {
387 mDefName = gSystem->ExpandPathName(
name.c_str());
392 int last = GetLast() + 1;
393 for (
int i = 0;
i < last; ++
i) {
404 TFile* file = TFile::Open(gSystem->ExpandPathName(fname.c_str()));
406 LOGP(error,
"No file {}", fname);
409 if (!dirname.empty() && dirname[0] !=
' ') {
410 if (!file->Get(dirname.c_str())) {
411 LOGP(error,
"No {} directory in file {}", dirname, fname);
416 file->cd(dirname.c_str());
419 TList* lst = gDirectory->GetListOfKeys();
422 while ((
key =
static_cast<TKey*
>(nextKey()))) {
423 if (FindObject(
key->GetName())) {
426 TString clName =
key->GetClassName();
427 if (!(clName.BeginsWith(
"TH") || clName.BeginsWith(
"TProfile") || clName.BeginsWith(
"TGraph"))) {
428 printf(
"Object %s of type %s is not processed\n",
key->GetName(), clName.Data());
432 int id = hst->GetUniqueID();
433 if (
auto*
h =
dynamic_cast<TH1*
>(hst)) {
438 if (
auto* gr =
dynamic_cast<TGraph*
>(hst)) {
446 if (!dirname.empty()) {
447 nm += fmt::format(
"/{}", dirname);
455 int last = GetLast() + 1;
456 for (
int i = 0;
i < last; ++
i) {
461 hist->SetLineColor(tcolor);
462 hist->SetMarkerColor(tcolor);
463 TList* lst = hist->GetListOfFunctions();
465 int nf = lst->GetSize();
466 for (
int j = 0;
j < nf; ++
j) {
468 if (fnc->InheritsFrom(
"TF1")) {
469 static_cast<TF1*
>(fnc)->SetLineColor(tcolor);
470 static_cast<TF1*
>(fnc)->SetLineWidth(1);
471 static_cast<TF1*
>(fnc)->ResetBit(TF1::kNotDraw);
472 }
else if (fnc->InheritsFrom(
"TPaveStats")) {
473 static_cast<TPaveStats*
>(fnc)->SetTextColor(tcolor);
482 int last = GetLast() + 1;
483 for (
int i = 0;
i < last; ++
i) {
488 hist->SetMarkerStyle(mstyle);
489 hist->SetMarkerSize(msize);
495 int last = GetLast() + 1;
496 for (
int i = 0;
i < last; ++
i) {
501 hist->SetMarkerSize(msize);
Class for time synchronization of RawReader instances.
void purify(bool emptyToo=kFALSE)
TH1F * getHisto1F(int id) const
int addHisto(TH1 *histo, int at=-1)
HistoManager * createClone(const std::string &prefix) const
void scaleHistos(Double_t c1=1.)
void addPrefix(const std::string &pref="")
void setMarkerStyle(Style_t mstyle=1, Size_t msize=1)
int load(const std::string &fname, const std::string &dirname="")
TProfile * getHistoP(int id) const
HistoManager(const std::string &dirname="", const std::string &fname="histoman.root", bool load=kFALSE, const std::string &prefix="")
void addHistos(const HistoManager *hm, Double_t c1=1.)
void Delete(Option_t *option="") override
void setColor(int tcolor=1)
void divideHistos(const HistoManager *hm)
void Clear(Option_t *option="") override
TGraph * getGraph(int id) const
void Print(Option_t *option="") const override
TH2F * getHisto2F(int id) const
void setMarkerSize(Size_t msize=1)
void write(TFile *file=nullptr)
TH1 * getHisto(int id) const
int addGraph(TGraph *gr, int at=-1)
void setFile(TFile *file)
void setFileName(const std::string &fname)
void multiplyHistos(const HistoManager *hm)
GLuint const GLchar * name
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...