Jpp  18.5.2
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 const 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 417 of file Head.hh.

417 {}

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:868
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:868
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:866
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& d = _hdr_dict();
224  if ( d.count(key) == 0 ) return -1;
225  auto v = d.at(key);
226  auto i = std::find (v.begin(), v.end(), field );
227  if (i == v.end()) return -1;
228  return i - v.begin();
229  }
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
static const std::map< std::string, std::vector< std::string > > & _hdr_dict()
Get internal description of the known lines in header.
Definition: Head.hh:316
data_type v[N+1][M+1]
Definition: JPolint.hh:866
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 241 of file Head.hh.

242  {
243  int idx = get_index_of_field(key, field);
244 
245  if ( idx == -1 )
246  {
247  THROW(Exception, "Failed to find" << key << " " << field);
248  }
249 
250  return get_field( key, idx );
251  }
#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 264 of file Head.hh.

265  {
266  using namespace std;
267 
268  if ( field == "" ) get_line( key ) = value;
269 
270  int idx = get_index_of_field( key, field );
271 
272  if ( idx < 0 )
273  {
274  THROW(Exception, "GFailed to find field in header line: " << key << " " << field);
275  }
276 
277  vector<string> vals = splitstring( get_line( key ) );
278 
279  // if the fields before do not exist, add padding
280  while ( int( vals.size() ) <= idx ) vals.push_back("0");
281 
282  vals[idx] = value;
283  ostringstream ss;
284 
285  for (unsigned i = 0; i < vals.size() ; i++ )
286  {
287  ss << vals[i];
288  if ( i != vals.size() - 1) ss << " ";
289  }
290  set_line( key, ss.str() );
291 
292  }
#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 299 of file Head.hh.

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

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

368  {
369  return stod ( get_field("genvol", "numberOfEvents") );
370  }
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 377 of file Head.hh.

378  {
379  return stod ( get_field("DAQ", "livetime") );
380  }
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 388 of file Head.hh.

389  {
390  return stod ( get_field("livetime", "numberOfSeconds") );
391  }
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 398 of file Head.hh.

399  {
400  return Vec( stod( get_field("coord_origin", "x") ),
401  stod( get_field("coord_origin", "y") ),
402  stod( get_field("coord_origin", "z") ));
403  }
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 410 of file Head.hh.

411  {
412  return Vec( stod( get_field("translate", "x") ),
413  stod( get_field("translate", "y") ),
414  stod( get_field("translate", "z") ));
415  }
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 424 of file Head.hh.

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

Member Data Documentation

int Head::ROOT_IO_VERSION = -1
static

Streamer version as obtained from ROOT file.

Definition at line 429 of file Head.hh.


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