REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestDetectorElectronDiffusionProcess.cxx
1
15
16#include "TRestDetectorElectronDiffusionProcess.h"
17
18using namespace std;
19
21
22TRestDetectorElectronDiffusionProcess::TRestDetectorElectronDiffusionProcess() { Initialize(); }
23
24TRestDetectorElectronDiffusionProcess::TRestDetectorElectronDiffusionProcess(const char* configFilename) {
25 Initialize();
26 if (LoadConfigFromFile(configFilename)) {
27 LoadDefaultConfig();
28 }
29}
30
31TRestDetectorElectronDiffusionProcess::~TRestDetectorElectronDiffusionProcess() { delete fOutputHitsEvent; }
32
33void TRestDetectorElectronDiffusionProcess::LoadDefaultConfig() {
34 SetTitle("Default config");
35
36 fElectricField = 2000;
37 fAttachment = 0;
38 fGasPressure = -1;
39}
40
42 SetSectionName(this->ClassName());
43 SetLibraryVersion(LIBRARY_VERSION);
44
45 fElectricField = 0;
46 fAttachment = 0;
47 fGasPressure = -1;
48
49 fTransversalDiffusionCoefficient = -1;
50 fLongitudinalDiffusionCoefficient = -1;
51 fWValue = 0;
52 fFanoFactor = 0;
53
54 fOutputHitsEvent = new TRestDetectorHitsEvent();
55 fInputHitsEvent = nullptr;
56
57 fGas = nullptr;
58 fReadout = nullptr;
59
60 fRandom = nullptr;
61 fCheckIsInside = true;
62}
63
64void TRestDetectorElectronDiffusionProcess::LoadConfig(const string& configFilename, const string& name) {
65 if (LoadConfigFromFile(configFilename, name)) {
66 LoadDefaultConfig();
67 }
68}
69
71 fRandom = new TRandom3(fSeed);
72
73 fGas = GetMetadata<TRestDetectorGas>();
74 if (fGas == nullptr) {
75 if (fLongitudinalDiffusionCoefficient == -1 || fTransversalDiffusionCoefficient == -1) {
76 RESTWarning << "Gas has not been initialized" << RESTendl;
77 RESTError
78 << "TRestDetectorElectronDiffusionProcess: diffusion parameters are not defined in the rml "
79 "file!"
80 << RESTendl;
81 exit(-1);
82 }
83 if (fWValue == -1) {
84 RESTWarning << "Gas has not been initialized" << RESTendl;
85 RESTError
86 << "TRestDetectorElectronDiffusionProcess: gas work function has not been defined in the "
87 "rml file!"
88 << RESTendl;
89 exit(-1);
90 }
91 } else {
92#ifndef USE_Garfield
93 RESTError << "A TRestDetectorGas definition was found but REST was not linked to Garfield libraries."
94 << RESTendl;
95 RESTError
96 << "Please, remove the TRestDetectorGas definition, and add gas parameters inside the process "
97 "TRestDetectorElectronDiffusionProcess"
98 << RESTendl;
99 exit(1);
100#endif
101 if (fGasPressure <= 0) {
102 fGasPressure = fGas->GetPressure();
103 }
104 fGas->SetPressure(fGasPressure);
105
106 if (fElectricField <= 0) {
107 fElectricField = fGas->GetElectricField();
108 }
109 fGas->SetElectricField(fElectricField);
110
111 if (fWValue <= 0) {
112 fWValue = fGas->GetWvalue();
113 }
114 fGas->SetW(fWValue);
115
116 if (fFanoFactor <= 0) {
117 fFanoFactor = fGas->GetGasFanoFactor();
118 }
119
120 if (fLongitudinalDiffusionCoefficient < 0) {
121 fLongitudinalDiffusionCoefficient = fGas->GetLongitudinalDiffusion();
122 } // (cm)^1/2
123
124 if (fTransversalDiffusionCoefficient < 0) {
125 fTransversalDiffusionCoefficient = fGas->GetTransversalDiffusion();
126 } // (cm)^1/2
127 }
128
129 fReadout = GetMetadata<TRestDetectorReadout>();
130 if (fReadout == nullptr) {
131 cout << "REST ERROR: Readout has not been initialized" << endl;
132 exit(-1);
133 }
134}
135
137 fInputHitsEvent = (TRestDetectorHitsEvent*)inputEvent;
138 fOutputHitsEvent->SetEventInfo(fInputHitsEvent);
139
140 set<int> hitsToProcess; // indices of the hits to process (we do not want to process veto hits)
141 for (int n = 0; n < static_cast<int>(fInputHitsEvent->GetNumberOfHits()); n++) {
142 if (fInputHitsEvent->GetType(n) == REST_HitType::VETO) {
143 // keep unprocessed hits as they are
144 fOutputHitsEvent->AddHit(fInputHitsEvent->GetX(n), fInputHitsEvent->GetY(n),
145 fInputHitsEvent->GetZ(n), fInputHitsEvent->GetEnergy(n),
146 fInputHitsEvent->GetTime(n), fInputHitsEvent->GetType(n));
147 } else {
148 hitsToProcess.insert(n);
149 }
150 }
151
152 if (hitsToProcess.empty()) {
153 return nullptr;
154 }
155
156 double totalEnergy = 0;
157 for (const auto& hitIndex : hitsToProcess) {
158 totalEnergy += fInputHitsEvent->GetEnergy(hitIndex);
159 }
160
161 bool isAttached;
162
163 Double_t wValue = fWValue;
164 const auto totalElectrons = static_cast<unsigned int>(totalEnergy * REST_Units::eV / wValue);
165
166 // TODO: double check this
167 if (fMaxHits > 0 && totalElectrons > fMaxHits) {
168 // set a fake w-value if max hits are limited. this fake w-value will be larger
169 wValue = (totalEnergy * REST_Units::eV) / fMaxHits;
170 }
171
172 for (const auto& hitIndex : hitsToProcess) {
173 TRestHits* hits = fInputHitsEvent->GetHits();
174
175 const auto energy = hits->GetEnergy(hitIndex);
176 const auto time = hits->GetTime(hitIndex);
177 const auto type = hits->GetType(hitIndex);
178
179 if (energy <= 0) {
180 continue;
181 }
182
183 const Double_t x = hits->GetX(hitIndex);
184 const Double_t y = hits->GetY(hitIndex);
185 const Double_t z = hits->GetZ(hitIndex);
186
187 for (int p = 0; p < fReadout->GetNumberOfReadoutPlanes(); p++) {
188 TRestDetectorReadoutPlane* plane = fReadout->GetReadoutPlane(p);
189 const auto planeType = plane->GetType();
190 if (planeType == "veto") {
191 // do not drift veto planes
192 continue;
193 }
194
195 if (fCheckIsInside && !plane->IsInside({x, y, z})) {
196 continue;
197 }
198
199 Double_t driftDistance = plane->GetDistanceTo({x, y, z});
200
201 double numberOfElectrons = energy * REST_Units::eV / wValue;
202 if (fUseFanoFactor) {
203 if (fFanoFactor <= 0) {
204 throw runtime_error("Fano factor not valid");
205 }
206 const auto sigma = TMath::Sqrt(fFanoFactor * numberOfElectrons);
207 numberOfElectrons = fRandom->Gaus(numberOfElectrons, sigma);
208 } else if (fPoissonElectronExcitation) {
209 // this is probably a bad idea, we only keep it for backwards compatibility
210 numberOfElectrons = fRandom->Poisson(energy * REST_Units::eV / fWValue);
211 }
212
213 if (wValue != fWValue) {
214 // reduce the number of electrons to improve speed
215 numberOfElectrons = static_cast<unsigned int>(numberOfElectrons * fWValue / wValue);
216 }
217
218 if (numberOfElectrons <= 0) {
219 numberOfElectrons = 1;
220 }
221
222 const Double_t energyPerElectron = energy * REST_Units::eV / numberOfElectrons;
223
224 Double_t longitudinalDiffusion =
225 10. * TMath::Sqrt(driftDistance / 10.) * fLongitudinalDiffusionCoefficient; // mm
226 Double_t transversalDiffusion =
227 10. * TMath::Sqrt(driftDistance / 10.) * fTransversalDiffusionCoefficient; // mm
228
229 for (unsigned int i = 0; i < numberOfElectrons; i++) {
230 if (fAttachment > 0) {
231 // TODO: where is this formula from?
232 isAttached = (fRandom->Uniform(0, 1) > pow(1 - fAttachment, driftDistance / 10.));
233 } else {
234 isAttached = false;
235 }
236
237 if (isAttached) {
238 continue;
239 }
240
241 TVector3 positionAfterDiffusion = {x, y, z};
242 if (transversalDiffusion > 0) {
243 positionAfterDiffusion += {
244 fRandom->Gaus(0, transversalDiffusion), //
245 fRandom->Gaus(0, transversalDiffusion), //
246 0 //
247 };
248 }
249 if (longitudinalDiffusion > 0) {
250 positionAfterDiffusion += {
251 0, //
252 0, //
253 fRandom->Gaus(0, longitudinalDiffusion) //
254 };
255 }
256
257 if (plane->GetDistanceTo(positionAfterDiffusion) < 0 && longitudinalDiffusion > 0) {
258 // electron has been moved under the plane
259 positionAfterDiffusion.SetZ(
260 plane->GetPosition().Z() +
261 1E-6 * plane->GetNormal().Z()); // add a delta to make sure readout finds it
262 }
263 if (plane->GetDistanceTo(positionAfterDiffusion) > plane->GetHeight() &&
264 longitudinalDiffusion > 0) {
265 // electron has been moved over the plane
266 positionAfterDiffusion.SetZ(
267 plane->GetPosition().Z() + plane->GetHeight() -
268 1E-6 * plane->GetNormal().Z()); // add a delta to make sure readout finds it
269 }
270
271 if (fCheckIsInside && !plane->IsInside(positionAfterDiffusion)) {
272 // electron has been moved outside the readout plane
273 continue;
274 }
275
276 const double electronEnergy =
277 fUnitElectronEnergy ? 1 : energyPerElectron * REST_Units::keV / REST_Units::eV;
278
280 cout << "Adding hit. x : " << positionAfterDiffusion.X()
281 << " y : " << positionAfterDiffusion.Y() << " z : " << positionAfterDiffusion.Z()
282 << " en : " << energyPerElectron * REST_Units::keV / REST_Units::eV << " keV"
283 << endl;
284 }
285 fOutputHitsEvent->AddHit(positionAfterDiffusion.X(), positionAfterDiffusion.Y(),
286 positionAfterDiffusion.Z(), electronEnergy, time, type);
287 }
288 break; // avoid redoing for other planes (happens if fCheckIsInside is false)
289 }
290 }
291
292 if (this->GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Debug) {
293 cout << "TRestDetectorElectronDiffusionProcess. Processed hits total energy : " << totalEnergy
294 << endl;
295 cout << "TRestDetectorElectronDiffusionProcess. Hits processed : " << hitsToProcess.size() << endl;
296 cout << "TRestDetectorElectronDiffusionProcess. Hits added : " << fOutputHitsEvent->GetNumberOfHits()
297 << endl;
299 GetChar();
300 }
301 }
302
303 return fOutputHitsEvent;
304}
305
307
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);
313 fAttachment = StringToDouble(GetParameter("attachment", "0"));
314 fLongitudinalDiffusionCoefficient =
315 StringToDouble(GetParameter("longitudinalDiffusionCoefficient", "-1"));
316 if (fLongitudinalDiffusionCoefficient == -1)
317 fLongitudinalDiffusionCoefficient = StringToDouble(GetParameter("longDiff", "-1"));
318 else {
319 RESTWarning << "longitudinalDiffusionCoefficient is now OBSOLETE! It will soon disappear."
320 << RESTendl;
321 RESTWarning << " Please use the shorter form of this parameter : longDiff" << RESTendl;
322 }
323
324 fTransversalDiffusionCoefficient = StringToDouble(GetParameter("transversalDiffusionCoefficient", "-1"));
325 if (fTransversalDiffusionCoefficient == -1)
326 fTransversalDiffusionCoefficient = StringToDouble(GetParameter("transDiff", "-1"));
327 else {
328 RESTWarning << "transversalDiffusionCoefficient is now OBSOLETE! It will soon disappear." << RESTendl;
329 RESTWarning << " Please use the shorter form of this parameter : transDiff" << RESTendl;
330 }
331 fMaxHits = StringToInteger(GetParameter("maxHits", "1000"));
332 fSeed = static_cast<UInt_t>(StringToInteger(GetParameter("seed", "0")));
333 fPoissonElectronExcitation = StringToBool(GetParameter("poissonElectronExcitation", "true"));
334 fUnitElectronEnergy = StringToBool(GetParameter("unitElectronEnergy", "false"));
335 fCheckIsInside = StringToBool(GetParameter("checkIsInside", "true"));
336 fUseFanoFactor = StringToBool(GetParameter("useFano", "false"));
337}
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.
Definition: TRestEvent.h:38
void SetEventInfo(TRestEvent *eve)
Definition: TRestEvent.cxx:137
It saves a 3-coordinate position and an energy for each punctual deposition.
Definition: TRestHits.h:39
endl_t RESTendl
Termination flag object for TRestStringOutput.
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
void SetSectionName(std::string sName)
set the section name, clear the section content
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.
@ 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.