CSVMaker.cxx
Go to the documentation of this file.
2 
3 #include <functional>
4 #include <iomanip>
5 #include <iostream>
6 #include <limits>
7 #include <stdexcept>
8 #include <vector>
9 
10 #include "CAFAna/Core/Var.h"
11 #include "CAFAna/Core/MultiVar.h"
12 
14 
15 namespace ana
16 {
17 
18 template<class T, class S>
20  std::ostream &output,
21  const std::vector<T> &values,
22  bool firstValuePrinted,
23  S separator,
24  std::function<void (std::ostream &, const T&)> printValue
25 )
26 {
27  if (values.empty()) {
28  return firstValuePrinted;
29  }
30 
31  size_t startIdx = 0;
32 
33  if (! firstValuePrinted)
34  {
35  printValue(output, values[0]);
36  startIdx = 1;
37  }
38 
39  for (size_t i = startIdx; i < values.size(); i++)
40  {
41  output << separator;
42  printValue(output, values[i]);
43  }
44 
45  return true;
46 }
47 
48 CSVMaker::CSVMaker(const std::string &input, const std::string &output)
49  : SpectrumLoader(input), cut(nullptr), ofile(output)
50 {
51  if (! ofile) {
52  throw std::runtime_error("Failed to open output file");
53  }
54 
55  ofile << std::setprecision(std::numeric_limits<double>::max_digits10);
56 }
57 
58 void CSVMaker::setPrecision(int precision)
59 {
60  ofile << std::setprecision(precision);
61 }
62 
64 {
65  bool firstValuePrinted = false;
66 
67  firstValuePrinted = printSeparatedValues<NamedVar, char>(
68  ofile, vars, firstValuePrinted, ',',
69  [this] (auto &output, auto &namedVar) { output << namedVar.first; }
70  );
71 
72  printSeparatedValues<NamedMultiVar, char>(
73  ofile, multiVars, firstValuePrinted, ',',
74  [this] (auto &output, auto &namedMultiVar)
75  { output << namedMultiVar.first; }
76  );
77 
78  ofile << std::endl;
79 }
80 
82 {
83  bool firstValuePrinted = false;
84 
85  firstValuePrinted = printSeparatedValues<NamedVar, char>(
86  ofile, vars, firstValuePrinted, ',',
87  [sr,this] (auto &output, auto &namedVar) { output << namedVar.second(sr); }
88  );
89 
90  printSeparatedValues<NamedMultiVar, char>(
91  ofile, multiVars, firstValuePrinted, ',',
92  [sr,this] (auto &output, auto &namedMultiVar)
93  {
94  std::vector<double> values = namedMultiVar.second(sr);
95 
96  output << '"';
97 
98  printSeparatedValues<double, char>(
99  output, values, false, ',',
100  [this] (auto &output, auto &x) { output << x; }
101  );
102 
103  output << '"';
104  }
105  );
106 
107  ofile << std::endl;
108 }
109 
111 {
112  vars.emplace_back(std::make_pair(name, var));
113 }
114 
115 void CSVMaker::addMultiVar(const std::string &name, const MultiVar &multiVar)
116 {
117  multiVars.emplace_back(std::make_pair(name, multiVar));
118 }
119 
120 void CSVMaker::addVars(const std::vector<NamedVar> &vars)
121 {
122  this->vars.insert(this->vars.end(), vars.begin(), vars.end());
123 }
124 
125 void CSVMaker::addMultiVars(const std::vector<NamedMultiVar> &multiVars)
126 {
127  this->multiVars.insert(
128  this->multiVars.end(), multiVars.begin(), multiVars.end()
129  );
130 }
131 
133 {
134  this->cut = std::make_unique<Cut>(cut);
135 }
136 
138 {
139  if ((fSpillCut != nullptr) && !(*fSpillCut)(&sr->spill)) {
140  return;
141  }
142 
143  if ((cut != nullptr) && !(*cut)(sr)) {
144  return;
145  }
146 
147  printRow(sr);
148 }
149 
151 {
152  if (! Gone()) {
153  printHeader();
154  }
155 
157 
158  ofile.close();
159 }
160 
161 }
caf::Proxy< caf::SRSpill > spill
Definition: SRProxy.h:2143
const XML_Char * name
Definition: expat.h:151
ofstream output
void setCut(const Cut &cut)
Definition: CSVMaker.cxx:132
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
#define S(x, n)
std::vector< NamedMultiVar > multiVars
Definition: CSVMaker.h:24
void printHeader()
Definition: CSVMaker.cxx:63
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2126
std::pair< Spectrum *, CheatDecomp * > make_pair(SpectrumLoaderBase &loader_data, SpectrumLoaderBase &loader_mc, HistAxis *axis, Cut *cut, const SystShifts &shift, const Var &wei)
Definition: DataMCLoad.C:336
void addMultiVars(const std::vector< NamedMultiVar > &multiVars)
Definition: CSVMaker.cxx:125
std::unique_ptr< SpillCut > fSpillCut
Cut applied to the spill branch for every event.
bool printSeparatedValues(std::ostream &output, const std::vector< T > &values, bool firstValuePrinted, S separator, std::function< void(std::ostream &, const T &)> printValue)
Definition: CSVMaker.cxx:19
std::ofstream ofile
Definition: CSVMaker.h:21
void setPrecision(int precision)
Definition: CSVMaker.cxx:58
std::vector< NamedVar > vars
Definition: CSVMaker.h:23
caf::StandardRecord * sr
virtual void Go() override
Load all the registered spectra.
std::unique_ptr< Cut > cut
Definition: CSVMaker.h:20
void addMultiVar(const std::string &name, const MultiVar &multiVar)
Definition: CSVMaker.cxx:115
void addVar(const std::string &name, const Var &var)
Definition: CSVMaker.cxx:110
const Cut cut
Definition: exporter_fd.C:30
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
CSVMaker(const std::string &input, const std::string &output)
Definition: CSVMaker.cxx:48
void printRow(caf::SRProxy *sr)
Definition: CSVMaker.cxx:81
double T
Definition: Xdiff_gwt.C:5
virtual bool Gone() const
Indicate whether or not Go has been called.
void Go() override
Load all the registered spectra.
Definition: CSVMaker.cxx:150
void addVars(const std::vector< NamedVar > &vars)
Definition: CSVMaker.cxx:120
void HandleRecord(caf::SRProxy *sr) override
Definition: CSVMaker.cxx:137
enum BeamMode string