LDMX Software
|
This class manages all ROOT file input/output operations. More...
#include <EventFile.h>
Public Member Functions | |
EventFile (const framework::config::Parameters ¶ms, const std::string &filename, EventFile *parent, bool isOutputFile, bool isSingleOutput, bool isLoopable) | |
Constructor to make a general file. | |
EventFile (const framework::config::Parameters ¶m, const std::string &fileName, bool isLoopable) | |
Constructor to make a pileup overlay file. | |
EventFile (const framework::config::Parameters ¶m, const std::string &fileName, EventFile *parent, bool isSingleOutput=false) | |
Class constructor for cloning data from a "parent" file. | |
EventFile (const framework::config::Parameters ¶ms, const std::string &fileName) | |
Class constructor to make a file to read in an event root file. | |
~EventFile () | |
Destructor. | |
bool | isCorrupted () const |
Check if the file we have is corrupted. | |
void | addDrop (const std::string &rule) |
Add a rule for dropping collections from the output. | |
void | setupEvent (Event *evt) |
Set an Event object containing the event data to work with this file. | |
void | updateParent (EventFile *parent) |
Change pointer to different parent file. | |
Event * | getEvent () |
Get the Event object containing the event data. | |
bool | nextEvent (bool storeCurrentEvent=true) |
Prepare the next event. | |
int | skipToEvent (int offset) |
Skip events using an offset. | |
void | writeRunHeader (ldmx::RunHeader &runHeader) |
Write the run header into the run map. | |
void | writeRunTree () |
Write the map of run headers to the file as a TTree of RunHeader. | |
ldmx::RunHeader * | getRunHeaderPtr (int runNumber) |
Update the RunHeader for a given run, if it exists in the input file. | |
ldmx::RunHeader & | getRunHeader (int runNumber) |
Get the RunHeader for a given run, if it exists in the input file. | |
const std::string & | getFileName () |
Private Member Functions | |
void | importRunHeaders () |
Fill the internal map of run numbers to RunHeader objects from the input file. | |
Private Attributes | |
Long64_t | entries_ {-1} |
The number of entries in the tree. | |
Long64_t | ientry_ {-1} |
The current entry in the tree. | |
std::string | fileName_ |
The file name. | |
bool | isOutputFile_ |
True if file is an output file being written to disk. | |
bool | isSingleOutput_ |
True if there is only one output file. | |
bool | isLoopable_ {false} |
True if this is an input file with pileup overlay events */. | |
TFile * | file_ {nullptr} |
The backing TFile for this EventFile. | |
TTree * | tree_ {nullptr} |
The tree with event data. | |
EventFile * | parent_ {nullptr} |
A parent file containing event data. | |
Event * | event_ {nullptr} |
The object containing the actual event data (trees and branches). | |
std::vector< std::pair< std::string, bool > > | preCloneRules_ |
Pre-clone rules. | |
std::vector< std::string > | reactivateRules_ |
Vector of drop rules that have been parsed and need to be used to reactivate these branches on the input tree. | |
std::map< int, std::pair< bool, ldmx::RunHeader * > > | runMap_ |
Map of run numbers to RunHeader objects. | |
This class manages all ROOT file input/output operations.
Definition at line 26 of file EventFile.h.
framework::EventFile::EventFile | ( | const framework::config::Parameters & | params, |
const std::string & | filename, | ||
EventFile * | parent, | ||
bool | isOutputFile, | ||
bool | isSingleOutput, | ||
bool | isLoopable | ||
) |
Constructor to make a general file.
This is not used directly, but it is called in the more specialised constructors. This method is mainly focused on reducing code copying.
[in] | params | The parameters used to configure this EventFile. |
[in] | filename | the name of the file to read/write |
[in] | parent | a pointer to the parent file to copy |
[in] | isOutputFile | true if this file is written out |
[in] | isSingleOutput | true if only one output file is being written to |
[in] | isLoopable | true for an input file where events can be reused |
Definition at line 13 of file EventFile.cxx.
References entries_, file_, fileName_, framework::config::Parameters::getParameter(), importRunHeaders(), isOutputFile_, parent_, preCloneRules_, reactivateRules_, and tree_.
framework::EventFile::EventFile | ( | const framework::config::Parameters & | param, |
const std::string & | fileName, | ||
bool | isLoopable | ||
) |
Constructor to make a pileup overlay file.
This is an additional input file from which collections are pulled to be overlaid with simulated hit collections.
[in] | params | The parameters used to configure this EventFile. |
fileName | The file name. | |
isLoopable | true if we want to reset the event counter and keep processing from the start when we hit the end of the event tree in this input file (set in the call in the producer) |
Definition at line 93 of file EventFile.cxx.
framework::EventFile::EventFile | ( | const framework::config::Parameters & | param, |
const std::string & | fileName, | ||
EventFile * | parent, | ||
bool | isSingleOutput = false |
||
) |
Class constructor for cloning data from a "parent" file.
This is used for output files when there is an input file. (OR for files with a parent EventFile to clone)
[in] | params | The parameters used to configure this EventFile. |
[in] | fileName | The file name. |
[in] | parent | Parent file for cloning data tree. |
[in] | isSingleOutput | boolean check if only one output file is being written to |
Definition at line 101 of file EventFile.cxx.
framework::EventFile::EventFile | ( | const framework::config::Parameters & | params, |
const std::string & | fileName | ||
) |
Class constructor to make a file to read in an event root file.
This constructor just wraps the constructor above and sets the parent to null and the parameters isOutputFile/isSingleOutput to false.
This is used for all input files.
[in] | params | The parameters used to configure this EventFile. |
[in] | fileName | The file name. |
Definition at line 97 of file EventFile.cxx.
framework::EventFile::~EventFile | ( | ) |
Destructor.
Make sure to close the file when we destruct
Definition at line 106 of file EventFile.cxx.
References file_, isOutputFile_, and tree_.
void framework::EventFile::addDrop | ( | const std::string & | rule | ) |
Add a rule for dropping collections from the output.
This needs to be called after setupEvent. This method uses the event to help drop collections.
The rules should be of the following form: <drop,keep, or ignore> exp where exp is an expression that matches the collectionName. ignore ==> any branch with name matching exp is not even read in from the input file (if it exists) keep ==> any branch with name matching exp is read in from the input (if exists) and written to output (if exists) drop ==> any branch with name matching exp is read in from the input (if exists) and NOT written to output
The default behavior for all branches is keep.
ROOT uses the internal TRegexp to match branch names to the passed expression and set the status of the branch (whether it will be read or not). This internal object has different rules than real regular expressions, so it is best to keep it safe and only use asterisks or full names. Additionally, the rules you pass are analyzed in succession, so you can go from something more general to something more specific.
For example to drop all EcalSimHits.* drop EcalSimHits.*
or drop scoring plane collections drop .*ScoringPlane.*
or drop scoring plane collections but keep EcalScoringPlane collection drop .*ScoringPlane.* keep EcalScoringPlane.*
rule | The rule for dropping collections. |
Definition at line 123 of file EventFile.cxx.
References framework::Event::addDrop(), event_, preCloneRules_, and reactivateRules_.
Referenced by framework::Process::run().
|
inline |
|
inline |
Definition at line 258 of file EventFile.h.
References fileName_.
Referenced by framework::Process::run().
ldmx::RunHeader & framework::EventFile::getRunHeader | ( | int | runNumber | ) |
Get the RunHeader for a given run, if it exists in the input file.
runNumber | The run number. |
Exception | if there is no RunHeader in the map with the given run number. |
Definition at line 393 of file EventFile.cxx.
References getRunHeaderPtr().
ldmx::RunHeader * framework::EventFile::getRunHeaderPtr | ( | int | runNumber | ) |
Update the RunHeader for a given run, if it exists in the input file.
[in] | runNumber | The run number. |
Definition at line 386 of file EventFile.cxx.
References runMap_.
Referenced by getRunHeader(), and framework::Process::run().
|
private |
Fill the internal map of run numbers to RunHeader objects from the input file.
If this file is an output file and parent_ and parent_->file_ are valid pointers, then the run headers are imported from parent_->file_.
Otherwise, we try to import run headers from file_.
Does not check if any run headers are getting overwritten!
Any RunHeaders read in from this function are owned by this instance of EventFile and are deleted in close().
Definition at line 402 of file EventFile.cxx.
References file_, isOutputFile_, parent_, and runMap_.
Referenced by EventFile(), and updateParent().
bool framework::EventFile::isCorrupted | ( | ) | const |
Check if the file we have is corrupted.
The check on if the file is corrupted is only helpful for input files, but we attempt to have a resonable definition for output files as well.
There are two ways a file can be corrupted and these may not be mutually exclusive.
For output files, we just check the IsZombie flag of the TFile. Again, since we are actively writing to this file, a corruption check is not very stable.
Definition at line 118 of file EventFile.cxx.
References file_, isOutputFile_, and tree_.
Referenced by framework::Process::run().
bool framework::EventFile::nextEvent | ( | bool | storeCurrentEvent = true | ) |
Prepare the next event.
We do the necessary set-up if ientry_ < 0. This means if we are an output file with a parent file, we clone our parent tree to our tree so we have the same branches.
After the first entry (when ientry_ >= 0), we "close up" the last event, filling our tree if we storeCurrentEvent is true and telling the event bus to clear.
Going to the next event depends on the configuration of the event file. THere are three cases.
[in] | storeCurrentEvent | Should we save the current event to the output (if we are an output file)? |
Definition at line 182 of file EventFile.cxx.
References framework::Event::beforeFill(), framework::Event::Clear(), entries_, event_, file_, ientry_, isLoopable_, isOutputFile_, isSingleOutput_, framework::Event::nextEvent(), nextEvent(), framework::Event::onEndOfEvent(), parent_, preCloneRules_, reactivateRules_, framework::Event::setInputTree(), framework::Event::setOutputTree(), and tree_.
Referenced by nextEvent(), and framework::Process::run().
void framework::EventFile::setupEvent | ( | Event * | evt | ) |
Set an Event object containing the event data to work with this file.
evt | The Event object with event data. |
Definition at line 264 of file EventFile.cxx.
References framework::Event::createTree(), entries_, event_, ientry_, isOutputFile_, parent_, framework::Event::setInputTree(), framework::Event::setOutputTree(), and tree_.
Referenced by framework::Process::run().
int framework::EventFile::skipToEvent | ( | int | offset | ) |
void framework::EventFile::updateParent | ( | EventFile * | parent | ) |
Change pointer to different parent file.
We assume that the parent file is an EventFile configured to be an input file. This allows us to assume that the tree_ member variable of parent is valid.
parent | pointer to new parent file |
Definition at line 298 of file EventFile.cxx.
References file_, ientry_, importRunHeaders(), parent_, preCloneRules_, reactivateRules_, and tree_.
Referenced by framework::Process::run().
void framework::EventFile::writeRunHeader | ( | ldmx::RunHeader & | runHeader | ) |
Write the run header into the run map.
Any RunHeader passed here is not owned or cleaned up by this EventFile instance.
runHeader | The run header to write into the map |
Exception | if run number is already in run map |
Definition at line 373 of file EventFile.cxx.
References ldmx::RunHeader::getRunNumber(), and runMap_.
Referenced by framework::Process::run().
void framework::EventFile::writeRunTree | ( | ) |
Write the map of run headers to the file as a TTree of RunHeader.
Deletes any RunHeaders that this instance of EventFile owns.
Exception | if call this function on a non-output file. |
ROOT requires us to be in the correct directory when we create the output TTree for us to have it written into the correct location.
In order to allow downstream processors to getHistoDirectory() and enter that directory for this histograms we need to enter the output event file again here so that our run tree ends up in the correct location.
Definition at line 329 of file EventFile.cxx.
References file_, fileName_, isOutputFile_, and runMap_.
Referenced by framework::Process::run().
|
private |
The number of entries in the tree.
Definition at line 281 of file EventFile.h.
Referenced by EventFile(), nextEvent(), setupEvent(), and skipToEvent().
|
private |
The object containing the actual event data (trees and branches).
Definition at line 308 of file EventFile.h.
Referenced by addDrop(), getEvent(), nextEvent(), and setupEvent().
|
private |
The backing TFile for this EventFile.
Definition at line 299 of file EventFile.h.
Referenced by EventFile(), importRunHeaders(), isCorrupted(), nextEvent(), updateParent(), writeRunTree(), and ~EventFile().
|
private |
The file name.
Definition at line 287 of file EventFile.h.
Referenced by EventFile(), getFileName(), and writeRunTree().
|
private |
The current entry in the tree.
Definition at line 284 of file EventFile.h.
Referenced by nextEvent(), setupEvent(), skipToEvent(), and updateParent().
|
private |
True if this is an input file with pileup overlay events */.
Definition at line 296 of file EventFile.h.
Referenced by nextEvent().
|
private |
True if file is an output file being written to disk.
Definition at line 290 of file EventFile.h.
Referenced by EventFile(), importRunHeaders(), isCorrupted(), nextEvent(), setupEvent(), writeRunTree(), and ~EventFile().
|
private |
True if there is only one output file.
Definition at line 293 of file EventFile.h.
Referenced by nextEvent().
|
private |
A parent file containing event data.
Definition at line 305 of file EventFile.h.
Referenced by EventFile(), importRunHeaders(), nextEvent(), setupEvent(), and updateParent().
|
private |
Pre-clone rules.
The series of rules to call before cloning/copying the parent tree.
Definition at line 316 of file EventFile.h.
Referenced by addDrop(), EventFile(), nextEvent(), and updateParent().
|
private |
Vector of drop rules that have been parsed and need to be used to reactivate these branches on the input tree.
The branches were initial deactivated so they don't get cloned to output tree.
Definition at line 325 of file EventFile.h.
Referenced by addDrop(), EventFile(), nextEvent(), and updateParent().
|
private |
Map of run numbers to RunHeader objects.
The value object is a pair that should remain internal.
Definition at line 337 of file EventFile.h.
Referenced by getRunHeaderPtr(), importRunHeaders(), writeRunHeader(), and writeRunTree().
|
private |
The tree with event data.
Definition at line 302 of file EventFile.h.
Referenced by EventFile(), isCorrupted(), nextEvent(), setupEvent(), updateParent(), and ~EventFile().