hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Process.cxx
Go to the documentation of this file.
1
9#include "Process.h"
10#include "EventFile.h"
11#include "HpsEventFile.h"
12#include "TH1.h"
13
15
16//TODO Fix this better
17
19 try {
20 int cfile = 0;
21 for (auto ifile : input_files_) {
22 std::cout << "Processing file " << ifile << std::endl;
23
24 for (auto module : sequence_) {
25 module->initialize(ifile, output_files_[cfile]);
26 module->process();
27 module->finalize();
28 }
29 //Pass to next file
30 ++cfile;
31
32 } //ifile
33 } catch (std::exception& e) {
34 std::cerr<<"Error:"<<e.what()<<std::endl;
35 }
36} //Process::runOnHisto
37
39 try {
40 int n_events_processed = 0;
41 HpsEvent event;
42 TH1D * event_h = new TH1D("event_h","Number of Events Processed;;Events", 21, -10.5, 10.5);
43 int cfile =0 ;
44 for (auto ifile : input_files_) {
45 std::cout<<"Processing file "<<ifile<<std::endl;
46 HpsEventFile* file(nullptr);
47 if (!output_files_.empty()) {
48 file = new HpsEventFile(ifile, output_files_[cfile]);
49 file->setupEvent(&event);
50 }
51 for (auto module : sequence_) {
52 module->initialize(event.getTree());
53 module->setFile(file->getOutputFile());
54 }
55 while (file->nextEvent() && (event_limit_ < 0 || (n_events_processed < event_limit_))) {
56 if (n_events_processed%1000 == 0)
57 std::cout<<"Event:"<<n_events_processed<<std::endl;
58
59 //In this way if the processing fails (like an event doesn't pass the selection, the other modules aren't run on that event)
60 for (auto module : sequence_) {
61 module->process(&event);
62 }
63 //event.Clear();
64 event_h->Fill(0.0);
65 ++n_events_processed;
66 }
67 //Pass to next file
68 ++cfile;
69 // Finalize all modules
70
71 //Select the output file for storing the results of the processors.
72 file->resetOutputFileDir();
73 event_h->Write();
74 for (auto module : sequence_) {
75 //TODO:Change the finalize method
76 module->finalize();
77 }
78 // TODO Check all these destructors
79 if (file) {
80 file->close();
81 delete file;
82 file = nullptr;
83 delete event_h;
84 event_h = nullptr;
85 }
86 }
87 } catch (std::exception& e) {
88 std::cerr<<"Error:"<<e.what()<<std::endl;
89 }
90}
91
93
94 try {
95
96 int n_events_processed = 0;
97 TH1D * event_h = new TH1D("event_h","Number of Events Processed;;Events", 21, -10.5, 10.5);
98
99 if (input_files_.empty())
100 throw std::runtime_error("Please specify files to process.");
101
102 // Create an object used to manage the input and output files.
103 Event event;
104
105 int cfile = 0;
106 for (auto ifile : input_files_) {
107
108 std::cout << "---- [ hpstr ][ Process ]: Processing file "
109 << ifile << std::endl;
110
111
112 //TODO:: Change the order here.
113
114 // Open the output file if an output file path has been specified.
115 EventFile* file{nullptr};
116 if (!output_files_.empty()) {
117 file = new EventFile(ifile, output_files_[cfile]);
118 file->setupEvent(&event);
119 }
120
121 TTree* tree = new TTree("HPS_Event","HPS event tree");
122 event.setTree(tree);
123 // first, notify everyone that we are starting
124 for (auto module : sequence_) {
125 module->initialize(tree);
126 }
127
128 //In the case of additional output files from the processors this restores the correct ProcessID storage
129 file->resetOutputFileDir();
130
131 // Process all events.
132 while (file->nextEvent() && (event_limit_ < 0 || (n_events_processed < event_limit_))) {
133 if (n_events_processed%1000 == 0)
134 std::cout << "---- [ hpstr ][ Process ]: Event: " << n_events_processed << std::endl;
135 event.Clear();
136 bool passEvent = true;
137
138 for (auto module : sequence_) {
139 passEvent = passEvent && module->process(&event);
140 //if (!module->process(&event))
141 if (!passEvent)
142 break;
143 }
144 ++n_events_processed;
145 event_h->Fill(0.0);
146 if (passEvent) {
147 file->FillEvent();
148 }
149 }
150 ++cfile;
151
152 //Prepare to write to file
153 file->resetOutputFileDir();
154 event_h->Write();
155 // Finalize all modules.
156 for (auto module : sequence_) {
157 module->finalize();
158 }
159
160 if (file) {
161 file->close();
162 delete file;
163 file = nullptr;
164 delete event_h;
165 event_h = nullptr;
166 }
167
168 }
169
170 } catch (std::exception& e) {
171 std::cerr << "---- [ hpstr ][ Process ]: Error! " << e.what() << std::endl;
172 }
173}
174
175void Process::addFileToProcess(const std::string& filename) {
176 input_files_.push_back(filename);
177}
178
179void Process::addOutputFileName(const std::string& output_filename) {
180 output_files_.push_back(output_filename);
181}
182
184 sequence_.push_back(mod);
185}
186
Class for managing io files.
Class which represents the process under execution.
Definition Event.h:35
description
void close()
description
virtual bool nextEvent()
description
void resetOutputFileDir()
description
void setupEvent(IEvent *ievent)
description
void addFileToProcess(const std::string &filename)
Add an input file name to the list.
Definition Process.cxx:175
void addOutputFileName(const std::string &output_filename)
Add an output file name to the list.
Definition Process.cxx:179
void run()
Definition Process.cxx:92
std::vector< std::string > output_files_
Definition Process.h:147
int event_limit_
Definition Process.h:138
void runOnHisto()
Definition Process.cxx:18
void runOnRoot()
Definition Process.cxx:38
std::vector< Processor * > sequence_
Definition Process.h:141
void addToSequence(Processor *event_proc)
Add an event processor to the linear sequence of processors to run in this job.
Definition Process.cxx:183
Process()
Class constructor.
Definition Process.cxx:14
std::vector< std::string > input_files_
Definition Process.h:144
Base class for all event processing components.
Definition Processor.h:34