CbmRoot
Loading...
Searching...
No Matches
CbmUtils.h
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#ifndef CBMUTILS_H_
6#define CBMUTILS_H_
7
8#include "CbmMCDataManager.h"
9
10#include "FairRootManager.h"
11#include "Logger.h"
12
13#include <sstream> // for string, stringstream
14#include <vector> // for vector
15
16class TCanvas;
17class TH1;
18class TH1D;
19class TH2;
20class TH2D;
21
22namespace Cbm
23{
24
25 template<class T>
26 std::string ToString(const T& value)
27 {
28 std::stringstream ss;
29 ss << (T) value;
30 return ss.str();
31 }
32
33 template<class T>
34 std::string NumberToString(const T& value, int precision = 1)
35 {
36 // First determine number of digits in float
37 std::string digis = ToString<int>(value);
38 int ndigis = digis.size();
39
40 std::stringstream ss;
41 ss.precision(ndigis + precision);
42 ss << value;
43 return ss.str();
44 }
45
46 /* Returns -1 if x<0, +1 if x>0, 0 if x==0 */
47 template<class T>
48 int Sign(const T& x)
49 {
50 static const T ZERO = 0;
51 return (x > ZERO) ? 1 : ((x < ZERO) ? -1 : 0);
52 }
53
54 /*
55 \brief Tries to get object from FairRootManager. If object is not found create Fatal error.
56 \param[in] objName Name of the object.
57 \param[in] description Optional description for LOG, usually class name + method name.
58 */
59 template<typename T>
60 T* GetOrFatal(const std::string& objName, const std::string& description = "")
61 {
62 FairRootManager* ioman = FairRootManager::Instance();
63 if (ioman == nullptr) { LOG(fatal) << description << " No FairRootManager!"; }
64 T* obj = static_cast<T*>(ioman->GetObject(objName.c_str()));
65 if (obj == nullptr) { LOG(fatal) << description << " No " << objName << " object!"; }
66 return obj;
67 }
68
69 /*
70 \brief Tries to get CbmMCDataArray from CbmMCDataManager. If object is not found create Fatal error.
71 \param[in] objName Name of the object.
72 \param[in] description Optional description for LOG, usually class name + method name.
73 */
74 CbmMCDataArray* InitOrFatalMc(const std::string& objName, const std::string& description = "");
75
76 void SaveCanvasAsImage(TCanvas* c, const std::string& dir, const std::string& option = "eps;png;gif");
77 void SaveCanvasAsImageImpl(const std::string& imageType, TCanvas* c, const std::string& dir,
78 const std::string& option);
79
80 std::string FindAndReplace(const std::string& name, const std::string& oldSubstr, const std::string& newSubstr);
81
82 std::vector<std::string> Split(const std::string& name, char delimiter);
83
84 /*
85 * \brief Divide 1D histograms and return result histogram h = h1 / h2.
86 * \param[in] h1 Pointer to the first histogram.
87 * \param[in] h2 Pointer to the second histogram.
88 * \param[in] histName Name of the result histogram. if histName = "" then histName = h1->GetName() + "_divide"
89 * \param[in] scale Scale factor of result histogram.
90 * \param[in] titleYaxis Y axis title of result histogram.
91 */
92 TH1D* DivideH1(TH1* h1, TH1* h2, const std::string& histName = "", double scale = 100.,
93 const std::string& titleYaxis = "Efficiency [%]");
94
95
96 /*
97 * \brief Divide 2D histograms and return result histogram h = h1 / h2.
98 * \param[in] h1 Pointer to the first histogram.
99 * \param[in] h2 Pointer to the second histogram.
100 * \param[in] histName Name of the result histogram. if histName = "" then histName = h1->GetName() + "_divide"
101 * \param[in] scale Scale factor of result histogram.
102 * \param[in] titleZaxis Z axis title of result histogram.
103 */
104 TH2D* DivideH2(TH2* h1, TH2* h2, const std::string& histName = "", double scale = 100.,
105 const std::string& titleZaxis = "Efficiency [%]");
106
107} // namespace Cbm
108
109#endif /* CBMUTILS_H_ */
Access to a MC data branch for time-based analysis.
int Sign(const T &x)
Definition CbmUtils.h:48
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
std::string ToString(const T &value)
Definition CbmUtils.h:26
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
std::string NumberToString(const T &value, int precision=1)
Definition CbmUtils.h:34