Diana Software
MApplyCalibration.cc
Go to the documentation of this file.
1 #include "MApplyCalibration.hh"
2 #include "QBaseType.hh"
3 #include "QEvent.hh"
4 #include "QRawEvent.hh"
5 #include "QMatrix.hh"
6 #include "QString.hh"
7 #include "TF1.h"
10 
12 
13 using std::string;
14 using std::cout;
15 using std::endl;
16 using namespace Diana;
17 
19 {
20  // called once in a sequence
21 }
22 
24 {
25  // called before event loop
26  fInput = GetString("ParametersInput", "");
27 
28  // get dataset
29  fDataset = GetInt("Dataset",-1,false);
30  if(fDataset < 0) {
31  GlobalHandle<QInt> dHandle("Dataset");
32  GlobalData().Get("",&dHandle,"");
33  fDataset = dHandle.Get();
34  }
35 
36  fAmplitudeLabel = GetString("AmplitudeLabel","CorrectAmplitudes@Amplitude");
37  ev.Add<QDouble>("Energy");
38  ev.Add<QDouble>("EnergyUncertainty");
39  fBlackList.clear();
40 
41  ev.Require<QPulseInfo>("DAQ","PulseInfo");
42  ev.Require<QHeader>("DAQ","Header");
43  ev.RequireByLabel<QDouble>(fAmplitudeLabel);
44 }
45 
47 {
48  // called event by event
49  const QPulseInfo& pulseInfo = ev.Get<QPulseInfo>("DAQ","PulseInfo");
50  const QHeader& header = ev.Get<QHeader>("DAQ","Header");
51  int channel = pulseInfo.GetChannelId();
52  int run = header.GetRun();
53 
54  const double amp = ev.GetByLabel<QDouble>(fAmplitudeLabel);
55  double energy = -1;
56 
57  // get calibration parameters for this channel
58  QCalibrationParametersHandle paramhandle(channel,fDataset);
59  GlobalData().Get(GetString("CalCoefficientsOwner","CalCoefficients",false),&paramhandle,fInput);
60 
61  if (!paramhandle.IsValid()) {
62  if(fBlackList.count(channel)) return;
63  fBlackList.insert(channel);
64  Error("Channel %d, will not be calibrated: %s",channel,paramhandle.GetError().GetDescription().c_str());
65  return;
66  }
67 
68  // check that the amplitude to be calibrated is the same on which the calibration parameters were calculated (stored in DB)
69  // this check is performed only when the input of the calibration parameters is DB
70  if (fInput == "DB" && fChannels.find(channel) == fChannels.end()) {
71  fChannels.insert(channel);
72  std::string sLabelstr;
73  GlobalHandle<QString> sLabel("StabilizationLabel");
74  sLabel.SetChannel(channel);
75  sLabel.SetRun(run);
76  GlobalData().Get(fAmplitudeLabel.GetOwner(),&sLabel,GetString("StabilizationLabelInput","CurrentReader",false));
77  if (sLabel.IsValid()) {
78  sLabelstr = sLabel.Get();
79  }
80  QGlobalLabel stabLabel = sLabelstr;;
81 
82  if (stabLabel.fOwner != paramhandle.GetStabLabel())
83  {
84  Error("The amplitude to be calibrated (%s) is not the same on which the calibration parameters were calculated (%s). Calibrating %s anyway.",stabLabel.fOwner.c_str(),paramhandle.GetStabLabel().c_str(),fAmplitudeLabel.GetStringLabel().c_str());
85  }
86 
87  // writes a GlobalData (QString) with the name of the calibration module
88  GlobalHandle<QString> clabel("CalibrationLabel");
89  clabel.SetChannel(channel);
90  clabel.SetDataset(fDataset);
91  QGlobalLabel label;
92  label.fOwner= paramhandle.GetLabel().fOwner;
93  label.fName= paramhandle.GetLabel().fName;
94  clabel.Set(label.GetStringLabel());
95  GlobalData().Set(&clabel,"CurrentWriter");
96 
97  }
98 
99  // Calculate energy from calibration function
100  const QCalibrationParameters& calParams = paramhandle.Get();
101  energy = calParams.fFunction.Eval(amp);
102  // eliminate 'nan's
103  if (!(energy >=0) && !(energy<=0)) return;
104 
105  ev.Get<QDouble>("Energy") = energy;
106 
107  // Calculate estimated error on energy by evaluating function at limits of parameters
108  double *params = calParams.fFunction.GetParameters();
109  const double *paramErrors = calParams.fFunction.GetParErrors();
110  int nParams = calParams.fFunction.GetNpar();
111 
112  TF1 calFunction;
113  calParams.fFunction.Copy(calFunction);
114 
115  double *limitParams = new double[nParams];
116  for (int i = 0; i < nParams; i++)
117  limitParams[i] = params[i] + paramErrors[i];
118  double energyUpper = calFunction.EvalPar(&amp, limitParams);
119  for (int i = 0; i < nParams; i++)
120  limitParams[i] = params[i] - paramErrors[i];
121  double energyLower = calFunction.EvalPar(&amp, limitParams);
122 
123  ev.Get<QDouble>("EnergyUncertainty") = (energyUpper - energyLower) / 2;
124 }
125 
127 {
128  // called at the end of the event loop
129 }
130 
double amp
Definition: CheckOF.C:32
const int channel
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
Use calibration coefficients and stabilized amplitudes to compute energy.
void Done()
Done method.
void Init(Diana::QEvent &ev)
Init method.
~MApplyCalibration()
destructor
void Do(Diana::QEvent &ev)
Do method.
base types wrapped into a QObject. Currently implemented types are QInt QDouble and QFloat....
Definition: QBaseType.hh:17
global handle for calibration parameters
const std::string & GetStabLabel() const
object containing calibration data (function, coefficients, source and residuals) and run numbers of ...
diana event
Definition: QEvent.hh:46
const Q & GetByLabel(const QEventLabel &label) const
Get a QObject in read mode by label.
Definition: QEvent.hh:135
void RequireByLabel(const QEventLabel &label) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:242
void Require(const std::string &owner, const std::string &name) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:232
void Get(const char *owner, ReadHandle< Q > &handle) const
Get a QObject Handle in read mode.
Definition: QEvent.hh:74
void Add(WriteHandle< Q > &handle)
Add a QObject to the event.
Definition: QEvent.hh:193
Label for global QObject's.
Definition: QGlobalLabel.hh:19
std::string fName
Object name.
Definition: QGlobalLabel.hh:44
std::string fOwner
Object owner.
Definition: QGlobalLabel.hh:42
std::string GetStringLabel() const
convert label to string
Raw event: basic information like run number and time.
Definition: QHeader.hh:16
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
const int & GetChannelId() const
Get ChannelId.
Definition: QPulseInfo.hh:22
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...