CbmRoot
CbmUtils.cxx
Go to the documentation of this file.
1 #include "CbmUtils.h"
2 
3 #include <RtypesCore.h> // for Int_t
4 
5 #include <TAxis.h> // for TAxis
6 #include <TCanvas.h> // for TCanvas
7 #include <TH1.h> // for TH1D, TH1
8 #include <TH2.h> // for TH2, TH2D
9 #include <TSystem.h> // for TSystem, gSystem
10 
11 #include <stddef.h> // for size_t
12 #include <string> // for operator+, allocator, operator!=, char_traits
13 #include <vector> // for vector
14 
15 using std::string;
16 using std::vector;
17 
18 namespace Cbm {
19 
20  void SaveCanvasAsImage(TCanvas* c,
21  const std::string& dir,
22  const std::string& option) {
23  if (dir == "") return;
24  if (option.find("eps") != std::string::npos) {
25  string dir2 = dir + "/eps/";
26  gSystem->mkdir(dir2.c_str(),
27  true); // create directory if it does not exist
28  c->SaveAs(
29  std::string(dir2 + std::string(c->GetTitle()) + ".eps").c_str());
30  }
31  if (option.find("png") != std::string::npos) {
32  string dir2 = dir + "/png/";
33  gSystem->mkdir(dir2.c_str(), true);
34  c->SaveAs(
35  std::string(dir2 + std::string(c->GetTitle()) + ".png").c_str());
36  }
37  if (option.find("gif") != std::string::npos) {
38  string dir2 = dir + "/gif/";
39  gSystem->mkdir(dir2.c_str(), true);
40  c->SaveAs(
41  std::string(dir2 + std::string(c->GetTitle()) + ".gif").c_str());
42  }
43  }
44 
45  string FindAndReplace(const string& name,
46  const string& oldSubstr,
47  const string& newSubstr) {
48  string newName = name;
49  Int_t startPos = name.find(oldSubstr);
50  newName.replace(startPos, oldSubstr.size(), newSubstr);
51  return newName;
52  }
53 
54  vector<string> Split(const string& name, char delimiter) {
55  vector<string> result;
56  std::size_t begin = 0;
57  std::size_t end = name.find_first_of(delimiter);
58  while (end != string::npos) {
59  string str = name.substr(begin, end - begin);
60  if (str[0] == delimiter) str.erase(0, 1);
61  result.push_back(str);
62  begin = end;
63  end = name.find_first_of(delimiter, end + 1);
64  }
65  result.push_back(name.substr(begin + 1));
66  return result;
67  }
68 
69 
70  TH1D* DivideH1(TH1* h1,
71  TH1* h2,
72  const string& histName,
73  double scale,
74  const string& titleYaxis) {
75  int nBins = h1->GetNbinsX();
76  double min = h1->GetXaxis()->GetXmin();
77  double max = h1->GetXaxis()->GetXmax();
78  string hname = string(h1->GetName()) + "_divide";
79  if (histName != "") hname = histName;
80 
81  TH1D* h3 = new TH1D(histName.c_str(), hname.c_str(), nBins, min, max);
82  h3->GetXaxis()->SetTitle(h1->GetXaxis()->GetTitle());
83  h3->GetYaxis()->SetTitle(titleYaxis.c_str());
84  h1->Sumw2();
85  h2->Sumw2();
86  h3->Sumw2();
87  h3->Divide(h1, h2, 1., 1., "B");
88  h3->Scale(scale);
89  return h3;
90  }
91 
92  TH2D* DivideH2(TH2* h1,
93  TH2* h2,
94  const string& histName,
95  double scale,
96  const string& titleZaxis) {
97  int nBinsX = h1->GetNbinsX();
98  double minX = h1->GetXaxis()->GetXmin();
99  double maxX = h1->GetXaxis()->GetXmax();
100  int nBinsY = h1->GetNbinsY();
101  double minY = h1->GetYaxis()->GetXmin();
102  double maxY = h1->GetYaxis()->GetXmax();
103  string hname = string(h1->GetName()) + "_divide";
104  if (histName != "") hname = histName;
105 
106  TH2D* h3 = new TH2D(
107  hname.c_str(), hname.c_str(), nBinsX, minX, maxX, nBinsY, minY, maxY);
108  h3->GetXaxis()->SetTitle(h1->GetXaxis()->GetTitle());
109  h3->GetYaxis()->SetTitle(h1->GetYaxis()->GetTitle());
110  h3->GetZaxis()->SetTitle(titleZaxis.c_str());
111  h1->Sumw2();
112  h2->Sumw2();
113  h3->Sumw2();
114  h3->Divide(h1, h2, 1., 1., "B");
115  h3->Scale(scale);
116  return h3;
117  }
118 
119 } // namespace Cbm
Cbm::FindAndReplace
string FindAndReplace(const string &name, const string &oldSubstr, const string &newSubstr)
Definition: CbmUtils.cxx:45
Cbm::DivideH2
TH2D * DivideH2(TH2 *h1, TH2 *h2, const string &histName, double scale, const string &titleZaxis)
Definition: CbmUtils.cxx:92
min
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition: L1/vectors/P4_F32vec4.h:33
Cbm::DivideH1
TH1D * DivideH1(TH1 *h1, TH1 *h2, const string &histName, double scale, const string &titleYaxis)
Definition: CbmUtils.cxx:70
CbmUtils.h
Cbm::SaveCanvasAsImage
void SaveCanvasAsImage(TCanvas *c, const std::string &dir, const std::string &option)
Definition: CbmUtils.cxx:20
max
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: L1/vectors/P4_F32vec4.h:36
Cbm::Split
vector< string > Split(const string &name, char delimiter)
Definition: CbmUtils.cxx:54
Cbm
Definition: CbmGeometryUtils.cxx:31