ATLAS Offline Software
LWHist.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 // //
8 // Implementation of class LWHist //
9 // //
10 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11 // Initial version: March 2009 //
12 // //
14 
15 #include "LWHists/LWHist.h"
16 #include "LWHists/LWHist1D.h"
17 #include "LWHists/LWHist2D.h"
18 #include "LWHists/LWHistControls.h"
19 #include "LWHists/LWHistStats.h"
20 #include "LWPools.h"
21 #include "LWStrUtils.h"
22 #include "LWBinLabels.h"
23 #include "TH1.h"
24 #include <cassert>
25 #include <cstring>
26 #include <cmath>
27 #include <mutex>
28 
29 //____________________________________________________________________
31 {
32  if (!h) {
33  //std::cout<<"LWHist::safeDelete ERROR: Called with null pointer!"<<std::endl;
34  return;
35  }
36  if (h->m_nBytesFromPool) {
37  h->~LWHist();
38  LWPools::release(reinterpret_cast<char*>(h),h->m_nBytesFromPool);
40  std::cout<<"LWHists INFO: Last active histogram deleted. Triggering complete pool cleanup."<<std::endl;
42  }
43  } else {
44  delete h;
45  }
46 }
47 
48 #define DEFAULT_MARKERSIZE 1
49 #define DEFAULT_MARKERCOLOR 1
50 #define DEFAULT_MARKERSTYLE 1
51 #define DEFAULT_MINIMUM -1111
52 #define DEFAULT_MAXIMUM -1111
54 public:
56  m_option(0),
57  m_xAxis(0),
58  m_yAxis(0),
59  m_zAxis(0),
66  {
71  }
72 
73  char * m_option;
77  float m_minimum;
78  float m_maximum;
79  float m_markerSize;
82 
83 private:
86 
87 };
88 
89 //____________________________________________________________________
91 {
92  if (!m_decorations)
94 }
95 
97 {
98  const TH1 * hroot= getROOTHistBaseNoAlloc();
99  if (hroot)
100  return hroot->GetMarkerColor();
101 
103 }
104 
106 {
107  const TH1 * hroot= getROOTHistBaseNoAlloc();
108  if (hroot)
109  return hroot->GetMarkerStyle();
111 }
112 
114 {
115  const TH1 * hroot= getROOTHistBaseNoAlloc();
116  if (hroot)
117  return hroot->GetMarkerSize();
119 }
120 
122 {
123  TH1 * hroot= getROOTHistBaseNoAlloc();
124  if (hroot) {
125  hroot->SetMarkerColor(c);
126  return;
127  }
129  return;
132 }
134 {
135  TH1 * hroot= getROOTHistBaseNoAlloc();
136  if (hroot) {
137  hroot->SetMarkerStyle(s);
138  assert(GetMarkerStyle()==s);
139  return;
140  }
142  assert(GetMarkerStyle()==s);
143  return;
144  }
147  assert(GetMarkerStyle()==s);
148 }
149 
150 void LWHist::SetMarkerSize( float sz ) {
151  TH1 * hroot= getROOTHistBaseNoAlloc();
152  if (hroot) {
153  hroot->SetMarkerSize(sz);
154  return;
155  }
157  return;
160 }
161 
162 void LWHist::SetMinimum(const double& m )
163 {
164  TH1 * hroot= getROOTHistBaseNoAlloc();
165  if (hroot) {
166  hroot->SetMinimum(m);
167  return;
168  }
169  if (!m_decorations&&fabs(m-DEFAULT_MINIMUM)<1.0e-5)
170  return;
173 }
174 
175 void LWHist::SetMaximum(const double& m )
176 {
177  TH1 * hroot= getROOTHistBaseNoAlloc();
178  if (hroot) {
179  hroot->SetMaximum(m);
180  return;
181  }
182  if (!m_decorations&&fabs(m-DEFAULT_MAXIMUM)<1.0e-5)
183  return;
186 }
187 
188 //____________________________________________________________________
189 LWHist::LWHist( const char* n,
190  const char* t,
191  bool rootbackend )
192  : m_nBytesFromPool(0),
193  m_usingROOTBackend(rootbackend),
194  m_ownsRootHisto(true),
195  m_name(0),
196  m_title(0),
197  m_decorations(0),
198  m_customData(0)
199 {
200  //Fixme: Sanity check of input parameters
201  assert(typeid(short)==typeid(Color_t));
202  assert(typeid(short)==typeid(Style_t));
203  assert(typeid(float)==typeid(Size_t));
204  if (!m_usingROOTBackend) {
207  }
209  static std::once_flag first_flag;
210  std::call_once (first_flag,
211  []() { std::cout<<"LWHists INFO: Using light weight histograms"<<std::endl; });
212 }
213 
214 //____________________________________________________________________
216 {
217  LWHist::clear();
219 }
220 
221 //____________________________________________________________________
222 void LWHist::SetName(const char*n)
223 {
224  TH1 * hroot= getROOTHistBaseNoAlloc();
225  if (hroot) {
226  hroot->SetName(n);
227  return;
228  }
230 }
231 
232 //____________________________________________________________________
233 void LWHist::SetTitle(const char*t)
234 {
235  TH1 * hroot= getROOTHistBaseNoAlloc();
236  if (hroot) {
237  hroot->SetTitle(t);
238  return;
239  }
241 }
242 
243 //____________________________________________________________________
244 void LWHist::SetNameTitle(const char*n,const char*t)
245 {
246  TH1 * hroot= getROOTHistBaseNoAlloc();
247  if (hroot) {
248  hroot->SetNameTitle(n,t);
249  return;
250  }
253 }
254 
255 //____________________________________________________________________
256 bool LWHist::apply(TH1*h) const
257 {
258  if (!h)
259  return false;
260  assert(!m_usingROOTBackend);
261 
262  if (!LWStrUtils::stringsEqual(h->GetTitle(),GetTitle()))
263  h->SetTitle(GetTitle());
264 
266  TAxis * a = h->GetXaxis();
267  assert(a);
269  a->SetTitle(m_decorations->m_xAxis->m_title);
270  if (m_decorations->m_xAxis->m_labelSize!=a->GetLabelSize())
271  a->SetLabelSize(m_decorations->m_xAxis->m_labelSize);
274  }
276  TAxis * a = h->GetYaxis();
277  assert(a);
279  a->SetTitle(m_decorations->m_yAxis->m_title);
280  if (m_decorations->m_yAxis->m_labelSize!=a->GetLabelSize())
281  a->SetLabelSize(m_decorations->m_yAxis->m_labelSize);
284  }
286  TAxis * a = h->GetZaxis();
287  assert(a);
289  a->SetTitle(m_decorations->m_zAxis->m_title);
290  if (m_decorations->m_zAxis->m_labelSize!=a->GetLabelSize())
291  a->SetLabelSize(m_decorations->m_zAxis->m_labelSize);
294  }
295  h->SetMarkerColor( GetMarkerColor() );
296  h->SetMarkerStyle( GetMarkerStyle() );
297  h->SetMarkerSize( GetMarkerSize() );
299  h->SetMinimum(m_decorations->m_minimum);
301  h->SetMaximum(m_decorations->m_maximum);
303  h->SetOption(m_decorations->m_option);
304 
305  return true;
306 }
307 
308 //____________________________________________________________________
310 {
311  this->ensureInitDecorations();
312  if (!m_decorations->m_xAxis)
313  this->m_decorations->m_xAxis = usingROOTBackend() ? new LWHistAxis(this) : MP_NEW(LWHistAxis)(this);
314 
315  return m_decorations->m_xAxis;
316 }
317 
318 //____________________________________________________________________
320 {
321  this->ensureInitDecorations();
322  if (!m_decorations->m_yAxis)
323  this->m_decorations->m_yAxis = usingROOTBackend() ? new LWHistAxis(this) : MP_NEW(LWHistAxis)(this);
324  return m_decorations->m_yAxis;
325 }
326 
327 //____________________________________________________________________
329 {
330  this->ensureInitDecorations();
331  if (!m_decorations->m_zAxis)
332  this->m_decorations->m_zAxis = usingROOTBackend() ? new LWHistAxis(this) : MP_NEW(LWHistAxis)(this);
333  return m_decorations->m_zAxis;
334 }
335 
336 //Axis implementation:
337 //Constructor and destructor should do nothing due to our use of the mem-pool.
338 LWHist::LWHistAxis::LWHistAxis(LWHist *hist) : m_hist(hist), m_labelSize(0.035),
339  m_title(0),m_binLabels(0) {}
340 const TAxis * LWHist::LWHistAxis::rootAxis() const
341 {
342  const TH1* hroot=std::as_const(m_hist)->getROOTHistBaseNoAlloc();
343  return hroot ? (isXAxis() ? hroot->GetXaxis() : (isYAxis() ? hroot->GetYaxis():hroot->GetZaxis())) : 0;
344 }
345 
347 {
348  TH1* hroot=m_hist->getROOTHistBaseNoAlloc();
349  return hroot ? (isXAxis() ? hroot->GetXaxis() : (isYAxis() ? hroot->GetYaxis():hroot->GetZaxis())) : 0;
350 }
351 
353 {
354  TAxis*a=rootAxis();
355  if (a) a->SetLabelSize(s);
356  else m_labelSize = s;
357 }
358 
360 {
361  TAxis*a=rootAxis();
362  if (a) a->SetTitle(t);
364 }
365 
366 const char* LWHist::GetName() const
367 {
368  const TH1 * hroot = getROOTHistBaseNoAlloc();
369  return hroot ? hroot->GetName() : m_name;
370 }
371 
372 const char* LWHist::GetTitle() const
373 {
374  const TH1 * hroot = getROOTHistBaseNoAlloc();
375  return hroot ? hroot->GetTitle() : m_title;
376 }
377 
378 
380 bool LWHist::LWHistAxis::isXAxis() const { return this==m_hist->m_decorations->m_xAxis; }
381 bool LWHist::LWHistAxis::isYAxis() const { return this==m_hist->m_decorations->m_yAxis; }
382 
384 {
385  const TAxis*a=rootAxis();
386  if (a)
387  return a->GetNbins();
388  return isXAxis() ? m_hist->actualGetNBinsX() : (isYAxis()?m_hist->actualGetNBinsY():0);
389 }
390 
392 {
393  const TAxis*a=rootAxis();
394  if (a)
395  return a->GetBinCenter(bin);
396  return isXAxis()
397  ? m_hist->actualGetBinCenterX(bin)
398  : (isYAxis()?m_hist->actualGetBinCenterY(bin):0.0);
399 }
400 
401 const char * LWHist::LWHistAxis::GetBinLabel(unsigned bin) const
402 {
403  const TAxis*a=rootAxis();
404  if (a)
405  return a->GetBinLabel(bin);
406  if (!m_binLabels)
407  return "";
408  return m_binLabels->getBinLabel(bin);
409 }
410 
411 void LWHist::LWHistAxis::SetBinLabel(unsigned bin, const char* label)
412 {
413  TAxis*a=rootAxis();
414  if (a) {
415  a->SetBinLabel(bin,label);
416  return;
417  }
418  if (!m_binLabels)
419  m_binLabels = MP_NEW(LWBinLabels);
420  m_binLabels->setBinLabel(bin,label);
421 }
422 
423 unsigned LWHist::LWHistAxis::FindBin(const double& x)
424 {
425  TAxis*a=rootAxis();
426  if (a)
427  return a->FindBin(x);
428  return isXAxis() ? m_hist->actualFindBinX(x) : (isYAxis()?m_hist->actualFindBinY(x):0);
429 }
430 
431 void LWHist::SetXTitle(const char *t)
432 {
433  TH1 * hroot= getROOTHistBaseNoAlloc();
434  if (hroot) {
435  hroot->SetXTitle(t);
436  return;
437  }
438  GetXaxis()->SetTitle(t);
439 }
440 void LWHist::SetYTitle(const char *t)
441 {
442  TH1 * hroot= getROOTHistBaseNoAlloc();
443  if (hroot) {
444  hroot->SetYTitle(t);
445  return;
446  }
447  GetYaxis()->SetTitle(t);
448 }
449 void LWHist::SetZTitle(const char *t)
450 {
451  TH1 * hroot= getROOTHistBaseNoAlloc();
452  if (hroot) {
453  hroot->SetZTitle(t);
454  return;
455  }
456  GetZaxis()->SetTitle(t);
457 }
458 
459 //____________________________________________________________________
460 void LWHist::SetOption(const char* option)
461 {
462  TH1 * hroot= getROOTHistBaseNoAlloc();
463  if (hroot) {
464  hroot->SetOption(option);
465  return;
466  }
469 }
470 
471 
472 
473 //____________________________________________________________________
475 {
476  if (m_decorations) {
477  if (usingROOTBackend()) {
478  delete m_decorations->m_xAxis;
480  delete m_decorations->m_yAxis;
482  delete m_decorations->m_zAxis;
484  delete m_decorations;
485  } else {
487  }
488  m_decorations = 0;
489  }
492 }
LWHist::LWHistAxis::GetNbins
unsigned GetNbins() const
Definition: LWHist.cxx:383
LWHist
Definition: LWHist.h:26
LWPools.h
LWHist::GetZaxis
LWHistAxis * GetZaxis()
Definition: LWHist.cxx:328
LWHist::SetZTitle
void SetZTitle(const char *)
Definition: LWHist.cxx:449
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
LWStrUtils.h
fitman.sz
sz
Definition: fitman.py:527
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
LWHistStats.h
LWHist::ensureInitDecorations
void ensureInitDecorations()
Definition: LWHist.cxx:90
DEFAULT_MAXIMUM
#define DEFAULT_MAXIMUM
Definition: LWHist.cxx:52
LWHist::LWHistDecorations::~LWHistDecorations
~LWHistDecorations()
Definition: LWHist.cxx:65
PlotCalibFromCool.label
label
Definition: PlotCalibFromCool.py:78
plotmaker.hist
hist
Definition: plotmaker.py:148
LWStrUtils::releaseString
static void releaseString(char *&c)
Definition: LWStrUtils.h:70
LWBinLabels
Definition: LWBinLabels.h:25
LWHist::GetMarkerSize
float GetMarkerSize() const
Definition: LWHist.cxx:113
bin
Definition: BinsDiffFromStripMedian.h:43
LWStrUtils::stringsEqual
static bool stringsEqual(const char *c1, const char *c2)
Definition: LWStrUtils.h:79
LWHist1D.h
LWHist::SetName
void SetName(const char *)
Definition: LWHist.cxx:222
LWPools::release
static void release(char *, unsigned length)
DEFAULT_MINIMUM
#define DEFAULT_MINIMUM
Definition: LWHist.cxx:51
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
DEFAULT_MARKERSIZE
#define DEFAULT_MARKERSIZE
Definition: LWHist.cxx:48
LWHist::LWHistDecorations::m_markerStyle
short m_markerStyle
Definition: LWHist.cxx:81
LWHist::SetOption
void SetOption(const char *option=" ")
Definition: LWHist.cxx:460
LWHist::LWHistDecorations::LWHistDecorations
LWHistDecorations()
Definition: LWHist.cxx:55
LWHist::LWHistDecorations::m_xAxis
LWHist::LWHistAxis * m_xAxis
Definition: LWHist.cxx:74
x
#define x
LWHist::LWHistDecorations::m_yAxis
LWHist::LWHistAxis * m_yAxis
Definition: LWHist.cxx:75
LWHist::LWHistAxis::~LWHistAxis
~LWHistAxis()
Definition: LWHist.cxx:379
LWHist::GetMarkerColor
short GetMarkerColor() const
Definition: LWHist.cxx:96
LWHist::SetMarkerStyle
void SetMarkerStyle(short s=1)
Definition: LWHist.cxx:133
LWHist::LWHistDecorations::LWHistDecorations
LWHistDecorations(const LWHistDecorations &)
LWHist::GetXaxis
LWHistAxis * GetXaxis()
Definition: LWHist.cxx:309
LWHist::usingROOTBackend
bool usingROOTBackend() const
Definition: LWHist.h:73
LWHist::GetTitle
const char * GetTitle() const
Definition: LWHist.cxx:372
LWHist::LWHistDecorations::m_markerColor
short m_markerColor
Definition: LWHist.cxx:80
MP_NEW
#define MP_NEW(Class)
Definition: LWPools.h:26
LWHist::LWHistAxis::m_title
char * m_title
Definition: LWHist.h:133
LWHist::LWHistAxis::isXAxis
bool isXAxis() const
Definition: LWHist.cxx:380
LWHist::LWHistAxis::SetTitle
void SetTitle(const char *)
Definition: LWHist.cxx:359
beamspotman.n
n
Definition: beamspotman.py:731
extractSporadic.h
list h
Definition: extractSporadic.py:97
LWBinLabels.h
LWHist::LWHistAxis::SetLabelSize
void SetLabelSize(float size=0.04)
Definition: LWHist.cxx:352
LWHist::LWHistAxis::rootAxis
const TAxis * rootAxis() const
Definition: LWHist.cxx:340
LWHist::LWHist
LWHist(const char *name, const char *title, bool rootbackend)
Definition: LWHist.cxx:189
LWHist::GetName
const char * GetName() const
Definition: LWHist.cxx:366
MP_DELETE
#define MP_DELETE(Ptr)
Definition: LWPools.h:27
LWHist::m_name
char * m_name
Definition: LWHist.h:99
LWHist::LWHistDecorations::m_minimum
float m_minimum
Definition: LWHist.cxx:77
DEFAULT_MARKERSTYLE
#define DEFAULT_MARKERSTYLE
Definition: LWHist.cxx:50
LWStrUtils::setStringFromInput
static void setStringFromInput(const char *input, char *&target)
Definition: LWStrUtils.h:58
LWHist::~LWHist
virtual ~LWHist()
Definition: LWHist.cxx:215
LWHist::m_usingROOTBackend
const bool m_usingROOTBackend
Definition: LWHist.h:94
LWHist::LWHistAxis
Definition: LWHist.h:109
LWHist::SetMinimum
void SetMinimum(const double &minimum=-1111)
Definition: LWHist.cxx:162
LWHist::SetNameTitle
void SetNameTitle(const char *name, const char *title)
Definition: LWHist.cxx:244
LWHist::SetXTitle
void SetXTitle(const char *)
Definition: LWHist.cxx:431
LWHist::LWHistAxis::isYAxis
bool isYAxis() const
Definition: LWHist.cxx:381
LWHist::SetMarkerSize
void SetMarkerSize(float sz=1)
Definition: LWHist.cxx:150
LWHistControls::releaseAllHeldMemory
static void releaseAllHeldMemory()
Definition: LWHistControls.cxx:41
LWHist::LWHistAxis::SetBinLabel
void SetBinLabel(unsigned bin, const char *label)
Definition: LWHist.cxx:411
LWHist::SetYTitle
void SetYTitle(const char *)
Definition: LWHist.cxx:440
LWHist::clear
virtual void clear()
Definition: LWHist.cxx:474
LWHist::SetMarkerColor
void SetMarkerColor(short c=1)
Definition: LWHist.cxx:121
LWHist::m_title
char * m_title
Definition: LWHist.h:100
LWHist::LWHistAxis::FindBin
unsigned FindBin(const double &x)
Definition: LWHist.cxx:423
LWHist::safeDelete
static void safeDelete(LWHist *)
Definition: LWHist.cxx:30
LWHist::LWHistAxis::m_labelSize
float m_labelSize
Definition: LWHist.h:132
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
a
TList * a
Definition: liststreamerinfos.cxx:10
h
DEFAULT_MARKERCOLOR
#define DEFAULT_MARKERCOLOR
Definition: LWHist.cxx:49
LWHist::LWHistDecorations::m_markerSize
float m_markerSize
Definition: LWHist.cxx:79
LWHist::LWHistAxis::LWHistAxis
LWHistAxis(const LWHistAxis &)
LWHist::apply
virtual bool apply(TH1 *) const
Definition: LWHist.cxx:256
LWHist::LWHistDecorations::operator=
LWHistDecorations & operator=(const LWHistDecorations &)
TH1
Definition: rootspy.cxx:268
LWHist::getROOTHistBaseNoAlloc
virtual const TH1 * getROOTHistBaseNoAlloc() const =0
LWHist::LWHistDecorations::m_zAxis
LWHist::LWHistAxis * m_zAxis
Definition: LWHist.cxx:76
LWHist::LWHistAxis::GetBinCenter
double GetBinCenter(int bin) const
Definition: LWHist.cxx:391
LWHist::LWHistDecorations::m_maximum
float m_maximum
Definition: LWHist.cxx:78
LWHist::m_decorations
LWHistDecorations * m_decorations
Definition: LWHist.h:101
LWHist::SetTitle
void SetTitle(const char *)
Definition: LWHist.cxx:233
LWHist::LWHistAxis::m_binLabels
LWBinLabels * m_binLabels
Definition: LWHist.h:134
LWHistControls.h
LWHist::GetMarkerStyle
short GetMarkerStyle() const
Definition: LWHist.cxx:105
LWHist::LWHistDecorations
Definition: LWHist.cxx:53
LWBinLabels::apply
void apply(TAxis *) const
Definition: LWBinLabels.cxx:38
LWHist::LWHistAxis::GetBinLabel
const char * GetBinLabel(unsigned bin) const
Definition: LWHist.cxx:401
python.compressB64.c
def c
Definition: compressB64.py:93
LWHist::GetYaxis
LWHistAxis * GetYaxis()
Definition: LWHist.cxx:319
LWHist.h
LWHistStats::s_nActiveHists
static std::atomic< long > s_nActiveHists
Definition: LWHistStats.h:36
LWHist::LWHistDecorations::m_option
char * m_option
Definition: LWHist.cxx:73
LWHist2D.h
LWHist::SetMaximum
void SetMaximum(const double &maximum=-1111)
Definition: LWHist.cxx:175