ATLAS Offline Software
HistogramFiller.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef AthenaMonitoringKernel_HistogramFiller_h
6 #define AthenaMonitoringKernel_HistogramFiller_h
7 
8 #include <functional>
9 #include <memory>
10 #include <vector>
12 
16 
17 class TProfile;
18 class TProfile2D;
19 
20 namespace Monitored {
21 
22  // Forward declare generic histogram filler (see HistogramFillerUtils.h)
23  namespace detail {
24  template<typename H, typename W, typename M, typename ...Ms>
25  void doFill(H* hist, W weight, size_t i, const M& m1, const Ms&... m);
26  template<typename W>
27  void doFill(TProfile*, W, size_t, const double&, const char* const&);
28  template<typename W>
29  void doFill(TProfile*, W, size_t, const char* const&, const char* const&);
30  template<typename W>
31  void doFill(TProfile2D*, W, size_t, const double&, const double&, const char* const&);
32  template<typename W>
33  void doFill(TProfile2D*, W, size_t, const char* const&, const char* const&, const char* const&);
34  template<typename W>
35  void doFill(TProfile2D*, W, size_t, const char* const&, const double&, const char* const&);
36  template<typename W>
37  void doFill(TProfile2D*, W, size_t, const double&, const char* const&, const char* const&);
38  }
39 
44  public:
50  HistogramFiller(const HistogramDef& histDef, std::shared_ptr<IHistogramProvider> histogramProvider)
51  : m_histDef(new HistogramDef(histDef)),
52  m_histogramProvider(histogramProvider) {}
59  : m_histDef(hf.m_histDef),
61 
65  virtual ~HistogramFiller() {}
66 
67 
71  struct VariablesPack {
73  VariablesPack( const std::initializer_list<const Monitored::IMonitoredVariable*>& v)
74  :var( v ) {}
75 
79  size_t size () const {
80  return var.size() - std::count( var.begin(), var.end(), nullptr );
81  }
82 
86  inline void reset() {
87  std::fill( var.begin(), var.end(), nullptr );
88  cut = nullptr;
89  weight = nullptr;
90  }
91 
95  inline void set( unsigned index, const IMonitoredVariable* ptr ) {
96  if ( ATH_UNLIKELY( var.size() <= index ) ) {
97  var.resize(index+1);
98  }
99  var[index] = ptr;
100  }
101 
105  std::vector<std::string> names() const {
106  std::vector<std::string> r;
107  for ( const auto* v: var )
108  if ( v != nullptr )
109  r.push_back( v->name() );
110  return r;
111  }
112 
113  std::vector<const Monitored::IMonitoredVariable*> var = { nullptr, nullptr, nullptr, nullptr };
116  };
117 
122  virtual unsigned fill( const VariablesPack& ) const = 0;
123 
127  void touch() const {
128  histogram<void>();
129  }
130 
131 
132  const std::vector<std::string>& histogramVariablesNames() const {
133  return m_histDef->name;
134  }
135 
136  const std::string& histogramWeightName() const {
137  return m_histDef->weight;
138  }
139 
140  const std::string& histogramCutMaskName() const {
141  return m_histDef->cutMask;
142  }
143 
144  const std::unique_lock<std::mutex> getLock() const {
145  return std::unique_lock(m_lock);
146  }
147 
148  protected:
149  template <class H>
150  H* histogram() const {
151  return static_cast<H*>(m_histogramProvider->histogram());
152  }
153 
154  // convenience function to provide a function that interprets the cutmask
155  std::pair<size_t, std::function<bool(size_t)>> getCutMaskFunc(const Monitored::IMonitoredVariable* mask ) const {
156  std::function<bool(size_t)> cutMaskValue = [] (size_t){ return true; }; // default is true
157  size_t maskSize = 1;
158  if ( mask != nullptr ) {
159  maskSize = mask->size();
160  if (maskSize == 1) {
161  if (!mask->get(0)) {
162  // globally fails cut; zero first argument is a signal that one can abort
163  return std::make_pair(0, [](size_t){ return false; });
164  // otherwise, default cutMaskValue is sufficient
165  }
166  } else {
167  return std::make_pair(maskSize, [mask](size_t i){ return static_cast<bool>(mask->get(i)); });
168  }
169  }
170  return std::make_pair(maskSize, cutMaskValue);
171  }
172 
183  template<class H, typename W, typename C, typename ...Ms>
184  unsigned fill(W weight, C cut, const Ms&... m) const {
185  auto hist = this->histogram<H>();
186 
187  size_t i = 0;
188  for (; i < std::max({m.size()...}); ++i ) {
189  if ( cut(i) ) {
190  detail::doFill(hist, weight, i, m...);
191  }
192  }
193  return i;
194  }
195 
196  std::shared_ptr<HistogramDef> m_histDef;
197  std::shared_ptr<IHistogramProvider> m_histogramProvider;
199 
200  private:
202  };
203 }
204 #endif /* AthenaMonitoringKernel_HistogramFiller_HistogramFiller_h */
beamspotman.r
def r
Definition: beamspotman.py:676
IHistogramProvider.h
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
Monitored::HistogramFiller::histogramVariablesNames
const std::vector< std::string > & histogramVariablesNames() const
Definition: HistogramFiller.h:132
Monitored::HistogramFiller::histogram
H * histogram() const
Definition: HistogramFiller.h:150
Monitored::HistogramFiller::m_histDef
std::shared_ptr< HistogramDef > m_histDef
Definition: HistogramFiller.h:196
IMonitoredVariable.h
Monitored::HistogramFiller::histogramWeightName
const std::string & histogramWeightName() const
Definition: HistogramFiller.h:136
Monitored::HistogramFiller::m_histogramProvider
std::shared_ptr< IHistogramProvider > m_histogramProvider
Definition: HistogramFiller.h:197
index
Definition: index.py:1
JetTiledMap::W
@ W
Definition: TiledEtaPhiMap.h:44
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
HistogramDef.h
plotmaker.hist
hist
Definition: plotmaker.py:148
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
Monitored::HistogramFiller::VariablesPack::cut
const Monitored::IMonitoredVariable * cut
pointer to cut mask variable, typically absent
Definition: HistogramFiller.h:115
Monitored::HistogramFiller::getLock
const std::unique_lock< std::mutex > getLock() const
Definition: HistogramFiller.h:144
Monitored::HistogramFiller::fill
unsigned fill(W weight, C cut, const Ms &... m) const
Fill histogram from IMonitoredVariable.
Definition: HistogramFiller.h:184
detail
Definition: extract_histogram_tag.cxx:14
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
Monitored::HistogramDef
the internal class used to keep parsed Filler properties
Definition: HistogramDef.h:15
Monitored::HistogramFiller::HistogramFiller
HistogramFiller(const HistogramFiller &hf)
Copy constructor.
Definition: HistogramFiller.h:58
Monitored::HistogramFiller::VariablesPack::set
void set(unsigned index, const IMonitoredVariable *ptr)
sets monitored variable at the index (may need to reszie vector of variables)
Definition: HistogramFiller.h:95
Monitored::HistogramFiller::VariablesPack::VariablesPack
VariablesPack()
Definition: HistogramFiller.h:72
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
AthUnlikelyMacros.h
Monitored::HistogramFiller::VariablesPack::weight
const Monitored::IMonitoredVariable * weight
pointer to weight variable, typically absent
Definition: HistogramFiller.h:114
Monitored::HistogramFiller::VariablesPack
helper class to pass variables to fillers
Definition: HistogramFiller.h:71
Monitored::detail::doFill
void doFill(H *hist, W weight, size_t i, const M &m1, const Ms &... m)
Perform (arbitrary dimension) histogram fill with weight.
Definition: HistogramFillerUtils.h:164
Monitored::IMonitoredVariable
Definition: IMonitoredVariable.h:14
H
#define H(x, y, z)
Definition: MD5.cxx:114
ROBbits::maskSize
const uint32_t maskSize
Definition: TrigMonROBData.cxx:17
python.changerun.m1
m1
Definition: changerun.py:32
lumiFormat.i
int i
Definition: lumiFormat.py:85
python.TrigEgammaMonitorHelper.TProfile
def TProfile(*args, **kwargs)
Definition: TrigEgammaMonitorHelper.py:81
Monitored
Generic monitoring tool for athena components.
Definition: GenericMonitoringTool.h:30
Monitored::HistogramFiller::histogramCutMaskName
const std::string & histogramCutMaskName() const
Definition: HistogramFiller.h:140
Monitored::HistogramFiller::VariablesPack::size
size_t size() const
number of variables in the pack ( not counting the weight and mask )
Definition: HistogramFiller.h:79
BindingsTest.cut
cut
This script demonstrates how to call a C++ class from Python Also how to use PyROOT is shown.
Definition: BindingsTest.py:13
Monitored::HistogramFiller::fill
virtual unsigned fill(const VariablesPack &) const =0
Method that actually fills the ROOT object.
Monitored::HistogramFiller::VariablesPack::names
std::vector< std::string > names() const
names of all varaibles stored
Definition: HistogramFiller.h:105
fill
void fill(H5::Group &out_file, size_t iterations)
Definition: test-hdf5-writer.cxx:95
Monitored::HistogramFiller::m_lock
std::mutex m_lock
Definition: HistogramFiller.h:198
Monitored::HistogramFiller
Base class for all histogram fillers.
Definition: HistogramFiller.h:43
Monitored::HistogramFiller::operator=
HistogramFiller & operator=(HistogramFiller const &)=delete
python.PyAthena.v
v
Definition: PyAthena.py:154
Monitored::HistogramFiller::VariablesPack::var
std::vector< const Monitored::IMonitoredVariable * > var
storage for variables, default size of 4, serves all histograming uses
Definition: HistogramFiller.h:113
DeMoScan.index
string index
Definition: DeMoScan.py:364
Monitored::HistogramFiller::touch
void touch() const
Ensure histogram exists.
Definition: HistogramFiller.h:127
Monitored::HistogramFiller::VariablesPack::reset
void reset()
removes all monitored variables
Definition: HistogramFiller.h:86
Monitored::HistogramFiller::VariablesPack::VariablesPack
VariablesPack(const std::initializer_list< const Monitored::IMonitoredVariable * > &v)
Definition: HistogramFiller.h:73
Monitored::HistogramFiller::getCutMaskFunc
std::pair< size_t, std::function< bool(size_t)> > getCutMaskFunc(const Monitored::IMonitoredVariable *mask) const
Definition: HistogramFiller.h:155
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
Monitored::HistogramFiller::HistogramFiller
HistogramFiller(const HistogramDef &histDef, std::shared_ptr< IHistogramProvider > histogramProvider)
Default constructor.
Definition: HistogramFiller.h:50
Monitored::HistogramFiller::~HistogramFiller
virtual ~HistogramFiller()
Virtual destructor.
Definition: HistogramFiller.h:65