16#include "TRestDetectorElectronDiffusionProcess.h"
22TRestDetectorElectronDiffusionProcess::TRestDetectorElectronDiffusionProcess() {
Initialize(); }
24TRestDetectorElectronDiffusionProcess::TRestDetectorElectronDiffusionProcess(
const char* configFilename) {
31TRestDetectorElectronDiffusionProcess::~TRestDetectorElectronDiffusionProcess() {
delete fOutputHitsEvent; }
33void TRestDetectorElectronDiffusionProcess::LoadDefaultConfig() {
34 SetTitle(
"Default config");
36 fElectricField = 2000;
49 fTransversalDiffusionCoefficient = -1;
50 fLongitudinalDiffusionCoefficient = -1;
55 fInputHitsEvent =
nullptr;
61 fCheckIsInside =
true;
64void TRestDetectorElectronDiffusionProcess::LoadConfig(
const string& configFilename,
const string& name) {
71 fRandom =
new TRandom3(fSeed);
73 fGas = GetMetadata<TRestDetectorGas>();
74 if (fGas ==
nullptr) {
75 if (fLongitudinalDiffusionCoefficient == -1 || fTransversalDiffusionCoefficient == -1) {
76 RESTWarning <<
"Gas has not been initialized" <<
RESTendl;
78 <<
"TRestDetectorElectronDiffusionProcess: diffusion parameters are not defined in the rml "
84 RESTWarning <<
"Gas has not been initialized" <<
RESTendl;
86 <<
"TRestDetectorElectronDiffusionProcess: gas work function has not been defined in the "
93 RESTError <<
"A TRestDetectorGas definition was found but REST was not linked to Garfield libraries."
96 <<
"Please, remove the TRestDetectorGas definition, and add gas parameters inside the process "
97 "TRestDetectorElectronDiffusionProcess"
101 if (fGasPressure <= 0) {
102 fGasPressure = fGas->GetPressure();
104 fGas->SetPressure(fGasPressure);
106 if (fElectricField <= 0) {
112 fWValue = fGas->GetWvalue();
116 if (fFanoFactor <= 0) {
120 if (fLongitudinalDiffusionCoefficient < 0) {
121 fLongitudinalDiffusionCoefficient = fGas->GetLongitudinalDiffusion();
124 if (fTransversalDiffusionCoefficient < 0) {
125 fTransversalDiffusionCoefficient = fGas->GetTransversalDiffusion();
129 fReadout = GetMetadata<TRestDetectorReadout>();
130 if (fReadout ==
nullptr) {
131 cout <<
"REST ERROR: Readout has not been initialized" << endl;
140 set<int> hitsToProcess;
141 for (
int n = 0; n < static_cast<int>(fInputHitsEvent->GetNumberOfHits()); n++) {
142 if (fInputHitsEvent->GetType(n) == REST_HitType::VETO) {
144 fOutputHitsEvent->
AddHit(fInputHitsEvent->
GetX(n), fInputHitsEvent->
GetY(n),
145 fInputHitsEvent->
GetZ(n), fInputHitsEvent->GetEnergy(n),
146 fInputHitsEvent->GetTime(n), fInputHitsEvent->GetType(n));
148 hitsToProcess.insert(n);
152 if (hitsToProcess.empty()) {
156 double totalEnergy = 0;
157 for (
const auto& hitIndex : hitsToProcess) {
158 totalEnergy += fInputHitsEvent->GetEnergy(hitIndex);
163 Double_t wValue = fWValue;
164 const auto totalElectrons =
static_cast<unsigned int>(totalEnergy * REST_Units::eV / wValue);
167 if (fMaxHits > 0 && totalElectrons > fMaxHits) {
169 wValue = (totalEnergy * REST_Units::eV) / fMaxHits;
172 for (
const auto& hitIndex : hitsToProcess) {
173 TRestHits* hits = fInputHitsEvent->GetHits();
175 const auto energy = hits->GetEnergy(hitIndex);
176 const auto time = hits->GetTime(hitIndex);
177 const auto type = hits->GetType(hitIndex);
183 const Double_t x = hits->GetX(hitIndex);
184 const Double_t y = hits->GetY(hitIndex);
185 const Double_t z = hits->GetZ(hitIndex);
187 for (
int p = 0; p < fReadout->GetNumberOfReadoutPlanes(); p++) {
189 const auto planeType = plane->GetType();
190 if (planeType ==
"veto") {
195 if (fCheckIsInside && !plane->
IsInside({x, y, z})) {
199 Double_t driftDistance = plane->GetDistanceTo({x, y, z});
201 double numberOfElectrons = energy * REST_Units::eV / wValue;
202 if (fUseFanoFactor) {
203 if (fFanoFactor <= 0) {
204 throw runtime_error(
"Fano factor not valid");
206 const auto sigma = TMath::Sqrt(fFanoFactor * numberOfElectrons);
207 numberOfElectrons = fRandom->Gaus(numberOfElectrons, sigma);
208 }
else if (fPoissonElectronExcitation) {
210 numberOfElectrons = fRandom->Poisson(energy * REST_Units::eV / fWValue);
213 if (wValue != fWValue) {
215 numberOfElectrons =
static_cast<unsigned int>(numberOfElectrons * fWValue / wValue);
218 if (numberOfElectrons <= 0) {
219 numberOfElectrons = 1;
222 const Double_t energyPerElectron = energy * REST_Units::eV / numberOfElectrons;
224 Double_t longitudinalDiffusion =
225 10. * TMath::Sqrt(driftDistance / 10.) * fLongitudinalDiffusionCoefficient;
226 Double_t transversalDiffusion =
227 10. * TMath::Sqrt(driftDistance / 10.) * fTransversalDiffusionCoefficient;
229 for (
unsigned int i = 0; i < numberOfElectrons; i++) {
230 if (fAttachment > 0) {
232 isAttached = (fRandom->Uniform(0, 1) > pow(1 - fAttachment, driftDistance / 10.));
241 TVector3 positionAfterDiffusion = {x, y, z};
242 if (transversalDiffusion > 0) {
243 positionAfterDiffusion += {
244 fRandom->Gaus(0, transversalDiffusion),
245 fRandom->Gaus(0, transversalDiffusion),
249 if (longitudinalDiffusion > 0) {
250 positionAfterDiffusion += {
253 fRandom->Gaus(0, longitudinalDiffusion)
257 if (plane->GetDistanceTo(positionAfterDiffusion) < 0 && longitudinalDiffusion > 0) {
259 positionAfterDiffusion.SetZ(
260 plane->GetPosition().Z() +
261 1E-6 * plane->GetNormal().Z());
263 if (plane->GetDistanceTo(positionAfterDiffusion) > plane->GetHeight() &&
264 longitudinalDiffusion > 0) {
266 positionAfterDiffusion.SetZ(
267 plane->GetPosition().Z() + plane->GetHeight() -
268 1E-6 * plane->GetNormal().Z());
271 if (fCheckIsInside && !plane->IsInside(positionAfterDiffusion)) {
276 const double electronEnergy =
277 fUnitElectronEnergy ? 1 : energyPerElectron * REST_Units::keV / REST_Units::eV;
280 cout <<
"Adding hit. x : " << positionAfterDiffusion.X()
281 <<
" y : " << positionAfterDiffusion.Y() <<
" z : " << positionAfterDiffusion.Z()
282 <<
" en : " << energyPerElectron * REST_Units::keV / REST_Units::eV <<
" keV"
285 fOutputHitsEvent->
AddHit(positionAfterDiffusion.X(), positionAfterDiffusion.Y(),
286 positionAfterDiffusion.Z(), electronEnergy, time, type);
293 cout <<
"TRestDetectorElectronDiffusionProcess. Processed hits total energy : " << totalEnergy
295 cout <<
"TRestDetectorElectronDiffusionProcess. Hits processed : " << hitsToProcess.size() << endl;
296 cout <<
"TRestDetectorElectronDiffusionProcess. Hits added : " << fOutputHitsEvent->GetNumberOfHits()
303 return fOutputHitsEvent;
309 fGasPressure = GetDblParameterWithUnits(
"gasPressure", -1.);
310 fElectricField = GetDblParameterWithUnits(
"electricField", -1.);
311 fWValue = GetDblParameterWithUnits(
"WValue", 0.0) * REST_Units::eV;
312 fFanoFactor = GetDblParameterWithUnits(
"fanoFactor", 0.0);
314 fLongitudinalDiffusionCoefficient =
316 if (fLongitudinalDiffusionCoefficient == -1)
319 RESTWarning <<
"longitudinalDiffusionCoefficient is now OBSOLETE! It will soon disappear."
321 RESTWarning <<
" Please use the shorter form of this parameter : longDiff" <<
RESTendl;
325 if (fTransversalDiffusionCoefficient == -1)
328 RESTWarning <<
"transversalDiffusionCoefficient is now OBSOLETE! It will soon disappear." <<
RESTendl;
329 RESTWarning <<
" Please use the shorter form of this parameter : transDiff" <<
RESTendl;
333 fPoissonElectronExcitation = StringToBool(
GetParameter(
"poissonElectronExcitation",
"true"));
334 fUnitElectronEnergy = StringToBool(
GetParameter(
"unitElectronEnergy",
"false"));
335 fCheckIsInside = StringToBool(
GetParameter(
"checkIsInside",
"true"));
336 fUseFanoFactor = StringToBool(
GetParameter(
"useFano",
"false"));
virtual void SetW(double value)
Sets the electric field of the drift volume. Given in V/mm.
virtual void SetElectricField(double value)
Sets the electric field. Must be given in V/mm.
virtual Double_t GetElectricField() const
Returns the electric field in V/mm.
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
void EndProcess() override
To be executed at the end of the run (outside event loop)
void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void Initialize() override
Making default settings.
Double_t GetGasFanoFactor() const
Returns the gas fano factor.
Double_t GetX(int n) const
Returns the X-coordinate of hit entry n in mm.
Double_t GetY(int n) const
Returns the Y-coordinate of hit entry n in mm.
Double_t GetZ(int n) const
Returns the Z-coordinate of hit entry n in mm.
void AddHit(Double_t x, Double_t y, Double_t z, Double_t en, Double_t t=0, REST_HitType type=XYZ)
Adds a new hit to this event.
bool IsInside(const TVector3 &point) const
Check if the point is inside any module of the readout plane.
TRestDetectorReadoutPlane * GetReadoutPlane(int p)
Returns a pointer to the readout plane by index.
A base class for any REST event.
void SetEventInfo(TRestEvent *eve)
It saves a 3-coordinate position and an energy for each punctual deposition.
@ REST_Extreme
show everything
@ 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.
Double_t StringToDouble(std::string in)
Gets a double from a string.
Int_t StringToInteger(std::string in)
Gets an integer from a string.