11#include "Framework/EventFile.h"
13#include "Framework/Exception/Exception.h"
14#include "Framework/Logger.h"
15#include "Framework/NtupleManager.h"
17#include "Framework/RunHeader.h"
24 : conditions_{*this} {
35 configuration.
getParameter<
int>(
"compressionSetting", 9);
37 configuration.
getParameter<
bool>(
"skipCorruptedInputFiles",
false);
40 configuration.
getParameter<std::vector<std::string>>(
"inputFiles", {});
42 configuration.
getParameter<std::vector<std::string>>(
"outputFiles", {});
44 configuration.
getParameter<std::vector<std::string>>(
"keep", {});
56 configuration.
getParameter<std::vector<std::string>>(
"libraries", {})};
57 std::for_each(libs.begin(), libs.end(), [](
auto &lib) {
58 PluginFactory::getInstance().loadLibrary(lib);
62 configuration.
getParameter<
bool>(
"skimDefaultIsKeep",
true));
64 configuration.
getParameter<std::vector<std::string>>(
"skimRules", {})};
65 for (
size_t i = 0; i < skimRules.size(); i += 2) {
70 configuration.
getParameter<std::vector<framework::config::Parameters>>(
72 if (sequence.empty() &&
73 configuration.
getParameter<
bool>(
"testingMode",
false)) {
76 "No sequence has been defined. What should I be doing?\nUse "
77 "p.sequence to tell me what processors to run.");
79 for (
auto proc : sequence) {
80 auto className{proc.getParameter<std::string>(
"className")};
81 auto instanceName{proc.getParameter<std::string>(
"instanceName")};
83 className, instanceName, *
this);
87 "Unable to create instance '" + instanceName +
"' of class '" +
89 "'. Did you load the library that this class is apart of?");
92 proc.getParameter<std::vector<framework::config::Parameters>>(
94 if (!histograms.empty()) {
102 auto conditionsObjectProviders{
103 configuration.
getParameter<std::vector<framework::config::Parameters>>(
104 "conditionsObjectProviders", {})};
105 for (
auto cop : conditionsObjectProviders) {
106 auto className{cop.getParameter<std::string>(
"className")};
107 auto objectName{cop.getParameter<std::string>(
"objectName")};
108 auto tagName{cop.getParameter<std::string>(
"tagName")};
114 bool logPerformance =
115 configuration.
getParameter<
bool>(
"logPerformance",
false);
116 if (logPerformance) {
117 std::vector<std::string> names{
sequence_.size()};
118 for (std::size_t i{0}; i <
sequence_.size(); i++) {
145 auto n_events_processed{0};
159 std::size_t i_proc{0};
167 module->onProcessStart();
178 EXCEPTION_RAISE(
"InvalidConfig",
179 "No input files or output files were given.");
181 ldmx_log(warn) <<
"Several output files given with no input files. "
183 <<
"' will be used.";
208 ldmx_log(warn) <<
"The totalEvents was set, so maxEvents and "
209 "maxTriesPerEvent will be ignored!";
212 while (n_events_processed < event_limit) {
225 bool completed =
process(n_events_processed, numTries, theEvent);
230 if (completed) numTries = 0;
236 n_events_processed++;
247 ldmx_log(info) << runHeader;
251 if (n_events_processed < totalTries / 10000) {
253 <<
"Less than 1 event out of every 10k events tried was accepted!";
255 <<
"This could be an issue with your filtering and biasing procedure "
256 "since this is incredibly inefficient.";
264 bool singleOutput =
false;
269 EXCEPTION_RAISE(
"Process",
270 "Unable to handle case of different number of input and "
271 "output files (other than zero/one ouput file).");
281 ldmx_log(warn) <<
"Input file '" << infilename
282 <<
"' was found to be corrupted. Skipping.";
287 "We should never get here. "
288 "EventFile is corrupted but we aren't skipping corrupted inputs. "
289 "EventFile should be throwing its own exceptions in this case.");
293 ldmx_log(info) <<
"Opening file " << infilename;
302 if (!singleOutput or ifile == 0) {
311 masterFile = outFile;
313 EXCEPTION_RAISE(
"Process",
"Unable to construct output file for " +
322 masterFile = outFile;
329 masterFile = &inFile;
332 bool event_completed =
true;
346 ldmx_log(info) <<
"Got new run header from '"
351 ldmx_log(warn) <<
"Run header for run " << wasRun
352 <<
" was not found!";
356 event_completed =
process(n_events_processed, 1, theEvent);
361 n_events_processed++;
364 bool leave_early{
false};
366 ldmx_log(info) <<
"Reached event limit of " <<
eventLimit_ <<
" events";
371 ldmx_log(warn) <<
"Processing interrupted";
375 ldmx_log(info) <<
"Closing file " << infilename;
381 if (outFile and !singleOutput) {
409 module->onProcessEnd();
426 TDirectory *child = owner->mkdir((
char *)dirName.c_str());
427 if (child) child->cd();
432 TDirectory *owner{
nullptr};
438 "You did not provide the necessary histogram file name to "
439 "put your histograms (or performance data) in.\n Provide this "
440 "name in the python configuration with 'p.histogramFile = "
441 "\"myHistFile.root\"' where p is the Process object.");
456 std::size_t i_proc{0};
459 if (
dynamic_cast<Producer *
>(module)) {
462 dynamic_cast<Producer *
>(module)->beforeNewRun(header);
477 module->onNewRun(header);
489 ldmx_log(info) <<
"Processing " << n + 1 <<
" Run "
490 <<
event.getEventHeader().getRun() <<
" Event "
491 <<
event.getEventHeader().getEventNumber() <<
" ("
492 << t.AsString(
"lc") <<
")";
496 std::size_t i_proc{0};
502 if (
dynamic_cast<Producer *
>(module)) {
503 (
dynamic_cast<Producer *
>(module))->produce(event);
504 }
else if (
dynamic_cast<Analyzer *
>(module)) {
505 (
dynamic_cast<Analyzer *
>(module))->analyze(event);
527 std::size_t i_proc{0};
532 module->onFileOpen(file);
541 std::size_t i_proc{0};
546 module->onFileClose(file);
Base classes for all user event processing components to extend.
Class implementing an event buffer system for storing event data.
Class which provides a singleton module factory that creates EventProcessor objects.
Class which represents the process under execution.
Specific exception used to abort an event.
Base class for a module which does not produce a data product.
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.
virtual void configure(framework::config::Parameters ¶meters)
Callback for the EventProcessor to configure itself from the given set of parameters.
void createHistograms(const std::vector< framework::config::Parameters > &histos)
Internal function which is used to create histograms passed from the python configuration @parma hist...
TDirectory * getHistoDirectory()
Access/create a directory in the histogram file for this event processor to create histograms and ana...
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.
EventProcessor * createEventProcessor(const std::string &classname, const std::string &moduleInstanceName, Process &process)
Make an event processor.
static PluginFactory & getInstance()
Get the factory instance.
std::vector< EventProcessor * > sequence_
Ordered list of EventProcessors to execute.
std::vector< std::string > outputFiles_
List of output file names.
ldmx::RunHeader * runHeader_
Pointer to the current RunHeader, used for Conditions information.
bool skipCorruptedInputFiles_
allow the Process to skip input files that are corrupted
std::vector< std::string > dropKeepRules_
Set of drop/keep rules.
void run()
Run the process.
void newRun(ldmx::RunHeader &header)
Run through the processors and let them know that we are starting a new run.
std::string histoFilename_
Filename for histograms and other user products.
StorageControl storageController_
Storage controller.
TDirectory * openHistoFile()
Open a ROOT TFile to write histograms and TTrees.
int totalEvents_
Number of events we'd like to produce independetly of the number of tries it would take.
int maxTries_
Maximum number of attempts to make before giving up on an event.
~Process()
Class Destructor.
void onFileClose(EventFile &file) const
File is begin closed.
std::vector< std::string > inputFiles_
List of input files to process.
TDirectory * makeHistoDirectory(const std::string &dirName)
Construct a TDirectory* for the given module.
std::string passname_
Processing pass name.
performance::Tracker * performance_
class with calls backs to track performance measurements of software
const ldmx::EventHeader * eventHeader_
Pointer to the current EventHeader, used for Conditions information.
int compressionSetting_
Compression setting to pass to output files.
bool process(int n, int n_tries, Event &event) const
Process the input event through the sequence of processors.
int logFrequency_
The frequency with which event info is printed.
int runForGeneration_
Run number to use if generating events.
Process()
Private dummy constructor We hide it here because it shouldn't be used anywhere else.
void onFileOpen(EventFile &file) const
File is being opened.
Conditions conditions_
Set of ConditionsProviders.
int eventLimit_
Limit on events to process.
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.
TFile * histoTFile_
TFile for histograms and other user products.
Base class for a module which produces a data product.
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.
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
All classes in the ldmx-sw project use this namespace.