KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVDetectorSignalExpression.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Thu Jun 20 15:36:03 2019
2 //Author: John Frankland,,,
3 
4 #include "KVDetector.h"
6 
8 
9 
10 
20  : KVDetectorSignal(type, det)
21 {
22  // \param[in] type the typename for this expression, will be used as an alias for the expression
23  // \param[in] _expr a mathematical expression using names of signals already defined for detector \a det. See TFormula
24  // class for valid operators/functions.
25  // \param[in] det the detector to which this expression is to be associated
26  //
27  // If no valid signals are contained in the expression (i.e. signals not already defined for the
28  // detector \a det), IsValid() returns kFALSE and the expression should not be used
29 
30  int nsigs = 0;
31  KVString expr = _expr;
32  SetLabel(_expr);
33  TIter it_sig(&det->GetListOfDetectorSignals());
34  KVDetectorSignal* ds;
35  fRaw = kTRUE;
36  while ((ds = (KVDetectorSignal*)it_sig())) {
37  if (expr.Contains(ds->GetName())) {
38  fSignals.push_back(ds);
39  if (!ds->IsRaw()) fRaw = kFALSE;
40  expr.ReplaceAll(ds->GetName(), Form("[%d]", nsigs));
41  if (expr.CompareTo(_expr) != 0) ++nsigs; // a replacement was made => a signal was found
42  }
43  }
44  if (nsigs) {
45  fFormula.reset(new TFormula(type, expr));
46  fValid = kTRUE;
47  }
48  else
49  fValid = kFALSE;
50 
51  SetTitle(Form("Signal calculated as %s for detector %s", _expr.Data(), det->GetName()));
52 }
53 
54 
55 
66 
69 {
70  // Constructor to be used with expressions which use explict references to detector signals
71  // in the form [DET]::[SIG] where [DET] is the detector label and [SIG] is the signal name.
72  // The list must contain the detectors whose labels are referenced.
73  //
74  // \note any signals which are not defined will be systematically evaluated as zero
75  //
76  // \param[in] type the typename for this expression, will be used as an alias for the expression
77  // \param[in] _expr a mathematical expression using explicit references to names of detector signals
78  // \param[in] dets a list of pointers to the detectors whose signals are to be used in the expression
79 
80  int nsigs = 0;
81  KVString expr = _expr;
82  SetLabel(_expr);
83  fRaw = kTRUE;
84  // examine each term in expression
85  _expr.Begin("+-*/()");
86  bool multidetexpr = false;
87  bool explicit_det_reference = false;
88  KVDetector* det = nullptr;
89  while (!_expr.End()) {
90  KVString t = _expr.Next();
91  // check if we have an explicit reference to a detector
92  if (t.Contains("::")) {
93  explicit_det_reference = true;
94  // check if more than 1 detector is referenced
95  t.Begin("::");
96  auto det_label = t.Next();
97  auto _det = (KVDetector*)dets->FindObjectByLabel(det_label);
98  if (!_det) {
99  // reference to unknown detector
100  Error("KVDetectorSignalExpression(const Char_t*,const KVString&,const KVSeqCollection*)",
101  "Use of reference to unknown detector with label %s in expression %s",
102  det_label.Data(), _expr.Data());
103  fValid = kFALSE;
104  return;
105  }
106  auto sig_name = t.Next();
107  auto dsig = _det->GetDetectorSignal(sig_name);
108  KVString det_sig_ref = Form("%s::%s", det_label.Data(), sig_name.Data());
109  if (!dsig) {
110  // reference to unknown signal
111  Error("KVDetectorSignalExpression(const Char_t*,const KVString&,const KVSeqCollection*)",
112  "Use of reference to undefined signal %s for detector %s in expression %s : will evaluate as 0",
113  sig_name.Data(), _det->GetName(), _expr.Data());
114  expr.ReplaceAll(det_sig_ref, "0");
115  }
116  else {
117  if (expr.Contains(det_sig_ref)) {
118  expr.ReplaceAll(det_sig_ref, Form("[%d]", nsigs));
119  fSignals.push_back(dsig);
120  ++nsigs;
121  if (!dsig->IsRaw()) fRaw = kFALSE;
122  }
123  }
124  if (det && (_det != det)) multidetexpr = true;
125  det = _det;
126  }
127  }
128  if (!explicit_det_reference) {
129  fValid = kFALSE;
130  Error("KVDetectorSignalExpression(const Char_t*,const KVString&,const KVSeqCollection*)",
131  "Expression %s must contain explicit references to detector labels",
132  _expr.Data());
133  return;
134  }
135  if (nsigs) {
136  fFormula.reset(new TFormula(type, expr));
137  fValid = kTRUE;
138  }
139  else
140  fValid = kFALSE;
141 
142  if (multidetexpr)
143  SetTitle(Form("Signal calculated as %s", _expr.Data()));
144  else {
145  // only 1 detector is referenced in the expression
146  SetDetector(det);
147  SetTitle(Form("Signal calculated as %s for detector %s", _expr.Data(), det->GetName()));
148  }
149 }
150 
151 
152 
156 
158 {
159  // \param[in] params comma-separated list of "param=value" pairs if extra parameters are required to evaluate any signals in the expression
160  // \returns value of the expression using all current values of signals
161 
162  int nsigs = fSignals.size();
163  for (int i = 0; i < nsigs; ++i) {
164  fFormula->SetParameter(i, fSignals[i]->GetValue(params));
165  }
166  return fFormula->Eval(0);
167 }
168 
169 
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
double Double_t
const Bool_t kTRUE
int type
char * Form(const char *fmt,...)
void SetLabel(const Char_t *lab)
Definition: KVBase.h:194
Signal output from a mathematical combination of other signals.
Double_t GetValue(const KVNameValueList &params="") const
std::unique_ptr< TFormula > fFormula
std::vector< KVDetectorSignal * > fSignals
KVDetectorSignalExpression(const Char_t *type, const KVString &_expr, KVDetector *det)
Base class for output signal data produced by a detector.
virtual Bool_t IsRaw() const
void SetDetector(const KVDetector *d)
Base class for detector geometry description.
Definition: KVDetector.h:159
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
KaliVeda extensions to ROOT collection classes.
virtual TObject * FindObjectByLabel(const Char_t *) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void Begin(TString delim) const
Definition: KVString.cpp:565
Bool_t End() const
Definition: KVString.cpp:634
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695
virtual const char * GetName() const
virtual void SetTitle(const char *title="")
virtual void Error(const char *method, const char *msgfmt,...) const
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
TString & ReplaceAll(const char *s1, const char *s2)