Diana Software
MEnergyFilter.cc
Go to the documentation of this file.
1 #include "MEnergyFilter.hh"
2 #include "QEvent.hh"
3 #include "QRawEvent.hh"
4 #include "QRunData.hh"
5 #include "QBaseType.hh"
6 #include "QString.hh"
7 #include "QPulseInfo.hh"
8 #include "QInterval.hh"
9 #include "QRunDataHandle.hh"
10 #include "QEnergyRangesHandle.hh"
11 #include "QFrameWorkConfig.hh"
12 #include <map>
13 #include <set>
14 #include <string>
15 
16 using std::map;
17 using std::set;
18 using std::string;
19 
20 using namespace Diana;
21 
23 
24  void MEnergyFilter::GetCuts(const Diana::QEvent& ev)
25 {
26  // define Energy ranges input (file ore DB, for the moment only file work)
27  fRangesInput = GetString("EnergyRangesInput", "EnergyRanges.txt");
28 
29  // define Energy ranges output (file ore DB, for the moment only file work)
30  fRangesOutput = GetString("EnergyRangesOutput","" );
31 
32  QEnergyRangesHandle eHandle;
33  fRangesName = GetString("EnergyRangesName",eHandle.GetName(),false);
34  fRangesOwner = GetString("EnergyRangesOwner","User",false);
35 
36  // define Energy label
37  fEnergyLabel = GetString("EnergyLabel","ApplyCalibration@Energy");
38 
39  ev.RequireByLabel<QDouble>(fEnergyLabel);
40  ev.Require<QPulseInfo>("DAQ","PulseInfo");
41  ev.Require<QHeader>("DAQ","Header");
42  fBlackList.clear();
43  fChannelList.clear();
44  fHandles.clear();
45 }
46 
47 
48 bool MEnergyFilter::Filter(const Diana::QEvent& ev)
49 {
50  // perform selection: true if the event passes the cuts
51  const QPulseInfo& pi = ev.Get<QPulseInfo>("DAQ","PulseInfo");
52  const QHeader& header = ev.Get<QHeader>("DAQ","Header");
53  const int run = header.GetRun();
54 
55  if(run != fRunData.fNumber) {
57  GlobalData().Get<QRunData>("",&rHandle,"");
58  fRunData = rHandle.Get();
59 
60  // get dataset
61  GlobalHandle<QInt> dHandle("Dataset");
62  GlobalData().Get("",&dHandle,"");
63  fDataset = dHandle.Get();
64  }
65 
66  // get channel of event
67  const int& channel = pi.GetChannelId();
68  fEnergyRange.Clear();
69 
70  // get energy ranges for that channel
71  QEnergyRangesHandle eHandle(fRangesName);
72  eHandle.SetChannel(channel);
73  GlobalData().Get(fRangesOwner,&eHandle,fRangesInput);
74  if(!eHandle.IsValid()) {
75  eHandle.SetCalibVersion(GetConfig().fVersionTag);
76  eHandle.SetDataset(fDataset);
77  GlobalHandle<QString> clabelh("CalibrationLabel");
78  clabelh.SetDataset(fDataset);
79  clabelh.SetChannel(channel);
80  GlobalData().Get(fEnergyLabel.owner,&clabelh,"CurrentReader");
81  if(clabelh.IsValid()) {
82  eHandle.SetCalibLabel(clabelh.Get());
83  } else {
84  eHandle.SetCalibLabel("");
85  }
86  GlobalData().Get("EnergyFilter",&eHandle,fRangesInput);
87  }
88 
89  if(!eHandle.IsValid() && !fBlackList.count(channel))
90  {
91  fEnergyMin = GetDouble("EnergyMin",0);
92  fEnergyMax = GetDouble("EnergyMax",100000);
93  Info("Channel %d has no energy range: %s. Using [%f,%f]",channel,eHandle.GetError().GetDescription().c_str(),fEnergyMin,fEnergyMax);
94  fBlackList.insert(channel);
95  }
96  if(eHandle.IsValid() && !fBlackList.count(channel)
97  && eHandle.Get().GetMax() < eHandle.Get().GetMin()) {
98 
99  fEnergyMin = GetDouble("EnergyMin",0);
100  fEnergyMax = GetDouble("EnergyMax",100000);
101  Error("Channel %d has wrong energy range. fMin = %f; fMax = %f. Using [%f,%f]",channel,eHandle.Get().GetMin(),eHandle.Get().GetMax(),fEnergyMin,fEnergyMax);
102  fBlackList.insert(channel);
103  }
104 
105  if(fBlackList.count(channel)) {
106  // cfg defined range, to be applied to channels for which no energy range is defined in the file
107  fEnergyRange.SetMax(fEnergyMax);
108  fEnergyRange.SetMin(fEnergyMin);
109  }
110  else fEnergyRange = eHandle.Get();
111 
112  // if output is given dump the energy ranges on file or DB
113  if(!fRangesOutput.empty() && !fChannelList.count(channel)) {
114  QEnergyRangesHandle* eHandleDs = new QEnergyRangesHandle(fRangesName);
115  eHandleDs->SetCalibVersion(GetConfig().fVersionTag);
116  eHandleDs->SetChannel(channel);
117  eHandleDs->SetDataset(fDataset);
118  eHandleDs->Set(fEnergyRange);
119 
120 
121  GlobalHandle<QString> clabelh("CalibrationLabel");
122  clabelh.SetDataset(fDataset);
123  clabelh.SetChannel(channel);
124  GlobalData().Get(fEnergyLabel.owner,&clabelh,"CurrentReader");
125  if(clabelh.IsValid()) {
126  eHandleDs->SetCalibLabel(clabelh.Get());
127  } else {
128  eHandleDs->SetCalibLabel("");
129  }
130  fHandles.insert(eHandleDs);
131  fChannelList.insert(channel);
132  }
133 
134  // get energy of event
135  const double energy = ev.GetByLabel<QDouble>(fEnergyLabel);
136  bool passed = false;
137  if (fEnergyRange.Contains(energy)) passed = true;
138 
139  return passed;
140 
141 }
142 
144 {
145  if(fRangesOutput.empty()) return;
146  std::set<QEnergyRangesHandle*>::iterator iter = fHandles.begin();
147  while(iter != fHandles.end()) {
148  QEnergyRangesHandle* handle = *iter;
149  // get amplitude version (new)
150  QModuleConfig mconf;
151  GlobalHandle<QFrameWorkConfig> fwcHandle("Config");
152  GlobalData().Get("Diana",&fwcHandle,"");
154  if(!fwcHandle.IsValid()) {
155  err = fwcHandle.GetError();
156  } else if(!handle->GetCalibLabel().empty()){
157  QEventLabel eLabel(handle->GetCalibLabel());
158  err = fwcHandle.Get().GetModuleByLabel(eLabel.GetOwner(),mconf);
159  }
160  if(fRangesOutput =="DB" && err != QERR_SUCCESS) Error("%s",err.GetDescription().c_str());
161 
162  const std::string& versiontag = mconf.fVersionTag;
163  handle->SetCalibVersion(versiontag);
164 
165  GlobalData().Set(handle,fRangesOutput);
166  delete handle;
167  iter++;
168  }
169 }
170 
171 
err
Definition: CheckOF.C:114
QRunDataHandle rHandle(753)
const int channel
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
@ QERR_SUCCESS
Definition: QError.hh:27
Applies an energy threshold depending on the channel.
bool Filter(const Diana::QEvent &ev)
return true if ev passes the cuts
base types wrapped into a QObject. Currently implemented types are QInt QDouble and QFloat....
Definition: QBaseType.hh:17
global handle for EnergyRanges
void SetCalibVersion(const std::string &stabv)
const std::string & GetCalibLabel() const
void SetCalibLabel(const std::string &stabl)
error class with error type and description
Definition: QError.hh:115
label for QObject in the QEvent
Definition: QEventLabel.hh:23
const std::string & GetOwner() const
get owner
Definition: QEventLabel.hh:55
diana event
Definition: QEvent.hh:46
Raw event: basic information like run number and time.
Definition: QHeader.hh:16
int GetRun() const
destructor
Definition: QHeader.hh:22
QObject to store a QBaseModule configuration.
std::string fVersionTag
Version release tag.
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
const int & GetChannelId() const
Get ChannelId.
Definition: QPulseInfo.hh:22
global handle for QRunData
Basic run based info.
Definition: QRunData.hh:20
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...