CbmRoot
Loading...
Searching...
No Matches
CbmUtils.cxx
Go to the documentation of this file.
1/* Copyright (C) 2010-2021 GSI/JINR-LIT, Darmstadt/Dubna
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Andrey Lebedev [committer], Florian Uhlig */
4
5#include "CbmUtils.h"
6
7#include "boost/filesystem.hpp"
8
9#include <RtypesCore.h> // for Int_t
10#include <TAxis.h> // for TAxis
11#include <TCanvas.h> // for TCanvas
12#include <TH1.h> // for TH1D, TH1
13#include <TH2.h> // for TH2, TH2D
14#include <TSystem.h> // for TSystem, gSystem
15
16#include <cstddef> // for size_t
17#include <string> // for operator+, allocator, operator!=, char_traits
18#include <vector> // for vector
19
20using boost::filesystem::path;
21using std::string;
22using std::vector;
23
24namespace Cbm
25{
26
27 CbmMCDataArray* InitOrFatalMc(const std::string& objName, const std::string& description)
28 {
29 CbmMCDataManager* mcManager = GetOrFatal<CbmMCDataManager>("MCDataManager", description);
30 CbmMCDataArray* array = mcManager->InitBranch(objName.c_str());
31 if (array == nullptr) { LOG(fatal) << description << " No MCTrack!"; }
32 return array;
33 }
34
35 void SaveCanvasAsImage(TCanvas* c, const string& dir, const string& option)
36 {
37 if (dir == "") return;
38 SaveCanvasAsImageImpl("eps", c, dir, option);
39 SaveCanvasAsImageImpl("png", c, dir, option);
40 SaveCanvasAsImageImpl("gif", c, dir, option);
41 SaveCanvasAsImageImpl("pdf", c, dir, option);
42 SaveCanvasAsImageImpl("svg", c, dir, option);
43 }
44
45 void SaveCanvasAsImageImpl(const string& imageType, TCanvas* c, const string& dir, const string& option)
46 {
47 if (dir == "") return;
48 if (option.find(imageType) != string::npos) {
49 path fullPath = path(dir + "/" + imageType + "/" + string(c->GetTitle()) + "." + imageType);
50 string fullDir = fullPath.parent_path().string();
51 if (fullDir.length() > 0) {
52 gSystem->mkdir(fullDir.c_str(), true); // create directory if it does not exist
53 }
54 c->SaveAs(fullPath.c_str());
55 }
56 }
57
58 string FindAndReplace(const string& name, const string& oldSubstr, const string& newSubstr)
59 {
60 string newName = name;
61 Int_t startPos = name.find(oldSubstr);
62 newName.replace(startPos, oldSubstr.size(), newSubstr);
63 return newName;
64 }
65
66 vector<string> Split(const string& name, char delimiter)
67 {
68 vector<string> result;
69 std::size_t begin = 0;
70 std::size_t end = name.find_first_of(delimiter);
71 while (end != string::npos) {
72 string str = name.substr(begin, end - begin);
73 if (str[0] == delimiter) str.erase(0, 1);
74 result.push_back(str);
75 begin = end;
76 end = name.find_first_of(delimiter, end + 1);
77 }
78 result.push_back(name.substr(begin + 1));
79 return result;
80 }
81
82
83 TH1D* DivideH1(TH1* h1, TH1* h2, const string& histName, double scale, const string& titleYaxis)
84 {
85 int nBins = h1->GetNbinsX();
86 double min = h1->GetXaxis()->GetXmin();
87 double max = h1->GetXaxis()->GetXmax();
88 string hname = string(h1->GetName()) + "_divide";
89 if (histName != "") hname = histName;
90
91 TH1D* h3 = new TH1D(histName.c_str(), hname.c_str(), nBins, min, max);
92 h3->GetXaxis()->SetTitle(h1->GetXaxis()->GetTitle());
93 h3->GetYaxis()->SetTitle(titleYaxis.c_str());
94 h1->Sumw2();
95 h2->Sumw2();
96 h3->Sumw2();
97 h3->Divide(h1, h2, 1., 1., "B");
98 h3->Scale(scale);
99 return h3;
100 }
101
102 TH2D* DivideH2(TH2* h1, TH2* h2, const string& histName, double scale, const string& titleZaxis)
103 {
104 int nBinsX = h1->GetNbinsX();
105 double minX = h1->GetXaxis()->GetXmin();
106 double maxX = h1->GetXaxis()->GetXmax();
107 int nBinsY = h1->GetNbinsY();
108 double minY = h1->GetYaxis()->GetXmin();
109 double maxY = h1->GetYaxis()->GetXmax();
110 string hname = string(h1->GetName()) + "_divide";
111 if (histName != "") hname = histName;
112
113 TH2D* h3 = new TH2D(hname.c_str(), hname.c_str(), nBinsX, minX, maxX, nBinsY, minY, maxY);
114 h3->GetXaxis()->SetTitle(h1->GetXaxis()->GetTitle());
115 h3->GetYaxis()->SetTitle(h1->GetYaxis()->GetTitle());
116 h3->GetZaxis()->SetTitle(titleZaxis.c_str());
117 h1->Sumw2();
118 h2->Sumw2();
119 h3->Sumw2();
120 h3->Divide(h1, h2, 1., 1., "B");
121 h3->Scale(scale);
122 return h3;
123 }
124
125} // namespace Cbm
friend fscal max(fscal x, fscal y)
friend fscal min(fscal x, fscal y)
int Int_t
Access to a MC data branch for time-based analysis.
Task class creating and managing CbmMCDataArray objects.
CbmMCDataArray * InitBranch(const char *name)
TH1D * DivideH1(TH1 *h1, TH1 *h2, const string &histName, double scale, const string &titleYaxis)
Definition CbmUtils.cxx:83
T * GetOrFatal(const std::string &objName, const std::string &description="")
Definition CbmUtils.h:60
void SaveCanvasAsImage(TCanvas *c, const string &dir, const string &option)
Definition CbmUtils.cxx:35
string FindAndReplace(const string &name, const string &oldSubstr, const string &newSubstr)
Definition CbmUtils.cxx:58
vector< string > Split(const string &name, char delimiter)
Definition CbmUtils.cxx:66
TH2D * DivideH2(TH2 *h1, TH2 *h2, const string &histName, double scale, const string &titleZaxis)
Definition CbmUtils.cxx:102
CbmMCDataArray * InitOrFatalMc(const std::string &objName, const std::string &description)
Definition CbmUtils.cxx:27
void SaveCanvasAsImageImpl(const string &imageType, TCanvas *c, const string &dir, const string &option)
Definition CbmUtils.cxx:45