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 is_output_file,
bool isSingleOutput,
bool isLoopable)
16 : file_name_(filename),
17 is_output_file_(is_output_file),
18 is_single_output_(isSingleOutput),
19 is_loopable_(isLoopable),
24 if (!
file_->IsOpen() or !
file_->IsWritable()) {
25 EXCEPTION_RAISE(
"FileError",
26 "Output file '" +
file_name_ +
"' is not writable.");
33 file_->SetCompressionSettings(params.
get<
int>(
"compressionSetting", 9));
55 if (!
file_->IsOpen()) {
56 EXCEPTION_RAISE(
"FileError",
"Input file '" +
file_name_ +
57 "' is not readable or does not exist.");
60 bool skip_corrupted = params.
get<
bool>(
"skipCorruptedInputFiles",
false);
65 if (
file_->IsZombie()) {
66 if (not skip_corrupted) {
67 EXCEPTION_RAISE(
"FileError",
"Input file '" +
file_name_ +
68 "' is corrupted. Framework will not "
69 "attempt to recover this file.");
75 auto tree_name{params.
get<std::string>(
"tree_name")};
76 tree_ =
static_cast<TTree *
>(
file_->Get(tree_name.c_str()));
78 if (not skip_corrupted) {
79 EXCEPTION_RAISE(
"FileError",
"File '" +
file_name_ +
80 "' does not have a TTree named '" +
81 tree_name +
"' in it.");
92 const std::string &filename,
bool isLoopable)
93 :
EventFile(params, filename, nullptr, false, false, isLoopable) {}
96 const std::string &filename)
97 :
EventFile(params, filename, nullptr, false, false, false) {}
100 const std::string &filename,
EventFile *parent,
102 :
EventFile(params, filename, parent, true, isSingleOutput, false) {}
123 bool is_keep =
false, is_drop =
false, is_ignore =
false;
124 size_t i = rule.find(
"keep");
125 if (i != std::string::npos) {
129 i = rule.find(
"drop");
130 if (i != std::string::npos) {
134 i = rule.find(
"ignore");
135 if (i != std::string::npos) {
141 if (
int(is_keep) +
int(is_drop) +
int(is_ignore) != 1)
return;
143 std::string srule = rule.substr(offset);
144 for (i = srule.find_first_of(
" \t\n\r"); i != std::string::npos;
145 i = srule.find_first_of(
" \t\n\r"))
149 if (srule.length() == 0)
return;
152 if (srule.back() !=
'*') srule +=
".*";
157 srule.erase(std::remove(srule.begin(), srule.end(),
'.'), srule.end());
160 }
else if (is_ignore) {
163 srule.erase(std::remove(srule.begin(), srule.end(),
'.'), srule.end());
166 }
else if (is_drop) {
172 srule.erase(std::remove(srule.begin(), srule.end(),
'.'), srule.end());
187 EXCEPTION_RAISE(
"EventFile",
"No event tree in the file");
199 parent_->
tree_->SetBranchStatus(rule_pair.first.c_str(),
215 if (storeCurrentEvent)
309 parent_->
tree_->SetBranchStatus(rule_pair.first.c_str(), rule_pair.second);
329 EXCEPTION_RAISE(
"MisCall",
330 "Cannot write the run tree on an input event file.");
337 auto run_tree{
static_cast<TTree *
>(
file_->Get(
"LDMX_Run"))};
339 EXCEPTION_RAISE(
"RunTree",
340 "RunTree 'LDMX_Run' already exists in output file '" +
355 run_tree =
new TTree(
"LDMX_Run",
"LDMX run header");
359 run_tree->Branch(
"RunHeader",
"ldmx::RunHeader", &the_handle, 32000, 3);
362 for (
auto &[num, header_pair] :
run_map_) {
363 the_handle = header_pair.second;
365 if (header_pair.first)
delete header_pair.second;
375 EXCEPTION_RAISE(
"RunMap",
"Run map already contains a run with number '" +
376 std::to_string(run_number) +
"'.");
379 run_map_[run_number] = std::make_pair(
false, &run_header);
386 return run_map_.at(run_number).second;
396 EXCEPTION_RAISE(
"RunHeader",
"Unable to find header for run " +
397 std::to_string(run_number));
402 auto the_import_file{
file_};
408 if (the_import_file) {
410 TTreeReader old_run_tree(
"LDMX_Run", the_import_file);
411 TTreeReaderValue<ldmx::RunHeader> old_run_header(old_run_tree,
"RunHeader");
413 while (old_run_tree.Next()) {
414 auto *old_run_header_ptr = old_run_header.Get();
415 if (old_run_header_ptr !=
nullptr) {
418 run_map_[old_run_header_ptr->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.
TFile * file_
The backing TFile for this EventFile.
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.
std::map< int, std::pair< bool, ldmx::RunHeader * > > run_map_
Map of run numbers to RunHeader objects.
Long64_t entries_
The number of entries in the tree.
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.
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.
std::vector< std::pair< std::string, bool > > pre_clone_rules_
Pre-clone rules.
void writeRunHeader(ldmx::RunHeader &runHeader)
Write the run header into the run map.
bool is_loopable_
True if this is an input file with pileup overlay events */.
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.
std::string file_name_
The file name.
ldmx::RunHeader & getRunHeader(int runNumber)
Get the RunHeader for a given run, if it exists in the input file.
bool is_single_output_
True if there is only one output 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.
std::vector< std::string > reactivate_rules_
Vector of drop rules that have been parsed and need to be used to reactivate these branches on the in...
bool is_output_file_
True if file is an output file being written to disk.
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.
void clear()
Clear this object's data (including passengers).
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).
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.
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
All classes in the ldmx-sw project use this namespace.