The Gaudi Framework  v37r1 (a7f61348)
Gaudi::Histograming::Sink::details Namespace Reference

Classes

struct  Axis
 Small helper struct representing the Axis of an Histogram. More...
 
struct  ProfileWrapper
 helper Wrapper around TProfileX to be able to fill it More...
 
struct  TraitsBase
 Common base for Traits dealing with Histogram conversions to Root Provides generic implementation for creating the histogram and filling meta data The filling (method fill) is not implemented. More...
 

Functions

Axis jsonToAxis (nlohmann::json &jAxis)
 extract an Axis from json data More...
 
template<typename Traits , std::size_t... index>
std::tuple< typename Traits::Histo, std::stringjsonToRootHistogramInternal (std::string &dir, std::string &name, nlohmann::json const &j, std::index_sequence< index... >)
 generic function to convert json to a ROOT Histogram - internal implementation More...
 
TDirectory * changeDir (TFile &file, std::string dir)
 changes to the ROOT directory given in the current ROOT file and returns the current directory before the change More...
 
template<typename Histo >
nlohmann::json allAxisTojson (Histo const &h)
 
template<typename Histo >
nlohmann::json binsTojson (Histo const &h)
 
template<typename Histo >
nlohmann::json rootHistogramToJson (Histo const &h)
 automatic translation of Root Histograms to json More...
 

Function Documentation

◆ allAxisTojson()

template<typename Histo >
nlohmann::json Gaudi::Histograming::Sink::details::allAxisTojson ( Histo const &  h)

Definition at line 252 of file Utils.h.

252  {
253  if constexpr ( std::is_base_of_v<TH3D, Histo> ) {
254  return { *h.GetXaxis(), *h.GetYaxis(), *h.GetZaxis() };
255  } else if constexpr ( std::is_base_of_v<TProfile2D, Histo> || std::is_base_of_v<TH2D, Histo> ) {
256  return { *h.GetXaxis(), *h.GetYaxis() };
257  } else {
258  return { *h.GetXaxis() };
259  }
260  }

◆ binsTojson()

template<typename Histo >
nlohmann::json Gaudi::Histograming::Sink::details::binsTojson ( Histo const &  h)

Definition at line 263 of file Utils.h.

263  {
264  if constexpr ( std::is_base_of_v<TProfile, Histo> || std::is_base_of_v<TProfile2D, Histo> ) {
266  // ROOT TProfile interface being completely inconsistent, we have to play
267  // with different ways to access values of the histogram... one per value !
268  auto* sums = h.GetArray();
269  auto* sums2 = h.GetSumw2();
270  for ( unsigned long n = 0; n < (unsigned long)h.GetSize(); n++ ) {
271  j.push_back( nlohmann::json{ { h.GetBinEntries( n ), sums[n] }, sums2->At( n ) } );
272  }
273  return j;
274  } else {
275  return gsl::span{ h.GetArray(), (unsigned long)h.GetSize() };
276  }
277  }

◆ changeDir()

TDirectory* Gaudi::Histograming::Sink::details::changeDir ( TFile &  file,
std::string  dir 
)
inline

changes to the ROOT directory given in the current ROOT file and returns the current directory before the change

Definition at line 225 of file Utils.h.

225  {
226  // remember the current directory
227  auto previousDir = gDirectory;
228  // find or create the directory for the histogram
229  using namespace ranges;
230  auto is_delimiter = []( auto c ) { return c == '/' || c == '.'; };
231  auto transform_to_string = views::transform( []( auto&& rng ) { return rng | to<std::string>; } );
232  auto currentDir = accumulate( dir | views::split_when( is_delimiter ) | transform_to_string,
233  file.GetDirectory( "" ), []( auto current, auto&& dir_level ) {
234  if ( current ) {
235  // try to get next level
236  auto nextDir = current->GetDirectory( dir_level.c_str() );
237  // if it does not exist, create it
238  if ( !nextDir ) nextDir = current->mkdir( dir_level.c_str() );
239  // move to next level
240  current = nextDir;
241  }
242  return current;
243  } );
244  if ( !currentDir )
245  throw GaudiException( "Could not create directory " + dir, "Histogram::Sink::Root", StatusCode::FAILURE );
246  // switch to the directory
247  currentDir->cd();
248  return previousDir;
249  }

◆ jsonToAxis()

Axis Gaudi::Histograming::Sink::details::jsonToAxis ( nlohmann::json &  jAxis)
inline

extract an Axis from json data

Definition at line 101 of file Utils.h.

101  {
102  return { jAxis.at( "nBins" ).get<unsigned int>(), jAxis.at( "minValue" ).get<double>(),
103  jAxis.at( "maxValue" ).get<double>(),
104  ";" + jAxis.at( "title" ).get<std::string>() }; // ";" to prepare concatenations of titles
105  }

◆ jsonToRootHistogramInternal()

template<typename Traits , std::size_t... index>
std::tuple<typename Traits::Histo, std::string> Gaudi::Histograming::Sink::details::jsonToRootHistogramInternal ( std::string dir,
std::string name,
nlohmann::json const &  j,
std::index_sequence< index... >   
)

generic function to convert json to a ROOT Histogram - internal implementation

Definition at line 111 of file Utils.h.

113  {
114  // extract data from json
115  auto jsonAxis = j.at( "axis" );
116  auto axis = std::array{ jsonToAxis( jsonAxis[index] )... };
117  auto weights = j.at( "bins" ).get<std::vector<typename Traits::WeightType>>();
118  auto title = j.at( "title" ).get<std::string>();
119  auto nentries = j.at( "nEntries" ).get<unsigned int>();
120  // weird way ROOT has to give titles to axis
121  title += ( axis[index].title + ... );
122  // compute total number of bins, multiplying bins per axis
123  auto totNBins = ( ( axis[index].nBins + 2 ) * ... );
124  assert( weights.size() == totNBins );
125 
126  if ( name[0] == '/' ) {
127  dir = "";
128  name = name.substr( 1 );
129  }
130 
131  // take into account the case where name contains '/'s (e.g. "Group/Name") by
132  // moving the prefix into dir
133  if ( auto pos = name.rfind( '/' ); pos != std::string::npos ) {
134  dir += '/' + name.substr( 0, pos );
135  name = name.substr( pos + 1 );
136  }
137 
138  // Create Root histogram calling constructors with the args tuple
139  auto histo = Traits::create( name, title, axis[index]... );
140 
141  // fill Histo
142  for ( unsigned int i = 0; i < totNBins; i++ ) Traits::fill( histo, i, weights[i] );
143  // fill histo metadata, e.g. bins and number of entries
144  Traits::fillMetaData( histo, jsonAxis, nentries );
145 
146  return { histo, dir };
147  }

◆ rootHistogramToJson()

template<typename Histo >
nlohmann::json Gaudi::Histograming::Sink::details::rootHistogramToJson ( Histo const &  h)

automatic translation of Root Histograms to json

Definition at line 281 of file Utils.h.

281  {
282  std::string type = std::is_base_of_v<TProfile, Histo> || std::is_base_of_v<TProfile2D, Histo>
283  ? "histogram:ProfileHistogram:double"
284  : "histogram:Histogram:double";
285  return nlohmann::json{ { "type", type },
286  { "title", h.GetTitle() },
287  { "dimension", h.GetDimension() },
288  { "empty", (int)h.GetEntries() == 0 },
289  { "nEntries", (int)h.GetEntries() },
290  { "axis", allAxisTojson( h ) },
291  { "bins", binsTojson( h ) } };
292  }
std::string
STL class.
bug_34121.name
name
Definition: bug_34121.py:20
Properties.long
long
(c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations # # This software i...
Definition: Properties.py:15
std::vector
STL class.
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:87
GaudiException
Definition: GaudiException.h:31
ranges
Definition: details.h:30
gaudirun.c
c
Definition: gaudirun.py:527
Gaudi::Histograming::Sink::details::binsTojson
nlohmann::json binsTojson(Histo const &h)
Definition: Utils.h:263
ProduceConsume.j
j
Definition: ProduceConsume.py:101
AlgSequencer.h
h
Definition: AlgSequencer.py:32
std::array
STL class.
Gaudi::Histograming::Sink::details::allAxisTojson
nlohmann::json allAxisTojson(Histo const &h)
Definition: Utils.h:252
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
Gaudi::Histograming::Sink::details::jsonToAxis
Axis jsonToAxis(nlohmann::json &jAxis)
extract an Axis from json data
Definition: Utils.h:101
gaudirun.type
type
Definition: gaudirun.py:162
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
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Accumulators::accumulate
void accumulate(Counter &counter, const Container &container, Fun f=Identity{})
A helper function for accumulating data from a container into a counter This is internally using buff...
Definition: Accumulators.h:1204