14#include "Framework/EventFile.h"
16#include "Framework/Exception/Exception.h"
17#include "Framework/Logger.h"
18#include "Framework/NtupleManager.h"
19#include "Framework/RunHeader.h"
26 : conditions_{*this} {
39 configuration.
get<
bool>(
"skipCorruptedInputFiles",
false);
41 input_files_ = configuration.
get<std::vector<std::string>>(
"inputFiles", {});
43 configuration.
get<std::vector<std::string>>(
"outputFiles", {});
51 auto run{configuration.
get<
int>(
"run", -1)};
54 auto libs{configuration.
get<std::vector<std::string>>(
"libraries", {})};
55 std::set<std::string> libraries_loaded;
56 for (
const auto &lib : libs) {
57 if (libraries_loaded.find(lib) != libraries_loaded.end()) {
61 void *handle = dlopen(lib.c_str(), RTLD_NOW);
62 if (handle ==
nullptr) {
63 EXCEPTION_RAISE(
"LibraryLoadFailure",
64 "Error loading library '" + lib +
"':" + dlerror());
67 libraries_loaded.insert(lib);
71 configuration.
get<
bool>(
"skimDefaultIsKeep",
true));
72 auto skim_rules{configuration.
get<std::vector<std::string>>(
"skimRules", {})};
73 for (
size_t i = 0; i < skim_rules.size(); i += 2) {
77 auto sequence{configuration.
get<std::vector<framework::config::Parameters>>(
79 if (sequence.empty() && configuration.
get<
bool>(
"testingMode",
false)) {
82 "No sequence has been defined. What should I be doing?\nUse "
83 "p.sequence to tell me what processors to run.");
85 for (
auto proc : sequence) {
86 auto class_name{proc.get<std::string>(
"className")};
87 auto instance_name{proc.get<std::string>(
"instanceName")};
89 EventProcessor::Factory::get().make(class_name, instance_name, *
this)};
91 EXCEPTION_RAISE(
"UnableToCreate",
92 "The EventProcessor Factory was unable to create " +
93 instance_name +
" of type " + class_name +
94 ". Did you inherit from framework::Producer or "
95 "framework::Analyzer? "
96 "Did you DECLARE_PRODUCER or DECLARE_ANALYZER in the "
97 "implementation (.cxx) file? "
98 "Did you use the class's full name (including "
99 "namespaces) in the Python configuration class? "
100 "Does the Python configuration class reference the "
101 "correct library it is a part of?");
104 proc.get<std::vector<framework::config::Parameters>>(
"histograms", {})};
105 if (!histograms.empty()) {
106 ep.value()->getHistoDirectory();
107 ep.value()->createHistograms(histograms);
109 ep.value()->configure(proc);
113 auto conditions_object_providers{
114 configuration.
get<std::vector<framework::config::Parameters>>(
115 "conditionsObjectProviders", {})};
116 for (
auto cop : conditions_object_providers) {
117 auto class_name{cop.get<std::string>(
"className")};
118 auto object_name{cop.get<std::string>(
"objectName")};
119 auto tag_name{cop.get<std::string>(
"tagName")};
124 bool log_performance = configuration.
get<
bool>(
"logPerformance",
false);
125 if (log_performance) {
126 std::vector<std::string> names{
sequence_.size()};
127 for (std::size_t i{0}; i <
sequence_.size(); i++) {
154 auto n_events_processed{0};
168 std::size_t i_proc{0};
176 proc->onProcessStart();
187 EXCEPTION_RAISE(
"InvalidConfig",
188 "No input files or output files were given.");
190 ldmx_log(warn) <<
"Several output files given with no input files. "
192 <<
"' will be used.";
217 ldmx_log(warn) <<
"The totalEvents was set, so maxEvents and "
218 "maxTriesPerEvent will be ignored!";
221 while (n_events_processed < event_limit) {
234 bool completed =
process(n_events_processed, num_tries, the_event);
239 if (completed) num_tries = 0;
245 n_events_processed++;
254 run_header.
setRunEnd(std::time(
nullptr));
259 if (n_events_processed < total_tries / 10000) {
261 <<
"Less than 1 event out of every 10k events tried was accepted!";
263 <<
"This could be an issue with your filtering and biasing procedure "
264 "since this is incredibly inefficient.";
272 bool single_output =
false;
274 single_output =
true;
277 EXCEPTION_RAISE(
"Process",
278 "Unable to handle case of different number of input and "
279 "output files (other than zero/one ouput file).");
289 ldmx_log(warn) <<
"Input file '" << infilename
290 <<
"' was found to be corrupted. Skipping.";
295 "We should never get here. "
296 "EventFile is corrupted but we aren't skipping corrupted inputs. "
297 "EventFile should be throwing its own exceptions in this case.");
301 ldmx_log(info) <<
"Opening file " << infilename;
310 if (!single_output or ifile == 0) {
319 master_file = out_file;
321 EXCEPTION_RAISE(
"Process",
"Unable to construct output file for " +
330 master_file = out_file;
337 master_file = &in_file;
343 n_events_processed++;
346 bool event_completed =
true;
360 ldmx_log(info) <<
"Got new run header from '"
364 ldmx_log(warn) <<
"Run header for run " << was_run
365 <<
" was not found!";
369 event_completed =
process(n_events_processed, 1, the_event);
374 n_events_processed++;
377 bool leave_early{
false};
379 ldmx_log(info) <<
"Reached event limit of " <<
event_limit_
385 ldmx_log(warn) <<
"Processing interrupted";
389 ldmx_log(info) <<
"Closing file " << infilename;
395 if (out_file and !single_output) {
423 proc->onProcessEnd();
440 TDirectory *child = owner->mkdir((
char *)dirName.c_str());
441 if (child) child->cd();
446 TDirectory *owner{
nullptr};
452 "You did not provide the necessary histogram file name to "
453 "put your histograms (or performance data) in.\n Provide this "
454 "name in the python configuration with 'p.histogramFile = "
455 "\"myHistFile.root\"' where p is the Process object.");
475 std::size_t i_proc{0};
480 proc->beforeNewRun(header);
494 proc->onNewRun(header);
499 ldmx_log(info) << header;
508 ldmx_log(info) <<
"Processing " << n + 1 <<
" Run "
509 <<
event.getEventHeader().getRun() <<
" Event "
510 <<
event.getEventHeader().getEventNumber() <<
" ("
511 << t.AsString(
"lc") <<
")";
515 std::size_t i_proc{0};
521 proc->process(event);
542 std::size_t i_proc{0};
547 proc->onFileOpen(file);
556 std::size_t i_proc{0};
561 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 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.
void writeRunHeader(ldmx::RunHeader &runHeader)
Write the run header into the run map.
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.
std::vector< EventProcessor * > sequence_
Ordered list of EventProcessors to execute.
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< 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.