Jpp
Public Member Functions | Static Public Member Functions | List of all members
Head Struct Reference

The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred to as "tags") and values. More...

#include <Head.hh>

Inheritance diagram for Head:
TObject std::map< std::string, std::string > JAANET::JHead JAANET::getCorsikaHeader JAANET::getDAQHeader JAANET::getGenhenHeader JAANET::getGenieHeader JAANET::getGSeaGenHeader JAANET::getKM3Header JAANET::getKM3SimHeader JAANET::getMUPAGEHeader JAANET::getSireneHeader JAANET::JWeightEventHelper

Public Member Functions

bool have_line (std::string key) const
 Check availability of data with the given key. More...
 
const std::string & get_line (std::string key) const
 Get data with the given key. More...
 
std::string & get_line (std::string key)
 Get data with the given key. More...
 
void set_line (std::string key, std::string line)
 Set data with the given key. More...
 
std::string get_field (std::string key, int idx) const
 Get data with the given key at given index. More...
 
int get_index_of_field (std::string key, std::string field) const
 Get index of data with the given key at given field. More...
 
std::string get_field (std::string key, std::string field) const
 Get data with the given key at given field. More...
 
void set_field (std::string key, std::string field, std::string value)
 Set data with the given key at given field. More...
 
void print (std::ostream &out=std::cout) const
 Print header. More...
 
double ngen () const
 Get the number of generated events needed for computing event rates. More...
 
double daq_livetime () const
 Get the the live time provided by the DAQ sytstem (=number of processed timeslices * frametime). More...
 
double mc_livetime () const
 Get the Monte Carlo live time. More...
 
Vec coord_origin () const
 Get coordinate origin. More...
 
Vec translate () const
 Get coordinate translation. More...
 
virtual ~Head ()
 
 ClassDef (Head, 2)
 

Static Public Member Functions

static std::map< std::string, std::vector< std::string > > & _hdr_dict ()
 Get internal description of the known lines in header. More...
 

Detailed Description

The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred to as "tags") and values.

Definition at line 39 of file Head.hh.

Constructor & Destructor Documentation

◆ ~Head()

virtual Head::~Head ( )
inlinevirtual

Definition at line 309 of file Head.hh.

309 {}

Member Function Documentation

◆ have_line()

bool Head::have_line ( std::string  key) const
inline

Check availability of data with the given key.

Parameters
keykey
Returns
true if data are available; else false

Definition at line 48 of file Head.hh.

49  {
50  return count( key ) != 0;
51  }

◆ get_line() [1/2]

const std::string& Head::get_line ( std::string  key) const
inline

Get data with the given key.


This method throws a run-time exception if no data are available.

Parameters
keykey
Returns
data

Definition at line 60 of file Head.hh.

61  {
62  return this->at(key);
63  }

◆ get_line() [2/2]

std::string& Head::get_line ( std::string  key)
inline

Get data with the given key.


This method throws a run-time exception if no data are available.

Parameters
keykey
Returns
data

Definition at line 72 of file Head.hh.

73  {
74  return this->at(key);
75  }

◆ set_line()

void Head::set_line ( std::string  key,
std::string  line 
)
inline

Set data with the given key.

Parameters
keykey
linedata

Definition at line 83 of file Head.hh.

84  {
86  }

◆ get_field() [1/2]

std::string Head::get_field ( std::string  key,
int  idx 
) const
inline

Get data with the given key at given index.


This method throws a run-time exception if no data are available.

Parameters
keykey
idxindex
Returns
data

Definition at line 96 of file Head.hh.

97  {
98  using namespace std;
99 
101 
102  if ( idx < 0 || idx > int ( v.size() ) )
103  {
104  THROW(Exception, "Cannot find word number " << idx << " in line " << get_line(key) << " for key: " << key);
105  }
106  return v[idx];
107  }

◆ get_index_of_field()

int Head::get_index_of_field ( std::string  key,
std::string  field 
) const
inline

Get index of data with the given key at given field.


Note that this method uses the dictionary define in method Head::_hdr_dict.

Parameters
keykey
fieldfield
Returns
index (-1 if not present)

Definition at line 118 of file Head.hh.

119  {
120  auto v = _hdr_dict()[key];
121  auto i = std::find (v.begin(), v.end(), field );
122  if (i== v.end()) return -1;
123  return i - v.begin();
124  }

◆ get_field() [2/2]

std::string Head::get_field ( std::string  key,
std::string  field 
) const
inline

Get data with the given key at given field.


This method throws a run-time exception if no field is available.

Note that this method uses the dictionary define in method Head::_hdr_dict.

Parameters
keykey
fieldfield
Returns
data

Definition at line 136 of file Head.hh.

137  {
138  int idx = get_index_of_field(key, field);
139 
140  if ( idx == -1 )
141  {
142  THROW(Exception, "Failed to find" << key << " " << field);
143  }
144 
145  return get_field( key, idx );
146  }

◆ set_field()

void Head::set_field ( std::string  key,
std::string  field,
std::string  value 
)
inline

Set data with the given key at given field.


This method throws a run-time exception if no field available.

Note that this method uses the dictionary define in method Head::_hdr_dict.

Parameters
keykey
fieldfield
valuevakue

Definition at line 159 of file Head.hh.

160  {
161  using namespace std;
162 
163  if ( field == "" ) get_line( key ) = value;
164 
165  int idx = get_index_of_field( key, field );
166 
167  if ( idx < 0 )
168  {
169  THROW(Exception, "GFailed to find field in header line: " << key << " " << field);
170  }
171 
172  vector<string> vals = splitstring( get_line( key ) );
173 
174  // if the fields before do not exist, add padding
175  while ( int( vals.size() ) <= idx ) vals.push_back("0");
176 
177  vals[idx] = value;
178  ostringstream ss;
179  for( auto v : vals )
180  {
181  ss << v;
182  }
183  set_line( key, ss.str() );
184 
185  }

◆ print()

void Head::print ( std::ostream &  out = std::cout) const
inline

Print header.

Parameters
outoutput stream

Definition at line 192 of file Head.hh.

193  {
194  if (count("start_run")) out<< "start_run: " << at("start_run") << std::endl;
195 
196  for( auto& p : *this )
197  {
198  if ( p.first == "start_run" || p.first == "end_event" ) continue;
199  out<< p.first << ": " << p.second << std::endl ;
200  }
201  out<< "end_event:" << std::endl;
202  }

◆ _hdr_dict()

static std::map<std::string,std::vector<std::string> >& Head::_hdr_dict ( )
inlinestatic

Get internal description of the known lines in header.

Returns
internal dictionary

Definition at line 209 of file Head.hh.

210  {
211  using namespace std;
212 
213  // map with, for each tag (key), a vector of field-names
214 
215  static map<string,vector<string> > r;
216  if ( r.size() > 0 ) return r;
217 
218  string desc =
219  "DAQ:livetime\n"
220  "cut_primary cut_seamuon cut_in cut_nu:Emin Emax cosTmin cosTmax\n"
221  "generator physics simul: program version date time\n"
222  "seed:program level iseed\n"
223  "PM1_type_area:type area TTS\n"
224  "PDF:i1 i2\n"
225  "model:interaction muon scattering numberOfEnergyBins\n"
226  "can:zmin zmax r\n"
227  "genvol:zmin zmax r volume numberOfEvents\n"
228  "merge:time gain\n"
229  "coord_origin:x y z\n"
230  "translate:x y z\n"
231  "genhencut:gDir Emin\n"
232  "k40: rate time\n"
233  "norma:primaryFlux numberOfPrimaries\n"
234  "livetime:numberOfSeconds errorOfSeconds\n"
235  "flux:type key file_1 file_2\n"
236  "spectrum:alpha\n"
237  "fixedcan:xcenter ycenter zmin zmax radius\n"
238  "start_run:run_id";
239 
240  for( auto line: splitstring(desc,'\n') )
241  {
242  auto v = splitstring( line, ':');
243 
244  vector< string > fields = splitstring( v[1] );
245  for( auto key : splitstring( v[0] ) )
246  {
247  r[key] = fields;
248  }
249  }
250  return r;
251  }

◆ ngen()

double Head::ngen ( ) const
inline

Get the number of generated events needed for computing event rates.

Returns
number of events

Definition at line 259 of file Head.hh.

260  {
261  return stod ( get_field("genvol","numberOfEvents") );
262  }

◆ daq_livetime()

double Head::daq_livetime ( ) const
inline

Get the the live time provided by the DAQ sytstem (=number of processed timeslices * frametime).

Returns
live time [s]

Definition at line 269 of file Head.hh.

270  {
271  return stod ( get_field("DAQ","livetime") );
272  }

◆ mc_livetime()

double Head::mc_livetime ( ) const
inline

Get the Monte Carlo live time.

Returns
live time [s]

Definition at line 280 of file Head.hh.

281  {
282  return stod ( get_field("livetime","numberOfSeconds") );
283  }

◆ coord_origin()

Vec Head::coord_origin ( ) const
inline

Get coordinate origin.

Returns
position

Definition at line 290 of file Head.hh.

291  {
292  return Vec( stod( get_field("coord_origin","x") ),
293  stod( get_field("coord_origin","y") ),
294  stod( get_field("coord_origin","z") ));
295  }

◆ translate()

Vec Head::translate ( ) const
inline

Get coordinate translation.

Returns
translation

Definition at line 302 of file Head.hh.

303  {
304  return Vec( stod( get_field("translate","x") ),
305  stod( get_field("translate","y") ),
306  stod( get_field("translate","z") ));
307  }

◆ ClassDef()

Head::ClassDef ( Head  ,
 
)

The documentation for this struct was generated from the following file:
Head::_hdr_dict
static std::map< std::string, std::vector< std::string > > & _hdr_dict()
Get internal description of the known lines in header.
Definition: Head.hh:209
std::vector
Definition: JSTDTypes.hh:12
splitstring
std::vector< std::string > splitstring(const std::string &str, char delim=' ')
Split string into separate tokens.
Definition: Head.hh:23
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
Exception
General exception.
Definition: Exception.hh:13
std::map
Definition: JSTDTypes.hh:16
Head::get_line
const std::string & get_line(std::string key) const
Get data with the given key.
Definition: Head.hh:60
JTOOLS::v
data_type v[N+1][M+1]
Definition: JPolint.hh:707
Head::get_field
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition: Head.hh:96
Head::get_index_of_field
int get_index_of_field(std::string key, std::string field) const
Get index of data with the given key at given field.
Definition: Head.hh:118
std
Definition: jaanetDictionary.h:36
Vec
The Vec class is a straightforward 3-d vector, which also works in pyroot.
Definition: Vec.hh:12
JTOOLS::r
data_type r[M+1]
Definition: JPolint.hh:709
Head::set_line
void set_line(std::string key, std::string line)
Set data with the given key.
Definition: Head.hh:83