15#include "Framework/EventFile.h"
17#include "Framework/Exception/Exception.h"
18#include "Framework/Logger.h"
19#include "Framework/NtupleManager.h"
20#include "Framework/RunHeader.h"
26volatile std::sig_atomic_t preemption_received_ = 0;
31 : conditions_{*this} {
37 max_tries_ = configuration.
get<
int>(
"max_tries_per_event", 1);
44 configuration.
get<
bool>(
"skip_corrupted_input_files",
false);
46 input_files_ = configuration.
get<std::vector<std::string>>(
"input_files", {});
48 configuration.
get<std::vector<std::string>>(
"output_files", {});
56 auto run{configuration.
get<
int>(
"run", -1)};
59 auto libs{configuration.
get<std::vector<std::string>>(
"libraries", {})};
60 std::set<std::string> libraries_loaded;
61 for (
const auto& lib : libs) {
62 if (libraries_loaded.find(lib) != libraries_loaded.end()) {
66 void* handle = dlopen(lib.c_str(), RTLD_NOW);
67 if (handle ==
nullptr) {
68 EXCEPTION_RAISE(
"LibraryLoadFailure",
69 "Error loading library '" + lib +
"':" + dlerror());
72 libraries_loaded.insert(lib);
76 configuration.
get<
bool>(
"skim_default_is_keep",
true));
78 configuration.
get<std::vector<std::string>>(
"skim_rules", {})};
79 for (
size_t i = 0; i < skim_rules.size(); i += 2) {
83 auto sequence{configuration.
get<std::vector<framework::config::Parameters>>(
85 if (sequence.empty() && configuration.
get<
bool>(
"testing_mode",
false)) {
88 "No sequence has been defined. What should I be doing?\nUse "
89 "p.sequence to tell me what processors to run.");
91 for (
auto proc : sequence) {
92 auto class_name{proc.get<std::string>(
"class_name")};
93 auto instance_name{proc.get<std::string>(
"instance_name")};
95 EventProcessor::Factory::get().make(class_name, instance_name, *
this)};
97 EXCEPTION_RAISE(
"UnableToCreate",
98 "The EventProcessor Factory was unable to create " +
99 instance_name +
" of type " + class_name +
100 ". Did you inherit from framework::Producer or "
101 "framework::Analyzer? "
102 "Did you DECLARE_PRODUCER or DECLARE_ANALYZER in the "
103 "implementation (.cxx) file? "
104 "Did you use the class's full name (including "
105 "namespaces) in the Python configuration class? "
106 "Does the Python configuration class reference the "
107 "correct library it is a part of?");
110 proc.get<std::vector<framework::config::Parameters>>(
"histograms", {})};
111 if (!histograms.empty()) {
112 ep.value()->getHistoDirectory();
113 ep.value()->createHistograms(histograms);
115 ep.value()->configure(proc);
119 auto conditions_object_providers{
120 configuration.
get<std::vector<framework::config::Parameters>>(
121 "conditions_object_providers", {})};
122 for (
auto cop : conditions_object_providers) {
123 auto class_name{cop.get<std::string>(
"class_name")};
124 auto object_name{cop.get<std::string>(
"object_name")};
125 auto tag_name{cop.get<std::string>(
"tag_name")};
130 bool log_performance = configuration.
get<
bool>(
"log_performance",
false);
131 if (log_performance) {
132 std::vector<std::string> names{
sequence_.size()};
133 for (std::size_t i{0}; i <
sequence_.size(); i++) {
160 auto n_events_processed{0};
174 std::size_t i_proc{0};
182 proc->onProcessStart();
193 EXCEPTION_RAISE(
"InvalidConfig",
194 "No input files or output files were given.");
196 ldmx_log(warn) <<
"Several output files given with no input files. "
198 <<
"' will be used.";
211 run_header->setRunStart(std::time(
nullptr));
223 ldmx_log(warn) <<
"The total_events was set, so max_events and "
224 "max_tries_per_event will be ignored!";
227 while (n_events_processed < event_limit) {
229 if (preemption_received_) {
231 <<
"Preemption signal received, stopping event generation";
247 bool completed =
process(n_events_processed, num_tries, the_event);
252 if (completed) num_tries = 0;
258 n_events_processed++;
267 run_header->setRunEnd(std::time(
nullptr));
268 run_header->setNumTries(total_tries);
272 if (n_events_processed < total_tries / 10000) {
274 <<
"Less than 1 event out of every 10k events tried was accepted!";
276 <<
"This could be an issue with your filtering and biasing procedure "
277 "since this is incredibly inefficient.";
285 bool single_output =
false;
287 single_output =
true;
290 EXCEPTION_RAISE(
"Process",
291 "Unable to handle case of different number of input and "
292 "output files (other than zero/one ouput file).");
302 ldmx_log(warn) <<
"Input file '" << infilename
303 <<
"' was found to be corrupted. Skipping.";
308 "We should never get here. "
309 "EventFile is corrupted but we aren't skipping corrupted inputs. "
310 "EventFile should be throwing its own exceptions in this case.");
314 ldmx_log(info) <<
"Opening file " << infilename;
323 if (!single_output or ifile == 0) {
332 master_file = out_file;
334 EXCEPTION_RAISE(
"Process",
"Unable to construct output file for " +
343 master_file = out_file;
350 master_file = &in_file;
356 n_events_processed++;
359 bool event_completed =
true;
360 while (!preemption_received_ &&
374 ldmx_log(info) <<
"Got new run header from '"
378 ldmx_log(warn) <<
"Run header for run " << was_run
379 <<
" was not found!";
383 event_completed =
process(n_events_processed, 1, the_event);
388 n_events_processed++;
391 if (preemption_received_) {
392 ldmx_log(fatal) <<
"Preemption signal received, stopping event "
393 "processing and closing files";
396 bool leave_early{
false};
398 ldmx_log(info) <<
"Reached event limit of " <<
event_limit_
404 ldmx_log(warn) <<
"Processing interrupted";
408 ldmx_log(info) <<
"Closing file " << infilename;
414 if (out_file and !single_output) {
442 proc->onProcessEnd();
459 TDirectory* child = owner->mkdir((
char*)dirName.c_str());
460 if (child) child->cd();
465 TDirectory* owner{
nullptr};
471 "You did not provide the necessary histogram file name to "
472 "put your histograms (or performance data) in.\n Provide this "
473 "name in the python configuration with 'p.histogramFile = "
474 "\"myHistFile.root\"' where p is the Process object.");
494 std::size_t i_proc{0};
499 proc->beforeNewRun(header);
513 proc->onNewRun(header);
518 ldmx_log(info) << header;
527 ldmx_log(info) <<
"Processing " << n + 1 <<
" Run "
528 <<
event.getEventHeader().getRun() <<
" Event "
529 <<
event.getEventHeader().getEventNumber() <<
" ("
530 << t.AsString(
"lc") <<
")";
534 std::size_t i_proc{0};
540 proc->process(event);
561 std::size_t i_proc{0};
566 proc->onFileOpen(file);
575 std::size_t i_proc{0};
580 proc->onFileClose(file);
Base classes for all user event processing components to extend.
Class implementing an event buffer system for storing event data.
Class which represents the process under execution.
Specific exception used to abort an event.
void onProcessStart()
Calls onProcessStart for all ConditionsObjectProviders.
void onNewRun(ldmx::RunHeader &)
Calls onNewRun for all ConditionsObjectProviders.
void createConditionsObjectProvider(const std::string &classname, const std::string &instancename, const std::string &tagname, const framework::config::Parameters ¶ms)
Create a ConditionsObjectProvider given the information.
This class manages all ROOT file input/output operations.
void updateParent(EventFile *parent)
Change pointer to different parent file.
const std::string & getFileName()
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 writeRunHeader(std::shared_ptr< 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.
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.
bool isCorrupted() const
Check if the file we have is corrupted.
Base class for all event processing components.
Implements an event buffer system for storing event data.
int getEventNumber() const
Get the event number.
void onEndOfFile()
Perform end of file action.
ldmx::EventHeader & getEventHeader()
Get the event header.
const ldmx::EventHeader * getEventHeaderPtr()
Get the event header as a pointer.
void clear()
Reset all of the variables to their limits.
static NtupleManager & getInstance()
void reset()
Reset NtupleManager to blank state.
int log_frequency_
The frequency with which event info is printed.
bool skip_corrupted_input_files_
allow the Process to skip input files that are corrupted
std::string histo_filename_
Filename for histograms and other user products.
int max_tries_
Maximum number of attempts to make before giving up on an event.
int run_for_generation_
Run number to use if generating events.
int compression_setting_
Compression setting to pass to output files.
void run()
Run the process.
int event_limit_
Limit on events to process.
std::string pass_name_
Processing pass name.
void newRun(ldmx::RunHeader &header)
Run through the processors and let them know that we are starting a new run.
TFile * histo_t_file_
TFile for histograms and other user products.
TDirectory * openHistoFile()
Open a ROOT TFile to write histograms and TTrees.
int total_events_
Number of events we'd like to produce independetly of the number of tries it would take.
~Process()
Class Destructor.
void onFileClose(EventFile &file) const
File is begin closed.
std::vector< EventProcessor * > sequence_
Ordered list of EventProcessors to execute.
std::vector< std::string > drop_keep_rules_
Set of drop/keep rules.
ldmx::RunHeader * run_header_
Pointer to the current RunHeader, used for Conditions information.
TDirectory * makeHistoDirectory(const std::string &dirName)
Construct a TDirectory* for the given module.
performance::Tracker * performance_
class with calls backs to track performance measurements of software
int min_events_
When reading a file in, what's the first event to read.
StorageControl storage_controller_
Storage controller.
std::vector< std::string > output_files_
List of output file names.
std::vector< std::string > input_files_
List of input files to process.
const ldmx::EventHeader * event_header_
Pointer to the current EventHeader, used for Conditions information.
bool process(int n, int n_tries, Event &event) const
Process the input event through the sequence of processors.
void onFileOpen(EventFile &file) const
File is being opened.
Conditions conditions_
Set of ConditionsProviders.
Process(const framework::config::Parameters &configuration)
Class constructor.
int getRunNumber() const
Get the current run number or the run number to be used when initiating new events from the job.
framework::config::Parameters config_
The parameters used to configure this class.
void setDefaultKeep(bool keep)
Set the default state.
bool keepEvent(bool event_completed) const
Determine if the current event should be kept, based on the defined rules.
void addRule(const std::string &processor_pat, const std::string &purpose_pat)
Add a listening rule.
void resetEventState()
Reset the event-by-event state.
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.