CMFEventListToTextFile_module.cc
Go to the documentation of this file.
1 //
2 // Created by Brian Rebel on 2019-09-12.
3 //
4 // Module to take eventlisttree files and produce a text file for
5 // comparison with CAF events
6 //
7 
8 #include <memory>
9 #include <cmath>
10 #include <string>
11 #include <vector>
12 #include <set>
13 
14 // Framework includes
18 #include "fhiclcpp/ParameterSet.h"
19 #include "cetlib_except/exception.h"
20 
21 // NOvA includes
24 
25 #include "TTree.h"
26 #include "TFile.h"
27 #include "TH1.h"
28 
29 namespace cmf {
30 
31  struct SelectOutput {
32 
34  : selName("noname")
35  , energy (std::numeric_limits<double>::min())
36  , cvn (std::numeric_limits<double>::min())
37  , wgt (1.)
38  , run (std::numeric_limits<int>::min())
39  , subrun (std::numeric_limits<int>::min())
40  , event (std::numeric_limits<int>::min())
41  , slice (std::numeric_limits<int>::min())
42  , cycle (std::numeric_limits<int>::min())
43  {}
44 
45 
47  double e,
48  double id,
49  double w,
50  int r,
51  int sr,
52  int ev,
53  int sl,
54  int cy)
55  : selName(sel)
56  , energy (e)
57  , cvn (id)
58  , wgt (w)
59  , run (r)
60  , subrun (sr)
61  , event (ev)
62  , slice (sl)
63  , cycle (cy)
64  {}
65 
67  double energy;
68  double cvn;
69  double wgt;
70  int run;
71  int subrun;
72  int event;
73  int slice;
74  int cycle;
75 
76  friend std::ostream& operator << (std::ostream & o, cmf::SelectOutput const& so);
77  };
78 
79  //------------------------------------------------------------
80  std::ostream& operator<< (std::ostream& o, cmf::SelectOutput const& so)
81  {
82  o << so.selName
83  << " "
84  << so.run
85  << " "
86  << so.subrun
87  << " "
88  << so.event
89  << " "
90  << so.slice
91  << " "
92  << so.cycle
93  << " "
94  << so.energy
95  << " "
96  << so.cvn
97  << " "
98  << so.wgt;
99 
100  return o;
101  }
102 
103 
105  public:
106  explicit EventListToTextFile(fhicl::ParameterSet const& pset);
107  virtual ~EventListToTextFile();
108 
109  // Plugins should not be copied or assigned.
110  EventListToTextFile(EventListToTextFile const &) = delete;
112  EventListToTextFile & operator = (EventListToTextFile const &) = delete;
113  EventListToTextFile & operator = (EventListToTextFile &&) = delete;
114 
115  void analyze(art::Event const& e) override;
116  void reconfigure(fhicl::ParameterSet const& p) ;
117  void endJob() override;
118 
119  private:
120 
121  static void MakeMetaDataToUseList(std::set<cmf::MetaData> & mdList);
122  void TreeContentsToOutput(std::vector<SelectOutput> & outputVec,
123  TTree* eventListTree,
124  cmf::MetaData const& md);
125 
126  std::string fTreeFileName; ///< name of the event list tree file we are using
127  std::string fTreeDirName; ///< name of the directory containing the trees in the file
128  };
129 
130  //......................................................................
132  : EDAnalyzer(pset)
133  {
134  this->reconfigure(pset);
135  }
136 
137  //......................................................................
139  = default;
140 
141  //......................................................................
143  {
144  fTreeFileName = p.get<std::string>("InputTreeFileName" );
145  fTreeDirName = p.get<std::string>("InputTreeDirectoryName");
146  }
147 
148  //......................................................................
149  // since we are using CAF files, which are not art files, we don"t need the
150  // analyze function to do anything
152  {
153  }
154 
155  //......................................................................
156  // Method to clear out the collections of data products after the
157  // writeResults method is called.
158  void EventListToTextFile::TreeContentsToOutput(std::vector<SelectOutput> & outputVec,
159  TTree* eventListTree,
160  cmf::MetaData const& md)
161 
162  {
163  // set the locations of the variables we want
164  cmf::EventId evId;
165  cmf::WeightVars wgtVars;
166  cmf::TruthVars trVars;
167  cmf::GENIEVars gVars;
168  cmf::DataVars dataVars;
169 
170  eventListTree->SetBranchAddress("eventId", &evId);
171  eventListTree->SetBranchAddress("dataVars", &dataVars);
172 
173  if(md.isMC){
174  eventListTree->SetBranchAddress("weightVars", &wgtVars);
175  eventListTree->SetBranchAddress("truthVars", &trVars);
176  eventListTree->SetBranchAddress("genieVars", &gVars);
177  }
178 
179  double energy;
180 
181  for(int i = 0; i < eventListTree->GetEntriesFast(); ++i){
182  eventListTree->GetEntry(i);
183 
184  cmf::DataVarVals dvv(dataVars);
185  cmf::MCVarVals mcvv(trVars, wgtVars, gVars);
186  energy = dvv.val_at(cmf::kNu_RecoE, md);
187 
188  outputVec.emplace_back(cmf::cSelectionType_Strings[md.selectionType],
189  energy,
190  dvv.val_at(cmf::kNuE_CVN, md),
192  evId.run,
193  evId.subrun,
194  evId.event,
195  evId.slice,
196  evId.cycle);
197  } // end loop over the tree contents
198  }
199 
200  //......................................................................
201  void EventListToTextFile::MakeMetaDataToUseList(std::set<cmf::MetaData> & mdList)
202  {
203  std::set<novadaq::cnv::DetId> dets({novadaq::cnv::kNEARDET, novadaq::cnv::kFARDET});
204  std::set<cmf::SelectionType_t> sels({cmf::kNuESelectionLowPID,
212  std::set<cmf::FileType_t> fts({cmf::kDataFile,
213  cmf::kBeam,
214  cmf::kSwap,
219  // for the epochs just use one in the FHC and one in the RHC
220  std::set<std::string> eps({"1",
221  "6"});
222 
223  bool isMC;
225  for(auto const& ft : fts){
226  isMC = !(ft == cmf::kDataFile);
227 
228  for(auto const& det : dets){
229 
230  // the near detector only had data and beam files
231  if(det == novadaq::cnv::kNEARDET &&
232  ft != cmf::kDataFile &&
233  ft != cmf::kBeam ) continue;
234 
235  for(auto const& ep : eps){
236  for(auto const& sel : sels){
237 
238  md = cmf::MetaData(isMC,
239  det,
240  ft,
241  sel,
243  ep);
244 
245  // only nue selections have the rock *swap files and
246  // the nc selection does not have cosmic background files
247  if((!md.IsNuESelected() &&
248  (ft == cmf::kRockNonSwap ||
249  ft == cmf::kRockFluxSwap)) ||
250  (md.IsNCSelected() &&
251  ft == cmf::kCosmicBackgroundFile)) continue;
252 
253  mdList.insert(md);
254 
255  } // end loop over epochs
256  } // end loop over detectors
257  } // end loop over selections
258  } // end loop over file types
259  }
260 
261  //......................................................................
263  {
264 
265  // first get a set of MetaData together that have the right
266  // combinations of detector/general selection type/file type/epoch
267  // so that we can loop over them to pull trees out of the ROOT file
268  std::set<cmf::MetaData> mdCombos;
269  this->MakeMetaDataToUseList(mdCombos);
270 
271  // open the eventlisttree file and loop over the meta data tree to find
272  // the event list trees.
273  auto *treeFile = new TFile(fTreeFileName.c_str(), "READ");
274 
275  // loop over the metadata tree entries in the file
276  TTree *metadataTree = dynamic_cast<TTree *>(treeFile->Get((fTreeDirName + "/metadata").c_str()));
277 
278  if(!metadataTree){
279  throw cet::exception("EventListManipulator")
280  << "cannot find metadata tree: "
281  << fTreeDirName + "/metadata";
282  }
283 
284  // loop over the entries in the metadataTree
285  cmf::MetaData *md = nullptr;
286  cmf::SpillSummary *ss = nullptr;
287  metadataTree->SetBranchAddress("metadata", &md);
288  metadataTree->SetBranchAddress("spillsummary", &ss);
289 
290  // We have potentially hadd'ed several files together to get the
291  // eventlisttree, so make a set of all metadata that match the
292  // categories of those in mdCombos
293  std::map<cmf::MetaData, std::set<cmf::MetaData> > mdToUseFromFile;
294  cmf::MetaData tmpMD;
295  std::string epoch;
296  for(int ctr = 0; ctr < metadataTree->GetEntries(); ++ctr){
297  metadataTree->GetEntry(ctr);
298 
299  epoch = (md->BeamType() == cmf::kFHC) ? "1" : "6";
300 
301  tmpMD = cmf::MetaData(md->isMC,
302  md->detector,
303  md->fileType,
304  md->selectionType,
306  epoch);
307 
308  if(mdCombos.find(tmpMD) != mdCombos.end()) mdToUseFromFile[tmpMD].insert(*md);
309  }
310 
311  // now loop over the metadata entries in mdToUseFromFile to extract the trees we want
312  // write out a single file for the metadata from a
313  std::vector<cmf::SelectOutput> output;
314  TTree* eventListTree;
315  for(auto const& mdItr : mdToUseFromFile){
316  output.clear();
317  for(auto const& itr : mdItr.second){
318 
319  eventListTree = dynamic_cast<TTree *>(treeFile->Get((fTreeDirName + itr.ToString()).c_str()));
320 
321  this->TreeContentsToOutput(output, eventListTree, itr);
322  }
323 
324  std::ofstream outFile("selectedEvents_" + mdItr.first.ToString() + "_2018.txt");
325 
326  for(auto const& itr : output)
327  outFile << itr;
328 
329  } // end loop over map of metadata to use
330 
331  } // end endJob
332 
334 
335 } // end cmf namespace
336 
ofstream output
std::string fTreeDirName
name of the directory containing the trees in the file
EventListToTextFile(fhicl::ParameterSet const &pset)
const std::string cSelectionType_Strings[12]
Definition: Constants.h:115
static const unsigned char kNu_RecoE
Definition: VarVals.h:35
static void MakeMetaDataToUseList(std::set< cmf::MetaData > &mdList)
const char * p
Definition: xmltok.h:285
Float_t ss
Definition: plot.C:24
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
bool isMC
Definition: Structs.h:64
static const unsigned char kNuE_CVN
Definition: VarVals.h:31
DEFINE_ART_MODULE(TestTMapFile)
Defines an enumeration for prong classification.
static const unsigned char kXSecCVPPFX_Weight
Definition: VarVals.h:55
float val_at(unsigned char const &varkey, cmf::MetaData const &md) const
Definition: VarVals.cxx:315
friend std::ostream & operator<<(std::ostream &o, cmf::SelectOutput const &so)
Base container for the MC related Vars that constitute an event.
Definition: VarVals.h:778
int subrun
Definition: VarVals.h:593
Far Detector at Ash River, MN.
TFile * treeFile
cmf::SelectionType_t selectionType
Definition: Structs.h:67
TFile * outFile
Definition: PlotXSec.C:135
const Cut sels[kNumSels]
Definition: vars.h:44
T get(std::string const &key) const
Definition: ParameterSet.h:231
double energy
Definition: plottest35.C:25
Near Detector in the NuMI cavern.
void analyze(art::Event const &e) override
Base container for the MC related Vars that constitute an event.
Definition: VarVals.h:835
Definition: run.py:1
SelectOutput(std::string const &sel, double e, double id, double w, int r, int sr, int ev, int sl, int cy)
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
Module to combine a set of results into a single file.
Definition: Event.cxx:24
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
cmf::FileType_t fileType
Definition: Structs.h:66
TRandom3 r(0)
std::string fTreeFileName
name of the event list tree file we are using
cmf::BeamType_t BeamType() const
Definition: Structs.cxx:183
novadaq::cnv::DetId detector
Definition: Structs.h:65
Float_t e
Definition: plot.C:35
Float_t w
Definition: plot.C:20
float val_at(unsigned char const &varkey) const
Access a Var by name.
Definition: VarVals.cxx:75
void TreeContentsToOutput(std::vector< SelectOutput > &outputVec, TTree *eventListTree, cmf::MetaData const &md)
void reconfigure(fhicl::ParameterSet const &p)
static constexpr Double_t sr
Definition: Munits.h:164
bool IsNuESelected() const
Definition: Structs.cxx:284
bool IsNCSelected() const
Definition: Structs.cxx:291