All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
core::PostProcessorBase Class Referenceabstract

A generic tree-reading event-by-event processor. More...

#include <PostProcessorBase.hh>

Inheritance diagram for core::PostProcessorBase:
ana::SBNOsc::Chi2Sensitivity ana::SBNOsc::Covariance ana::SBNOsc::Flatten ana::SBNOsc::Selection

Public Member Functions

 PostProcessorBase ()
 
virtual ~PostProcessorBase ()
 
void Run (std::vector< std::string > filelist)
 
void Initialize (char *config=NULL, const std::string &output_fname="", unsigned n_workers=1)
 

Protected Member Functions

virtual void ProcessEvent (const event::Event *event)=0
 
virtual void ProcessSubRun (const SubRun *subrun)
 
virtual void ProcessFileMeta (const FileMeta *filemeta)
 
virtual void FileSetup (TFile *f, TTree *eventTree)
 
virtual void FileCleanup (TTree *eventTree)
 
virtual void Initialize (fhicl::ParameterSet *config=NULL)=0
 
virtual void InitializeThread ()
 
virtual void Finalize ()
 
unsigned NWorkers ()
 
unsigned WorkerID ()
 

Protected Attributes

ProviderManagerfProviderManager
 Interface for provider access. More...
 
int fConfigExperimentID
 

Private Member Functions

void ProcessFile (const std::string &fname)
 

Private Attributes

unsigned fNWorkers
 
std::vector< std::thread::id > fThreadIDs
 

Detailed Description

A generic tree-reading event-by-event processor.

Definition at line 43 of file PostProcessorBase.hh.

Constructor & Destructor Documentation

core::PostProcessorBase::PostProcessorBase ( )

Constructor

Definition at line 23 of file PostProcessorBase.cxx.

ProviderManager * fProviderManager
Interface for provider access.
core::PostProcessorBase::~PostProcessorBase ( )
virtual

Destructor

Definition at line 26 of file PostProcessorBase.cxx.

26 {}

Member Function Documentation

virtual void core::PostProcessorBase::FileCleanup ( TTree *  eventTree)
inlineprotectedvirtual

Any cleanup needed per file

Parameters
eventTreethe TTree associated with the sbncode event.

Reimplemented in ana::SBNOsc::Selection, ana::SBNOsc::Covariance, and ana::SBNOsc::Chi2Sensitivity.

Definition at line 95 of file PostProcessorBase.hh.

95 {}
virtual void core::PostProcessorBase::FileSetup ( TFile *  f,
TTree *  eventTree 
)
inlineprotectedvirtual

Setup anything needed per file

Parameters
fthe TFile being opened
eventTreethe TTree associated with the sbncode event. Use this TTree to set branch addresses for everything other than the sbncode event.

Files are guaranteed to be processed in the order they are specified on the command line for sbn-postprocess

Reimplemented in ana::SBNOsc::Selection, and ana::SBNOsc::Flatten.

Definition at line 88 of file PostProcessorBase.hh.

88 {}
virtual void core::PostProcessorBase::Finalize ( )
inlineprotectedvirtual

Perform user-level finalization. Called after all events have been processed.

Reimplemented in ana::SBNOsc::Selection, ana::SBNOsc::Covariance, ana::SBNOsc::Flatten, and ana::SBNOsc::Chi2Sensitivity.

Definition at line 112 of file PostProcessorBase.hh.

112 {}
void core::PostProcessorBase::Initialize ( char *  config = NULL,
const std::string &  output_fname = "",
unsigned  n_workers = 1 
)

Perform user-level initialization.

Parameters
configA configuration, as a JSON filename.

Definition at line 29 of file PostProcessorBase.cxx.

29  {
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) {
36  fProviderManager = new ProviderManager((Experiment)fConfigExperimentID, "", false);
37  }
38 
39  fNWorkers = n_workers;
40 
41  Initialize(cfg);
42 }
Experiment
Definition: Experiment.hh:13
void Initialize(char *config=NULL, const std::string &output_fname="", unsigned n_workers=1)
fhicl::ParameterSet * LoadConfig(char *configfile)
Definition: Loader.cxx:80
ProviderManager * fProviderManager
Interface for provider access.
virtual void core::PostProcessorBase::Initialize ( fhicl::ParameterSet *  config = NULL)
protectedpure virtual

Perform user-level initialization.

Parameters
configA configuration, as a JSON object.

Implemented in ana::SBNOsc::Selection, ana::SBNOsc::Covariance, ana::SBNOsc::Flatten, and ana::SBNOsc::Chi2Sensitivity.

virtual void core::PostProcessorBase::InitializeThread ( )
inlineprotectedvirtual

Perform user-level initialization per-thread. Only called when multiple workers are specified on the command line.

Reimplemented in ana::SBNOsc::Flatten.

Definition at line 109 of file PostProcessorBase.hh.

109 {}
unsigned core::PostProcessorBase::NWorkers ( )
inlineprotected

Definition at line 117 of file PostProcessorBase.hh.

117 { return fNWorkers; }
virtual void core::PostProcessorBase::ProcessEvent ( const event::Event event)
protectedpure virtual

Process one event.

Parameters
eventThe sbncode event for the current event

Implemented in ana::SBNOsc::Selection, ana::SBNOsc::Covariance, ana::SBNOsc::Flatten, and ana::SBNOsc::Chi2Sensitivity.

void core::PostProcessorBase::ProcessFile ( const std::string &  fname)
private

Definition at line 44 of file PostProcessorBase.cxx.

44  {
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 }
string fname
Definition: demo.py:5
BEGIN_PROLOG could also be cerr
virtual void ProcessEvent(const event::Event *event)=0
virtual void ProcessSubRun(const SubRun *subrun)
virtual void FileSetup(TFile *f, TTree *eventTree)
virtual void FileCleanup(TTree *eventTree)
The standard subrun data definition.
Definition: SubRun.hh:23
The standard event data definition.
Definition: Event.hh:228
virtual void ProcessFileMeta(const FileMeta *filemeta)
Metadata for each input file.
Definition: FileMeta.hh:16
virtual void core::PostProcessorBase::ProcessFileMeta ( const FileMeta filemeta)
inlineprotectedvirtual

Reimplemented in ana::SBNOsc::Selection, and ana::SBNOsc::Flatten.

Definition at line 75 of file PostProcessorBase.hh.

75 {}
virtual void core::PostProcessorBase::ProcessSubRun ( const SubRun subrun)
inlineprotectedvirtual
void core::PostProcessorBase::Run ( std::vector< std::string >  filelist)

Run

Parameters
filelistThe list of files to be processed by this PostProcessor

Definition at line 104 of file PostProcessorBase.cxx.

104  {
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 }
string fname
Definition: demo.py:5
std::vector< std::thread::id > fThreadIDs
void ProcessFile(const std::string &fname)
ProviderManager * fProviderManager
Interface for provider access.
unsigned core::PostProcessorBase::WorkerID ( )
protected

Definition at line 96 of file PostProcessorBase.cxx.

96  {
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 }
std::vector< std::thread::id > fThreadIDs

Member Data Documentation

int core::PostProcessorBase::fConfigExperimentID
protected

Definition at line 115 of file PostProcessorBase.hh.

unsigned core::PostProcessorBase::fNWorkers
private

Definition at line 123 of file PostProcessorBase.hh.

ProviderManager* core::PostProcessorBase::fProviderManager
protected

Interface for provider access.

Definition at line 114 of file PostProcessorBase.hh.

std::vector<std::thread::id> core::PostProcessorBase::fThreadIDs
private

Definition at line 124 of file PostProcessorBase.hh.


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