ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtPulse.cxx
Go to the documentation of this file.
1 #include "AtPulse.h"
2 // IWYU pragma: no_include <ext/alloc_traits.h>
3 
4 #include "AtContainerManip.h"
5 #include "AtDigiPar.h"
6 #include "AtElectronicResponse.h"
7 #include "AtMap.h" // for AtMap, AtMap::InhibitType, AtMap::...
8 #include "AtPad.h"
9 #include "AtPadArray.h"
10 #include "AtPadBase.h" // for AtPadBase
11 #include "AtRawEvent.h"
12 #include "AtSimulatedPoint.h"
13 
14 #include <FairLogger.h> // for Logger, LOG
15 
16 #include <Math/Point2D.h>
17 #include <Math/Point2Dfwd.h> // for XYPoint
18 #include <Rtypes.h> // for Int_t
19 #include <TAxis.h>
20 #include <TMath.h> // for Gamma, Sqrt
21 #include <TRandom.h>
22 #include <TString.h> // for TString
23 
24 #include <utility> // for move
25 
27 AtPulse::AtPulse(AtMapPtr map, ResponseFunc response) : fMap(map), fResponse(response) // NOLINT
28 {
29  // Make sure the pad plane is generated so we can just access it for reading info (ie multiple threads will not be
30  // trying to create the underlying TH2poly.
31  fMap->GeneratePadPlane();
32 }
33 
35  : fMap(other.fMap), fEventID(other.fEventID), fGain(other.fGain), fLowGainFactor(other.fLowGainFactor),
36  fGETGain(other.fGETGain), fPeakingTime(other.fPeakingTime), fTBTime(other.fTBTime), fNumTbs(other.fNumTbs),
37  fTBEntrance(other.fTBEntrance), fTBPadPlane(other.fTBPadPlane), fResponse(other.fResponse),
38  fUseFastGain(other.fUseFastGain), fNoiseSigma(other.fNoiseSigma), fSaveCharge(other.fSaveCharge),
39  fDoConvolution(other.fDoConvolution), fAvgGainDeviation(other.fAvgGainDeviation)
40 {
41 
42  // For reasons unknown, copying the historgam from other (calling copy constructor) causes a huge performance hit.
43  // Recreating from scratch does not.
44  fPadCharge.resize(fMap->GetNumPads());
45  for (Int_t padS = 0; padS < fMap->GetNumPads(); padS++) {
46  auto maxTime = fTBTime * fNumTbs; // maxTime in ns
47  fPadCharge[padS] =
48  std::make_unique<TH1F>(TString::Format("%d", padS), TString::Format("%d", padS), fNumTbs, 0, maxTime);
49  fPadCharge[padS]->SetDirectory(nullptr);
50  }
51 
53  fGainFunc = (other.fGainFunc) ? std::make_unique<TF1>(*other.fGainFunc) : nullptr;
54 }
55 
56 AtRawEvent AtPulse::GenerateEvent(std::vector<SimPointPtr> &vec)
57 {
58  auto vecPtr = ContainerManip::GetPointerVector(vec);
59  return GenerateEvent(vecPtr);
60 }
61 
62 AtRawEvent AtPulse::GenerateEvent(std::vector<AtSimulatedPoint *> &vec)
63 {
64  // Reset the list of historgrams with charge info.
65  Reset();
66 
67  // Fill the charge histogram and apply the
68  int numFilled = 0;
69  for (auto &point : vec)
70  numFilled += AssignElectronsToPad(point);
71  LOG(info) << "Skipped " << (double)(vec.size() - numFilled) / vec.size() * 100 << "% of " << vec.size()
72  << " points.";
73 
74  AtRawEvent ret;
75  for (auto padNum : fPadsWithCharge) {
76  AtPad *pad = ret.AddPad(padNum);
77  FillPad(*pad, *fPadCharge[padNum]);
78  }
79  return ret;
80 }
81 
82 void AtPulse::FillPad(AtPad &pad, TH1F &hist)
83 {
84  auto axis = hist.GetXaxis();
85  double binWidth = axis->GetBinWidth(10);
86  auto charge = std::make_unique<AtPadArray>();
87 
88  for (int kk = 1; kk <= fNumTbs; ++kk) {
89  double nEle = hist.GetBinContent(kk);
90  if (nEle > 0) {
91  // Scale the saved charge down so its closer to reco
92  charge->SetArray(kk - 1, nEle * fGETGain * fResponse(pad.GetPadNum(), fPeakingTime));
93  // charge->SetArray(kk - 1, nEle);
94  if (!fDoConvolution) {
95  pad.SetADC(kk - 1, 0);
96  continue;
97  }
98 
99  // Do the convolution
100  for (int nn = kk - 1; nn < fNumTbs; ++nn) {
101  double binCenter = axis->GetBinCenter(kk);
102  double time = ((double)nn + 0.5) * binWidth - binCenter;
103  double newADC = pad.GetADC(nn) + nEle * fResponse(pad.GetPadNum(), time);
104  pad.SetADC(nn, newADC);
105  }
106  }
107  }
108 
109  pad.SetValidPad(true);
110  pad.SetPadCoord(fMap->CalcPadCenter(pad.GetPadNum()));
111  pad.SetPedestalSubtracted(true);
112  if (fSaveCharge)
113  pad.AddAugment("Q", std::move(charge));
114 
115  ApplyNoise(pad);
116 }
117 
119 {
120  for (int i = 0; i < fNumTbs; ++i) {
121  pad.SetADC(i, pad.GetADC(i) * fGETGain);
122  if (fNoiseSigma != 0)
123  pad.SetADC(i, pad.GetADC(i) * gRandom->Gaus(0, fNoiseSigma));
124  }
125 }
126 
128 {
129  for (auto padNum : fPadsWithCharge)
130  fPadCharge[padNum]->Reset();
131  fPadsWithCharge.clear();
132 }
134 {
135 
136  fGain = fPar->GetGain();
137  fGETGain = fPar->GetGETGain(); // Get the electronics gain in fC
138  fGETGain = 1.602e-19 * 4096 / (fGETGain * 1e-15); // Scale to gain and correct for ADC
139  fPeakingTime = fPar->GetPeakingTime() / 1000.; // in us
140  fTBTime = fPar->GetTBTime() / 1000.; // in us
141 
142  fTBEntrance = fPar->GetTBEntrance();
143  fTBPadPlane = fTBEntrance - fPar->GetZPadPlane() / 10. / fTBTime / fPar->GetDriftVelocity();
144 
145  fGainFunc = std::make_unique<TF1>(
146  "gain", "pow([1]+1,[1]+1)/ROOT::Math::tgamma([1]+1)*pow((x/[0]),[1])*exp(-([1]+1)*(x/[0]))", 0,
147  fGain * 5); // Polya distribution of gain
148  fGainFunc->SetParameter(0, fGain);
149  fGainFunc->SetParameter(1, 1);
150 
151  auto b = fGainFunc->GetParameter(1);
152  fAvgGainDeviation = fGain / (b + 1);
154  TMath::Sqrt(TMath::Gamma(b + 3) / TMath::Gamma(b + 1) -
155  TMath::Gamma(b + 2) * TMath::Gamma(b + 2) / TMath::Gamma(b + 1) / TMath::Gamma(b + 1));
156 
157  LOG(info) << "Gain: " << fGain;
158  LOG(info) << "GETGain: " << fGETGain;
159  LOG(info) << "Peaking time: " << fPeakingTime;
160  LOG(info) << "TB Time: " << fTBTime;
161  LOG(info) << "TB entrance: " << fTBEntrance;
162  LOG(info) << "TB Pad Plane: " << fTBPadPlane;
163 
164  // Create all of the historgrmas
165  fPadCharge.resize(fMap->GetNumPads());
166  for (Int_t padS = 0; padS < fMap->GetNumPads(); padS++) {
167  auto maxTime = fTBTime * fNumTbs; // maxTime in ns
168  fPadCharge[padS] =
169  std::make_unique<TH1F>(TString::Format("%d", padS), TString::Format("%d", padS), fNumTbs, 0, maxTime);
170  fPadCharge[padS]->SetDirectory(nullptr);
171  }
172 
173  // If there is not a response function create a default one
174  if (fResponse == nullptr)
176 }
177 
183 {
184  if (point == nullptr)
185  return false;
186 
187  auto coord = point->GetPosition();
188  auto eTime = coord.z() + fTBPadPlane * fTBTime; // us
189  auto charge = point->GetCharge(); // number of electrons
190  auto pos = XYPoint(coord.X(), coord.Y());
191 
192  int padNum = fMap->GetPadNum(pos);
193  if (padNum < 0 || padNum >= fMap->GetNumPads())
194  return false;
195 
196  double gain = GetGain(padNum, charge);
197  if (gain == 0)
198  return false;
199 
200  fPadCharge[padNum]->Fill(eTime, gain * charge);
201  fPadsWithCharge.insert(padNum);
202  return true;
203 }
204 
205 double AtPulse::GetGain(int padNum, int numElectrons)
206 {
207  if (fMap->IsInhibited(padNum) == AtMap::InhibitType::kTotal)
208  return 0;
209  if (numElectrons <= 0)
210  return 0;
211 
212  double lowGain = 1;
213  if (fMap->IsInhibited(padNum) == AtMap::InhibitType::kLowGain)
214  lowGain = fLowGainFactor;
215 
216  if (fUseFastGain && numElectrons > 10)
217  return gRandom->Gaus(fGain, fAvgGainDeviation / TMath::Sqrt(numElectrons)) * lowGain;
218 
219  double g = 0;
220  for (Int_t i = 0; i < numElectrons; i++)
221  g += fGainFunc->GetRandom();
222  return g / numElectrons * lowGain;
223 }
AtPad.h
AtRawEvent.h
AtDigiPar::GetZPadPlane
Double_t GetZPadPlane() const
Definition: AtDigiPar.h:50
AtPulse::AssignElectronsToPad
virtual bool AssignElectronsToPad(AtSimulatedPoint *point)
Definition: AtPulse.cxx:182
AtPulse::fAvgGainDeviation
double fAvgGainDeviation
Definition: AtPulse.h:54
AtPad::SetValidPad
void SetValidPad(Bool_t val=kTRUE)
Definition: AtPad.h:82
XYPoint
ROOT::Math::XYPoint XYPoint
Definition: AtPulse.cxx:26
AtPulse::GenerateEvent
AtRawEvent GenerateEvent(std::vector< SimPointPtr > &vec)
Definition: AtPulse.cxx:56
AtPad::SetPadCoord
void SetPadCoord(const XYPoint &point)
Definition: AtPad.h:87
AtPulse::Reset
void Reset()
Definition: AtPulse.cxx:127
AtElectronicResponse.h
AtDigiPar::GetPeakingTime
Int_t GetPeakingTime() const
Definition: AtDigiPar.h:63
AtDigiPar::GetTBEntrance
Int_t GetTBEntrance() const
Definition: AtDigiPar.h:49
AtPulse::fDoConvolution
bool fDoConvolution
Definition: AtPulse.h:48
XYPoint
ROOT::Math::XYPoint XYPoint
Definition: AtPatternCircle2D.cxx:16
AtPad::SetPedestalSubtracted
void SetPedestalSubtracted(Bool_t val=kTRUE)
Definition: AtPad.h:86
AtPulse::fPeakingTime
double fPeakingTime
GET Gain (ADC ch/electron).
Definition: AtPulse.h:38
AtPulse::fUseFastGain
bool fUseFastGain
Response function of the electronics.
Definition: AtPulse.h:45
AtPad::SetADC
void SetADC(const trace &val)
Definition: AtPad.h:91
AtPulse::ResponseFunc
std::function< double(int, double)> ResponseFunc
Function (or callable object) to use as the response function. Parameters are padNum and time (us)
Definition: AtPulse.h:27
AtRawEvent
Definition: AtRawEvent.h:34
AtPulse::fSaveCharge
bool fSaveCharge
Sigma of random gaussian noise to apply to trace.
Definition: AtPulse.h:47
AtDigiPar::GetTBTime
Int_t GetTBTime() const
returns the time duration of a time bucket in given sampling time in ns.
Definition: AtDigiPar.cxx:17
AtSimulatedPoint.h
AtSimulatedPoint
Definition: AtSimulatedPoint.h:16
AtPulse::SetParameters
void SetParameters(const AtDigiPar *fPar)
Definition: AtPulse.cxx:133
AtMap::InhibitType::kLowGain
@ kLowGain
AtPulse::fNumTbs
int fNumTbs
Time bucket size in us.
Definition: AtPulse.h:40
AtPulse::AtMapPtr
std::shared_ptr< AtMap > AtMapPtr
Definition: AtPulse.h:24
AtPulse::fGETGain
double fGETGain
If pad is AtMap::kLowGain multiply gain by this factor.
Definition: AtPulse.h:37
AtPadArray.h
AtPulse::fNoiseSigma
double fNoiseSigma
Definition: AtPulse.h:46
AtDigiPar::GetGain
Double_t GetGain() const
Definition: AtDigiPar.h:59
AtPulse::GetGain
double GetGain(int padNum, int numElectrons)
Definition: AtPulse.cxx:205
AtPulse::FillPad
void FillPad(AtPad &pad, TH1F &hist)
Definition: AtPulse.cxx:82
AtPulse::fPadsWithCharge
std::set< int > fPadsWithCharge
Definition: AtPulse.h:51
AtDigiPar.h
AtDigiPar
Definition: AtDigiPar.h:14
AtPulse::fPadCharge
std::vector< std::unique_ptr< TH1F > > fPadCharge
Definition: AtPulse.h:50
AtPulse::fLowGainFactor
double fLowGainFactor
Micromegas gain.
Definition: AtPulse.h:36
AtDigiPar::GetGETGain
Double_t GetGETGain() const
Definition: AtDigiPar.h:62
AtPulse::fTBTime
double fTBTime
Electronic peaking time in us.
Definition: AtPulse.h:39
ContainerManip::GetPointerVector
std::vector< T * > GetPointerVector(const std::vector< T > &vec)
Definition: AtContainerManip.h:105
AtMap::InhibitType::kTotal
@ kTotal
AtPad::GetPadNum
Int_t GetPadNum() const
Definition: AtPad.h:96
AtPad::GetADC
const trace & GetADC() const
Definition: AtPad.cxx:97
AtRawEvent::AddPad
AtPad * AddPad(Ts &&...params)
Create a new pad in this event.
Definition: AtRawEvent.h:82
AtContainerManip.h
AtSimulatedPoint::GetPosition
virtual ROOT::Math::XYZVector GetPosition()
Definition: AtSimulatedPoint.cxx:46
AtPulse::AtPulse
AtPulse(AtMapPtr map, ResponseFunc response=nullptr)
Definition: AtPulse.cxx:27
AtPulse::fGainFunc
std::unique_ptr< TF1 > fGainFunc
Definition: AtPulse.h:53
AtPulse::fTBPadPlane
int fTBPadPlane
Window location in timebuckets (from config)
Definition: AtPulse.h:42
AtMap.h
AtPulse::ApplyNoise
void ApplyNoise(AtPad &pad)
Definition: AtPulse.cxx:118
AtPad
Container class for AtPadBase objects.
Definition: AtPad.h:38
AtDigiPar::GetDriftVelocity
Double_t GetDriftVelocity() const
Definition: AtDigiPar.h:58
AtPulse
Definition: AtPulse.h:22
AtPulse::fGain
double fGain
EventID.
Definition: AtPulse.h:35
AtPulse::fTBEntrance
int fTBEntrance
Number of time buckers.
Definition: AtPulse.h:41
AtPadBase.h
AtPulse::fMap
AtMapPtr fMap
AtTPC map.
Definition: AtPulse.h:32
AtPad::AddAugment
AtPadBase * AddAugment(std::string name, std::unique_ptr< AtPadBase > augment)
Definition: AtPad.cxx:63
ElectronicResponse::AtNominalResponse
Nominal response of GET electronics.
Definition: AtElectronicResponse.h:53
AtSimulatedPoint::GetCharge
Int_t GetCharge()
Definition: AtSimulatedPoint.cxx:62
AtPulse::fResponse
ResponseFunc fResponse
Pad plane location in TBs (calculated from DriftVelocity, TBEntrance, ZPadPlane.
Definition: AtPulse.h:44
AtPulse.h