REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestDetectorSignalEvent.cxx
1 
21 
22 #include "TRestDetectorSignalEvent.h"
23 
24 #include <TMath.h>
25 
26 using namespace std;
27 
28 ClassImp(TRestDetectorSignalEvent);
29 
30 TRestDetectorSignalEvent::TRestDetectorSignalEvent() {
31  // TRestDetectorSignalEvent default constructor
32  Initialize();
33 }
34 
35 TRestDetectorSignalEvent::~TRestDetectorSignalEvent() {
36  // TRestDetectorSignalEvent destructor
37 }
38 
41  fSignal.clear();
42  fPad = nullptr;
43  fMinValue = 1E10;
44  fMaxValue = -1E10;
45  fMinTime = 1E10;
46  fMaxTime = -1E10;
47 }
48 
49 void TRestDetectorSignalEvent::AddSignal(const TRestDetectorSignal& signal) {
50  if (signalIDExists(signal.GetSignalID())) {
51  cout << "Warning. Signal ID : " << signal.GetSignalID()
52  << " already exists. Signal will not be added to signal event" << endl;
53  return;
54  }
55 
56  fSignal.emplace_back(signal);
57 }
58 
59 void TRestDetectorSignalEvent::RemoveSignalWithId(Int_t sId) {
60  Int_t index = GetSignalIndex(sId);
61 
62  if (index == -1) {
63  std::cout << "Warning. Signal ID : " << sId
64  << " does not exist. Signal will not be removed from signal event" << std::endl;
65  return;
66  }
67 
68  fSignal.erase(fSignal.begin() + index);
69 }
70 
71 Int_t TRestDetectorSignalEvent::GetSignalIndex(Int_t signalID) {
72  for (int i = 0; i < GetNumberOfSignals(); i++)
73  if (fSignal[i].GetSignalID() == signalID) return i;
74  return -1;
75 }
76 
77 Double_t TRestDetectorSignalEvent::GetIntegral(Int_t startBin, Int_t endBin) {
78  Double_t sum = 0;
79 
80  for (int i = 0; i < GetNumberOfSignals(); i++) sum += fSignal[i].GetIntegral(startBin, endBin);
81 
82  return sum;
83 }
84 
85 Double_t TRestDetectorSignalEvent::GetIntegralWithTime(Double_t startTime, Double_t endTime) {
86  Double_t sum = 0;
87  for (int n = 0; n < GetNumberOfSignals(); n++) sum += fSignal[n].GetIntegralWithTime(startTime, endTime);
88 
89  return sum;
90 }
91 
92 void TRestDetectorSignalEvent::AddChargeToSignal(Int_t signalID, Double_t time, Double_t charge) {
93  Int_t signalIndex = GetSignalIndex(signalID);
94  if (signalIndex == -1) {
95  signalIndex = GetNumberOfSignals();
96  TRestDetectorSignal signal;
97  signal.SetSignalID(signalID);
98  AddSignal(signal);
99  }
100 
101  fSignal[signalIndex].IncreaseAmplitude(time, charge);
102 }
103 
104 void TRestDetectorSignalEvent::PrintEvent() {
106 
107  for (int i = 0; i < GetNumberOfSignals(); i++) {
108  const auto& signal = fSignal[i];
109  signal.Print();
110  }
111 }
112 
113 // TODO: GetMaxTimeFast, GetMinTimeFast, GetMaxValueFast that return the value of fMinTime, fMaxTime, etc.
114 void TRestDetectorSignalEvent::SetMaxAndMin() {
115  fMinValue = 1E10;
116  fMaxValue = -1E10;
117  fMinTime = 1E10;
118  fMaxTime = -1E10;
119 
120  for (int s = 0; s < GetNumberOfSignals(); s++) {
121  if (fMinTime > fSignal[s].GetMinTime()) fMinTime = fSignal[s].GetMinTime();
122  if (fMaxTime < fSignal[s].GetMaxTime()) fMaxTime = fSignal[s].GetMaxTime();
123 
124  if (fMinValue > fSignal[s].GetMinValue()) fMinValue = fSignal[s].GetMinValue();
125  if (fMaxValue < fSignal[s].GetMaxValue()) fMaxValue = fSignal[s].GetMaxValue();
126  }
127 }
128 
129 Double_t TRestDetectorSignalEvent::GetMaxValue() {
130  SetMaxAndMin();
131  return fMaxValue;
132 }
133 
134 Double_t TRestDetectorSignalEvent::GetMinValue() {
135  SetMaxAndMin();
136  return fMinValue;
137 }
138 
139 Double_t TRestDetectorSignalEvent::GetMinTime() {
140  Double_t minTime = 1.e10;
141  for (int s = 0; s < GetNumberOfSignals(); s++)
142  if (minTime > fSignal[s].GetMinTime()) {
143  minTime = fSignal[s].GetMinTime();
144  }
145  return minTime;
146 }
147 
148 Double_t TRestDetectorSignalEvent::GetMaxTime() {
149  Double_t maxTime = -1.e10;
150  for (int s = 0; s < GetNumberOfSignals(); s++) {
151  if (maxTime < fSignal[s].GetMaxTime()) {
152  maxTime = fSignal[s].GetMaxTime();
153  }
154  }
155  return maxTime;
156 }
157 
158 // Draw current event in a TPad
159 TPad* TRestDetectorSignalEvent::DrawEvent(const TString& option) {
160  delete fPad;
161 
162  int nSignals = this->GetNumberOfSignals();
163 
164  if (nSignals == 0) {
165  cout << "Empty event " << endl;
166  return nullptr;
167  }
168 
169  fMinValue = 1E10;
170  fMaxValue = -1E10;
171  fMinTime = 1E10;
172  fMaxTime = -1E10;
173 
174  fPad = new TPad(this->GetName(), " ", 0, 0, 1, 1);
175  fPad->Draw();
176  fPad->cd();
177  fPad->DrawFrame(GetMinTime(), GetMinValue(), GetMaxTime(), GetMaxValue());
178 
179  char title[256];
180  sprintf(title, "Event ID %d", this->GetID());
181 
182  TMultiGraph* mg = new TMultiGraph();
183  mg->SetTitle(title);
184  mg->GetXaxis()->SetTitle("Time [us]");
185  mg->GetXaxis()->SetTitleOffset(1.1);
186  mg->GetYaxis()->SetTitle("Amplitude [a.u.]");
187  mg->GetYaxis()->SetTitleOffset(0.8);
188 
189  mg->GetYaxis()->SetTitleSize(1.4 * mg->GetYaxis()->GetTitleSize());
190  mg->GetXaxis()->SetTitleSize(1.4 * mg->GetXaxis()->GetTitleSize());
191  mg->GetYaxis()->SetLabelSize(1.25 * mg->GetYaxis()->GetLabelSize());
192  mg->GetXaxis()->SetLabelSize(1.25 * mg->GetXaxis()->GetLabelSize());
193 
194  for (int n = 0; n < nSignals; n++) {
195  TGraph* gr = fSignal[n].GetGraph(n + 1);
196  mg->Add(gr);
197  }
198 
199  fPad->cd();
200  mg->Draw("");
201 
202  return fPad;
203 }
TPad * DrawEvent(const TString &option="")
Draw the event.
virtual void PrintEvent() const
Definition: TRestEvent.cxx:187
virtual void Initialize()=0
Definition: TRestEvent.cxx:73