RunData.h
Go to the documentation of this file.
1 // RunData.h
2 //
3 // David Adams
4 // May 2018
5 //
6 // Detector conditions that are typically constant over the
7 // course of a run.
8 //
9 // Schema are documented at
10 // https://wiki.dunescience.org/wiki/ProtoDUNE_run_configuration
11 
12 #ifndef RunData_H
13 #define RunData_H
14 
15 #include <string>
16 #include <vector>
17 #include <iostream>
18 #include <TFormula.h>
20 
21 class RunData {
22 
23 public:
24 
25  using Name = std::string;
26  using Index = unsigned int;
27  using IndexVector = std::vector<Index>;
28 
29  // Ctor from a run.
30  explicit RunData(Index a_run =0) : m_run(a_run) { };
31 
32  // Return a value.
33  Index run() const { return m_run; }
34  Name cryostat() const { return m_cryostat; }
35  const IndexVector& apas() const { return m_apas; }
36  float gain() const { return m_gain; }
37  float shaping() const { return m_shaping; }
38  float baseline() const { return m_baseline; }
39  float leakage() const { return m_leakage; }
40  float hvfrac() const { return m_hvfrac; }
42  Index pulserSource() const { return m_pulserSource; }
43  Index pulserPeriod() const { return m_pulserPeriod; }
44  Name phaseGroup() const { return m_phaseGroup; }
45  const IndexVector& phases() const { return m_phases; }
46 
47  // Return if value is defined.
48  bool isValid() const { return run(); }
49  bool haveRun() const { return run(); }
50  bool haveCryostat() const { return cryostat().size(); }
51  bool haveApas() const { return apas().size(); }
52  bool haveGain() const { return m_gain; }
53  bool haveShaping() const { return m_shaping; }
54  bool haveBaseline() const { return m_baseline; }
55  bool haveLeakage() const { return m_leakage; }
56  bool haveHvfrac() const { return m_hvfrac; }
57  bool havePulserAmplitude() const { return m_pulserAmplitude != 999; }
58  bool havePulserSource() const { return m_pulserSource != 0; }
59  bool havePulserPeriod() const { return m_pulserPeriod != 0; }
60  bool havePhaseGroup() const { return m_phaseGroup.size(); }
61  bool havePhases() const { return m_phaseGroup.size(); }
62 
63  // Setters.
64  void setRun(Index val) { m_run = val; }
66  void setApas(const IndexVector& val) { m_apas = val; }
67  void setGain(float val) { m_gain = val; }
68  void setShaping(float val) { m_shaping = val; }
69  void setBaseline(float val) { m_baseline = val; }
70  void setLeakage(float val) { m_leakage = val; }
71  void setHvfrac(float val) { m_hvfrac = val; }
76  void setPhases(const IndexVector& val) { m_phases = val; }
77 
78  // Set params for a formula.
79  // E.g. gainshap = "[gain]*[shaping]"
80  // Returns
81  // nset = # params for which values were set
82  // nerr = # params for which values are missing
83  struct SetStat {
84  Index nset =0;
85  Index nerr =0;
86  };
87  SetStat setFormulaPars(TFormula* form) {
88  SetStat sstat;
89  const std::string myname = "setFormulaPars: ";
90  int npar = form->GetNpar();
91  for ( int ipar=0; ipar<npar; ++ipar ) {
92  std::string spar = form->GetParName(ipar);
93  if ( spar == "run" ) {
94  if ( haveRun() ) {
95  form->SetParameter("run", run());
96  ++sstat.nset;
97  } else {
98  std::cout << myname << "WARNING: RunData does not have run." << std::endl;
99  ++sstat.nerr;
100  }
101  } else if ( spar == "gain" ) {
102  if ( haveGain() ) {
103  form->SetParameter("gain", gain());
104  ++sstat.nset;
105  } else {
106  std::cout << myname << "WARNING: RunData does not have gain." << std::endl;
107  ++sstat.nerr;
108  }
109  } else if ( spar == "shaping" ) {
110  if ( haveShaping() ) {
111  form->SetParameter("shaping", shaping());
112  ++sstat.nset;
113  } else {
114  std::cout << myname << "WARNING: RunData does not have shaping." << std::endl;
115  ++sstat.nerr;
116  }
117  }
118  }
119  return sstat;
120  }
121 
122  // Set parameters in a ParFormula formula.
124  const std::string myname = "setFormulaPars: ";
125  ParFormula::Names fpars = form.pars();
126  int nbad = 0;
127  for ( Name spar : fpars ) {
128  if ( spar == "run" ) {
129  if ( haveRun() ) {
130  form.setParValue("run", run());
131  } else {
132  std::cout << myname << "WARNING: RunData does not have run." << std::endl;
133  ++nbad;
134  }
135  } else if ( spar == "gain" ) {
136  if ( haveGain() ) {
137  form.setParValue("gain", gain());
138  } else {
139  std::cout << myname << "WARNING: RunData does not have gain." << std::endl;
140  ++nbad;
141  }
142  } else if ( spar == "shaping" ) {
143  if ( haveShaping() ) {
144  form.setParValue("shaping", shaping());
145  } else {
146  std::cout << myname << "WARNING: RunData does not have shaping." << std::endl;
147  ++nbad;
148  }
149  }
150  }
151  return nbad;
152  }
153 
154  // Accessors.
155  Index& accessRun() { return m_run; }
158  float& accessGain() { return m_gain; }
159  float& accessShaping() { return m_shaping; }
160  float& accessBaseline() { return m_baseline; }
161  float& accessLeakage() { return m_leakage; }
162  float& accessHvfrac() { return m_hvfrac; }
168 
169  std::ostream& print(std::ostream& lhs =std::cout) const {
170  const std::string sep = "\n";
171  if ( ! isValid() ) {
172  lhs << "Invalid run data.";
173  return lhs;
174  }
175  lhs << "Data for run " << run();
176  if ( haveCryostat() ) lhs << sep << " Cryostat: " << cryostat();
177  if ( haveApas() ) lhs << sep << " APAs: ";
178  int doComma = 0;
179  for ( Index iapa : apas() ) lhs << (doComma++ ? ", " : "") << iapa;
180  if ( haveGain() ) lhs << sep << " Gain: " << gain() << " mV/fC";
181  if ( haveShaping() ) lhs << sep << " Shaping time: " << shaping() << " us";
182  if ( haveBaseline() ) lhs << sep << " Baseline: " << baseline() << " mV";
183  if ( haveLeakage() ) lhs << sep << " Leakage cur.: " << leakage() << " pA";
184  if ( haveHvfrac() ) lhs << sep << " HV frac.: " << hvfrac();
185  if ( havePulserAmplitude() ) lhs << sep << " Pulser ampl.: " << pulserAmplitude();
186  if ( havePulserSource() ) lhs << sep << " Pulser source: " << pulserSource();
187  if ( havePulserPeriod() ) lhs << sep << " Pulser period: " << pulserPeriod();
188  if ( havePhaseGroup() ) { lhs << sep << " Phases grouped by " << phaseGroup() << ": [";
189  bool first = true;
190  for ( Index ipha : phases() ) {
191  if ( first ) first = false;
192  else lhs << ", ";
193  lhs << ipha;
194  }
195  lhs << "]";
196  }
197  return lhs;
198  }
199 
200  void clear() {
201  m_run = 0;
202  m_cryostat = "";
203  m_apas.clear();
204  m_gain = 0.0;
205  m_shaping = 0.0;
206  m_baseline = 0.0;
207  m_leakage = 0.0;
208  m_hvfrac = 0.0;
209  m_pulserAmplitude = 999;
210  m_pulserSource = 0; // 1=preamp, 2=FEMB
211  m_pulserPeriod = 0;
212  m_phaseGroup = "";
213  m_phases.clear();
214  }
215 
216 private:
217 
218  // Data.
222  float m_gain = 0.0;
223  float m_shaping = 0.0;
224  float m_baseline = 0.0;
225  float m_leakage = 0.0;
226  float m_hvfrac = 0.0;
228  Index m_pulserSource = 0; // 1=preamp, 2=FEMB
232 
233 };
234 
235 inline std::ostream& operator<<(std::ostream& lhs, const RunData& rhs) {
236  return rhs.print(lhs);
237 }
238 
239 #endif
IndexVector m_apas
Definition: RunData.h:221
bool havePhases() const
Definition: RunData.h:61
float m_baseline
Definition: RunData.h:224
bool havePulserPeriod() const
Definition: RunData.h:59
void setApas(const IndexVector &val)
Definition: RunData.h:66
Index pulserAmplitude() const
Definition: RunData.h:41
void clear()
Definition: RunData.h:200
bool havePulserSource() const
Definition: RunData.h:58
const IndexVector & phases() const
Definition: RunData.h:45
Index m_run
Definition: RunData.h:219
void setPhaseGroup(Name val)
Definition: RunData.h:75
void setGain(float val)
Definition: RunData.h:67
virtual Names pars() const =0
Name phaseGroup() const
Definition: RunData.h:44
std::string string
Definition: nybbler.cc:12
const IndexVector & apas() const
Definition: RunData.h:35
float & accessGain()
Definition: RunData.h:158
Index run() const
Definition: RunData.h:33
float & accessBaseline()
Definition: RunData.h:160
bool haveShaping() const
Definition: RunData.h:53
float & accessLeakage()
Definition: RunData.h:161
std::ostream & print(std::ostream &lhs=std::cout) const
Definition: RunData.h:169
void setPulserSource(Index val)
Definition: RunData.h:73
RunData(Index a_run=0)
Definition: RunData.h:30
Name m_phaseGroup
Definition: RunData.h:230
IndexVector m_phases
Definition: RunData.h:231
IndexVector & accessPhases()
Definition: RunData.h:167
bool isValid() const
Definition: RunData.h:48
float m_hvfrac
Definition: RunData.h:226
bool havePhaseGroup() const
Definition: RunData.h:60
IndexVector & accessApas()
Definition: RunData.h:157
float gain() const
Definition: RunData.h:36
bool haveApas() const
Definition: RunData.h:51
std::ostream & operator<<(std::ostream &lhs, const RunData &rhs)
Definition: RunData.h:235
Name & accessPhaseGroup()
Definition: RunData.h:166
Name & accessCryostat()
Definition: RunData.h:156
Index pulserPeriod() const
Definition: RunData.h:43
std::vector< Index > IndexVector
Definition: RunData.h:27
float m_shaping
Definition: RunData.h:223
virtual int setParValue(Name parnam, Value parval)=0
bool haveRun() const
Definition: RunData.h:49
Index m_pulserPeriod
Definition: RunData.h:229
bool haveCryostat() const
Definition: RunData.h:50
float shaping() const
Definition: RunData.h:37
std::vector< Name > Names
Definition: ParFormula.h:24
Index & accessPulserAmplitude()
Definition: RunData.h:163
void setHvfrac(float val)
Definition: RunData.h:71
bool haveLeakage() const
Definition: RunData.h:55
float hvfrac() const
Definition: RunData.h:40
Name m_cryostat
Definition: RunData.h:220
bool haveGain() const
Definition: RunData.h:52
Index & accessPulserSource()
Definition: RunData.h:164
void setPhases(const IndexVector &val)
Definition: RunData.h:76
int setFormulaPars(ParFormula &form)
Definition: RunData.h:123
float baseline() const
Definition: RunData.h:38
float & accessHvfrac()
Definition: RunData.h:162
Name cryostat() const
Definition: RunData.h:34
void setLeakage(float val)
Definition: RunData.h:70
float & accessShaping()
Definition: RunData.h:159
bool haveBaseline() const
Definition: RunData.h:54
void setCryostat(Name val)
Definition: RunData.h:65
Index & accessPulserPeriod()
Definition: RunData.h:165
void setRun(Index val)
Definition: RunData.h:64
Index m_pulserAmplitude
Definition: RunData.h:227
void setPulserAmplitude(Index val)
Definition: RunData.h:72
void setShaping(float val)
Definition: RunData.h:68
float m_leakage
Definition: RunData.h:225
SetStat setFormulaPars(TFormula *form)
Definition: RunData.h:87
bool haveHvfrac() const
Definition: RunData.h:56
float m_gain
Definition: RunData.h:222
Index m_pulserSource
Definition: RunData.h:228
std::string Name
Definition: RunData.h:25
void setPulserPeriod(Index val)
Definition: RunData.h:74
unsigned int Index
Definition: RunData.h:26
QTextStream & endl(QTextStream &s)
bool havePulserAmplitude() const
Definition: RunData.h:57
float leakage() const
Definition: RunData.h:39
Index & accessRun()
Definition: RunData.h:155
void setBaseline(float val)
Definition: RunData.h:69
Index pulserSource() const
Definition: RunData.h:42