REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestDetectorSignalRecoveryProcess.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 
52 //
94 #include "TRestDetectorSignalRecoveryProcess.h"
95 
96 using namespace std;
97 
99 
104 
118  Initialize();
119 
120  if (LoadConfigFromFile(configFilename) == -1) LoadDefaultConfig();
121 
122  PrintMetadata();
123 }
124 
129 
134  SetName("removeChannels-Default");
135  SetTitle("Default config");
136 }
137 
143  SetSectionName(this->ClassName());
144  SetLibraryVersion(LIBRARY_VERSION);
145 
146  fInputSignalEvent = nullptr;
147  fOutputSignalEvent = new TRestDetectorSignalEvent();
148 }
149 
162 void TRestDetectorSignalRecoveryProcess::LoadConfig(const string& configFilename, const string& name) {
163  if (LoadConfigFromFile(configFilename, name) == -1) LoadDefaultConfig();
164 }
165 
172  fReadout = GetMetadata<TRestDetectorReadout>();
173 
174  if (fReadout == nullptr) {
175  RESTError << "TRestDetectorSignalRecoveryProcess. Readout has not been initialized!" << RESTendl;
176  exit(-1);
177  }
178 }
179 
184  fInputSignalEvent = (TRestDetectorSignalEvent*)evInput;
185 
186  for (int n = 0; n < fInputSignalEvent->GetNumberOfSignals(); n++)
187  fOutputSignalEvent->AddSignal(*fInputSignalEvent->GetSignal(n));
188 
189  Int_t idL;
190  Int_t idR;
191  for (unsigned int x = 0; x < fChannelIds.size(); x++) {
192  Int_t type = GetAdjacentSignalIds(fChannelIds[x], idL, idR);
193  RESTDebug << "Channel id : " << fChannelIds[x] << " Left : " << idL << " Right : " << idR << RESTendl;
194 
195  if (idL == -1 || idR == -1) continue;
196 
197  TRestDetectorSignal* leftSgnl = fInputSignalEvent->GetSignalById(idL);
198  TRestDetectorSignal* rightSgnl = fInputSignalEvent->GetSignalById(idR);
199 
204 
205  if (leftSgnl == nullptr || rightSgnl == nullptr) continue;
206 
207  TRestDetectorSignal recoveredSignal;
208  recoveredSignal.SetID(fChannelIds[x]);
209 
210  if (type == 1) // Only one dead channel
211  {
212  for (int n = 0; n < leftSgnl->GetNumberOfPoints(); n++) {
213  recoveredSignal.IncreaseAmplitude(leftSgnl->GetTime(n), leftSgnl->GetData(n) / 2.);
215  leftSgnl->IncreaseAmplitude(leftSgnl->GetTime(n), -1. * leftSgnl->GetData(n) / 2.);
216  }
217 
218  for (int n = 0; n < rightSgnl->GetNumberOfPoints(); n++) {
219  recoveredSignal.IncreaseAmplitude(rightSgnl->GetTime(n), rightSgnl->GetData(n) / 2.);
221  rightSgnl->IncreaseAmplitude(rightSgnl->GetTime(n), -1. * rightSgnl->GetData(n) / 2.);
222  }
223  } else if (type == 2 || type == 3) { // We got two dead-channels
224  if (type == 2) // The other dead channel is the one at the left
225  {
226  for (int n = 0; n < leftSgnl->GetNumberOfPoints(); n++)
227  recoveredSignal.IncreaseAmplitude(leftSgnl->GetTime(n), leftSgnl->GetData(n) / 6.);
228 
229  for (int n = 0; n < rightSgnl->GetNumberOfPoints(); n++)
230  recoveredSignal.IncreaseAmplitude(rightSgnl->GetTime(n), 2 * rightSgnl->GetData(n) / 6.);
231  }
232 
233  if (type == 3) // The other dead channel is the one at the right
234  {
235  for (int n = 0; n < leftSgnl->GetNumberOfPoints(); n++)
236  recoveredSignal.IncreaseAmplitude(leftSgnl->GetTime(n), 2 * leftSgnl->GetData(n) / 6.);
237 
238  for (int n = 0; n < rightSgnl->GetNumberOfPoints(); n++)
239  recoveredSignal.IncreaseAmplitude(rightSgnl->GetTime(n), rightSgnl->GetData(n) / 6.);
240  }
241 
244  for (int n = 0; n < leftSgnl->GetNumberOfPoints(); n++)
245  leftSgnl->IncreaseAmplitude(leftSgnl->GetTime(n), -1. * leftSgnl->GetData(n) / 4.);
246  for (int n = 0; n < rightSgnl->GetNumberOfPoints(); n++)
247  rightSgnl->IncreaseAmplitude(rightSgnl->GetTime(n), -1. * rightSgnl->GetData(n) / 4.);
248  } else {
249  RESTWarning << "Adjacent channels for signal " << fChannelIds[x] << " not found " << RESTendl;
250  continue;
251  }
252 
253  if (fOutputSignalEvent->GetSignalIndex(fChannelIds[x]) > 0)
254  fOutputSignalEvent->RemoveSignalWithId(fChannelIds[x]);
255 
256  fOutputSignalEvent->AddSignal(recoveredSignal);
257 
258  /*cout << "Channel recovered!! " << endl;
259  if( leftSgnl != nullptr && rightSgnl != nullptr )
260  for( int n = 0; n < nPoints; n++ )
261  cout << "Sample " << n << " : " << leftSgnl->GetData(n) << " + " << rightSgnl->GetData(n) << "
262  = " << recoveredSignal.GetData(n) << endl;*/
263 
264  RESTDebug << "Channel recovered!! " << RESTendl;
265  if (leftSgnl != nullptr && rightSgnl != nullptr)
266  for (int n = 0; n < leftSgnl->GetNumberOfPoints(); n++)
267  RESTDebug << "Sample " << n << " : " << leftSgnl->GetData(n) << " + " << rightSgnl->GetData(n)
268  << " = " << recoveredSignal.GetData(n) << RESTendl;
269  }
270 
271  RESTDebug << "Channels after : " << fOutputSignalEvent->GetNumberOfSignals() << RESTendl;
272 
273  return fOutputSignalEvent;
274 }
275 
287 int TRestDetectorSignalRecoveryProcess::GetAdjacentSignalIds(Int_t signalId, Int_t& idLeft, Int_t& idRight) {
288  idLeft = -1;
289  idRight = -1;
290 
291  for (int p = 0; p < fReadout->GetNumberOfReadoutPlanes(); p++) {
292  TRestDetectorReadoutPlane* plane = fReadout->GetReadoutPlane(p);
293  for (size_t m = 0; m < plane->GetNumberOfModules(); m++) {
294  TRestDetectorReadoutModule* mod = plane->GetModule(m);
295  // We iterate over all readout modules searching for the one that contains
296  // our signal id
297  if (mod->IsDaqIDInside(signalId)) {
298  // If we find it we use the readoutModule id, and the signalId
299  // corresponding to the physical readout channel
300  Int_t readoutChannelID = mod->DaqToReadoutChannel(signalId);
301 
302  idLeft = mod->GetChannel(readoutChannelID - 1)->GetDaqID();
303  idRight = mod->GetChannel(readoutChannelID + 1)->GetDaqID();
304 
305  // If idLeft is a dead channel we take the previous channel
306  if (std::find(fChannelIds.begin(), fChannelIds.end(), idLeft) != fChannelIds.end()) {
307  idLeft = mod->GetChannel(readoutChannelID - 2)->GetDaqID();
308  return 2;
309  }
310 
311  // If idRight is a dead channel we take the next channel
312  if (std::find(fChannelIds.begin(), fChannelIds.end(), idRight) != fChannelIds.end()) {
313  idRight = mod->GetChannel(readoutChannelID + 2)->GetDaqID();
314  return 3;
315  }
316  return 1;
317  }
318  }
319  }
320  return 0;
321 }
Int_t GetDaqID() const
Returns the corresponding daq channel id.
Bool_t IsDaqIDInside(Int_t daqID)
Determines if a given daqID number is in the range of the module.
TRestDetectorReadoutChannel * GetChannel(size_t n)
Returns a pointer to a readout channel by index.
Int_t DaqToReadoutChannel(Int_t daqChannel)
Returns the physical readout channel index for a given daq id channel number.
size_t GetNumberOfModules() const
Returns the total number of modules in the readout plane.
TRestDetectorReadoutModule * GetModule(size_t mod)
Returns a pointer to a readout module using its std::vector index.
A process allowing to recover selected channels from a TRestRawSignalEvent.
void InitProcess() override
Function to initialize the process. TRestDetectorSignalRecoveryProcess requires to get a pointer to T...
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
int GetAdjacentSignalIds(Int_t signalId, Int_t &idLeft, Int_t &idRight)
It returns the channel daq id of the adjacent readout channels. It will properly identify that we got...
void Initialize() override
Function to initialize input/output event members and define the section name.
TRestEvent * ProcessEvent(TRestEvent *eventInput) 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 IncreaseAmplitude(TVector2 p)
If the point already exists inside the detector signal event, the amplitude value will be added to th...
A base class for any REST event.
Definition: TRestEvent.h:38