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(pset),
68  fNuTree(0), fNuRec(0),
70  fStandardRecordLabel(pset.get<std::string>("StandardRecordLabel")),
71  fOutputName(pset.get<std::string>("OutputName")),
72  fFillNuTree(pset.get<bool>("FillNuTree")),
73  fDataTier(pset.get<std::string>("DataTier")),
74  fFileExtension(pset.get<std::string>("FileExtension"))
75  {
76  }
77 
78  //......................................................................
80  {
81  }
82 
83  //......................................................................
85  {
86  if(!fFile) {
87  if(fOutputName == "") {
88 
89  // Filename wasn't set in the FCL. Construct it based on the input
90  // filename.
91  char temp[fb.fileName().size() + 1];
92  std::strcpy(temp, fb.fileName().c_str());
93  fOutputName = basename(temp);
94 
95  // if input is PID file (most likely should be), be a little careful
96  // Allows for dots in the file name than just the last bit (eg. prod5.1)
97  if(fOutputName.find(".pid.root") != std::string::npos) {
98  const size_t pidpos = fOutputName.find(".pid.root");
99  fOutputName.resize(pidpos);
100  }
101  else{
102  const size_t dotpos = fOutputName.find('.');
103  assert(dotpos != std::string::npos); // Must have a dot, surely?
104  fOutputName.resize(dotpos);
105  }
106  }
107 
109 
111 
112  }
113  }
114 
115 
116  //......................................................................
118  {
119  assert(!fFile);
120  assert(!fOutputName.empty());
121 
122  mf::LogInfo("FlatMaker") << "Output filename is " << fOutputName;
123 
124  consumes<std::vector<caf::StandardRecord>>(fStandardRecordLabel);
125  consumes<caf::SRSpill>(fStandardRecordLabel);
126  if(fFillNuTree) consumes<caf::SRSpillTruthBranch>(fStandardRecordLabel);
127 
128  fFile = new TFile(fOutputName.c_str(), "RECREATE");
129 
130  fTree = new TTree("recTree", "recTree");
131  // Have trouble with memory usage (because several trees are open at
132  // once?). Set the maximum buffer usage (per tree) to 3MB (10x less than
133  // default)
134  fTree->SetAutoFlush(-3*1000*1000);
135  fRec = new flat::Flat<caf::StandardRecord>(fTree, "rec", "", 0);
136 
137  fSpillTree = new TTree("spillTree", "spillTree");
138  fSpillTree->SetAutoFlush(-3*1000*1000);
139  fSpillRec = new flat::Flat<caf::SRSpill>(fSpillTree, "spill", "", 0);
140 
141  if(fFillNuTree){
142  fNuTree = new TTree("nuTree", "nuTree");
143  fNuTree->SetAutoFlush(-3*1000*1000);
144  fNuRec = new flat::Flat<caf::SRNeutrino>(fNuTree, "nu", "", 0);
145  }
146  }
147 
148  //......................................................................
150  {
151  }
152 
153  //......................................................................
155  {
156  ++fTotalEvents;
157 
159  evt.getByLabel(fStandardRecordLabel, srcol);
160 
161  for(const caf::StandardRecord& sr: *srcol){
162  fRec->Clear();
163  fRec->Fill(sr);
164  fTree->Fill();
165  }
166 
167 
169  evt.getByLabel(fStandardRecordLabel, spill);
170 
171  fSpillRec->Clear();
172  fSpillRec->Fill(*spill);
173  fSpillTree->Fill();
174 
175  // Should we have checked isgoodspill?
176  fTotalPOT += spill->spillpot;
177  fTotalLivetime += spill->livetime;
178 
179 
180  if(fFillNuTree){
182  evt.getByLabel(fStandardRecordLabel, srtruthcol);
183 
184  for(const caf::SRSpillTruthBranch& sr: *srtruthcol){
185  fNuRec->Clear();
186  fNuRec->Fill(sr.nu);
187  fNuTree->Fill();
188  }
189  }
190  }
191 
192  //......................................................................
194  {
195  fFile->cd();
196 
197  fTree->Write();
198  delete fRec;
199 
200  fSpillTree->Write();
201  delete fSpillRec;
202 
203  if(fFillNuTree){
204  fNuTree->Write();
205  delete fNuRec;
206  }
207 
208  fFile->cd();
209 
210  std::map<std::string, std::string> metadata = meta::MetadataManager::getInstance().GetMetadata();
211  metadata["data_tier"] = fDataTier;
212  fFile->mkdir("metadata")->cd();
213 
214  for(const auto& m: metadata){
215  TObjString(m.second.c_str()).Write(m.first.c_str());
216  }
217 
218  fFile->cd();
219 
220  TH1* hPOT = new TH1D("TotalPOT", "TotalPOT;; POT", 1, 0, 1);
221  hPOT->Fill(.5, fTotalPOT);
222  hPOT->Write();
223 
224  TH1* hEvents = new TH1D("TotalEvents", "TotalEvents;; Events", 1, 0, 1);
225  hEvents->Fill(.5, fTotalEvents);
226  hEvents->Write();
227 
228  TH1* hLivetime = new TH1D("TotalLivetime", "TotalLiveTime;;Seconds", 1, 0, 1);
229  hLivetime->Fill(.5, fTotalLivetime);
230  hLivetime->Write();
231 
232  fFile->Close();
233  }
234 
236 
237 } // end namespace flat
238 ////////////////////////////////////////////////////////////////////////
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
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
std::string const & fileName() const
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