EventList.cxx
Go to the documentation of this file.
2 
3 #include "CAFAna/Core/Spectrum.h"
5 
7 
8 #include <iostream>
9 
10 #include "TFile.h"
11 #include "TTree.h"
12 
13 namespace ana
14 {
15  /// Helper class for \ref MakeTextListFile
16  class ASCIIMaker: public SpectrumLoader
17  {
18  public:
20  const std::vector<Cut>& cut,
21  const std::vector<FILE*>& f,
22  const std::vector<const Var*>& floatVars,
23  const std::vector<const Var*>& intVars)
24  : SpectrumLoader(wildcard),
25  fCut(cut),
26  fFile(f),
27  fFloatVars(floatVars),
28  fIntVars(intVars),
29  fNPassed(0)
30  {
31  }
32 
34  const std::vector<Cut>& cut,
35  const std::vector<FILE*>& f,
36  const std::vector<const MultiVar*>& multivars,
37  const std::vector<const MultiVar*>& /*multiIntVars*/)
38  : SpectrumLoader(wildcard),
39  fCut(cut),
40  fFile(f),
41  fMultiVars(multivars),
42  fNPassed(0)
43  {
44  }
45 
46  ASCIIMaker(const std::vector<std::string>& fnames,
47  const std::vector<Cut>& cut,
48  const std::vector<FILE*>& f,
49  const std::vector<const Var*>& floatVars,
50  const std::vector<const Var*>& intVars)
51  : SpectrumLoader(fnames),
52  fCut(cut),
53  fFile(f),
54  fFloatVars(floatVars),
55  fIntVars(intVars),
56  fNPassed(0)
57  {
58  }
59 
61  {
62  std::cout << "Selected " << fNPassed << " slices." << std::endl;
63  }
64 
65  void HandleRecord(caf::SRProxy* sr) override
66  {
67  if(fSpillCut && !(*fSpillCut)(&sr->spill)) return;
68 
69  for(unsigned int ic = 0; ic < fCut.size(); ic++){
70  if(!fCut[ic](sr))
71  continue;
72 
73  ++fNPassed;
74 
75  for(const Var* v: fIntVars){
76  fprintf(fFile[ic], "%d ", int(std::round((*v)(sr))));
77  }
78 
79  for(const Var* v: fFloatVars){
80  fprintf(fFile[ic], "%g ", (*v)(sr));
81  }
82 
83  for(const MultiVar* mv: fMultiVars){
84  for(const double md: (*mv)(sr)){
85  fprintf(fFile[ic], "%g ", md);
86  }
87  }
88 
89  fprintf(fFile[ic], "\n");
90  }// end loop over cuts
91  }
92  protected:
93  std::vector<Cut> fCut;
94  std::vector<FILE*> fFile;
95  std::vector<const Var*> fFloatVars;
96  std::vector<const Var*> fIntVars;
97  std::vector<const MultiVar*> fMultiVars;
98  int fNPassed;
99  };
100 
101  /// Helper class for \ref MakeEventTTreeFile
102  class TreeMaker: public SpectrumLoader
103  {
104  public:
106  const std::vector<std::pair<std::string, Cut>>& cuts,
107  const std::vector<std::pair<std::string, Var>>& floatVars,
108  const std::vector<std::pair<std::string, Var>>& intVars)
109  : SpectrumLoader(wildcard),
110  fFloats(floatVars.size()), fInts(intVars.size())
111  {
112  for(unsigned int ic = 0; ic < cuts.size(); ++ic){
113  fCuts.push_back(cuts[ic].second);
114  fTrees.push_back(new TTree(cuts[ic].first.c_str(),
115  cuts[ic].first.c_str()));
116  for(unsigned int iv = 0; iv < floatVars.size(); ++iv){
117  fFloatVars.push_back(floatVars[iv].second);
118  fTrees.back()->Branch(floatVars[iv].first.c_str(), &fFloats[iv]);
119  }
120  for(unsigned int iv = 0; iv < intVars.size(); ++iv){
121  fIntVars.push_back(intVars[iv].second);
122  fTrees.back()->Branch(intVars[iv].first.c_str(), &fInts[iv]);
123  }
124  }
125  }
126 
127  void HandleRecord(caf::SRProxy* sr) override
128  {
129  if(fSpillCut && !(*fSpillCut)(&sr->spill)) return;
130 
131  bool any = false;
132 
133  for(unsigned int ic = 0; ic < fCuts.size(); ++ic){
134  if(!fCuts[ic](sr)) continue;
135 
136  if(!any){
137  any = true;
138  for(unsigned int iv = 0; iv < fFloatVars.size(); ++iv){
139  fFloats[iv] = fFloatVars[iv](sr);
140  }
141  for(unsigned int iv = 0; iv < fIntVars.size(); ++iv){
142  fInts[iv] = fIntVars[iv](sr);
143  }
144  }
145 
146  fTrees[ic]->Fill();
147  }
148  }
149 
150  void Write()
151  {
152  for(TTree* tr: fTrees) tr->Write();
153  }
154 
155  protected:
156  std::vector<Cut> fCuts;
157  std::vector<Var> fFloatVars;
158  std::vector<Var> fIntVars;
159  std::vector<TTree*> fTrees;
160  std::vector<float> fFloats;
161  std::vector<int> fInts;
162  };
163 
164  //----------------------------------------------------------------------
165  //
166  // All the variants below have the same bodies (here), but slightly different
167  // arguments. We have T=wildcard/file list and U=Var/MultiVar
168  template<class T, class U>
169  void MakeTextListFileHelper(const T& source,
170  const std::vector<Cut>& cut,
171  const std::vector<std::string>& output,
172  const std::vector<U>& floatVars,
173  const std::vector<U>& intVars,
174  const SpillCut* spillCut)
175  {
176  assert(output.size() == cut.size());
177 
178  std::vector<FILE*> files;
179  for(const std::string& out: output)
180  files.push_back(fopen(out.c_str(), "w"));
181 
182  ASCIIMaker maker(source, cut, files, floatVars, intVars);
183  if(spillCut) maker.SetSpillCut(*spillCut);
184  maker.Go();
185 
186  for(const std::string& out: output)
187  std::cout << "Wrote text list file " << out << std::endl;
188 
189  for(FILE* f: files) fclose(f);
190  }
191 
192  //----------------------------------------------------------------------
194  const std::vector<Cut>& cut,
195  const std::vector<std::string>& output,
196  const std::vector<const Var*>& floatVars,
197  const std::vector<const Var*>& intVars,
198  const SpillCut* spillCut)
199  {
200  MakeTextListFileHelper(wildcard, cut, output, floatVars, intVars, spillCut);
201  }
202 
203  //----------------------------------------------------------------------
204 
205  void MakeTextListFile(const std::vector<std::string>& fnames,
206  const std::vector<Cut>& cut,
207  const std::vector<std::string>& output,
208  const std::vector<const Var*>& floatVars,
209  const std::vector<const Var*>& intVars,
210  const SpillCut* spillCut)
211  {
212  MakeTextListFileHelper(fnames, cut, output, floatVars, intVars, spillCut);
213  }
214 
215  //----------------------------------------------------------------------
217  const std::vector<Cut>& cut,
218  const std::vector<std::string>& output,
219  const std::vector<const MultiVar*>& multivars,
220  const SpillCut* spillCut)
221  {
222  MakeTextListFileHelper(wildcard, cut, output, multivars, {}, spillCut);
223  }
224 
225  //----------------------------------------------------------------------
226  //
227  // T can be a wildcard or list of file names
228  template<class T>
229  void MakeEventListFileHelper(const T& source,
230  const std::vector<Cut>& cuts,
231  const std::vector<std::string>& outputs,
232  bool includeSliceIndex,
233  bool includeSliceTime,
234  bool includeCycleNumber,
235  const SpillCut* spillCut,
236  bool includeBatchNumber)
237  {
238  assert(cuts.size() == outputs.size());
239 
240  std::vector<const Var*> intVars = {new SIMPLEVAR(hdr.run),
241  new SIMPLEVAR(hdr.subrun)};
242 
243  if(includeCycleNumber) intVars.push_back(new SIMPLEVAR(hdr.cycle));
244  if(includeBatchNumber) intVars.push_back(new SIMPLEVAR(hdr.batch));
245  intVars.push_back(new SIMPLEVAR(hdr.evt));
246  if(includeSliceIndex) intVars.push_back(new SIMPLEVAR(hdr.subevt));
247 
248  std::vector<const Var*> floatVars;
249  if(includeSliceTime) floatVars.push_back(new SIMPLEVAR(hdr.subevtmeantime));
250 
251  MakeTextListFile(source, cuts, outputs, floatVars, intVars, spillCut);
252  }
253 
254  //----------------------------------------------------------------------
256  const std::vector<Cut>& cuts,
257  const std::vector<std::string>& outputs,
258  bool includeSliceIndex,
259  bool includeSliceTime,
260  bool includeCycleNumber,
261  const SpillCut* spillCut,
262  bool includeBatchNumber)
263  {
264  MakeEventListFileHelper(wildcard, cuts, outputs,
265  includeSliceIndex, includeSliceTime, includeCycleNumber, spillCut, includeBatchNumber);
266  }
267 
268  //----------------------------------------------------------------------
269  void MakeEventListFile(const std::vector<std::string>& fnames,
270  const std::vector<Cut>& cuts,
271  const std::vector<std::string>& outputs,
272  bool includeSliceIndex,
273  bool includeSliceTime,
274  bool includeCycleNumber,
275  const SpillCut* spillCut,
276  bool includeBatchNumber)
277  {
278  MakeEventListFileHelper(fnames, cuts, outputs,
279  includeSliceIndex,
280  includeSliceTime,
281  includeCycleNumber,
282  spillCut,
283  includeBatchNumber);
284  }
285 
286  //----------------------------------------------------------------------
288  const Cut& cut,
289  const std::string& output,
290  bool includeSliceIndex,
291  bool includeSliceTime,
292  bool includeCycleNumber,
293  const SpillCut* spillCut,
294  bool includeBatchNumber)
295  {
296  MakeEventListFileHelper(wildcard, {cut}, {output},
297  includeSliceIndex,
298  includeSliceTime,
299  includeCycleNumber,
300  spillCut,
301  includeBatchNumber);
302  }
303 
304  //----------------------------------------------------------------------
305  void MakeEventListFile(const std::vector<std::string>& fnames,
306  const Cut& cut,
307  const std::string& output,
308  bool includeSliceIndex,
309  bool includeSliceTime,
310  bool includeCycleNumber,
311  const SpillCut* spillCut,
312  bool includeBatchNumber)
313  {
314  MakeEventListFileHelper(fnames, {cut}, {output},
315  includeSliceIndex,
316  includeSliceTime,
317  includeCycleNumber,
318  spillCut,
319  includeBatchNumber);
320 
321  }
322 
323  //----------------------------------------------------------------------
325  const std::string& output,
326  const std::vector<std::pair<std::string, Cut>>& cuts,
327  const std::vector<std::pair<std::string, Var>>& floatVars,
328  const std::vector<std::pair<std::string, Var>>& intVars,
329  const SpillCut* spillCut)
330  {
331  // Scope all the TTrees within this
332  TFile fout(output.c_str(), "RECREATE");
333 
334  TreeMaker maker(wildcard, cuts, floatVars, intVars);
335 
336  if(spillCut) maker.SetSpillCut(*spillCut);
337  maker.Go();
338 
339  fout.cd();
340  maker.Write();
341  fout.Close();
342  std::cout << "Wrote TTree file " << output << std::endl;
343  }
344 
345 }
caf::Proxy< caf::SRSpill > spill
Definition: SRProxy.h:2142
ofstream output
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
void HandleRecord(caf::SRProxy *sr) override
Definition: EventList.cxx:65
std::vector< Var > fFloatVars
Definition: EventList.cxx:157
ASCIIMaker(const std::string &wildcard, const std::vector< Cut > &cut, const std::vector< FILE * > &f, const std::vector< const Var * > &floatVars, const std::vector< const Var * > &intVars)
Definition: EventList.cxx:19
void MakeEventListFileHelper(const T &source, const std::vector< Cut > &cuts, const std::vector< std::string > &outputs, bool includeSliceIndex, bool includeSliceTime, bool includeCycleNumber, const SpillCut *spillCut, bool includeBatchNumber)
Definition: EventList.cxx:229
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2125
void MakeEventListFile(const std::string &wildcard, const std::vector< Cut > &cuts, const std::vector< std::string > &outputs, bool includeSliceIndex, bool includeSliceTime, bool includeCycleNumber, const SpillCut *spillCut, bool includeBatchNumber)
Make a set of files listing all the events passing cuts One output file is created for each cut in th...
Definition: EventList.cxx:255
std::vector< const Var * > fIntVars
Definition: EventList.cxx:96
TreeMaker(const std::string &wildcard, const std::vector< std::pair< std::string, Cut >> &cuts, const std::vector< std::pair< std::string, Var >> &floatVars, const std::vector< std::pair< std::string, Var >> &intVars)
Definition: EventList.cxx:105
std::vector< Cut > fCuts
Definition: EventList.cxx:156
std::vector< FILE * > fFile
Definition: EventList.cxx:94
ASCIIMaker(const std::vector< std::string > &fnames, const std::vector< Cut > &cut, const std::vector< FILE * > &f, const std::vector< const Var * > &floatVars, const std::vector< const Var * > &intVars)
Definition: EventList.cxx:46
void SetSpillCut(const SpillCut &cut)
Helper class for MakeTextListFile.
Definition: EventList.cxx:16
Helper class for MakeEventTTreeFile.
Definition: EventList.cxx:102
fvar< T > round(const fvar< T > &x)
Definition: round.hpp:23
std::unique_ptr< SpillCut > fSpillCut
Cut applied to the spill branch for every event.
fclose(fg1)
std::vector< Cut > fCut
Definition: EventList.cxx:93
#define SIMPLEVAR(CAFNAME)
For Vars where literally all you need is a single CAF variable.
Definition: Var.h:88
void MakeTextListFile(const std::string &wildcard, const std::vector< Cut > &cut, const std::vector< std::string > &output, const std::vector< const Var * > &floatVars, const std::vector< const Var * > &intVars, const SpillCut *spillCut)
Make a file listing all the events passing the specified cut.
Definition: EventList.cxx:193
std::vector< const MultiVar * > fMultiVars
Definition: EventList.cxx:97
std::vector< const Var * > fFloatVars
Definition: EventList.cxx:95
std::vector< std::string > wildcard(const std::string &wildcardString)
Definition: convert.C:9
caf::StandardRecord * sr
void MakeTextListFileHelper(const T &source, const std::vector< Cut > &cut, const std::vector< std::string > &output, const std::vector< U > &floatVars, const std::vector< U > &intVars, const SpillCut *spillCut)
Definition: EventList.cxx:169
virtual void Go() override
Load all the registered spectra.
std::vector< float > fFloats
Definition: EventList.cxx:160
OStream cout
Definition: OStream.cxx:6
const Cut cut
Definition: exporter_fd.C:30
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
void MakeEventTTreeFile(const std::string &wildcard, const std::string &output, const std::vector< std::pair< std::string, Cut >> &cuts, const std::vector< std::pair< std::string, Var >> &floatVars, const std::vector< std::pair< std::string, Var >> &intVars, const SpillCut *spillCut)
Make a ROOT file listing all the events passing the specified cut.
Definition: EventList.cxx:324
ASCIIMaker(const std::string &wildcard, const std::vector< Cut > &cut, const std::vector< FILE * > &f, const std::vector< const MultiVar * > &multivars, const std::vector< const MultiVar * > &)
Definition: EventList.cxx:33
assert(nhit_max >=nhit_nbins)
std::vector< Var > fIntVars
Definition: EventList.cxx:158
double T
Definition: Xdiff_gwt.C:5
Template for Cut and SpillCut.
Definition: Cut.h:15
void HandleRecord(caf::SRProxy *sr) override
Definition: EventList.cxx:127
std::vector< int > fInts
Definition: EventList.cxx:161
std::vector< TTree * > fTrees
Definition: EventList.cxx:159
enum BeamMode string