ATLAS Offline Software
PlotBase.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // -------------------------------------------------------------
6 // PlotBase is the base class for all plotting structures,
7 //
8 // It has an initialize function that provides a hook called
9 // initializePlots where histograms should be booked.
10 // It also provides the Booking methods (e.g. Book1D) which
11 // hide the implementation of the booking from the user
12 //
13 // author: Felix Socher <Felix.Socher@cern.ch>
14 // -------------------------------------------------------------
15 
17 #include "TH1F.h"
18 #include "TH2F.h"
19 #include "TH3F.h"
20 #include "TProfile.h"
21 #include <algorithm>
22 
23 PlotBase::PlotBase(PlotBase *pParent, const std::string &sDir) {
24  if (pParent != nullptr) {
25  pParent->RegisterSubPlot(this);
26  }
27  std::string sParentDirectory = (pParent != nullptr) ? pParent->getDirectory() : "";
28  m_sDirectory = sParentDirectory + sDir;
29  m_iDetailLevel = 0;
30 }
31 
32 void
34  for (auto *subNode: m_vSubNodes) {
35  subNode->initialize();
36  }
38 }
39 
40 void
42  for (auto *subNode: m_vSubNodes) {
43  subNode->finalize();
44  }
45  finalizePlots();
46 }
47 
48 void
49 PlotBase::setDetailLevel(int iDetailLevel) {
50  for (auto *subNode: m_vSubNodes) {
51  subNode->setDetailLevel(iDetailLevel);
52  }
53  m_iDetailLevel = iDetailLevel;
54 }
55 
56 std::vector<HistData>
58  std::vector<HistData> vBookedHistograms = m_vBookedHistograms;
59  for (const auto &subNode: m_vSubNodes) {
60  std::vector<HistData> subNodeHists = subNode->retrieveBookedHistograms();
61  vBookedHistograms.insert(vBookedHistograms.end(), subNodeHists.begin(), subNodeHists.end());
62  }
63  return vBookedHistograms;
64 }
65 
66 std::vector<TreeData>
68  std::vector<TreeData> vBookedTrees = m_vBookedTrees;
69  for (auto *subNode: m_vSubNodes) {
70  std::vector<TreeData> subNodeTrees = subNode->retrieveBookedTrees();
71  vBookedTrees.insert(vBookedTrees.end(), subNodeTrees.begin(), subNodeTrees.end());
72  }
73  return vBookedTrees;
74 }
75 
76 std::vector<EfficiencyData>
78  std::vector<EfficiencyData> vBookedEfficiencies = m_vBookedEfficiencies;
79  for (const auto &subNode: m_vSubNodes) {
80  std::vector<EfficiencyData> subNodeHists = subNode->retrieveBookedEfficiencies();
81  vBookedEfficiencies.insert(vBookedEfficiencies.end(), subNodeHists.begin(), subNodeHists.end());
82  }
83  return vBookedEfficiencies;
84 }
85 
86 
87 TH1F *
88 PlotBase::Book1D(const std::string &name, const std::string &labels, int nBins, float start, float end,
89  bool prependDir) {
90  std::string prefix = constructPrefix(m_sDirectory, prependDir);
91  Bool_t oldstat = TH1::AddDirectoryStatus();
92  TH1::AddDirectory(false);
93  TH1F *hist = new TH1F((prefix + name).c_str(), labels.c_str(), nBins, start, end);
94  TH1::AddDirectory(oldstat);
95 
96  hist->Sumw2();
97  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
98  return hist;
99 }
100 
101 TH1F *
102 PlotBase::Book1D(const std::string &name, TH1 *refHist, const std::string &labels, bool prependDir) {
103  std::string prefix = constructPrefix(m_sDirectory, prependDir);
104  Bool_t oldstat = TH1::AddDirectoryStatus();
105  TH1::AddDirectory(false);
106  TH1F *hist = new TH1F((prefix + name).c_str(), labels.c_str(), refHist->GetNbinsX(),
107  refHist->GetXaxis()->GetXbins()->GetArray());
108  hist->Sumw2();
109  TH1::AddDirectory(oldstat);
110 
111 
112  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
113  return hist;
114 }
115 
116 TH2F *
117 PlotBase::Book2D(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, int nBinsY,
118  float startY, float endY, bool prependDir) {
119  std::string prefix = constructPrefix(m_sDirectory, prependDir);
120  Bool_t oldstat = TH2::AddDirectoryStatus();
121  TH2::AddDirectory(false);
122  TH2F *hist = new TH2F((prefix + name).c_str(), labels.c_str(), nBinsX, startX, endX, nBinsY, startY, endY);
123  hist->Sumw2();
124  TH2::AddDirectory(oldstat);
125 
126 
127  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
128  return hist;
129 }
130 
131 TH2F *
132 PlotBase::Book2D(const std::string &name, TH2 *refHist, const std::string &labels, bool prependDir) {
133  return Book2D(name, labels, refHist->GetNbinsX(), refHist->GetXaxis()->GetXmin(), refHist->GetXaxis()->GetXmax(),
134  refHist->GetNbinsY(), refHist->GetYaxis()->GetXmin(), refHist->GetYaxis()->GetXmax(), prependDir);
135 }
136 
137 TH2F *
138 PlotBase::Book2D(const std::string &name, const std::string &labels, int nBinsX, Double_t *binsX, int nBinsY,
139  Double_t startY, Double_t endY, bool prependDir) {
140  std::string prefix = constructPrefix(m_sDirectory, prependDir);
141  Bool_t oldstat = TH2::AddDirectoryStatus();
142  TH2::AddDirectory(false);
143  TH2F *hist = new TH2F((prefix + name).c_str(), labels.c_str(), nBinsX, binsX, nBinsY, startY, endY);
144  hist->Sumw2();
145  TH2::AddDirectory(oldstat);
146  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
147  return hist;
148 }
149 
150 TH3F *
151 PlotBase::Book3D(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, int nBinsY,
152  float startY, float endY, int nBinsZ, float startZ, float endZ, bool prependDir) {
153  std::string prefix = constructPrefix(m_sDirectory, prependDir);
154  Bool_t oldstat = TH3::AddDirectoryStatus();
155  TH3::AddDirectory(false);
156  TH3F *hist = new TH3F((prefix + name).c_str(),
157  labels.c_str(), nBinsX, startX, endX, nBinsY, startY, endY, nBinsZ, startZ, endZ);
158  hist->Sumw2();
159  TH3::AddDirectory(oldstat);
160  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
161  return hist;
162 }
163 
164 TH3F *
165 PlotBase::Book3D(const std::string &name, TH3 *refHist, const std::string &labels, bool prependDir) {
166  std::string prefix = constructPrefix(m_sDirectory, prependDir);
167  Bool_t oldstat = TH3::AddDirectoryStatus();
168  TH3::AddDirectory(false);
169  TH3F *hist = new TH3F((prefix + name).c_str(), labels.c_str(), refHist->GetNbinsX(),
170  refHist->GetXaxis()->GetXbins()->GetArray(), refHist->GetNbinsY(),
171  refHist->GetYaxis()->GetXbins()->GetArray(), refHist->GetNbinsZ(),
172  refHist->GetZaxis()->GetXbins()->GetArray());
173  TH3::AddDirectory(oldstat);
174 
175  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
176  return hist;
177 }
178 
179 TProfile *
180 PlotBase::BookTProfile(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX,
181  float startY, float endY, bool prependDir, bool useRMS) {
182  std::string prefix = constructPrefix(m_sDirectory, prependDir);
183  TProfile *hist(nullptr);
184  Bool_t oldstat = TProfile::AddDirectoryStatus();
185  TProfile::AddDirectory(false);
186  std::string opt = useRMS ? "S" : "";
187  if ((startY == -1) and (endY == -1)) {
188  hist = new TProfile((prefix + name).c_str(), labels.c_str(), nBinsX, startX, endX, opt.c_str());
189  } else {
190  hist = new TProfile((prefix + name).c_str(), labels.c_str(), nBinsX, startX, endX, startY, endY, opt.c_str());
191  }
192  TProfile::AddDirectory(oldstat);
193  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
194  return hist;
195 }
196 
197 TProfile *
198 PlotBase::BookTProfile(const std::string &name, const std::string &labels, int nBinsX, float *binsX, bool prependDir) {
199  std::string prefix = constructPrefix(m_sDirectory, prependDir);
200  TProfile *hist(nullptr);
201  Bool_t oldstat = TProfile::AddDirectoryStatus();
202  TProfile::AddDirectory(false);
203 
204  hist = new TProfile((prefix + name).c_str(), labels.c_str(), nBinsX, binsX);
205  TProfile::AddDirectory(oldstat);
206  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
207  return hist;
208 }
209 
210 TProfile *
211 PlotBase::BookTProfileRangeY(const std::string &name, const std::string &labels, int nBinsX, double *binsX,
212  double startY, double endY, bool prependDir) {
213  std::string prefix = constructPrefix(m_sDirectory, prependDir);
214  TProfile *hist(nullptr);
215  Bool_t oldstat = TProfile::AddDirectoryStatus();
216  TProfile::AddDirectory(false);
217 
218  hist = new TProfile((prefix + name).c_str(), labels.c_str(), (Int_t) nBinsX, binsX, startY, endY);
219  TProfile::AddDirectory(oldstat);
220  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
221  return hist;
222 }
223 
224 TProfile2D *
225 PlotBase::BookTProfile2D(const std::string &name, const std::string &labels, const int nBinsX,
226  const double xlo, const double xhi, const int nBinsY, const double ylo, const double yhi,
227  bool prependDir, bool useRMS) {
228  std::string prefix = constructPrefix(m_sDirectory, prependDir);
229  Bool_t oldstat = TProfile2D::AddDirectoryStatus();
230  TProfile2D::AddDirectory(false);
231  std::string opt = useRMS ? "S" : "";
232  TProfile2D *hist = new TProfile2D((prefix + name).c_str(), labels.c_str(), nBinsX, xlo, xhi, nBinsY, ylo, yhi, opt.c_str());
233  TProfile2D::AddDirectory(oldstat);
234  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
235  return hist;
236 }
237 
238 TProfile2D *
239 PlotBase::BookTProfile2D(const std::string &name, const std::string &labels, const int nBinsX, double* binsX, const int nBinsY, double* binsY, bool prependDir, bool useRMS) {
240  std::string prefix = constructPrefix(m_sDirectory, prependDir);
241  Bool_t oldstat = TProfile2D::AddDirectoryStatus();
242  TProfile2D::AddDirectory(false);
243  std::string opt = useRMS ? "S" : "";
244  TProfile2D *hist = new TProfile2D((prefix + name).c_str(), labels.c_str(), nBinsX, binsX, nBinsY, binsY, opt.c_str());
245  TProfile2D::AddDirectory(oldstat);
246  m_vBookedHistograms.emplace_back(hist, m_sDirectory);
247  return hist;
248 }
249 
250 TEfficiency *
251 PlotBase::BookTEfficiency(const std::string &name, const std::string & labels, const int nBinsX, const float xlo, const float xhi, const bool prependDir){
252  std::string prefix = constructPrefix(m_sDirectory, prependDir);
253  //Bool_t oldstat = TEfficiency::AddDirectoryStatus();
254  TEfficiency *hist = new TEfficiency((prefix + name).c_str(), labels.c_str(), nBinsX, xlo, xhi);
255  //hist->SetAutoSave(0);
256  //hist->SetAtoFlush(0);
257  hist->SetDirectory(nullptr);
258  m_vBookedEfficiencies.emplace_back(hist, m_sDirectory);
259  //TEfficiency::AddDirectory(oldstat);
260  return hist;
261 }
262 
263 TEfficiency *
264 PlotBase::BookTEfficiency(const std::string &name, const std::string & labels, const int nBinsX, const float xlo, const float xhi, const int nBinsY, const float ylo, const float yhi, const bool prependDir){
265  std::string prefix = constructPrefix(m_sDirectory, prependDir);
266 
267  TEfficiency *hist = new TEfficiency((prefix + name).c_str(), labels.c_str(), nBinsX, xlo, xhi, nBinsY, ylo, yhi);
268  hist->SetDirectory(nullptr);
269  m_vBookedEfficiencies.emplace_back(hist, m_sDirectory);
270 
271  return hist;
272 }
273 
274 TTree *
275 PlotBase::BookTree(const std::string &name, bool prependDir) {
276  std::string prefix = constructPrefix(m_sDirectory, prependDir);
277  TTree *tree = new TTree((prefix + name).c_str(), "");
278 
279  tree->SetAutoSave(0);
280  tree->SetAutoFlush(0);
281  tree->SetDirectory(nullptr);
282  m_vBookedTrees.emplace_back(tree, m_sDirectory);
283  return tree;
284 }
285 
286 std::string
287 PlotBase::constructPrefix(std::string dir, bool prependDir) {
288  if (!prependDir) {
289  return "";
290  }
291  std::replace(dir.begin(), dir.end(), '/', '_');
292  return dir;
293 }
PlotBase::constructPrefix
static std::string constructPrefix(std::string dir, bool prependDir)
Definition: PlotBase.cxx:287
WritePulseShapeToCool.yhi
yhi
Definition: WritePulseShapeToCool.py:153
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
PlotBase::m_iDetailLevel
int m_iDetailLevel
Definition: PlotBase.h:100
PlotBase::m_vBookedHistograms
std::vector< HistData > m_vBookedHistograms
Definition: PlotBase.h:96
PlotBase::Book1D
TH1F * Book1D(const std::string &name, const std::string &labels, int nBins, float start, float end, bool prependDir=true)
Book a TH1D histogram.
Definition: PlotBase.cxx:88
PlotBase::BookTree
TTree * BookTree(const std::string &name, bool prependDir=true)
Book a TTree.
Definition: PlotBase.cxx:275
PlotBase::retrieveBookedTrees
std::vector< TreeData > retrieveBookedTrees()
Retrieve all booked trees.
Definition: PlotBase.cxx:67
PlotBase
Definition: PlotBase.h:33
TH2F
Definition: rootspy.cxx:420
python.copyTCTOutput.sDir
sDir
Definition: copyTCTOutput.py:60
plotmaker.hist
hist
Definition: plotmaker.py:148
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
PlotBase::initializePlots
virtual void initializePlots()
Definition: PlotBase.h:90
PlotBase::m_sDirectory
std::string m_sDirectory
Definition: PlotBase.h:99
PlotBase::m_vBookedEfficiencies
std::vector< EfficiencyData > m_vBookedEfficiencies
Definition: PlotBase.h:98
tree
TChain * tree
Definition: tile_monitor.h:30
PlotBase::BookTEfficiency
TEfficiency * BookTEfficiency(const std::string &name, const std::string &labels, const int nBinsX, const float xlo, const float xhi, const bool prependDir=true)
Book a (1-D) TEfficiency histogram.
Definition: PlotBase.cxx:251
TProfile2D
Definition: rootspy.cxx:531
PlotBase::Book2D
TH2F * Book2D(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, int nBinsY, float startY, float endY, bool prependDir=true)
Book a TH2D histogram.
Definition: PlotBase.cxx:117
python.TrigEgammaMonitorHelper.TH2F
def TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:45
PlotBase::retrieveBookedEfficiencies
std::vector< EfficiencyData > retrieveBookedEfficiencies()
Retrieve all booked efficiency objects.
Definition: PlotBase.cxx:77
PlotBase::BookTProfile2D
TProfile2D * BookTProfile2D(const std::string &name, const std::string &labels, const int nBinsX, const double xlo, const double xhi, const int nBinsY, const double ylo, const double yhi, bool prependDir=true, bool useRMS=false)
Book a TProfile 2D histogram with variable binning in x-axis and limits in y-values.
Definition: PlotBase.cxx:225
PlotBase::PlotBase
PlotBase(PlotBase *parent, const std::string &sDir)
Definition: PlotBase.cxx:23
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
PlotBase::BookTProfileRangeY
TProfile * BookTProfileRangeY(const std::string &name, const std::string &labels, int nBinsX, double *binsX, double startY, double endY, bool prependDir=true)
Book a TProfile histogram with variable binning in x-axis and limits in y-values.
Definition: PlotBase.cxx:211
PlotBase::RegisterSubPlot
void RegisterSubPlot(PlotBase *pPlotBase)
Definition: PlotBase.h:40
beamspotnt.labels
list labels
Definition: bin/beamspotnt.py:1447
PlotBase::m_vSubNodes
std::vector< PlotBase * > m_vSubNodes
Definition: PlotBase.h:95
PlotBase::retrieveBookedHistograms
std::vector< HistData > retrieveBookedHistograms()
Retrieve all booked histograms.
Definition: PlotBase.cxx:57
PlotBase::finalize
void finalize()
Definition: PlotBase.cxx:41
WritePulseShapeToCool.xhi
xhi
Definition: WritePulseShapeToCool.py:152
python.TrigEgammaMonitorHelper.TProfile
def TProfile(*args, **kwargs)
Definition: TrigEgammaMonitorHelper.py:81
PlotBase::initialize
void initialize()
Definition: PlotBase.cxx:33
ChangeHistoRange.binsY
list binsY
Definition: ChangeHistoRange.py:59
TH3
Definition: rootspy.cxx:440
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
WritePulseShapeToCool.xlo
xlo
Definition: WritePulseShapeToCool.py:133
TH2
Definition: rootspy.cxx:373
WritePulseShapeToCool.ylo
ylo
Definition: WritePulseShapeToCool.py:134
PlotBase::Book3D
TH3F * Book3D(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, int nBinsY, float startY, float endY, int nBinsZ, float startZ, float endZ, bool prependDir=true)
Book a TH3D histogram.
Definition: PlotBase.cxx:151
beamspotman.dir
string dir
Definition: beamspotman.py:623
dumpTgcDigiJitter.nBins
list nBins
Definition: dumpTgcDigiJitter.py:29
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
pmontree.opt
opt
Definition: pmontree.py:16
TH3F
Definition: rootspy.cxx:495
TProfile
Definition: rootspy.cxx:515
TH1F
Definition: rootspy.cxx:320
TH1
Definition: rootspy.cxx:268
PlotBase::BookTProfile
TProfile * BookTProfile(const std::string &name, const std::string &labels, int nBinsX, float startX, float endX, float startY=-1, float endY=-1, bool prependDir=true, bool useRMS=false)
Book a TProfile histogram.
Definition: PlotBase.cxx:180
ChangeHistoRange.binsX
list binsX
Definition: ChangeHistoRange.py:56
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
PlotBase.h
PlotBase::getDirectory
std::string getDirectory()
Definition: PlotBase.h:87
PlotBase::finalizePlots
virtual void finalizePlots()
Definition: PlotBase.h:91
PlotBase::m_vBookedTrees
std::vector< TreeData > m_vBookedTrees
Definition: PlotBase.h:97
PlotBase::setDetailLevel
void setDetailLevel(int iDetailLevel)
Definition: PlotBase.cxx:49