All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PostProcessorBase.cxx
Go to the documentation of this file.
1 #include <algorithm>
2 #include <TBranch.h>
3 #include <TFile.h>
4 #include <TTree.h>
5 #include <TParameter.h>
6 #include "fhiclcpp/ParameterSet.h"
7 #include "Event.hh"
8 #include "Loader.hh"
9 #include "PostProcessorBase.hh"
10 #include "Experiment.hh"
11 
20 
21 namespace core {
22 
23 PostProcessorBase::PostProcessorBase(): fProviderManager(NULL), fConfigExperimentID(-1), fNWorkers(1) {}
24 
25 
27 
28 
29 void PostProcessorBase::Initialize(char* config, const std::string &output_fname, unsigned n_workers) {
30  fhicl::ParameterSet* cfg = LoadConfig(config);
31  if (cfg == NULL) cfg = new fhicl::ParameterSet;
32  if (output_fname.size() != 0) cfg->put("OutputFile", output_fname);
33  fConfigExperimentID = cfg->get("ExperimentID", -1);
34 
35  if (fConfigExperimentID >= 0) {
37  }
38 
39  fNWorkers = n_workers;
40 
41  Initialize(cfg);
42 }
43 
44 void PostProcessorBase::ProcessFile(const std::string &fname) {
45  // get ROOT file
46  TFile f(fname.c_str());
47  if (f.IsZombie()) {
48  std::cerr << "Failed openning file: " << fname << ". "
49  << "Cleaning up and exiting." << std::endl;
50  return;
51  }
52 
53  TTree *event_tree = 0;
54  event::Event *event = 0;
55 
56  TTree *subrun_tree = 0;
57  SubRun *subrun = 0;
58 
59  TTree *filemeta_tree = 0;
60  FileMeta *filemeta = 0;
61 
62  f.GetObject("sbnana", event_tree);
63  event_tree->SetBranchAddress("events", &event);
64  FileSetup(&f, event_tree);
65  // process all events
66  for (int event_ind = 0; event_ind < event_tree->GetEntries(); event_ind++) {
67  event_tree->GetEntry(event_ind);
68  ProcessEvent(event);
69  }
70  // process all subruns
71  f.GetObject("sbnsubrun", subrun_tree);
72  if (subrun_tree == NULL) {
73  std::cerr << "Error: NULL subrun tree" << std::endl;
74  }
75  subrun_tree->SetBranchAddress("subruns", &subrun);
76 
77  for (int subrun_ind = 0; subrun_ind < subrun_tree->GetEntries(); subrun_ind++) {
78  subrun_tree->GetEntry(subrun_ind);
79  ProcessSubRun(subrun);
80  }
81 
82  // process all the file meta-data
83  f.GetObject("sbnfilemeta", filemeta_tree);
84  if (filemeta_tree == NULL) {
85  std::cerr << "Error: NULL filemeta tree" << std::endl;
86  }
87  filemeta_tree->SetBranchAddress("filemeta", &filemeta);
88  for (int filemeta_ind = 0; filemeta_ind < filemeta_tree->GetEntries(); filemeta_ind++) {
89  filemeta_tree->GetEntry(filemeta_ind);
90  ProcessFileMeta(filemeta);
91  }
92 
93  FileCleanup(event_tree);
94 }
95 
97  std::thread::id this_id = std::this_thread::get_id();
98  for (unsigned index = 0; index < fThreadIDs.size(); index++) {
99  if (this_id == fThreadIDs[index]) return index;
100  }
101  assert(false);
102 }
103 
104 void PostProcessorBase::Run(std::vector<std::string> inputFiles) {
105  // single threaded
106  if (fNWorkers == 1) {
107  fThreadIDs.push_back(std::this_thread::get_id());
108  for (auto const& fname: inputFiles) {
110  }
111  }
112  // multi-threaded
113  else {
114  fThreadIDs = std::vector<std::thread::id>(fNWorkers);
115  std::atomic<unsigned> f_index = 0;
116  std::atomic<unsigned> w_index = 0;
117  std::vector<std::thread> workers;
118  for (unsigned i = 0; i < fNWorkers; i++) {
119  workers.emplace_back([&]() {
120  // setup the mapping to thread indices
121  unsigned this_index = w_index.fetch_add(1);
122  fThreadIDs[this_index] = std::this_thread::get_id();
124  // start fetching files
125  unsigned file_index = f_index.fetch_add(1);
126  while (file_index < inputFiles.size()) {
127  ProcessFile(inputFiles[file_index]);
128  file_index = f_index.fetch_add(1);
129  }
130  });
131  }
132  for (std::thread &w: workers) w.join();
133  }
134 
135  if (fProviderManager != NULL) {
136  delete fProviderManager;
137  fProviderManager = NULL;
138  }
139 
140  // teardown
141  Finalize();
142 }
143 
144 } // namespace core
145 
Experiment
Definition: Experiment.hh:13
string fname
Definition: demo.py:5
Service provider with utility LAr functions.
BEGIN_PROLOG could also be cerr
virtual void ProcessEvent(const event::Event *event)=0
Contains data associated to particles from detector simulation.
void Initialize(char *config=NULL, const std::string &output_fname="", unsigned n_workers=1)
Access the description of auxiliary detector geometry.
Access the description of detector geometry.
Interface to LArSoft services.
virtual void ProcessSubRun(const SubRun *subrun)
std::vector< std::thread::id > fThreadIDs
virtual void FileSetup(TFile *f, TTree *eventTree)
virtual void FileCleanup(TTree *eventTree)
back track the reconstruction to the simulation
The standard subrun data definition.
Definition: SubRun.hh:23
void Run(std::vector< std::string > filelist)
The standard event data definition.
Definition: Event.hh:228
fhicl::ParameterSet * LoadConfig(char *configfile)
Definition: Loader.cxx:80
virtual void ProcessFileMeta(const FileMeta *filemeta)
void ProcessFile(const std::string &fname)
ProviderManager * fProviderManager
Interface for provider access.
Metadata for each input file.
Definition: FileMeta.hh:16