All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PostProcessorBase.hh
Go to the documentation of this file.
1 #ifndef __sbnanalysis_core_PostProcessorBase__
2 #define __sbnanalysis_core_PostProcessorBase__
3 
4 /**
5  * \file PostProcessorBase.hh
6  *
7  * A generic post-processor that reads an sbncode TTree
8  *
9  * Author: G. Putnam <gputnam@uchicago.edu>, 2018/10/08
10  */
11 
12 #include <thread>
13 #include <string>
14 #include <vector>
15 #include "gallery/Event.h"
16 #include "Loader.hh"
17 #include "Event.hh"
18 #include "SubRun.hh"
19 #include "ProviderManager.hh"
20 #include "FileMeta.hh"
21 
22 class TBranch;
23 class TFile;
24 class TTree;
25 template<typename Param>
26 class TParameter;
27 
28 namespace event {
29  class Event;
30 }
31 
32 namespace fhicl {
33  class ParameterSet;
34 }
35 
36 /** Core framework functionality. */
37 namespace core {
38 
39 /**
40  * \class core::PostProcessorBase
41  * \brief A generic tree-reading event-by-event processor.
42  */
44 public:
45  /** Constructor */
47 
48  /** Destructor */
49  virtual ~PostProcessorBase();
50 
51  /**
52  * Run
53  *
54  * \param filelist The list of files to be processed by this PostProcessor
55  */
56  void Run(std::vector<std::string> filelist);
57 
58  /**
59  * Perform user-level initialization.
60  *
61  * \param config A configuration, as a JSON filename.
62  */
63  void Initialize(char* config=NULL, const std::string &output_fname="", unsigned n_workers=1);
64 
65 protected:
66  /**
67  * Process one event.
68  *
69  * \param event The sbncode event for the current event
70  */
71  virtual void ProcessEvent(const event::Event *event) = 0;
72 
73  virtual void ProcessSubRun(const SubRun *subrun) {}
74 
75  virtual void ProcessFileMeta(const FileMeta *filemeta) {}
76 
77  /**
78  * Setup anything needed per file
79  *
80  * \param f the TFile being opened
81  * \param eventTree the TTree associated with the sbncode event.
82  * Use this TTree to set branch addresses for everything other than
83  * the sbncode event.
84  *
85  * Files are guaranteed to be processed in the order they are specified on
86  * the command line for sbn-postprocess
87  */
88  virtual void FileSetup(TFile *f, TTree *eventTree) {}
89 
90  /**
91  * Any cleanup needed per file
92  *
93  * \param eventTree the TTree associated with the sbncode event.
94  */
95  virtual void FileCleanup(TTree *eventTree) {}
96 
97  /**
98  * Perform user-level initialization.
99  *
100  * \param config A configuration, as a JSON object.
101  */
102  virtual void Initialize(fhicl::ParameterSet* config=NULL) = 0;
103 
104  /**
105  * Perform user-level initialization per-thread. Only
106  * called when multiple workers are specified on the
107  * command line.
108  */
109  virtual void InitializeThread() {}
110 
111  /** Perform user-level finalization. Called after all events have been processed. */
112  virtual void Finalize() {}
113 
114  ProviderManager* fProviderManager; //!< Interface for provider access
116 
117  unsigned NWorkers() { return fNWorkers; }
118  unsigned WorkerID();
119 
120 private:
121  void ProcessFile(const std::string &fname);
122 
123  unsigned fNWorkers;
124  std::vector<std::thread::id> fThreadIDs;
125 };
126 
127 } // namespace core
128 
129 
130 /** Macro to create plugin library for user-defined PostProcessors. */
131 #define DECLARE_SBN_POSTPROCESSOR(classname) extern "C" { \
132 core::PostProcessorBase* CreatePostProcessorObject() { return new classname; } \
133 void DestroyPostProcessorObject(core::PostProcessorBase* o) { delete o; } \
134 struct core::export_table_postprocess exports = { CreatePostProcessorObject, DestroyPostProcessorObject };}
135 
136 #endif // __sbnanalysis_core_PostProcessorBase__
137 
string fname
Definition: demo.py:5
virtual void ProcessEvent(const event::Event *event)=0
void Initialize(char *config=NULL, const std::string &output_fname="", unsigned n_workers=1)
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)
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
virtual void ProcessFileMeta(const FileMeta *filemeta)
void ProcessFile(const std::string &fname)
ProviderManager * fProviderManager
Interface for provider access.
A generic tree-reading event-by-event processor.
Metadata for each input file.
Definition: FileMeta.hh:16