The Gaudi Framework  v36r9p1 (5c15b2bb)
Gaudi::Histograming::Sink::details Namespace Reference

Classes

struct  Axis
 

Functions

Axis toAxis (nlohmann::json &jAxis)
 
template<typename Traits , std::size_t... index>
void saveRootHistoInternal (TFile &file, std::string dir, std::string name, nlohmann::json const &j, std::index_sequence< index... >)
 generic function to handle histograms - internal implemenatation More...
 
template<typename Traits >
void saveRootHisto (TFile &file, std::string dir, std::string name, nlohmann::json &j)
 generic method to save histograms to files More...
 

Function Documentation

◆ saveRootHisto()

template<typename Traits >
void Gaudi::Histograming::Sink::details::saveRootHisto ( TFile &  file,
std::string  dir,
std::string  name,
nlohmann::json &  j 
)

generic method to save histograms to files

Can be used in most cases as the handler function to register into Sink::Base contains all the boiler plate code and redirects specific code to the Traits template

The Traits type must provide the following static functions :

  • Histo create( std::string& name, std::string& title, Axis& axis... ) it should intanciate a new ROOT histogram instance and return it
  • void fillMetaData( Histo& histo, nlohmann::json const& jsonAxis, unsigned int nentries) it should fill metadata in the ROOT histo histogram provides from the list of axis number of entries
  • void fill( Histo& histo, unsigned int i, const WeightType& weight ) it should fill the given bin with the given value Last point, it should have a static constexpr unsigned int Dimension

Definition at line 152 of file RootHistogramSinkBase.h.

152  {
153  details::saveRootHistoInternal<Traits>( file, std::move( dir ), std::move( name ), j,
154  std::make_index_sequence<Traits::Dimension>() );
155  }

◆ saveRootHistoInternal()

template<typename Traits , std::size_t... index>
void Gaudi::Histograming::Sink::details::saveRootHistoInternal ( TFile &  file,
std::string  dir,
std::string  name,
nlohmann::json const &  j,
std::index_sequence< index... >   
)

generic function to handle histograms - internal implemenatation

Definition at line 68 of file RootHistogramSinkBase.h.

69  {
70  // extract data from json
71  auto jsonAxis = j.at( "axis" );
72  auto axis = std::array{ toAxis( jsonAxis[index] )... };
73  auto weights = j.at( "bins" ).get<std::vector<typename Traits::WeightType>>();
74  auto title = j.at( "title" ).get<std::string>();
75  auto nentries = j.at( "nEntries" ).get<unsigned int>();
76  // weird way ROOT has to give titles to axis
77  title += ( axis[index].title + ... );
78  // compute total number of bins, multiplying bins per axis
79  auto totNBins = ( ( axis[index].nBins + 2 ) * ... );
80  assert( weights.size() == totNBins );
81 
82  if ( name[0] == '/' ) {
83  dir = "";
84  name = name.substr( 1 );
85  }
86 
87  // take into account the case where name contains '/'s (e.g. "Group/Name") by
88  // moving the prefix into dir
89  if ( auto pos = name.rfind( '/' ); pos != std::string::npos ) {
90  dir += '/' + name.substr( 0, pos );
91  name = name.substr( pos + 1 );
92  }
93 
94  // remember the current directory
95  auto previousDir = gDirectory;
96 
97  // find or create the directory for the histogram
98  {
99  using namespace ranges;
100  auto is_delimiter = []( auto c ) { return c == '/' || c == '.'; };
101  auto transform_to_string = views::transform( []( auto&& rng ) { return rng | to<std::string>; } );
102 
103  auto currentDir = accumulate( dir | views::split_when( is_delimiter ) | transform_to_string,
104  file.GetDirectory( "" ), []( auto current, auto&& dir_level ) {
105  if ( current ) {
106  // try to get next level
107  auto nextDir = current->GetDirectory( dir_level.c_str() );
108  // if it does not exist, create it
109  if ( !nextDir ) nextDir = current->mkdir( dir_level.c_str() );
110  // move to next level
111  current = nextDir;
112  }
113  return current;
114  } );
115 
116  if ( !currentDir )
117  throw GaudiException( "Could not create directory " + dir, "Histogram::Sink::Root", StatusCode::FAILURE );
118  // switch to the directory
119  currentDir->cd();
120  }
121 
122  // Create Root histogram calling constructors with the args tuple
123  auto histo = Traits::create( name, title, axis[index]... );
124 
125  // fill Histo
126  for ( unsigned int i = 0; i < totNBins; i++ ) Traits::fill( histo, i, weights[i] );
127  // fill histo metadata, e.g. bins and number of entries
128  Traits::fillMetaData( histo, jsonAxis, nentries );
129  // write to file
130  histo.Write();
131 
132  // switch back to the previous directory
133  previousDir->cd();
134  }

◆ toAxis()

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

Definition at line 58 of file RootHistogramSinkBase.h.

58  {
59  return { jAxis.at( "nBins" ).get<unsigned int>(), jAxis.at( "minValue" ).get<double>(),
60  jAxis.at( "maxValue" ).get<double>(),
61  ";" + jAxis.at( "title" ).get<std::string>() }; // ";" to prepare concatenations of titles
62  }
std::string
STL class.
std::move
T move(T... args)
HistoEx.histo
histo
Definition: HistoEx.py:105
std::vector
STL class.
GaudiException
Definition: GaudiException.h:31
ranges
Definition: FunctionalDetails.h:34
gaudirun.c
c
Definition: gaudirun.py:525
TimingHistograms.name
name
Definition: TimingHistograms.py:25
std::vector::at
T at(T... args)
std::array
STL class.
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
Gaudi::Histograming::Sink::details::toAxis
Axis toAxis(nlohmann::json &jAxis)
Definition: RootHistogramSinkBase.h:58
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:1195