The Gaudi Framework  v36r9p1 (5c15b2bb)
GaudiHistos_1DFixedBinning.icpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ==================================== 1D ====================================
12 // ================================ Fixed Binning =============================
13 // ============================================================================
14 // book the 1D histogram (book on demand)
15 // ============================================================================
16 template <class PBASE>
17 AIDA::IHistogram1D* GaudiHistos<PBASE>::book1D( const std::string& title, const double low, const double high,
18  const unsigned long bins ) const {
19  //
20  if ( !produceHistos() ) { return nullptr; } // RETURN
21  //
22  // exist?
23  auto hist = histo1D( title );
24  // histogram is already booked
25  if ( hist ) { return hist; } // RETURN !!
26 
27  // propose the histogram ID
28  HistoID ID;
29  newHistoID( title, ID );
30 
31  // Create a new histogram and return
32  return this->book1D( ID, title, low, high, bins );
33 }
34 // ============================================================================
35 // book the 1D histogram with forced ID (book on demand)
36 // ============================================================================
37 template <class PBASE>
38 AIDA::IHistogram1D* GaudiHistos<PBASE>::book1D( const HistoID& ID, const std::string& title, const double low,
39  const double high, const unsigned long bins ) const {
40  //
41  if ( !produceHistos() ) { return nullptr; } // RETURN
42  //
43 
44  // Check ID
45  if ( ID.undefined() ) {
46  this->Error( "Undefined Histogram ID : Title='" + title + "'" ).ignore();
47  return nullptr;
48  }
49 
50  // exist?
51  auto* hist = histo1D( ID );
52  // histogram is already booked
53  if ( hist ) { return hist; } // RETURN !!
54 
55  // Histogram title
56  const std::string& htitle = ( title.empty() ? "Unnamed 1D Histogram ID=" + ID.idAsString() : title );
57 
58  // book the histogram
59  if ( ID.numeric() ) {
60  hist = this->histoSvc()->book( histoPath(), ID.numericID(), htitle, bins, low, high );
61  } else if ( ID.literal() ) {
62  hist = this->histoSvc()->book( histoPath() + "/" + ID.literalID(), htitle, bins, low, high );
63  }
64 
65  // check OK
66  if ( !hist ) {
67  this->Error( "IHistogram1D* points to NULL! ID='" + ID.idAsString() + "' title='" + htitle + "'" ).ignore();
68  return nullptr;
69  } // RETURN !!
70 
71  // add histogram into histogram storages
72  m_histo1DMapID[ID] = hist;
73  m_histo1DMapTitle[title] = hist;
74 
75  // Declare to monitoring service
76  monitorHisto( Gaudi::Utils::Histos::toBase( hist ), ID );
77 
78  // Printout and return
79  if ( this->msgLevel( MSG::DEBUG ) ) {
80  this->debug() << "Booked 1D Histogram : ID='" << ID << "' Path=" << histoPath() << " Title='"
81  << Gaudi::Utils::Histos::htitle( hist ) << "'" << endmsg;
82  }
83  return hist;
84 }
85 // ============================================================================
86 // fill the 1D histogram with the value and weight
87 // ============================================================================
88 template <class PBASE>
89 AIDA::IHistogram1D* GaudiHistos<PBASE>::fill( AIDA::IHistogram1D* histo, const double value, const double weight,
90  const std::string& title ) const {
91  if ( !histo ) { return nullptr; } // RETURN
92  //
93  if ( !checkForNaN() ) {
94  Gaudi::Utils::Histos::fill( histo, value, weight );
95  } else if ( std::isfinite( value ) && std::isfinite( weight ) ) {
96  Gaudi::Utils::Histos::fill( histo, value, weight );
97  } else if ( std::isnan( value ) || std::isnan( weight ) ) {
98  this->Warning( "fill():: 'NaN' value is skipped from the histogram '" +
99  Gaudi::Utils::Histos::htitle( histo, title ) + "'" )
100  .ignore();
101  } else {
102  this->Warning( "fill():: 'Infinite' value is skipped from the histogram '" +
103  Gaudi::Utils::Histos::htitle( histo, title ) + "'" )
104  .ignore();
105  }
106  // return
107  return histo;
108 }
109 // ============================================================================
110 // fill the 1D histogram (book on demand)
111 // ============================================================================
112 template <class PBASE>
113 AIDA::IHistogram1D* GaudiHistos<PBASE>::plot1D( const double value, const std::string& title, const double low,
114  const double high, const unsigned long bins,
115  const double weight ) const {
116  AIDA::IHistogram1D* h( nullptr );
117  if ( produceHistos() ) {
118  // retrieve or book the histogram
119  h = histo1D( title );
120  if ( !h ) { h = book1D( title, low, high, bins ); }
121  // fill the histogram
122  h = fill( h, value, weight, title );
123  }
124  return h;
125 }
126 // ============================================================================
127 // fill the 1D histogram with forced ID assignment (book on demand)
128 // ============================================================================
129 template <class PBASE>
130 AIDA::IHistogram1D* GaudiHistos<PBASE>::plot1D( const double value, const HistoID& ID, const std::string& title,
131  const double low, const double high, const unsigned long bins,
132  const double weight ) const {
133  AIDA::IHistogram1D* h( nullptr );
134  if ( produceHistos() ) {
135  // retrieve or book the histogram
136  h = histo1D( ID );
137  if ( !h ) { h = book1D( ID, title, low, high, bins ); }
138  // fill
139  h = fill( h, value, weight, title );
140  }
141  return h;
142 }
143 // ============================================================================
144 // book the 1D histogram
145 // ============================================================================
146 template <class PBASE>
147 AIDA::IHistogram1D* GaudiHistos<PBASE>::book( const Gaudi::Histo1DDef& hdef ) const {
148  return book1D( hdef.title(), hdef.lowEdge(), hdef.highEdge(), hdef.bins() );
149 }
150 // ============================================================================
151 // book the 1D histogram with forced ID
152 // ============================================================================
153 template <class PBASE>
154 AIDA::IHistogram1D* GaudiHistos<PBASE>::book( const HistoID& ID, const Gaudi::Histo1DDef& hdef ) const {
155  return book1D( ID, hdef.title(), hdef.lowEdge(), hdef.highEdge(), hdef.bins() );
156 }
157 // ============================================================================
158 // fill the 1D histogram (book on demand)
159 // ============================================================================
160 template <class PBASE>
161 AIDA::IHistogram1D* GaudiHistos<PBASE>::plot1D( const double value, const Gaudi::Histo1DDef& hdef,
162  const double weight ) const {
163  return plot1D( value, hdef.title(), hdef.lowEdge(), hdef.highEdge(), hdef.bins(), weight );
164 }
165 // ============================================================================
166 // fill the 1D histogram with forced ID assignment (book on demand)
167 // ============================================================================
168 template <class PBASE>
169 AIDA::IHistogram1D* GaudiHistos<PBASE>::plot1D( const double value, const HistoID& ID, const Gaudi::Histo1DDef& hdef,
170  const double weight ) const {
171  return plot1D( value, ID, hdef.title(), hdef.lowEdge(), hdef.highEdge(), hdef.bins(), weight );
172 }
173 // ============================================================================
174 // The END
175 // ============================================================================
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
GaudiHistos::plot1D
AIDA::IHistogram1D * plot1D(const double value, const std::string &title, const double low, const double high, const unsigned long bins=100, const double weight=1.0) const
fill the 1D histogram (book on demand)
Definition: GaudiHistos_1DFixedBinning.icpp:113
std::string
STL class.
GaudiAlg::ID
Definition: GaudiHistoID.h:53
HistoEx.histo
histo
Definition: HistoEx.py:105
GaudiAlg::ID::numericID
NumericID numericID() const noexcept
Returns the numerical ID.
Definition: GaudiHistoID.h:82
Gaudi::Histo1DDef
Definition: HistoDef.h:41
std::isnan
T isnan(T... args)
std::isfinite
T isfinite(T... args)
Gaudi::Utils::Histos::toBase
GAUDI_API AIDA::IBaseHistogram * toBase(AIDA::IHistogram1D *histo)
Definition: Fill.cpp:165
AlgSequencer.h
h
Definition: AlgSequencer.py:32
GaudiAlg::ID::undefined
bool undefined() const noexcept
Is this ID undefined.
Definition: GaudiHistoID.h:78
Gaudi::Utils::Histos::htitle
GAUDI_API std::string htitle(const AIDA::IBaseHistogram *histo, const std::string &title="")
get the title
Definition: Fill.cpp:119
GaudiHistos::book
AIDA::IHistogram1D * book(const std::string &title, const double low=0, const double high=100, const unsigned long bins=100) const
book the 1D histogram
Definition: GaudiHistos.h:1859
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
Gaudi::Histo1DDef::highEdge
double highEdge() const
get the high edge
Definition: HistoDef.h:65
Gaudi::Histo1DDef::lowEdge
double lowEdge() const
get the low edge
Definition: HistoDef.h:63
std::string::empty
T empty(T... args)
Gaudi::Histo1DDef::bins
int bins() const
get the number of bins
Definition: HistoDef.h:67
Gaudi::Histo1DDef::title
const std::string & title() const
get the title
Definition: HistoDef.h:69
Gaudi::Utils::Histos::fill
GAUDI_API void fill(AIDA::IHistogram1D *histo, const double value, const double weight=1.0)
simple function to fill AIDA::IHistogram1D objects
Definition: Fill.cpp:45
GaudiHistos::book1D
AIDA::IHistogram1D * book1D(const std::string &title, const double low=0, const double high=100, const unsigned long bins=100) const
book the 1D histogram
Definition: GaudiHistos_1DFixedBinning.icpp:17
GaudiAlg::ID::literalID
const LiteralID & literalID() const noexcept
Returns the ID as a LiteralID.
Definition: GaudiHistoID.h:80
GaudiAlg::ID::idAsString
GAUDI_API LiteralID idAsString() const
Return ID as string, for both numeric and literal IDs.
Definition: GaudiHistoID.cpp:30
GaudiAlg::ID::numeric
bool numeric() const noexcept
Is this ID numeric.
Definition: GaudiHistoID.h:74
GaudiAlg::ID::literal
bool literal() const noexcept
Is this ID numeric.
Definition: GaudiHistoID.h:76
GaudiHistos::fill
AIDA::IHistogram1D * fill(AIDA::IHistogram1D *histo, const double value, const double weight, const std::string &title="") const
fill the 1D histogram with the value and weight
Definition: GaudiHistos_1DFixedBinning.icpp:89