Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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.
 
const std::string & get_line (std::string key) const
 Get data with the given key.
 
std::string & get_line (std::string key)
 Get data with the given key.
 
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".
 
std::vector< std::string > get_lines (const std::string &tag) const
 Get all data compatible with the given key.
 
std::string set_line (std::string tag, std::string line, bool ensure_unique=true)
 Set data with the given tag.
 
std::string get_field (std::string key, int idx) const
 Get data with the given key at given index.
 
int get_index_of_field (std::string key, std::string field) const
 Get index of data with the given key at given field.
 
std::string get_field (std::string key, std::string field) const
 Get data with the given key at given field.
 
void set_field (std::string key, std::string field, std::string value)
 Set data with the given key at given field.
 
void print (std::ostream &out=std::cout) const
 Print header.
 
double ngen () const
 Get the number of generated events needed for computing event rates.
 
double daq_livetime () const
 Get the the live time provided by the DAQ sytstem (=number of processed timeslices * frametime).
 
double mc_livetime () const
 Get the Monte Carlo live time.
 
Vec coord_origin () const
 Get coordinate origin.
 
Vec translate () const
 Get coordinate translation.
 
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.
 
static void actionAtFileOpen (int version)
 Action method at file open.
 

Static Public Attributes

static int ROOT_IO_VERSION = -1
 Streamer version as obtained from ROOT file.
 

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

Constructor & Destructor Documentation

◆ ~Head()

virtual Head::~Head ( )
inlinevirtual

Definition at line 416 of file Head.hh.

416{}

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

78 {
79 return count( key ) != 0;
80 }

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

90 {
91 return this->at(key);
92 }

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

102 {
103 return this->at(key);
104 }

◆ matching_keys()

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

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

◆ get_lines()

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

155 {
157
158 for ( auto& key : matching_keys( tag ) ) {
159 r.push_back( get_line( key ) );
160 }
161
162 return r;
163 }
const std::string & get_line(std::string key) const
Get data with the given key.
Definition Head.hh:89
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".
Definition Head.hh:115

◆ set_line()

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

177 {
178 std::string k = tag;
179
180 if (ensure_unique)
181 for (int i = 1; find(k) != end() ; i++)
182 {
183 k = tag + "_" + std::to_string(i);
184 }
185
187 return k;
188 }

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

199 {
200 using namespace std;
201
203
204 if ( idx < 0 || idx >= int ( v.size() ) )
205 {
206 THROW(Exception, "Cannot find word number " << idx << " in line " << get_line(key) << " for key: " << key);
207 }
208 return v[idx];
209 }
std::vector< std::string > splitstring(const std::string &str, char delim=' ')
Split string at delimiter.
Definition Head.hh:43
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
General exception.
Definition Exception.hh:13

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

221 {
222 auto& d = _hdr_dict();
223 if ( d.count(key) == 0 ) return -1;
224 auto v = d.at(key);
225 auto i = std::find (v.begin(), v.end(), field );
226 if (i == v.end()) return -1;
227 return i - v.begin();
228 }
static const std::map< std::string, std::vector< std::string > > & _hdr_dict()
Get internal description of the known lines in header.
Definition Head.hh:315

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

241 {
242 int idx = get_index_of_field(key, field);
243
244 if ( idx == -1 )
245 {
246 THROW(Exception, "Failed to find" << key << " " << field);
247 }
248
249 return get_field( key, idx );
250 }
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:220
std::string get_field(std::string key, int idx) const
Get data with the given key at given index.
Definition Head.hh:198

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

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

◆ print()

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

Print header.

Parameters
outoutput stream

Definition at line 298 of file Head.hh.

299 {
300 if (count("start_run")) out << "start_run: " << at("start_run") << std::endl;
301
302 for ( auto& p : *this )
303 {
304 if ( p.first == "start_run" || p.first == "end_event" ) continue;
305 out << p.first << ": " << p.second << std::endl ;
306 }
307 out << "end_event:" << std::endl;
308 }

◆ _hdr_dict()

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

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

◆ ngen()

double Head::ngen ( ) const
inline

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

Returns
number of events

Definition at line 366 of file Head.hh.

367 {
368 return stod ( get_field("genvol", "numberOfEvents") );
369 }

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

377 {
378 return stod ( get_field("DAQ", "livetime") );
379 }

◆ mc_livetime()

double Head::mc_livetime ( ) const
inline

Get the Monte Carlo live time.

Returns
live time [s]

Definition at line 387 of file Head.hh.

388 {
389 return stod ( get_field("livetime", "numberOfSeconds") );
390 }

◆ coord_origin()

Vec Head::coord_origin ( ) const
inline

Get coordinate origin.

Returns
position

Definition at line 397 of file Head.hh.

398 {
399 return Vec( stod( get_field("coord_origin", "x") ),
400 stod( get_field("coord_origin", "y") ),
401 stod( get_field("coord_origin", "z") ));
402 }
The Vec class is a straightforward 3-d vector, which also works in pyroot.
Definition Vec.hh:13

◆ translate()

Vec Head::translate ( ) const
inline

Get coordinate translation.

Returns
translation

Definition at line 409 of file Head.hh.

410 {
411 return Vec( stod( get_field("translate", "x") ),
412 stod( get_field("translate", "y") ),
413 stod( get_field("translate", "z") ));
414 }

◆ actionAtFileOpen()

static void Head::actionAtFileOpen ( int version)
inlinestatic

Action method at file open.

Parameters
versionversion

Definition at line 423 of file Head.hh.

424 {
425 ROOT_IO_VERSION = version;
426 }
static int ROOT_IO_VERSION
Streamer version as obtained from ROOT file.
Definition Head.hh:428

◆ ClassDef()

Head::ClassDef ( Head ,
2  )

Member Data Documentation

◆ ROOT_IO_VERSION

int Head::ROOT_IO_VERSION = -1
static

Streamer version as obtained from ROOT file.

Definition at line 428 of file Head.hh.


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