REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestRawVetoAnalysisProcess.cxx
1 /*************************************************************************
2  * This file is part of the REST software framework. *
3  * *
4  * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5  * For more information see http://gifna.unizar.es/trex *
6  * *
7  * REST is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * REST is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have a copy of the GNU General Public License along with *
18  * REST in $REST_PATH/LICENSE. *
19  * If not, see http://www.gnu.org/licenses/. *
20  * For the list of contributors see $REST_PATH/CREDITS. *
21  *************************************************************************/
22 
137 #include "TRestRawVetoAnalysisProcess.h"
138 
139 using namespace std;
140 
142 
147 
163  Initialize();
164 
165  LoadConfig(configFilename);
166 }
167 
172 
177  SetName(this->ClassName());
178  SetTitle("Default config");
179 }
180 
193 void TRestRawVetoAnalysisProcess::LoadConfig(const string& configFilename, const string& name) {
194  if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
195 }
196 
202  // For example, try to initialize a pointer to existing metadata
203  // accessible from TRestRun
204 }
205 
211  SetSectionName(this->ClassName());
212  SetLibraryVersion(LIBRARY_VERSION);
213 
214  fSignalEvent = nullptr;
215 }
216 
221  fSignalEvent = (TRestRawSignalEvent*)inputEvent;
222 
223  map<int, Double_t> VetoMaxPeakAmplitude_map;
224  map<int, Double_t> VetoPeakTime_map;
225 
226  Int_t VetoAboveThreshold = 0;
227  Int_t NVetoAboveThreshold = 0;
228  Int_t VetoInTimeWindow = 0;
229  Int_t NVetoInTimeWindow = 0;
230 
231  fSignalEvent->SetRange(fRange);
232 
233  VetoMaxPeakAmplitude_map.clear();
234  VetoPeakTime_map.clear();
235 
236  // **************************************************************
237  // if list of veto Ids without groups is given ******************
238  // **************************************************************
239 
240  if (fVetoSignalId[0] != -1) {
241  // iterate over vetoes
242  for (unsigned int i = 0; i < fVetoSignalId.size(); i++) {
243  // Checks if channel (fVetoSignalId) participated in the event. If not, it
244  // is -1
245  if (fSignalEvent->GetSignalIndex(fVetoSignalId[i]) != -1) {
246  // We extract the parameters from the veto signal
247  TRestRawSignal* sgnl = fSignalEvent->GetSignalById(fVetoSignalId[i]);
248  // Deal with noise
249  sgnl->CalculateBaseLine(fBaseLineRange.X(), fBaseLineRange.Y(), "ROBUST");
250  sgnl->InitializePointsOverThreshold(TVector2(fPointThreshold, fSignalThreshold),
251  fPointsOverThreshold);
252 
253  // Save two maps with (veto panel ID, max amplitude) and (veto panel ID,
254  // peak time)
255  if (sgnl->GetPointsOverThreshold().size() >= (unsigned int)fPointsOverThreshold) {
256  // signal is not noise
257  VetoMaxPeakAmplitude_map[fVetoSignalId[i]] = sgnl->GetMaxPeakValue();
258  } else {
259  // signal is noise
260  VetoMaxPeakAmplitude_map[fVetoSignalId[i]] = 0;
261  }
262  VetoPeakTime_map[fVetoSignalId[i]] = sgnl->GetMaxPeakBin();
263  // We remove the signal from the event
264  fSignalEvent->RemoveSignalWithId(fVetoSignalId[i]);
265 
266  // check if signal is above threshold
267  if (sgnl->GetMaxPeakValue() > fThreshold) {
268  VetoAboveThreshold = 1;
269  NVetoAboveThreshold += 1;
270  }
271  // check if signal is in time window
272  if (sgnl->GetMaxPeakBin() > fTimeWindow[0] && sgnl->GetMaxPeakBin() < fTimeWindow[1]) {
273  VetoInTimeWindow = 1;
274  NVetoInTimeWindow += 1;
275  }
276  }
277  }
278 
279  SetObservableValue("PeakTime", VetoPeakTime_map);
280  SetObservableValue("MaxPeakAmplitude", VetoMaxPeakAmplitude_map);
281  if (fThreshold != -1) {
282  SetObservableValue("VetoAboveThreshold", VetoAboveThreshold);
283  SetObservableValue("NvetoAboveThreshold", NVetoAboveThreshold);
284  }
285  if (fTimeWindow[0] != -1) {
286  SetObservableValue("VetoInTimeWindow", VetoInTimeWindow);
287  SetObservableValue("NVetoInTimeWindow", NVetoInTimeWindow);
288  }
289  }
290 
291  // ***************************************************************
292  // if the veto ids are defined within the veto groups ************
293  // ***************************************************************
294 
295  // create observable names for veto groups
296  for (unsigned int i = 0; i < fVetoGroupNames.size(); i++) {
297  fPeakTime.push_back("PeakTime_" + fVetoGroupNames[i]);
298  fPeakAmp.push_back("MaxPeakAmplitude_" + fVetoGroupNames[i]);
299  }
300 
301  if (fVetoSignalId[0] == -1) {
302  // iterate over veto groups
303  for (unsigned int i = 0; i < fVetoGroupNames.size(); i++) {
304  // iterate over vetoes in each group
305  vector<double> groupIds = StringToElements(fVetoGroupIds[i], ",");
306  for (unsigned int j = 0; j < groupIds.size(); j++) {
307  // Checks if channel (groupIds) participated in the event. If not,
308  // it is -1
309  if (fSignalEvent->GetSignalIndex(groupIds[j]) != -1) {
310  // We extract the parameters from the veto signal
311  TRestRawSignal* sgnl = fSignalEvent->GetSignalById(groupIds[j]);
312  // Deal with noise
313  sgnl->CalculateBaseLine(fBaseLineRange.X(), fBaseLineRange.Y(), "ROBUST");
314  sgnl->InitializePointsOverThreshold(TVector2(fPointThreshold, fSignalThreshold),
315  fPointsOverThreshold);
316  // Save two maps with (veto panel ID, max amplitude) and (veto panel
317  // ID, peak time)
318  if (sgnl->GetPointsOverThreshold().size() >= (unsigned int)fPointsOverThreshold) {
319  // signal is not noise
320  VetoMaxPeakAmplitude_map[groupIds[j]] = sgnl->GetMaxPeakValue();
321  } else {
322  // signal is noise
323  VetoMaxPeakAmplitude_map[groupIds[j]] = 0;
324  }
325  VetoPeakTime_map[groupIds[j]] = sgnl->GetMaxPeakBin();
326  // We remove the signal from the event
327  fSignalEvent->RemoveSignalWithId(groupIds[j]);
328 
329  // check if signal is above threshold
330  if (sgnl->GetMaxPeakValue() > fThreshold) {
331  VetoAboveThreshold = 1;
332  NVetoAboveThreshold += 1;
333  }
334  // check if signal is in time window
335  if (sgnl->GetMaxPeakBin() > fTimeWindow[0] && sgnl->GetMaxPeakBin() < fTimeWindow[1]) {
336  VetoInTimeWindow = 1;
337  NVetoInTimeWindow += 1;
338  }
339  }
340  }
341  SetObservableValue(fPeakTime[i], VetoPeakTime_map);
342  SetObservableValue(fPeakAmp[i], VetoMaxPeakAmplitude_map);
343 
344  VetoMaxPeakAmplitude_map.clear();
345  VetoPeakTime_map.clear();
346  }
347 
348  if (fThreshold != -1) {
349  SetObservableValue("VetoAboveThreshold", VetoAboveThreshold);
350  SetObservableValue("NvetoAboveThreshold", NVetoAboveThreshold);
351  }
352  if (fTimeWindow[0] != -1) {
353  SetObservableValue("VetoInTimeWindow", VetoInTimeWindow);
354  SetObservableValue("NVetoInTimeWindow", NVetoInTimeWindow);
355  }
356  }
357 
358  if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) {
359  fSignalEvent->PrintEvent();
360 
362  }
363 
364  return fSignalEvent;
365 }
366 
370  auto it = find(fVetoGroupNames.begin(), fVetoGroupNames.end(), groupName);
371  if (it != fVetoGroupNames.end()) return it - fVetoGroupNames.begin();
372  return -1;
373 }
374 
377  Int_t index = GetGroupIndex(groupName);
378  if (index != -1) return fVetoGroupIds[index];
379  return std::string("-1");
380 }
381 
387  fBaseLineRange = StringTo2DVector(GetParameter("baseLineRange", "(5,55)"));
388  fRange = StringTo2DVector(GetParameter("range", "(5,507)"));
389  fThreshold = StringToInteger(GetParameter("threshold", "-1"));
390  fTimeWindow = StringToElements(GetParameter("timeWindow", "-1,-1"), ",");
391  if (fTimeWindow.size() != 2) {
392  cout << "Error: timeWindow has to consist of two comma-separated values." << endl;
393  GetChar();
394  }
395  std::vector<double> potpars = StringToElements(GetParameter("PointsOverThresholdPars", "1.5,1.5,4"), ",");
396  fPointThreshold = potpars[0];
397  fSignalThreshold = potpars[1];
398  fPointsOverThreshold = (Int_t)potpars[2];
399 
400  // **************************************************************
401  // ***** Vetoes are defined as a single list ********************
402  // **************************************************************
403 
404  fVetoSignalId = StringToElements(GetParameter("vetoSignalId", "-1"), ",");
405 
406  // **************************************************************
407  // ***** Vetoes are defined in groups ***************************
408  // **************************************************************
409 
410  // Read all the info from the veto group definitions
411 
412  TiXmlElement* vetoDefinition = GetElement("vetoGroup");
413 
414  while (vetoDefinition != nullptr) {
415  fVetoGroupNames.push_back(GetFieldValue("name", vetoDefinition));
416  fVetoGroupIds.push_back(GetFieldValue("signalIDs", vetoDefinition));
417  vetoDefinition = GetNextElement(vetoDefinition);
418  }
419 
420  // Stop, in case signalIDs and groups are defined separately
421  if (fVetoSignalId[0] != -1 && fVetoGroupNames.size() > 0) {
422  cout << "Error: veto groups and veto IDs defined separately!" << endl;
423  GetChar();
424  }
425 }
431  BeginPrintProcess();
432 
433  // Print output metadata using, metadata << endl;
434  for (unsigned int i = 0; i < fVetoGroupNames.size(); i++) {
435  RESTMetadata << "Veto group " << fVetoGroupNames[i] << " signal IDs: " << fVetoGroupIds[i]
436  << RESTendl;
437  }
438 
439  if (fVetoSignalId[0] != -1) {
440  for (unsigned int i = 0; i < fVetoSignalId.size(); i++) {
441  RESTMetadata << "Veto signal ID: " << fVetoSignalId[i] << RESTendl;
442  }
443  } else {
444  RESTMetadata << " " << RESTendl;
445  RESTMetadata << "All veto signal IDs: ";
446  for (unsigned int i = 0; i < fVetoGroupIds.size() - 1; i++) {
447  RESTMetadata << fVetoGroupIds[i] << ",";
448  }
449  RESTMetadata << fVetoGroupIds[fVetoGroupIds.size() - 1] << RESTendl;
450  }
451  if (fThreshold != -1) {
452  RESTMetadata << "Veto threshold: " << fThreshold << RESTendl;
453  }
454  if (fTimeWindow[0] != -1) {
455  RESTMetadata << "Peak time window: (" << fTimeWindow[0] << ", " << fTimeWindow[1] << ")" << RESTendl;
456  }
457  RESTMetadata << "Noise reduction: Points over Threshold parameters = (" << fPointThreshold << ", "
458  << fSignalThreshold << ", " << fPointsOverThreshold << ")" << RESTendl;
459 
460  EndPrintProcess();
461 }
A base class for any REST event.
Definition: TRestEvent.h:38
An event container for time rawdata signals with fixed length.
It defines a Short_t array with a physical parameter that evolves in time using a fixed time bin.
Double_t GetMaxPeakValue()
It returns the amplitude of the signal maximum, baseline will be corrected if CalculateBaseLine was c...
void CalculateBaseLine(Int_t startBin, Int_t endBin, const std::string &option="")
This method calculates the average and fluctuation of the baseline in the specified range and writes ...
void InitializePointsOverThreshold(const TVector2 &thrPar, Int_t nPointsOver, Int_t nPointsFlat=512)
It initializes the fPointsOverThreshold array with the indexes of data points that are found over thr...
std::vector< Int_t > GetPointsOverThreshold() const
Returns a std::vector containing the indexes of data points over threshold.
Int_t GetMaxPeakBin()
It returns the bin at which the maximum peak amplitude happens.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
void Initialize() override
Function to initialize input/output event members and define the section name and library version.
void InitProcess() override
Function to use in initialization of process members before starting to process the event.
void InitFromConfigFile() override
Function reading input parameters from the RML TRestRawVetoAnalysisProcess section.
std::string GetGroupIds(std::string groupName)
Function that returns a string of the signal IDs for the specified veto group.
TRestRawVetoAnalysisProcess()
Default constructor.
Int_t GetGroupIndex(std::string groupName)
Function that returns the index of a specified veto group within the group name vector and ID vector.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
void PrintMetadata() override
It prints out the process parameters stored in the metadata structure.
@ REST_Debug
+show the defined debug messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
Int_t StringToInteger(std::string in)
Gets an integer from a string.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.
std::vector< double > StringToElements(std::string in, std::string separator)
Convert the input string into a vector of double elements.