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