Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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::JWeightEventHelper JSUPPORT::JWeightFileScanner< JFileScanner_t >

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...
 
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 "_n". More...
 
std::vector< std::string > get_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...
 

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

Constructor & Destructor Documentation

virtual Head::~Head ( )
inlinevirtual

Definition at line 412 of file Head.hh.

412 {}

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

77  {
78  return count( key ) != 0;
79  }
std::vector< int > count
Definition: JAlgorithm.hh:180
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 88 of file Head.hh.

89  {
90  return this->at(key);
91  }
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 100 of file Head.hh.

101  {
102  return this->at(key);
103  }
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 114 of file Head.hh.

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

154  {
156 
157  for ( auto& key : matching_keys( tag ) ) {
158  r.push_back( get_line( key ) );
159  }
160 
161  return r;
162  }
data_type r[M+1]
Definition: JPolint.hh:742
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:114
const std::string & get_line(std::string key) const
Get data with the given key.
Definition: Head.hh:88
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 175 of file Head.hh.

176  {
177  std::string k = tag;
178 
179  if (ensure_unique)
180  for (int i = 1; find(k) != end() ; i++)
181  {
182  k = tag + "_" + std::to_string(i);
183  }
184 
186  return k;
187  }
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
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 197 of file Head.hh.

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

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

238  {
239  int idx = get_index_of_field(key, field);
240 
241  if ( idx == -1 )
242  {
243  THROW(Exception, "Failed to find" << key << " " << field);
244  }
245 
246  return get_field( key, idx );
247  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
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:219
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:197
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 260 of file Head.hh.

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

Print header.

Parameters
outoutput stream

Definition at line 295 of file Head.hh.

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

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

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

Returns
number of events

Definition at line 362 of file Head.hh.

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

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

Get the Monte Carlo live time.

Returns
live time [s]

Definition at line 383 of file Head.hh.

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

Get coordinate origin.

Returns
position

Definition at line 393 of file Head.hh.

394  {
395  return Vec( stod( get_field("coord_origin", "x") ),
396  stod( get_field("coord_origin", "y") ),
397  stod( get_field("coord_origin", "z") ));
398  }
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:197
Vec Head::translate ( ) const
inline

Get coordinate translation.

Returns
translation

Definition at line 405 of file Head.hh.

406  {
407  return Vec( stod( get_field("translate", "x") ),
408  stod( get_field("translate", "y") ),
409  stod( get_field("translate", "z") ));
410  }
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:197
Head::ClassDef ( Head  ,
 
)

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