FlatMaker_module.cc
Go to the documentation of this file.
7 #include "cetlib_except/exception.h"
10 
11 #include "FlatMaker/FlatRecord.h"
12 
15 
17 
18 #include "TFile.h"
19 #include "TH1.h"
20 #include "TTree.h"
21 
22 namespace flat
23 {
24  //.......................................................................
25  class FlatMaker: public art::EDProducer
26  {
27  public:
28  explicit FlatMaker(const fhicl::ParameterSet& pset);
29  virtual ~FlatMaker() noexcept;
30 
32  void InitializeOutfile();
33 
34  void beginJob();
35  void produce(art::Event& evt) noexcept;
36  void endJob();
37 
38  protected:
39  TFile* fFile;
40 
41  TTree* fTree;
42  TTree* fSpillTree;
43  TTree* fNuTree;
44 
45  TDirectory* fRecDir;
46  TDirectory* fSpillDir;
47  TDirectory* fNuDir;
48 
52 
53  double fTotalPOT;
56 
60 
63  };
64 
65  //.......................................................................
67  : art::EDProducer(),
68  fFile(0),
69  fNuTree(0), fNuRec(0),
71  fStandardRecordLabel(pset.get<std::string>("StandardRecordLabel")),
72  fOutputName(pset.get<std::string>("OutputName")),
73  fFillNuTree(pset.get<bool>("FillNuTree")),
74  fDataTier(pset.get<std::string>("DataTier")),
75  fFileExtension(pset.get<std::string>("FileExtension"))
76  {
77  }
78 
79  //......................................................................
81  {
82  }
83 
84  //......................................................................
86  {
87  if(!fFile) {
88  if(fOutputName == "") {
89 
90  // Filename wasn't set in the FCL. Construct it based on the input
91  // filename.
92  char temp[fb.fileName().size() + 1];
93  std::strcpy(temp, fb.fileName().c_str());
94  fOutputName = basename(temp);
95 
96  // if input is PID file (most likely should be), be a little careful
97  // Allows for dots in the file name than just the last bit (eg. prod5.1)
98  if(fOutputName.find(".pid.root") != std::string::npos) {
99  const size_t pidpos = fOutputName.find(".pid.root");
100  fOutputName.resize(pidpos);
101  }
102  else{
103  const size_t dotpos = fOutputName.find('.');
104  assert(dotpos != std::string::npos); // Must have a dot, surely?
105  fOutputName.resize(dotpos);
106  }
107  }
108 
110 
112 
113  }
114  }
115 
116 
117  //......................................................................
119  {
120  assert(!fFile);
121  assert(!fOutputName.empty());
122 
123  mf::LogInfo("FlatMaker") << "Output filename is " << fOutputName;
124 
125  consumes<std::vector<caf::StandardRecord>>(fStandardRecordLabel);
126  consumes<caf::SRSpill>(fStandardRecordLabel);
127  if(fFillNuTree) consumes<caf::SRSpillTruthBranch>(fStandardRecordLabel);
128 
129  fFile = new TFile(fOutputName.c_str(), "RECREATE");
130 
131  fTree = new TTree("recTree", "recTree");
132  // Have trouble with memory usage (because several trees are open at
133  // once?). Set the maximum buffer usage (per tree) to 3MB (10x less than
134  // default)
135  fTree->SetAutoFlush(-3*1000*1000);
136  fRec = new flat::Flat<caf::StandardRecord>(fTree, "rec", "", 0);
137 
138  fSpillTree = new TTree("spillTree", "spillTree");
139  fSpillTree->SetAutoFlush(-3*1000*1000);
140  fSpillRec = new flat::Flat<caf::SRSpill>(fSpillTree, "spill", "", 0);
141 
142  if(fFillNuTree){
143  fNuTree = new TTree("nuTree", "nuTree");
144  fNuTree->SetAutoFlush(-3*1000*1000);
145  fNuRec = new flat::Flat<caf::SRNeutrino>(fNuTree, "nu", "", 0);
146  }
147  }
148 
149  //......................................................................
151  {
152  }
153 
154  //......................................................................
156  {
157  ++fTotalEvents;
158 
160  evt.getByLabel(fStandardRecordLabel, srcol);
161 
162  for(const caf::StandardRecord& sr: *srcol){
163  fRec->Clear();
164  fRec->Fill(sr);
165  fTree->Fill();
166  }
167 
168 
170  evt.getByLabel(fStandardRecordLabel, spill);
171 
172  fSpillRec->Clear();
173  fSpillRec->Fill(*spill);
174  fSpillTree->Fill();
175 
176  // Should we have checked isgoodspill?
177  fTotalPOT += spill->spillpot;
178  fTotalLivetime += spill->livetime;
179 
180 
181  if(fFillNuTree){
183  evt.getByLabel(fStandardRecordLabel, srtruthcol);
184 
185  for(const caf::SRSpillTruthBranch& sr: *srtruthcol){
186  fNuRec->Clear();
187  fNuRec->Fill(sr.nu);
188  fNuTree->Fill();
189  }
190  }
191  }
192 
193  //......................................................................
195  {
196  fFile->cd();
197 
198  fTree->Write();
199  delete fRec;
200 
201  fSpillTree->Write();
202  delete fSpillRec;
203 
204  if(fFillNuTree){
205  fNuTree->Write();
206  delete fNuRec;
207  }
208 
209  fFile->cd();
210 
211  std::map<std::string, std::string> metadata = meta::MetadataManager::getInstance().GetMetadata();
212  metadata["data_tier"] = fDataTier;
213  fFile->mkdir("metadata")->cd();
214 
215  for(const auto& m: metadata){
216  TObjString(m.second.c_str()).Write(m.first.c_str());
217  }
218 
219  fFile->cd();
220 
221  TH1* hPOT = new TH1D("TotalPOT", "TotalPOT;; POT", 1, 0, 1);
222  hPOT->Fill(.5, fTotalPOT);
223  hPOT->Write();
224 
225  TH1* hEvents = new TH1D("TotalEvents", "TotalEvents;; Events", 1, 0, 1);
226  hEvents->Fill(.5, fTotalEvents);
227  hEvents->Write();
228 
229  TH1* hLivetime = new TH1D("TotalLivetime", "TotalLiveTime;;Seconds", 1, 0, 1);
230  hLivetime->Fill(.5, fTotalLivetime);
231  hLivetime->Write();
232 
233  fFile->Close();
234  }
235 
237 
238 } // end namespace flat
239 ////////////////////////////////////////////////////////////////////////
static MetadataManager & getInstance()
flat::Flat< caf::SRNeutrino > * fNuRec
Flat encoding of caf::SRNeutrino.
Definition: FlatRecord.h:476
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Flat encoding of caf::StandardRecord.
Definition: FlatRecord.h:2092
TDirectory * fSpillDir
virtual ~FlatMaker() noexcept
std::string const & fileName() const
Definition: FileBlock.h:38
Truth info for all neutrinos in the spill.
std::map< std::string, std::string > & GetMetadata()
void Fill(const caf::SRSpill &sr)
DEFINE_ART_MODULE(TestTMapFile)
void Fill(const caf::SRNeutrino &sr)
Definition: FlatRecord.cxx:943
void respondToOpenInputFile(const art::FileBlock &fb)
TDirectory * fNuDir
std::string fDataTier
flat::Flat< caf::StandardRecord > * fRec
TDirectory * fRecDir
int evt
flat::Flat< caf::SRSpill > * fSpillRec
std::string fFileExtension
caf::StandardRecord * sr
std::string fOutputName
void Fill(const caf::StandardRecord &sr)
Flat encoding of caf::SRSpill.
Definition: FlatRecord.h:1312
float livetime
Length of readout [s].
Definition: SRSpill.h:40
void produce(art::Event &evt) noexcept
std::string fStandardRecordLabel
The StandardRecord is the primary top-level object in the Common Analysis File trees.
assert(nhit_max >=nhit_nbins)
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
float spillpot
Definition: SRSpill.h:38
FlatMaker(const fhicl::ParameterSet &pset)
gm Write()
enum BeamMode string