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
138#include "TRestRawVetoAnalysisProcess.h"
139
140using namespace std;
141
143
148
164 Initialize();
165
166 LoadConfig(configFilename);
167}
168
173
178 SetName(this->ClassName());
179 SetTitle("Default config");
180}
181
194void TRestRawVetoAnalysisProcess::LoadConfig(const string& configFilename, const string& name) {
195 if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
196}
197
203
209 SetSectionName(this->ClassName());
210 SetLibraryVersion(LIBRARY_VERSION);
211
212 fSignalEvent = nullptr;
213}
214
219 fSignalEvent = (TRestRawSignalEvent*)inputEvent;
220
221 const auto run = GetRunInfo();
222 if (run != nullptr) {
224 }
225
226 map<int, Double_t> VetoMaxPeakAmplitude_map;
227 map<int, Double_t> VetoPeakTime_map;
228
229 Int_t VetoAboveThreshold = 0;
230 Int_t NVetoAboveThreshold = 0;
231 Int_t VetoInTimeWindow = 0;
232 Int_t NVetoInTimeWindow = 0;
233
234 fSignalEvent->SetRange(fRange);
235
236 VetoMaxPeakAmplitude_map.clear();
237 VetoPeakTime_map.clear();
238
239 // **************************************************************
240 // if list of veto Ids without groups is given ******************
241 // **************************************************************
242
243 if (fReadoutMetadata == nullptr) {
244 fReadoutMetadata = fSignalEvent->GetReadoutMetadata();
245 }
246
247 if (fReadoutMetadata != nullptr && fVetoSignalId[0] == -1) {
248 // iterate over signals and get type
249 fVetoSignalId.clear();
250 for (int i = 0; i < fSignalEvent->GetNumberOfSignals(); i++) {
251 TRestRawSignal* signal = fSignalEvent->GetSignal(i);
252 auto channelType = fReadoutMetadata->GetTypeForChannelDaqId(signal->GetID());
253 // uppercase
254 transform(channelType.begin(), channelType.end(), channelType.begin(), ::toupper);
255 if (channelType == "VETO") {
256 fVetoSignalId.push_back(signal->GetID());
257 }
258 }
259 }
260
261 if (fVetoSignalId[0] != -1) {
262 // iterate over vetoes
263 for (double i : fVetoSignalId) {
264 // Checks if channel (fVetoSignalId) participated in the event. If not, it
265 // is -1
266 if (fSignalEvent->GetSignalIndex(i) != -1) {
267 // We extract the parameters from the veto signal
268 TRestRawSignal* signal = fSignalEvent->GetSignalById(i);
269 // Deal with noise
270 signal->CalculateBaseLine(fBaseLineRange.X(), fBaseLineRange.Y(), "ROBUST");
271 signal->InitializePointsOverThreshold(TVector2(fPointThreshold, fSignalThreshold),
272 fPointsOverThreshold);
273
274 // Save two maps with (veto panel ID, max amplitude) and (veto panel ID, peak time)
275 if (signal->GetPointsOverThreshold().size() >= (unsigned int)fPointsOverThreshold) {
276 // signal is not noise
277 VetoMaxPeakAmplitude_map[i] = signal->GetMaxPeakValue();
278 } else {
279 // signal is noise
280 VetoMaxPeakAmplitude_map[i] = 0;
281 }
282 VetoPeakTime_map[i] = signal->GetMaxPeakBin();
283 // We remove the signal from the event
284 fSignalEvent->RemoveSignalWithId(i);
285
286 // check if signal is above threshold
287 if (signal->GetMaxPeakValue() > fThreshold) {
288 VetoAboveThreshold = 1;
289 NVetoAboveThreshold += 1;
290 }
291 // check if signal is in time window
292 if (signal->GetMaxPeakBin() > fTimeWindow[0] && signal->GetMaxPeakBin() < fTimeWindow[1]) {
293 VetoInTimeWindow = 1;
294 NVetoInTimeWindow += 1;
295 }
296 }
297 }
298
299 SetObservableValue("PeakTime", VetoPeakTime_map);
300 SetObservableValue("MaxPeakAmplitude", VetoMaxPeakAmplitude_map);
301 if (fThreshold != -1) {
302 SetObservableValue("VetoAboveThreshold", VetoAboveThreshold);
303 SetObservableValue("NvetoAboveThreshold", NVetoAboveThreshold);
304 }
305 if (fTimeWindow[0] != -1) {
306 SetObservableValue("VetoInTimeWindow", VetoInTimeWindow);
307 SetObservableValue("NVetoInTimeWindow", NVetoInTimeWindow);
308 }
309 }
310
311 // ***************************************************************
312 // if the veto ids are defined within the veto groups ************
313 // ***************************************************************
314
315 // create observable names for veto groups
316 for (const auto& fVetoGroupName : fVetoGroupNames) {
317 fPeakTime.push_back("PeakTime_" + fVetoGroupName);
318 fPeakAmp.push_back("MaxPeakAmplitude_" + fVetoGroupName);
319 }
320
321 if (fVetoSignalId[0] == -1) {
322 // iterate over veto groups
323 for (unsigned int i = 0; i < fVetoGroupNames.size(); i++) {
324 // iterate over vetoes in each group
325 vector<double> groupIds = StringToElements(fVetoGroupIds[i], ",");
326 for (double groupId : groupIds) {
327 // Checks if channel (groupIds) participated in the event. If not, it is -1
328 if (fSignalEvent->GetSignalIndex(groupId) != -1) {
329 // We extract the parameters from the veto signal
330 TRestRawSignal* signal = fSignalEvent->GetSignalById(groupId);
331 // Deal with noise
332 signal->CalculateBaseLine(fBaseLineRange.X(), fBaseLineRange.Y(), "ROBUST");
333 signal->InitializePointsOverThreshold(TVector2(fPointThreshold, fSignalThreshold),
334 fPointsOverThreshold);
335 // Save two maps with (veto panel ID, max amplitude) and (veto panel ID, peak time)
336 if (signal->GetPointsOverThreshold().size() >= (unsigned int)fPointsOverThreshold) {
337 // signal is not noise
338 VetoMaxPeakAmplitude_map[groupId] = signal->GetMaxPeakValue();
339 } else {
340 // signal is noise
341 VetoMaxPeakAmplitude_map[groupId] = 0;
342 }
343 VetoPeakTime_map[groupId] = signal->GetMaxPeakBin();
344 // We remove the signal from the event
345 fSignalEvent->RemoveSignalWithId(groupId);
346
347 // check if signal is above threshold
348 if (signal->GetMaxPeakValue() > fThreshold) {
349 VetoAboveThreshold = 1;
350 NVetoAboveThreshold += 1;
351 }
352 // check if signal is in time window
353 if (signal->GetMaxPeakBin() > fTimeWindow[0] &&
354 signal->GetMaxPeakBin() < fTimeWindow[1]) {
355 VetoInTimeWindow = 1;
356 NVetoInTimeWindow += 1;
357 }
358 }
359 }
360 SetObservableValue(fPeakTime[i], VetoPeakTime_map);
361 SetObservableValue(fPeakAmp[i], VetoMaxPeakAmplitude_map);
362
363 VetoMaxPeakAmplitude_map.clear();
364 VetoPeakTime_map.clear();
365 }
366
367 if (fThreshold != -1) {
368 SetObservableValue("VetoAboveThreshold", VetoAboveThreshold);
369 SetObservableValue("NvetoAboveThreshold", NVetoAboveThreshold);
370 }
371 if (fTimeWindow[0] != -1) {
372 SetObservableValue("VetoInTimeWindow", VetoInTimeWindow);
373 SetObservableValue("NVetoInTimeWindow", NVetoInTimeWindow);
374 }
375 }
376
378 fSignalEvent->PrintEvent();
379
381 }
382
383 return fSignalEvent;
384}
385
388Int_t TRestRawVetoAnalysisProcess::GetGroupIndex(const string& groupName) {
389 auto it = find(fVetoGroupNames.begin(), fVetoGroupNames.end(), groupName);
390 if (it != fVetoGroupNames.end()) {
391 return it - fVetoGroupNames.begin();
392 }
393 return -1;
394}
395
397string TRestRawVetoAnalysisProcess::GetGroupIds(const string& groupName) {
398 Int_t index = GetGroupIndex(groupName);
399 if (index != -1) {
400 return fVetoGroupIds[index];
401 }
402 return "-1";
403}
404
410 fBaseLineRange = StringTo2DVector(GetParameter("baseLineRange", "(5,55)"));
411 fRange = StringTo2DVector(GetParameter("range", "(5,507)"));
412 fThreshold = StringToInteger(GetParameter("threshold", "-1"));
413 fTimeWindow = StringToElements(GetParameter("timeWindow", "-1,-1"), ",");
414 if (fTimeWindow.size() != 2) {
415 cout << "Error: timeWindow has to consist of two comma-separated values." << endl;
416 GetChar();
417 }
418 std::vector<double> potpars = StringToElements(GetParameter("PointsOverThresholdPars", "1.5,1.5,4"), ",");
419 fPointThreshold = potpars[0];
420 fSignalThreshold = potpars[1];
421 fPointsOverThreshold = (Int_t)potpars[2];
422
423 // **************************************************************
424 // ***** Vetoes are defined as a single list ********************
425 // **************************************************************
426
427 fVetoSignalId = StringToElements(GetParameter("vetoSignalId", "-1"), ",");
428
429 // **************************************************************
430 // ***** Vetoes are defined in groups ***************************
431 // **************************************************************
432
433 // Read all the info from the veto group definitions
434
435 TiXmlElement* vetoDefinition = GetElement("vetoGroup");
436
437 while (vetoDefinition != nullptr) {
438 fVetoGroupNames.push_back(GetFieldValue("name", vetoDefinition));
439 fVetoGroupIds.push_back(GetFieldValue("signalIDs", vetoDefinition));
440 vetoDefinition = GetNextElement(vetoDefinition);
441 }
442
443 // Stop, in case signalIDs and groups are defined separately
444 if (fVetoSignalId[0] != -1 && !fVetoGroupNames.empty()) {
445 cout << "Error: veto groups and veto IDs defined separately!" << endl;
446 GetChar();
447 }
448}
449
456
457 if ((fVetoSignalId.empty() || fVetoSignalId[0] != -1) && fVetoGroupNames.empty()) {
458 RESTMetadata
459 << "Veto analysis process will apply to veto signals (to be determined during processing)"
460 << RESTendl;
461 return;
462 }
463
464 for (unsigned int i = 0; i < fVetoGroupNames.size(); i++) {
465 RESTMetadata << "Veto group " << fVetoGroupNames[i] << " signal IDs: " << fVetoGroupIds[i]
466 << RESTendl;
467 }
468
469 if (fVetoSignalId[0] != -1) {
470 for (double i : fVetoSignalId) {
471 RESTMetadata << "Veto signal ID: " << i << RESTendl;
472 }
473 } else {
474 RESTMetadata << " " << RESTendl;
475 RESTMetadata << "All veto signal IDs: ";
476 for (unsigned int i = 0; i < fVetoGroupIds.size() - 1; i++) {
477 RESTMetadata << fVetoGroupIds[i] << ",";
478 }
479 RESTMetadata << fVetoGroupIds[fVetoGroupIds.size() - 1] << RESTendl;
480 }
481 if (fThreshold != -1) {
482 RESTMetadata << "Veto threshold: " << fThreshold << RESTendl;
483 }
484 if (fTimeWindow[0] != -1) {
485 RESTMetadata << "Peak time window: (" << fTimeWindow[0] << ", " << fTimeWindow[1] << ")" << RESTendl;
486 }
487 RESTMetadata << "Noise reduction: Points over Threshold parameters = (" << fPointThreshold << ", "
488 << fSignalThreshold << ", " << fPointsOverThreshold << ")" << RESTendl;
489
490 EndPrintProcess();
491}
TRestRun * GetRunInfo() const
Return the pointer of the hosting TRestRun object.
void BeginPrintProcess()
[name, cut range]
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
A base class for any REST event.
Definition: TRestEvent.h:38
virtual void InitializeReferences(TRestRun *run)
Initialize dynamical references when loading the event from a root file.
Definition: TRestEvent.cxx:175
endl_t RESTendl
Termination flag object for TRestStringOutput.
TiXmlElement * GetElement(std::string eleDeclare, TiXmlElement *e=nullptr)
Get an xml element from a given parent element, according to its declaration.
Int_t LoadConfigFromFile(const std::string &configFilename, const std::string &sectionName="")
Give the file name, find out the corresponding section. Then call the main starter.
void SetLibraryVersion(TString version)
Set the library version of this metadata class.
TRestStringOutput::REST_Verbose_Level GetVerboseLevel()
returns the verboselevel in type of REST_Verbose_Level enumerator
std::string GetFieldValue(std::string parName, TiXmlElement *e)
Returns the field value of an xml element which has the specified name.
void SetSectionName(std::string sName)
set the section name, clear the section content
TiXmlElement * GetNextElement(TiXmlElement *e)
Get the next sibling xml element of this element, with same eleDeclare.
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.
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.
Int_t GetID() const
Returns the value of signal ID.
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 ...
std::vector< Int_t > GetPointsOverThreshold() const
Returns a std::vector containing the indexes of data points over threshold.
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...
Int_t GetMaxPeakBin()
It returns the bin at which the maximum peak amplitude happens.
Int_t GetGroupIndex(const std::string &groupName)
Function that returns the index of a specified veto group within the group name vector and ID vector.
std::vector< double > fVetoSignalId
Veto signal IDs.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
std::vector< std::string > fPeakTime
Peak Time observable names.
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.
Int_t fThreshold
Threshold of the vetoes.
Double_t fPointThreshold
PointsOverThreshold() Parameters:
void InitProcess() override
Function to use in initialization of process members before starting to process the event.
TRestRawSignalEvent * fSignalEvent
A pointer to the specific TRestRawSignalEvent.
std::vector< std::string > fPeakAmp
Max peak amplitude observable names.
TVector2 fRange
The range used to calculate the veto signal parameters.
std::vector< std::string > fVetoGroupIds
Veto signal IDs per group.
void InitFromConfigFile() override
Function reading input parameters from the RML TRestRawVetoAnalysisProcess section.
TRestRawVetoAnalysisProcess()
Default constructor.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
TVector2 fBaseLineRange
The range used to calculate the baseline parameters from the veto signal.
void PrintMetadata() override
It prints out the process parameters stored in the metadata structure.
std::vector< std::string > fVetoGroupNames
Veto group Names.
std::vector< double > fTimeWindow
Peak time window for cut.
std::string GetGroupIds(const std::string &groupName)
Function that returns a string of the signal IDs for the specified veto group.
@ 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.