Jpp  18.1.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | 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::JEvtWeightHelper JSUPPORT::JEvtWeightFileScanner< JFileScanner_t >

Classes

struct  tags
 

Public Member Functions

bool have_line (std::string key) const
 Check availability of data with the given key. More...
 
const std::stringget_line (std::string key) const
 Get data with the given key. More...
 
std::stringget_line (std::string key)
 Get data with the given key. More...
 
std::vector< std::stringmatching_keys (const std::string &tag) const
 In case of duplicate keys, they are internally stored in the map with a suffix "_n". More...
 
std::vector< std::stringget_lines (const std::string &tag) const
 Get all data compatible with the given key. More...
 
std::string set_line (std::string tag, std::string line, bool ensure_unique=true)
 Set data with the given tag. 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...
 
static void actionAtFileOpen (int version)
 Action method at file open. More...
 

Static Public Attributes

static int ROOT_IO_VERSION = -1
 Streamer version as obtained from ROOT file. 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 65 of file Head.hh.

Constructor & Destructor Documentation

virtual Head::~Head ( )
inlinevirtual

Definition at line 414 of file Head.hh.

414 {}

Member Function Documentation

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 78 of file Head.hh.

79  {
80  return count( key ) != 0;
81  }
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 90 of file Head.hh.

91  {
92  return this->at(key);
93  }
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 102 of file Head.hh.

103  {
104  return this->at(key);
105  }
std::vector< std::string> Head::matching_keys ( const std::string tag) const
inline

In case of duplicate keys, they are internally stored in the map with a suffix "_n".

This function returns all the keys that start with 'key' and end in "_n", with n an integer

Parameters
tagtag (without suffix)

Definition at line 116 of file Head.hh.

117  {
119 
120  auto match = [&] (const std::string & key) {
121 
122  if (key == tag) return true;
123 
124  if ( key.find( tag ) != 0 ) return false;
125 
126  // what is left should be of the form _d(ddd)
127  std::string left = key.substr( tag.length(), key.length() );
128  if (left.length() < 2 || left[0] != '_' ) return false ;
129  for ( unsigned i = 1; i < left.length(); i++ )
130  {
131  if (!std::isdigit( left[i] )) return false ;
132  }
133  return true;
134  };
135 
136  for ( auto& p : *this )
137  {
138  if ( match( p.first ) ) r.push_back( p.first );
139  }
140 
141  return r;
142  }
data_type r[M+1]
Definition: JPolint.hh:779
then awk string
std::vector< std::string > Head::get_lines ( const std::string tag) const
inline

Get all data compatible with the given key.

This means all data that is internally stored with "key_n", with n an integer
This method throws a run-time exception if no data are available.

Parameters
tagtag (without suffix)
Returns
data

Definition at line 155 of file Head.hh.

156  {
158 
159  for ( auto& key : matching_keys( tag ) ) {
160  r.push_back( get_line( key ) );
161  }
162 
163  return r;
164  }
data_type r[M+1]
Definition: JPolint.hh:779
std::vector< std::string > matching_keys(const std::string &tag) const
In case of duplicate keys, they are internally stored in the map with a suffix &quot;_n&quot;.
Definition: Head.hh:116
const std::string & get_line(std::string key) const
Get data with the given key.
Definition: Head.hh:90
std::string Head::set_line ( std::string  tag,
std::string  line,
bool  ensure_unique = true 
)
inline

Set data with the given tag.

The function will return the actual key that is used internally to store the result, which is equal to the tag with an optional "_n" added to ensure uniqueness.

Parameters
tagtag
linedata
ensure_uniqueadd '_n' (with n an integer) to the tag if it would overwrite an existing key.

Definition at line 177 of file Head.hh.

178  {
179  std::string k = tag;
180 
181  if (ensure_unique)
182  for (int i = 1; find(k) != end() ; i++)
183  {
184  k = tag + "_" + std::to_string(i);
185  }
186 
188  return k;
189  }
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
then awk string
std::string to_string(const T &value)
Convert value to string.
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 199 of file Head.hh.

200  {
201  using namespace std;
202 
204 
205  if ( idx < 0 || idx >= int ( v.size() ) )
206  {
207  THROW(Exception, "Cannot find word number " << idx << " in line " << get_line(key) << " for key: " << key);
208  }
209  return v[idx];
210  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
General exception.
Definition: Exception.hh:13
const std::string & get_line(std::string key) const
Get data with the given key.
Definition: Head.hh:90
data_type v[N+1][M+1]
Definition: JPolint.hh:777
std::vector< std::string > splitstring(const std::string &str, char delim= ' ')
Split string at delimiter.
Definition: Head.hh:44
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 221 of file Head.hh.

222  {
223  auto v = _hdr_dict()[key];
224  auto i = std::find (v.begin(), v.end(), field );
225  if (i == v.end()) return -1;
226  return i - v.begin();
227  }
data_type v[N+1][M+1]
Definition: JPolint.hh:777
static std::map< std::string, std::vector< std::string > > & _hdr_dict()
Get internal description of the known lines in header.
Definition: Head.hh:314
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 239 of file Head.hh.

240  {
241  int idx = get_index_of_field(key, field);
242 
243  if ( idx == -1 )
244  {
245  THROW(Exception, "Failed to find" << key << " " << field);
246  }
247 
248  return get_field( key, idx );
249  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
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:221
General exception.
Definition: Exception.hh:13
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition: Head.hh:199
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 262 of file Head.hh.

263  {
264  using namespace std;
265 
266  if ( field == "" ) get_line( key ) = value;
267 
268  int idx = get_index_of_field( key, field );
269 
270  if ( idx < 0 )
271  {
272  THROW(Exception, "GFailed to find field in header line: " << key << " " << field);
273  }
274 
275  vector<string> vals = splitstring( get_line( key ) );
276 
277  // if the fields before do not exist, add padding
278  while ( int( vals.size() ) <= idx ) vals.push_back("0");
279 
280  vals[idx] = value;
281  ostringstream ss;
282 
283  for (unsigned i = 0; i < vals.size() ; i++ )
284  {
285  ss << vals[i];
286  if ( i != vals.size() - 1) ss << " ";
287  }
288  set_line( key, ss.str() );
289 
290  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
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:221
std::string set_line(std::string tag, std::string line, bool ensure_unique=true)
Set data with the given tag.
Definition: Head.hh:177
General exception.
Definition: Exception.hh:13
const std::string & get_line(std::string key) const
Get data with the given key.
Definition: Head.hh:90
std::vector< std::string > splitstring(const std::string &str, char delim= ' ')
Split string at delimiter.
Definition: Head.hh:44
void Head::print ( std::ostream &  out = std::cout) const
inline

Print header.

Parameters
outoutput stream

Definition at line 297 of file Head.hh.

298  {
299  if (count("start_run")) out << "start_run: " << at("start_run") << std::endl;
300 
301  for ( auto& p : *this )
302  {
303  if ( p.first == "start_run" || p.first == "end_event" ) continue;
304  out << p.first << ": " << p.second << std::endl ;
305  }
306  out << "end_event:" << std::endl;
307  }
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 314 of file Head.hh.

315  {
316  using namespace std;
317 
318  // map with, for each tag (key), a vector of field-names
319 
320  static map<string, vector<string> > r;
321  if ( r.size() > 0 ) return r;
322 
323  string desc =
324  "DAQ:livetime\n"
325  "cut_primary cut_seamuon cut_in cut_nu:Emin Emax cosTmin cosTmax\n"
326  "generator physics simul:program version date time\n"
327  "seed:program level iseed\n"
328  "PM1_type_area:type area TTS\n"
329  "PDF:i1 i2\n"
330  "model:interaction muon scattering numberOfEnergyBins\n"
331  "can:zmin zmax r\n"
332  "genvol:zmin zmax r volume numberOfEvents\n"
333  "merge:time gain\n"
334  "coord_origin:x y z\n"
335  "translate:x y z\n"
336  "genhencut:gDir Emin\n"
337  "k40:rate time\n"
338  "norma:primaryFlux numberOfPrimaries\n"
339  "livetime:numberOfSeconds errorOfSeconds\n"
340  "flux:type key file_1 file_2\n"
341  "spectrum:alpha\n"
342  "fixedcan:xcenter ycenter zmin zmax radius\n"
343  "start_run:run_id";
344 
345  for ( auto line : splitstring(desc, '\n') )
346  {
347  auto v = splitstring( line, ':');
348 
349  vector< string > fields = splitstring( v[1] );
350  for ( auto key : splitstring( v[0] ) )
351  {
352  r[key] = fields;
353  }
354  }
355  return r;
356  }
data_type r[M+1]
Definition: JPolint.hh:779
data_type v[N+1][M+1]
Definition: JPolint.hh:777
std::vector< std::string > splitstring(const std::string &str, char delim= ' ')
Split string at delimiter.
Definition: Head.hh:44
double Head::ngen ( ) const
inline

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

Returns
number of events

Definition at line 364 of file Head.hh.

365  {
366  return stod ( get_field("genvol", "numberOfEvents") );
367  }
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition: Head.hh:199
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 374 of file Head.hh.

375  {
376  return stod ( get_field("DAQ", "livetime") );
377  }
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition: Head.hh:199
double Head::mc_livetime ( ) const
inline

Get the Monte Carlo live time.

Returns
live time [s]

Definition at line 385 of file Head.hh.

386  {
387  return stod ( get_field("livetime", "numberOfSeconds") );
388  }
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition: Head.hh:199
Vec Head::coord_origin ( ) const
inline

Get coordinate origin.

Returns
position

Definition at line 395 of file Head.hh.

396  {
397  return Vec( stod( get_field("coord_origin", "x") ),
398  stod( get_field("coord_origin", "y") ),
399  stod( get_field("coord_origin", "z") ));
400  }
The Vec class is a straightforward 3-d vector, which also works in pyroot.
Definition: Vec.hh:12
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition: Head.hh:199
Vec Head::translate ( ) const
inline

Get coordinate translation.

Returns
translation

Definition at line 407 of file Head.hh.

408  {
409  return Vec( stod( get_field("translate", "x") ),
410  stod( get_field("translate", "y") ),
411  stod( get_field("translate", "z") ));
412  }
The Vec class is a straightforward 3-d vector, which also works in pyroot.
Definition: Vec.hh:12
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition: Head.hh:199
static void Head::actionAtFileOpen ( int  version)
inlinestatic

Action method at file open.

Parameters
versionversion

Definition at line 421 of file Head.hh.

422  {
424  }
version
Definition: JEditTuneHV.sh:5
static int ROOT_IO_VERSION
Streamer version as obtained from ROOT file.
Definition: Head.hh:426
Head::ClassDef ( Head  ,
 
)

Member Data Documentation

int Head::ROOT_IO_VERSION = -1
static

Streamer version as obtained from ROOT file.

Definition at line 426 of file Head.hh.


The documentation for this struct was generated from the following files: