travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HistContainer.cxx
Go to the documentation of this file.
1 #include "TCanvas.h"
2 #include "TClass.h"
3 #include "TColor.h"
4 #include "TPaletteAxis.h"
5 #include "TROOT.h"
6 
7 #include "travex/utils.h"
8 #include "travex/HistContainer.h"
9 
10 using namespace tvx;
11 
12 
17 HistContainer::HistContainer(const std::string name, TDirectory* motherDir, const std::string option) :
18  TDirectoryFile(name.c_str(), name.c_str(), option.c_str(), motherDir),
19  fHs()
20 {
21  cd();
22 }
23 
24 
29 void HistContainer::Add(TH1* hist)
30 {
31  if (!hist || std::string(hist->GetName()).empty() ) {
32  TVX_WARNING("Cannot add invalid histogram");
33  return;
34  }
35 
36  std::string hist_name(hist->GetName());
37 
38  if ( FindHist(hist_name) )
39  TVX_WARNING("Replacing existing histogram %s", hist_name.c_str());
40 
41  fHs[hist_name].reset(hist);
42 }
43 
44 
53 void HistContainer::SaveAllAs(std::string prefix, std::string img_format)
54 {
55  static TCanvas canvas("canvas", "canvas", 1200, 600);
56  canvas.UseCurrentStyle();
57  canvas.SetGridx(true);
58  canvas.SetGridy(true);
59 
60  for (auto& iHist : fHs)
61  {
62  std::string histName = iHist.first;
63  std::unique_ptr<TH1>& hist = iHist.second;
64 
65  char* opts = (char*) hist->GetOption();
66 
67  if (strstr(opts, "XX")) canvas.SetLogx(true);
68  else canvas.SetLogx(false);
69 
70  if (strstr(opts, "XY")) canvas.SetLogy(true);
71  else canvas.SetLogy(false);
72 
73  if (strstr(opts, "XZ")) canvas.SetLogz(true);
74  else canvas.SetLogz(false);
75 
76  hist->Draw();
77 
78  TColor *color = nullptr;
79  float r, g, b;
80 
81  if (strstr(opts, "whit_zro")) {
82  hist->GetZaxis()->SetRangeUser(-1,1);
83  hist->SetContour(11);
84  gPad->Update();
85  TPaletteAxis *palette = (TPaletteAxis*) hist->GetListOfFunctions()->FindObject("palette");
86  color = gROOT->GetColor( palette->GetValueColor(0) );
87  color->GetRGB(r, g, b);
88  color->SetRGB(255, 255, 255);
89  }
90 
91  // Now check if there are other associated objects like functions and graphs
92  TList* sub_list = hist->GetListOfFunctions();
93  TIter next(sub_list);
94 
95  while ( TObject *iObj = (TObject*) next() )
96  {
97  if ( !iObj ) continue;
98  iObj->Draw();
99  }
100 
101  std::string sFileName = prefix + "/" + histName + "." + img_format;
102  canvas.SaveAs(sFileName.c_str());
103 
104  // Restore modified color
105  if (color) color->SetRGB(r, g, b);
106  }
107 }
HistContainer(const std::string name, TDirectory *motherDir=nullptr, const std::string option="")
Creates an empty histogram container.
Definition: Event.h:12
HistMap fHs
A container of unique pointers to TH1 objects indexed by names.
Definition: HistContainer.h:61
#define TVX_WARNING(...)
Definition: utils.h:8
const TH1 * FindHist(const std::string &hist_name) const
Returns a raw pointer to the histogram with hist_name name or nullptr such histogram does not exist...
Definition: HistContainer.h:77
void SaveAllAs(std::string prefix="./", std::string img_format="png")
Saves all histograms from the container as png images in the prefix directory.
void Add(TH1 *hist)
This container assumes the ownership of the histogram and can modify it.