LDMX Software
framework::Process Class Reference

Class which represents the process under execution. More...

#include <Process.h>

Public Member Functions

 Process (const framework::config::Parameters &configuration)
 Class constructor.
 
 ~Process ()
 Class Destructor.
 
const std::string & getPassName () const
 Get the processing pass label.
 
int getRunNumber () const
 Get the current run number or the run number to be used when initiating new events from the job.
 
const ldmx::EventHeadergetEventHeader () const
 Get the pointer to the current event header, if defined.
 
const ldmx::RunHeadergetRunHeader () const
 Get the pointer to the current run header, if defined.
 
ConditionsgetConditions ()
 Get a reference to the conditions system.
 
int getLogFrequency () const
 Get the frequency with which the event information is printed.
 
void run ()
 Run the process.
 
void requestFinish ()
 Request that the processing finish with this event.
 
TDirectory * makeHistoDirectory (const std::string &dirName)
 Construct a TDirectory* for the given module.
 
TDirectory * openHistoFile ()
 Open a ROOT TFile to write histograms and TTrees.
 
StorageControlgetStorageController ()
 Access the storage control unit for this process.
 
void setEventHeader (ldmx::EventHeader *h)
 Set the pointer to the current event header, used only for tests.
 

Private Member Functions

bool process (int n, int n_tries, Event &event) const
 Process the input event through the sequence of processors.
 
void newRun (ldmx::RunHeader &header, EventFile *out)
 Run through the processors and let them know that we are starting a new run.
 
void onFileOpen (EventFile &file) const
 File is being opened.
 
void onFileClose (EventFile &file) const
 File is begin closed.
 
 enableLogging ("Process")
 Turn on logging for our process.
 

Private Attributes

framework::config::Parameters config_
 The parameters used to configure this class.
 
std::string pass_name_
 Processing pass name.
 
int event_limit_
 Limit on events to process.
 
int min_events_
 When reading a file in, what's the first event to read.
 
int total_events_
 Number of events we'd like to produce independetly of the number of tries it would take.
 
int log_frequency_
 The frequency with which event info is printed.
 
int max_tries_
 Maximum number of attempts to make before giving up on an event.
 
bool skip_corrupted_input_files_
 allow the Process to skip input files that are corrupted
 
bool allow_incomplete_input_files_
 allow the Process to read input files whose runs never finished
 
StorageControl storage_controller_
 Storage controller.
 
std::vector< EventProcessor * > sequence_
 Ordered list of EventProcessors to execute.
 
Conditions conditions_
 Set of ConditionsProviders.
 
std::vector< std::string > input_files_
 List of input files to process.
 
std::vector< std::string > output_files_
 List of output file names.
 
int compression_setting_
 Compression setting to pass to output files.
 
std::vector< std::string > drop_keep_rules_
 Set of drop/keep rules.
 
int run_for_generation_ {1}
 Run number to use if generating events.
 
std::string histo_filename_
 Filename for histograms and other user products.
 
const ldmx::EventHeaderevent_header_ {0}
 Pointer to the current EventHeader, used for Conditions information.
 
ldmx::RunHeaderrun_header_ {0}
 Pointer to the current RunHeader, used for Conditions information.
 
TFile * histo_t_file_ {0}
 TFile for histograms and other user products.
 
performance::Trackerperformance_ {0}
 class with calls backs to track performance measurements of software
 

Detailed Description

Class which represents the process under execution.

Definition at line 37 of file Process.h.

Constructor & Destructor Documentation

◆ Process()

framework::Process::Process ( const framework::config::Parameters & configuration)

Class constructor.

Parameters
configurationParameters to configure process with

Definition at line 30 of file Process.cxx.

31 : conditions_{*this} {
32 config_ = configuration;
33
34 pass_name_ = configuration.get<std::string>("pass_name", "");
35 histo_filename_ = configuration.get<std::string>("histogram_file", "");
36
37 max_tries_ = configuration.get<int>("max_tries_per_event", 1);
38 event_limit_ = configuration.get<int>("max_events", -1);
39 min_events_ = configuration.get<int>("min_events", -1);
40 total_events_ = configuration.get<int>("total_events", -1);
41 log_frequency_ = configuration.get<int>("log_frequency", -1);
42 compression_setting_ = configuration.get<int>("compression_setting", 9);
44 configuration.get<bool>("skip_corrupted_input_files", false);
46 configuration.get<bool>("allow_incomplete_input_files", false);
47
48 input_files_ = configuration.get<std::vector<std::string>>("input_files", {});
50 configuration.get<std::vector<std::string>>("output_files", {});
51 drop_keep_rules_ = configuration.get<std::vector<std::string>>("keep", {});
52
53 event_header_ = 0;
54
55 // set up the logging for this run
56 logging::open(configuration.get<framework::config::Parameters>("logger", {}));
57
58 auto run{configuration.get<int>("run", -1)};
59 if (run > 0) run_for_generation_ = run;
60
61 auto libs{configuration.get<std::vector<std::string>>("libraries", {})};
62 std::set<std::string> libraries_loaded;
63 for (const auto& lib : libs) {
64 if (libraries_loaded.find(lib) != libraries_loaded.end()) {
65 continue;
66 }
67
68 void* handle = dlopen(lib.c_str(), RTLD_NOW);
69 if (handle == nullptr) {
70 EXCEPTION_RAISE("LibraryLoadFailure",
71 "Error loading library '" + lib + "':" + dlerror());
72 }
73
74 libraries_loaded.insert(lib);
75 }
76
78 configuration.get<bool>("skim_default_is_keep", true));
79 auto skim_rules{
80 configuration.get<std::vector<std::string>>("skim_rules", {})};
81 for (size_t i = 0; i < skim_rules.size(); i += 2) {
82 storage_controller_.addRule(skim_rules[i], skim_rules[i + 1]);
83 }
84
85 auto sequence{configuration.get<std::vector<framework::config::Parameters>>(
86 "sequence", {})};
87 if (sequence.empty() && configuration.get<bool>("testing_mode", false)) {
88 EXCEPTION_RAISE(
89 "NoSeq",
90 "No sequence has been defined. What should I be doing?\nUse "
91 "p.sequence to tell me what processors to run.");
92 }
93 for (auto proc : sequence) {
94 auto class_name{proc.get<std::string>("class_name")};
95 auto instance_name{proc.get<std::string>("instance_name")};
96 auto ep{
97 EventProcessor::Factory::get().make(class_name, instance_name, *this)};
98 if (not ep) {
99 EXCEPTION_RAISE("UnableToCreate",
100 "The EventProcessor Factory was unable to create " +
101 instance_name + " of type " + class_name +
102 ". Did you inherit from framework::Producer or "
103 "framework::Analyzer? "
104 "Did you DECLARE_PRODUCER or DECLARE_ANALYZER in the "
105 "implementation (.cxx) file? "
106 "Did you use the class's full name (including "
107 "namespaces) in the Python configuration class? "
108 "Does the Python configuration class reference the "
109 "correct library it is a part of?");
110 }
111 auto histograms{
112 proc.get<std::vector<framework::config::Parameters>>("histograms", {})};
113 if (!histograms.empty()) {
114 ep.value()->getHistoDirectory();
115 ep.value()->createHistograms(histograms);
116 }
117 ep.value()->configure(proc);
118 sequence_.push_back(ep.value());
119 }
120
121 auto conditions_object_providers{
122 configuration.get<std::vector<framework::config::Parameters>>(
123 "conditions_object_providers", {})};
124 for (auto cop : conditions_object_providers) {
125 auto class_name{cop.get<std::string>("class_name")};
126 auto object_name{cop.get<std::string>("object_name")};
127 auto tag_name{cop.get<std::string>("tag_name")};
128 conditions_.createConditionsObjectProvider(class_name, object_name,
129 tag_name, cop);
130 }
131
132 bool log_performance = configuration.get<bool>("log_performance", false);
133 if (log_performance) {
134 std::vector<std::string> names{sequence_.size()};
135 for (std::size_t i{0}; i < sequence_.size(); i++) {
136 names[i] = sequence_[i]->getName();
137 }
139 new performance::Tracker(makeHistoDirectory("performance"), names);
140 }
141}
void createConditionsObjectProvider(const std::string &classname, const std::string &instancename, const std::string &tagname, const framework::config::Parameters &params)
Create a ConditionsObjectProvider given the information.
int log_frequency_
The frequency with which event info is printed.
Definition Process.h:173
bool skip_corrupted_input_files_
allow the Process to skip input files that are corrupted
Definition Process.h:181
std::string histo_filename_
Filename for histograms and other user products.
Definition Process.h:223
int max_tries_
Maximum number of attempts to make before giving up on an event.
Definition Process.h:176
int run_for_generation_
Run number to use if generating events.
Definition Process.h:220
int compression_setting_
Compression setting to pass to output files.
Definition Process.h:214
void run()
Run the process.
Definition Process.cxx:157
int event_limit_
Limit on events to process.
Definition Process.h:162
std::string pass_name_
Processing pass name.
Definition Process.h:159
bool allow_incomplete_input_files_
allow the Process to read input files whose runs never finished
Definition Process.h:189
int total_events_
Number of events we'd like to produce independetly of the number of tries it would take.
Definition Process.h:170
std::vector< EventProcessor * > sequence_
Ordered list of EventProcessors to execute.
Definition Process.h:195
std::vector< std::string > drop_keep_rules_
Set of drop/keep rules.
Definition Process.h:217
TDirectory * makeHistoDirectory(const std::string &dirName)
Construct a TDirectory* for the given module.
Definition Process.cxx:487
performance::Tracker * performance_
class with calls backs to track performance measurements of software
Definition Process.h:235
int min_events_
When reading a file in, what's the first event to read.
Definition Process.h:165
StorageControl storage_controller_
Storage controller.
Definition Process.h:192
std::vector< std::string > output_files_
List of output file names.
Definition Process.h:205
std::vector< std::string > input_files_
List of input files to process.
Definition Process.h:202
const ldmx::EventHeader * event_header_
Pointer to the current EventHeader, used for Conditions information.
Definition Process.h:226
Conditions conditions_
Set of ConditionsProviders.
Definition Process.h:198
framework::config::Parameters config_
The parameters used to configure this class.
Definition Process.h:156
void setDefaultKeep(bool keep)
Set the default state.
void addRule(const std::string &processor_pat, const std::string &purpose_pat)
Add a listening rule.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78

References framework::StorageControl::addRule(), allow_incomplete_input_files_, compression_setting_, conditions_, config_, framework::Conditions::createConditionsObjectProvider(), drop_keep_rules_, event_header_, event_limit_, framework::config::Parameters::get(), histo_filename_, input_files_, log_frequency_, makeHistoDirectory(), max_tries_, min_events_, output_files_, pass_name_, performance_, run(), run_for_generation_, sequence_, framework::StorageControl::setDefaultKeep(), skip_corrupted_input_files_, storage_controller_, and total_events_.

◆ ~Process()

framework::Process::~Process ( )

Class Destructor.

Cleans up sequence of EventProcessors. These processors were created by ConfigurePython and should be deleted.

Definition at line 143 of file Process.cxx.

143 {
144 // need to delete the performance object so that it is
145 // written before we close the histogram file below
146 if (performance_) delete performance_;
147 for (EventProcessor* ep : sequence_) {
148 delete ep;
149 }
150 if (histo_t_file_) {
151 histo_t_file_->Write();
152 delete histo_t_file_;
153 histo_t_file_ = 0;
154 }
155}
TFile * histo_t_file_
TFile for histograms and other user products.
Definition Process.h:232

References histo_t_file_, performance_, and sequence_.

Member Function Documentation

◆ getConditions()

Conditions & framework::Process::getConditions ( )
inline

Get a reference to the conditions system.

Definition at line 79 of file Process.h.

79{ return conditions_; }

References conditions_.

Referenced by framework::ConditionsObjectProvider::requestParentCondition().

◆ getEventHeader()

const ldmx::EventHeader * framework::Process::getEventHeader ( ) const
inline

Get the pointer to the current event header, if defined.

Definition at line 69 of file Process.h.

69{ return event_header_; }

References event_header_.

Referenced by framework::Conditions::getConditionPtr().

◆ getLogFrequency()

int framework::Process::getLogFrequency ( ) const
inline

Get the frequency with which the event information is printed.

Returns
integer log frequency (negative if turned off)

Definition at line 85 of file Process.h.

85{ return log_frequency_; }

References log_frequency_.

◆ getPassName()

const std::string & framework::Process::getPassName ( ) const
inline

Get the processing pass label.

Returns
The processing pass label.

Definition at line 57 of file Process.h.

57{ return pass_name_; }

References pass_name_.

Referenced by framework::RandomNumberSeedService::onNewRun().

◆ getRunHeader()

const ldmx::RunHeader * framework::Process::getRunHeader ( ) const
inline

Get the pointer to the current run header, if defined.

Definition at line 74 of file Process.h.

74{ return run_header_; }
ldmx::RunHeader * run_header_
Pointer to the current RunHeader, used for Conditions information.
Definition Process.h:229

References run_header_.

◆ getRunNumber()

int framework::Process::getRunNumber ( ) const

Get the current run number or the run number to be used when initiating new events from the job.

Returns
int Run number

Definition at line 483 of file Process.cxx.

483 {
485}
int getRun() const
Return the run number.
Definition EventHeader.h:84

References event_header_, ldmx::EventHeader::getRun(), and run_for_generation_.

◆ getStorageController()

StorageControl & framework::Process::getStorageController ( )
inline

Access the storage control unit for this process.

Definition at line 110 of file Process.h.

110{ return storage_controller_; }

References storage_controller_.

◆ makeHistoDirectory()

TDirectory * framework::Process::makeHistoDirectory ( const std::string & dirName)

Construct a TDirectory* for the given module.

Definition at line 487 of file Process.cxx.

487 {
488 auto owner{openHistoFile()};
489 TDirectory* child = owner->mkdir((char*)dirName.c_str());
490 if (child) child->cd();
491 return child;
492}
TDirectory * openHistoFile()
Open a ROOT TFile to write histograms and TTrees.
Definition Process.cxx:494

References openHistoFile().

Referenced by Process().

◆ newRun()

void framework::Process::newRun ( ldmx::RunHeader & header,
EventFile * out )
private

Run through the processors and let them know that we are starting a new run.

The header is written to the output file once the producers have filled it, so a killed job still leaves it behind.

Parameters
[in]headerRunHeader for the new run
[in]outoutput file to write the header to, if any

Definition at line 516 of file Process.cxx.

516 {
517 // Producers are allowed to put parameters into
518 // the run header through 'beforeNewRun' method
519
520 // Put the version into the rh string param
521 header.setStringParameter("Pass = " + pass_name_ + ", version",
522 LDMXSW_VERSION);
523 if (performance_) performance_->start(performance::Callback::beforeNewRun, 0);
524 std::size_t i_proc{0};
525 for (auto proc : sequence_) {
526 i_proc++;
527 if (performance_)
528 performance_->start(performance::Callback::beforeNewRun, i_proc);
529 proc->beforeNewRun(header);
530 if (performance_)
531 performance_->stop(performance::Callback::beforeNewRun, i_proc);
532 }
533 if (performance_) performance_->stop(performance::Callback::beforeNewRun, 0);
534 // now run header has been modified by Producers,
535 // it is valid to read from for everyone else in 'onNewRun'
536 // header is final, so write it now in case we are killed
537 if (out) out->writeRunTree();
538 if (performance_) performance_->start(performance::Callback::onNewRun, 0);
539 conditions_.onNewRun(header);
540 i_proc = 0;
541 for (auto proc : sequence_) {
542 i_proc++;
543 if (performance_)
544 performance_->start(performance::Callback::onNewRun, i_proc);
545 proc->onNewRun(header);
546 if (performance_)
547 performance_->stop(performance::Callback::onNewRun, i_proc);
548 }
549 if (performance_) performance_->stop(performance::Callback::onNewRun, 0);
550 ldmx_log(info) << header;
551}
void onNewRun(ldmx::RunHeader &)
Calls onNewRun for all ConditionsObjectProviders.
void start(Callback cb, std::size_t i_proc)
start the timer for a specific callback and specific processor
Definition Tracker.cxx:90
void stop(Callback cb, std::size_t i_proc)
stop the timer for a specific callback and specific processor
Definition Tracker.cxx:94
void setStringParameter(const std::string &name, std::string value)
Set a string parameter value.
Definition RunHeader.h:260

References conditions_, framework::Conditions::onNewRun(), pass_name_, performance_, sequence_, ldmx::RunHeader::setStringParameter(), framework::performance::Tracker::start(), framework::performance::Tracker::stop(), and framework::EventFile::writeRunTree().

Referenced by run().

◆ onFileClose()

void framework::Process::onFileClose ( EventFile & file) const
private

File is begin closed.

Definition at line 605 of file Process.cxx.

605 {
606 if (performance_) performance_->start(performance::Callback::onFileClose, 0);
607 std::size_t i_proc{0};
608 for (auto proc : sequence_) {
609 i_proc++;
610 if (performance_)
611 performance_->start(performance::Callback::onFileClose, i_proc);
612 proc->onFileClose(file);
613 if (performance_)
614 performance_->stop(performance::Callback::onFileClose, i_proc);
615 }
616 if (performance_) performance_->stop(performance::Callback::onFileClose, 0);
617}

References performance_, sequence_, framework::performance::Tracker::start(), and framework::performance::Tracker::stop().

Referenced by run().

◆ onFileOpen()

void framework::Process::onFileOpen ( EventFile & file) const
private

File is being opened.

Definition at line 591 of file Process.cxx.

591 {
592 if (performance_) performance_->start(performance::Callback::onFileOpen, 0);
593 std::size_t i_proc{0};
594 for (auto proc : sequence_) {
595 i_proc++;
596 if (performance_)
597 performance_->start(performance::Callback::onFileOpen, i_proc);
598 proc->onFileOpen(file);
599 if (performance_)
600 performance_->stop(performance::Callback::onFileOpen, i_proc);
601 }
602 if (performance_) performance_->stop(performance::Callback::onFileOpen, 0);
603}

References performance_, sequence_, framework::performance::Tracker::start(), and framework::performance::Tracker::stop().

Referenced by run().

◆ openHistoFile()

TDirectory * framework::Process::openHistoFile ( )

Open a ROOT TFile to write histograms and TTrees.

Definition at line 494 of file Process.cxx.

494 {
495 TDirectory* owner{nullptr};
496
497 if (histo_filename_.empty()) {
498 // trying to write histograms/ntuples but no file defined
499 EXCEPTION_RAISE(
500 "NoHistFileName",
501 "You did not provide the necessary histogram file name to "
502 "put your histograms (or performance data) in.\n Provide this "
503 "name in the python configuration with 'p.histogramFile = "
504 "\"myHistFile.root\"' where p is the Process object.");
505 } else if (histo_t_file_ == nullptr) {
506 histo_t_file_ = new TFile(histo_filename_.c_str(), "RECREATE");
507 owner = histo_t_file_;
508 } else {
509 owner = histo_t_file_;
510 }
511 owner->cd();
512
513 return owner;
514}

References histo_filename_, and histo_t_file_.

Referenced by makeHistoDirectory().

◆ process()

bool framework::Process::process ( int n,
int n_tries,
Event & event ) const
private

Process the input event through the sequence of processors.

The input counters (for events and tries) are only used to print the status.

Parameters
[in]ncounter for number of events processed
[in]n_triescounter for number of tries on current event
[in,out]eventreference to event we are going to process
Returns
true if event was full processed (false if aborted)

Definition at line 553 of file Process.cxx.

553 {
554 if ((log_frequency_ != -1) && ((n + 1) % log_frequency_ == 0) &&
555 (n_try < 2)) {
556 // only printout event counter if we've enabled log frequency, the event
557 // matches the frequency and we are on the first try
558 TTimeStamp t;
559 ldmx_log(info) << "Processing " << n + 1 << " Run "
560 << event.getEventHeader().getRun() << " Event "
561 << event.getEventHeader().getEventNumber() << " ("
562 << t.AsString("lc") << ")";
563 }
564
565 if (performance_) performance_->start(performance::Callback::process, 0);
566 std::size_t i_proc{0};
567 try {
568 for (auto proc : sequence_) {
569 i_proc++;
570 if (performance_)
571 performance_->start(performance::Callback::process, i_proc);
572 proc->process(event);
573 if (performance_)
574 performance_->stop(performance::Callback::process, i_proc);
575 }
576 } catch (AbortEventException&) {
577 if (performance_) {
578 performance_->stop(performance::Callback::process, i_proc);
579 performance_->stop(performance::Callback::process, 0);
580 performance_->endEvent(false);
581 }
582 return false;
583 }
584 if (performance_) {
585 performance_->stop(performance::Callback::process, 0);
586 performance_->endEvent(true);
587 }
588 return true;
589}
void endEvent(bool completed)
inform us that we finished an event (and whether it was completed or not)
Definition Tracker.cxx:98

References framework::performance::Tracker::endEvent(), log_frequency_, performance_, sequence_, framework::performance::Tracker::start(), and framework::performance::Tracker::stop().

Referenced by run().

◆ requestFinish()

void framework::Process::requestFinish ( )
inline

Request that the processing finish with this event.

Definition at line 95 of file Process.h.

95{ event_limit_ = 0; }

References event_limit_.

◆ run()

void framework::Process::run ( )

Run the process.

Definition at line 157 of file Process.cxx.

157 {
159
160 // Counter to keep track of the number of events that have been
161 // procesed
162 auto n_events_processed{0};
163
164 // make sure the ntuple manager is in a blank state
166
167 // event bus for this process
168 Event the_event(pass_name_);
169 // the EventHeader object is created with the event bus as
170 // one of its members, we obtain a pointer for the header
171 // here so we can share it with the conditions system
172 event_header_ = the_event.getEventHeaderPtr();
173 the_event.getEventHeader().setRun(run_for_generation_);
174
175 // Start by notifying everyone that modules processing is beginning
176 std::size_t i_proc{0};
177 if (performance_)
178 performance_->start(performance::Callback::onProcessStart, 0);
180 for (auto proc : sequence_) {
181 i_proc++;
182 if (performance_)
183 performance_->start(performance::Callback::onProcessStart, i_proc);
184 proc->onProcessStart();
185 if (performance_)
186 performance_->stop(performance::Callback::onProcessStart, i_proc);
187 }
188 if (performance_)
189 performance_->stop(performance::Callback::onProcessStart, 0);
190
191 // If we have no input files, but do have an event number, run for
192 // that number of events and generate an output file.
193 if (input_files_.empty() && event_limit_ > 0) {
194 if (output_files_.empty()) {
195 EXCEPTION_RAISE("InvalidConfig",
196 "No input files or output files were given.");
197 } else if (output_files_.size() > 1) {
198 ldmx_log(warn) << "Several output files given with no input files. "
199 << "Only the first output file '" << output_files_.at(0)
200 << "' will be used.";
201 }
202 std::string output_file_name = output_files_.at(0);
203
204 // Configure the event file to create an output file with no parent. This
205 // requires setting the parameters isOutputFile and isSingleOutput to true.
206 EventFile out_file(config_, output_file_name, nullptr, true, true, false);
207 onFileOpen(out_file);
208 out_file.setupEvent(&the_event);
209
210 for (auto rule : drop_keep_rules_) out_file.addDrop(rule);
211
212 auto run_header = std::make_shared<ldmx::RunHeader>(run_for_generation_);
213 run_header->setRunStart(std::time(nullptr)); // set run starting
214 run_header_ = run_header.get(); // give handle to run header to process
215 out_file.writeRunHeader(run_header); // add run header to file
216
217 newRun(*run_header, &out_file);
218
219 int total_tries = 0; // total number of tries for entire run
220 int num_tries = 0; // number of tries for the current event number
221 int event_limit = event_limit_;
222 if (total_events_ > 0) {
223 // Have a warning at the first event
224 if (num_tries == 0)
225 ldmx_log(warn) << "The total_events was set, so max_events and "
226 "max_tries_per_event will be ignored!";
227 event_limit = total_events_;
228 }
229 while (n_events_processed < event_limit) {
230 // Check for preemption before processing each event
231 if (preemption_received_) {
232 ldmx_log(fatal)
233 << "Preemption signal received, stopping event generation";
234 break;
235 }
236
237 total_tries++;
238 num_tries++;
239
240 ldmx::EventHeader& eh = the_event.getEventHeader();
242 eh.setEventNumber(n_events_processed + 1);
243 eh.setTimestamp(TTimeStamp());
244
245 // reset the storage controller state
247 logging::Formatter::set(the_event.getEventNumber());
248
249 bool completed = process(n_events_processed, num_tries, the_event);
250
251 out_file.nextEvent(storage_controller_.keepEvent(completed));
252
253 // reset try counter only on successfully completed events
254 if (completed) num_tries = 0;
255
256 // we use modulo here insetad of >= because we want to carry
257 // the number of tries across the number of events processed boundary
258 // total_events_ is set let's not exit until that's reached
259 if (completed or (total_events_ < 0 and num_tries % max_tries_ == 0)) {
260 n_events_processed++; // increment events made
261 NtupleManager::getInstance().fill(); // fill ntuples
262 }
263
265 }
266
267 onFileClose(out_file);
268
269 run_header->setRunEnd(std::time(nullptr));
270 run_header->setNumTries(total_tries);
271 out_file.writeRunTree(not preemption_received_);
272
273 // Give a warning that this filter has very low efficiency
274 if (n_events_processed < total_tries / 10000) { // integer division is okay
275 ldmx_log(warn)
276 << "Less than 1 event out of every 10k events tried was accepted!";
277 ldmx_log(warn)
278 << "This could be an issue with your filtering and biasing procedure "
279 "since this is incredibly inefficient.";
280 }
281
282 } else {
283 // there are input files
284
285 EventFile* out_file(0);
286
287 bool single_output = false;
288 if (output_files_.size() == 1) {
289 single_output = true;
290 } else if (!output_files_.empty() and
291 output_files_.size() != input_files_.size()) {
292 EXCEPTION_RAISE("Process",
293 "Unable to handle case of different number of input and "
294 "output files (other than zero/one ouput file).");
295 }
296
297 // next, loop through the files
298 int ifile = 0;
299 int was_run = -1;
300 for (auto infilename : input_files_) {
301 EventFile in_file(config_, infilename);
302 if (in_file.isCorrupted()) {
304 ldmx_log(warn) << "Input file '" << infilename
305 << "' was found to be corrupted. Skipping.";
306 continue;
307 } else {
308 EXCEPTION_RAISE(
309 "BadCode",
310 "We should never get here. "
311 "EventFile is corrupted but we aren't skipping corrupted inputs. "
312 "EventFile should be throwing its own exceptions in this case.");
313 }
314 }
315
316 auto incomplete_runs{in_file.getIncompleteRuns()};
317 if (not incomplete_runs.empty()) {
318 std::string run_list;
319 for (int run : incomplete_runs) {
320 if (not run_list.empty()) run_list += ", ";
321 run_list += std::to_string(run);
322 }
324 ldmx_log(warn) << "Input file '" << infilename << "' holds run(s) "
325 << run_list
326 << " that were never finished, so events are missing. "
327 "Processing it anyway because "
328 "'allow_incomplete_input_files' is set.";
329 } else {
330 EXCEPTION_RAISE(
331 "IncompleteInput",
332 "Input file '" + infilename + "' holds run(s) " + run_list +
333 " that were never finished, so events are missing from the "
334 "end of them. Set 'allow_incomplete_input_files' on the "
335 "Process to read it anyway, but know that the sample it "
336 "gives you is biased.");
337 }
338 }
339
340 ldmx_log(info) << "Opening file " << infilename;
341 onFileOpen(in_file);
342
343 // configure event file that will be iterated over
344 EventFile* master_file;
345 if (!output_files_.empty()) {
346 // setup new output file if either
347 // 1) we are not in single output mode
348 // 2) this is the first input file
349 if (!single_output or ifile == 0) {
350 // setup new output file
351 out_file = new EventFile(config_, output_files_[ifile], &in_file,
352 single_output);
353 ifile++;
354
355 // setup theEvent we will iterate over
356 if (out_file) {
357 out_file->setupEvent(&the_event);
358 master_file = out_file;
359 } else {
360 EXCEPTION_RAISE("Process", "Unable to construct output file for " +
361 output_files_[ifile]);
362 }
363
364 for (auto rule : drop_keep_rules_) out_file->addDrop(rule);
365
366 } else {
367 // all other input files
368 out_file->updateParent(&in_file);
369 master_file = out_file;
370
371 } // check if in singleOutput mode
372
373 } else {
374 // empty output file list, use inputFile as master file
375 in_file.setupEvent(&the_event);
376 master_file = &in_file;
377 }
378
379 // In case we'd like to skip up to the event of min_events_
380 while (n_events_processed < (min_events_ - 1) &&
381 master_file->nextEvent(false)) {
382 n_events_processed++;
383 }
384
385 bool event_completed = true;
386 while (!preemption_received_ &&
387 master_file->nextEvent(
388 storage_controller_.keepEvent(event_completed)) &&
389 ((event_limit_ < 0) || (n_events_processed < event_limit_))) {
390 // clean up for storage control calculation
392 logging::Formatter::set(the_event.getEventNumber());
393
394 // notify for new run if necessary
395 if (the_event.getEventHeader().getRun() != was_run) {
396 was_run = the_event.getEventHeader().getRun();
397 ldmx::RunHeader* rh{master_file->getRunHeaderPtr(was_run)};
398 if (rh != nullptr) {
399 run_header_ = rh;
400 ldmx_log(info) << "Got new run header from '"
401 << master_file->getFileName() << "'";
402 newRun(*run_header_, out_file);
403 } else {
404 // no header means no newRun, so conditions stay uninitialised
405 EXCEPTION_RAISE(
406 "MissingRunHeader",
407 "Run header for run " + std::to_string(was_run) +
408 " was not found in '" + master_file->getFileName() +
409 "'. Conditions cannot be initialised without it.");
410 }
411 }
412
413 event_completed = process(n_events_processed, 1, the_event);
414
415 if (event_completed) NtupleManager::getInstance().fill();
417
418 n_events_processed++;
419 } // loop through events
420
421 if (preemption_received_) {
422 ldmx_log(fatal) << "Preemption signal received, stopping event "
423 "processing and closing files";
424 }
425
426 bool leave_early{false};
427 if (event_limit_ > 0 && n_events_processed == event_limit_) {
428 ldmx_log(info) << "Reached event limit of " << event_limit_
429 << " events";
430 leave_early = true;
431 }
432
433 if (event_limit_ == 0 && n_events_processed > event_limit_) {
434 ldmx_log(warn) << "Processing interrupted";
435 leave_early = true;
436 }
437
438 ldmx_log(info) << "Closing file " << infilename;
439 onFileClose(in_file);
440
441 // Reset the event in case of multiple input files
442 the_event.onEndOfFile();
443
444 if (out_file and !single_output) {
445 out_file->writeRunTree(not preemption_received_);
446 delete out_file;
447 out_file = nullptr;
448 }
449
450 if (leave_early) {
451 break;
452 }
453 } // loop through input files
454
455 if (out_file) {
456 // close outFile
457 // outFile would survive to here in single output mode
458 out_file->writeRunTree(not preemption_received_);
459 delete out_file;
460 out_file = nullptr;
461 }
462
463 } // are there input files? if-else tree
464
465 // finally, notify everyone that we are stopping
466 if (performance_) performance_->start(performance::Callback::onProcessEnd, 0);
467 i_proc = 0;
468 for (auto proc : sequence_) {
469 i_proc++;
470 if (performance_)
471 performance_->start(performance::Callback::onProcessEnd, i_proc);
472 proc->onProcessEnd();
473 if (performance_)
474 performance_->stop(performance::Callback::onProcessEnd, i_proc);
475 }
476 if (performance_) performance_->stop(performance::Callback::onProcessEnd, 0);
477
478 // we're done so let's close up the logging
479 logging::close();
481}
void onProcessStart()
Calls onProcessStart for all ConditionsObjectProviders.
void clear()
Reset all of the variables to their limits.
static NtupleManager & getInstance()
void reset()
Reset NtupleManager to blank state.
void onFileClose(EventFile &file) const
File is begin closed.
Definition Process.cxx:605
void newRun(ldmx::RunHeader &header, EventFile *out)
Run through the processors and let them know that we are starting a new run.
Definition Process.cxx:516
bool process(int n, int n_tries, Event &event) const
Process the input event through the sequence of processors.
Definition Process.cxx:553
void onFileOpen(EventFile &file) const
File is being opened.
Definition Process.cxx:591
bool keepEvent(bool event_completed) const
Determine if the current event should be kept, based on the defined rules.
void resetEventState()
Reset the event-by-event state.
static void set(int n)
set the event number in the current Formatter
Definition Logger.cxx:158
void absoluteStop()
literally last line of Process::run (if run compeletes without error)
Definition Tracker.cxx:88
void absoluteStart()
literally first line of Process::run
Definition Tracker.cxx:86
Provides header information an event such as event number and timestamp.
Definition EventHeader.h:44
void setEventNumber(int eventNumber)
Set the event number.
void setRun(int run)
Set the run number.
void setTimestamp(const TTimeStamp &timestamp)
Set the timestamp.
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:67

References framework::performance::Tracker::absoluteStart(), framework::performance::Tracker::absoluteStop(), framework::EventFile::addDrop(), allow_incomplete_input_files_, framework::NtupleManager::clear(), conditions_, config_, drop_keep_rules_, event_header_, event_limit_, framework::Event::getEventHeader(), framework::Event::getEventHeaderPtr(), framework::Event::getEventNumber(), framework::EventFile::getFileName(), framework::EventFile::getIncompleteRuns(), framework::NtupleManager::getInstance(), ldmx::EventHeader::getRun(), framework::EventFile::getRunHeaderPtr(), input_files_, framework::EventFile::isCorrupted(), framework::StorageControl::keepEvent(), max_tries_, min_events_, newRun(), framework::EventFile::nextEvent(), framework::Event::onEndOfFile(), onFileClose(), onFileOpen(), framework::Conditions::onProcessStart(), output_files_, pass_name_, performance_, process(), framework::NtupleManager::reset(), framework::StorageControl::resetEventState(), run(), run_for_generation_, run_header_, sequence_, framework::logging::Formatter::set(), ldmx::EventHeader::setEventNumber(), ldmx::EventHeader::setRun(), ldmx::EventHeader::setTimestamp(), framework::EventFile::setupEvent(), skip_corrupted_input_files_, framework::performance::Tracker::start(), framework::performance::Tracker::stop(), storage_controller_, total_events_, framework::EventFile::updateParent(), framework::EventFile::writeRunHeader(), and framework::EventFile::writeRunTree().

Referenced by Process(), and run().

◆ setEventHeader()

void framework::Process::setEventHeader ( ldmx::EventHeader * h)
inline

Set the pointer to the current event header, used only for tests.

Definition at line 115 of file Process.h.

115{ event_header_ = h; }

References event_header_.

Member Data Documentation

◆ allow_incomplete_input_files_

bool framework::Process::allow_incomplete_input_files_
private

allow the Process to read input files whose runs never finished

For experts. Such a file is missing events off the end of a run, so the sample it gives is biased.

Definition at line 189 of file Process.h.

Referenced by Process(), and run().

◆ compression_setting_

int framework::Process::compression_setting_
private

Compression setting to pass to output files.

Look at the documentation for the TFile constructor if you want to learn more details. Essentially, setting = 100*algo + level with algo = 0 being the global default.

Definition at line 214 of file Process.h.

Referenced by Process().

◆ conditions_

Conditions framework::Process::conditions_
private

Set of ConditionsProviders.

Definition at line 198 of file Process.h.

Referenced by getConditions(), newRun(), Process(), and run().

◆ config_

framework::config::Parameters framework::Process::config_
private

The parameters used to configure this class.

Definition at line 156 of file Process.h.

Referenced by Process(), and run().

◆ drop_keep_rules_

std::vector<std::string> framework::Process::drop_keep_rules_
private

Set of drop/keep rules.

Definition at line 217 of file Process.h.

Referenced by Process(), and run().

◆ event_header_

const ldmx::EventHeader* framework::Process::event_header_ {0}
private

Pointer to the current EventHeader, used for Conditions information.

Definition at line 226 of file Process.h.

226{0};

Referenced by getEventHeader(), getRunNumber(), Process(), run(), and setEventHeader().

◆ event_limit_

int framework::Process::event_limit_
private

Limit on events to process.

Definition at line 162 of file Process.h.

Referenced by Process(), requestFinish(), and run().

◆ histo_filename_

std::string framework::Process::histo_filename_
private

Filename for histograms and other user products.

Definition at line 223 of file Process.h.

Referenced by openHistoFile(), and Process().

◆ histo_t_file_

TFile* framework::Process::histo_t_file_ {0}
private

TFile for histograms and other user products.

Definition at line 232 of file Process.h.

232{0};

Referenced by openHistoFile(), and ~Process().

◆ input_files_

std::vector<std::string> framework::Process::input_files_
private

List of input files to process.

May be empty if this Process will generate new events.

Definition at line 202 of file Process.h.

Referenced by Process(), and run().

◆ log_frequency_

int framework::Process::log_frequency_
private

The frequency with which event info is printed.

Definition at line 173 of file Process.h.

Referenced by getLogFrequency(), Process(), and process().

◆ max_tries_

int framework::Process::max_tries_
private

Maximum number of attempts to make before giving up on an event.

Definition at line 176 of file Process.h.

Referenced by Process(), and run().

◆ min_events_

int framework::Process::min_events_
private

When reading a file in, what's the first event to read.

Definition at line 165 of file Process.h.

Referenced by Process(), and run().

◆ output_files_

std::vector<std::string> framework::Process::output_files_
private

List of output file names.

If empty, no output file will be created.

Definition at line 205 of file Process.h.

Referenced by Process(), and run().

◆ pass_name_

std::string framework::Process::pass_name_
private

Processing pass name.

Definition at line 159 of file Process.h.

Referenced by getPassName(), newRun(), Process(), and run().

◆ performance_

performance::Tracker* framework::Process::performance_ {0}
private

class with calls backs to track performance measurements of software

Definition at line 235 of file Process.h.

235{0};

Referenced by newRun(), onFileClose(), onFileOpen(), Process(), process(), run(), and ~Process().

◆ run_for_generation_

int framework::Process::run_for_generation_ {1}
private

Run number to use if generating events.

Definition at line 220 of file Process.h.

220{1};

Referenced by getRunNumber(), Process(), and run().

◆ run_header_

ldmx::RunHeader* framework::Process::run_header_ {0}
private

Pointer to the current RunHeader, used for Conditions information.

Definition at line 229 of file Process.h.

229{0};

Referenced by getRunHeader(), and run().

◆ sequence_

std::vector<EventProcessor*> framework::Process::sequence_
private

Ordered list of EventProcessors to execute.

Definition at line 195 of file Process.h.

Referenced by newRun(), onFileClose(), onFileOpen(), Process(), process(), run(), and ~Process().

◆ skip_corrupted_input_files_

bool framework::Process::skip_corrupted_input_files_
private

allow the Process to skip input files that are corrupted

Definition at line 181 of file Process.h.

Referenced by Process(), and run().

◆ storage_controller_

StorageControl framework::Process::storage_controller_
private

Storage controller.

Definition at line 192 of file Process.h.

Referenced by getStorageController(), Process(), and run().

◆ total_events_

int framework::Process::total_events_
private

Number of events we'd like to produce independetly of the number of tries it would take.

Be warned about infinite loops!

Definition at line 170 of file Process.h.

Referenced by Process(), and run().


The documentation for this class was generated from the following files: