3#include "TTreeReader.h"
7#include "Framework/EventFile.h"
8#include "Framework/Exception/Exception.h"
9#include "Framework/RunHeader.h"
14 const std::string &filename,
EventFile *parent,
15 bool isOutputFile,
bool isSingleOutput,
bool isLoopable)
16 : fileName_(filename),
17 isOutputFile_(isOutputFile),
18 isSingleOutput_(isSingleOutput),
19 isLoopable_(isLoopable),
24 if (!
file_->IsOpen() or !
file_->IsWritable()) {
25 EXCEPTION_RAISE(
"FileError",
26 "Output file '" +
fileName_ +
"' is not writable.");
33 file_->SetCompressionSettings(
56 if (!
file_->IsOpen()) {
57 EXCEPTION_RAISE(
"FileError",
"Input file '" +
fileName_ +
58 "' is not readable or does not exist.");
62 params.
getParameter<
bool>(
"skipCorruptedInputFiles",
false);
67 if (
file_->IsZombie()) {
68 if (not skip_corrupted) {
69 EXCEPTION_RAISE(
"FileError",
"Input file '" +
fileName_ +
70 "' is corrupted. Framework will not "
71 "attempt to recover this file.");
77 auto tree_name{params.
getParameter<std::string>(
"tree_name")};
78 tree_ =
static_cast<TTree *
>(
file_->Get(tree_name.c_str()));
80 if (not skip_corrupted) {
81 EXCEPTION_RAISE(
"FileError",
"File '" +
fileName_ +
82 "' does not have a TTree named '" +
83 tree_name +
"' in it.");
94 const std::string &filename,
bool isLoopable)
95 :
EventFile(params, filename, nullptr, false, false, isLoopable) {}
98 const std::string &filename)
99 :
EventFile(params, filename, nullptr, false, false, false) {}
102 const std::string &filename,
EventFile *parent,
104 :
EventFile(params, filename, parent, true, isSingleOutput, false) {}
125 bool isKeep =
false, isDrop =
false, isIgnore =
false;
126 size_t i = rule.find(
"keep");
127 if (i != std::string::npos) {
131 i = rule.find(
"drop");
132 if (i != std::string::npos) {
136 i = rule.find(
"ignore");
137 if (i != std::string::npos) {
143 if (
int(isKeep) +
int(isDrop) +
int(isIgnore) != 1)
return;
145 std::string srule = rule.substr(offset);
146 for (i = srule.find_first_of(
" \t\n\r"); i != std::string::npos;
147 i = srule.find_first_of(
" \t\n\r"))
151 if (srule.length() == 0)
return;
154 if (srule.back() !=
'*') srule +=
".*";
159 srule.erase(std::remove(srule.begin(), srule.end(),
'.'), srule.end());
162 }
else if (isIgnore) {
165 srule.erase(std::remove(srule.begin(), srule.end(),
'.'), srule.end());
174 srule.erase(std::remove(srule.begin(), srule.end(),
'.'), srule.end());
189 EXCEPTION_RAISE(
"EventFile",
"No event tree in the file");
217 if (storeCurrentEvent)
311 parent_->
tree_->SetBranchStatus(rulePair.first.c_str(), rulePair.second);
331 EXCEPTION_RAISE(
"MisCall",
332 "Cannot write the run tree on an input event file.");
339 auto runTree{
static_cast<TTree *
>(
file_->Get(
"LDMX_Run"))};
341 EXCEPTION_RAISE(
"RunTree",
342 "RunTree 'LDMX_Run' already exists in output file '" +
357 runTree =
new TTree(
"LDMX_Run",
"LDMX run header");
361 runTree->Branch(
"RunHeader",
"ldmx::RunHeader", &theHandle, 32000, 3);
364 for (
auto &[num, header_pair] :
runMap_) {
365 theHandle = header_pair.second;
367 if (header_pair.first)
delete header_pair.second;
377 EXCEPTION_RAISE(
"RunMap",
"Run map already contains a run with number '" +
378 std::to_string(runNumber) +
"'.");
381 runMap_[runNumber] = std::make_pair(
false, &runHeader);
388 return runMap_.at(runNumber).second;
398 EXCEPTION_RAISE(
"RunHeader",
399 "Unable to find header for run " + std::to_string(runNumber));
404 auto theImportFile{
file_};
412 TTreeReader oldRunTree(
"LDMX_Run", theImportFile);
413 TTreeReaderValue<ldmx::RunHeader> oldRunHeader(oldRunTree,
"RunHeader");
415 while (oldRunTree.Next()) {
416 auto *oldRunHeaderPtr = oldRunHeader.Get();
417 if (oldRunHeaderPtr !=
nullptr) {
420 runMap_[oldRunHeaderPtr->getRunNumber()] =
Class implementing an event buffer system for storing event data.
This class manages all ROOT file input/output operations.
void updateParent(EventFile *parent)
Change pointer to different parent file.
bool isSingleOutput_
True if there is only one output file.
TFile * file_
The backing TFile for this EventFile.
void addDrop(const std::string &rule)
Add a rule for dropping collections from the output.
std::string fileName_
The file name.
bool isLoopable_
True if this is an input file with pileup overlay events */.
std::map< int, std::pair< bool, ldmx::RunHeader * > > runMap_
Map of run numbers to RunHeader objects.
void setupEvent(Event *evt)
Set an Event object containing the event data to work with this file.
Long64_t entries_
The number of entries in the tree.
std::vector< std::string > reactivateRules_
Vector of drop rules that have been parsed and need to be used to reactivate these branches on the in...
std::vector< std::pair< std::string, bool > > preCloneRules_
Pre-clone rules.
void writeRunTree()
Write the map of run headers to the file as a TTree of RunHeader.
bool nextEvent(bool storeCurrentEvent=true)
Prepare the next event.
bool isOutputFile_
True if file is an output file being written to disk.
ldmx::RunHeader * getRunHeaderPtr(int runNumber)
Update the RunHeader for a given run, if it exists in the input file.
Long64_t ientry_
The current entry in the tree.
void writeRunHeader(ldmx::RunHeader &runHeader)
Write the run header into the run map.
void importRunHeaders()
Fill the internal map of run numbers to RunHeader objects from the input file.
int skipToEvent(int offset)
Skip events using an offset.
ldmx::RunHeader & getRunHeader(int runNumber)
Get the RunHeader for a given run, if it exists in the input file.
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 * parent_
A parent file containing event data.
TTree * tree_
The tree with event data.
Event * event_
The object containing the actual event data (trees and branches).
bool isCorrupted() const
Check if the file we have is corrupted.
Implements an event buffer system for storing event data.
TTree * createTree()
Create the output data tree.
void setOutputTree(TTree *tree)
Set the output data tree.
void beforeFill()
Action to be executed before the tree is filled.
void onEndOfEvent()
Perform end of event action (doesn't do anything right now).
void Clear()
Clear this object's data (including passengers).
bool nextEvent()
Go to the next event by retrieving the event header.
void setInputTree(TTree *tree)
Set the input data tree.
void addDrop(const std::string &exp)
Add a drop rule to the list of regex expressions to drop.
Class encapsulating parameters for configuring a processor.
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
All classes in the ldmx-sw project use this namespace.