Garfield++ 5.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
avalanche.cc
Go to the documentation of this file.
1/**
2 * avalanche.cc
3 * General program flow based on example code from the Garfield++ website.
4 *
5 * Demonstrates electron avalanche and induced signal readout with
6 * 2D finite-element visualization in Garfield++ with a LEM.
7 * LEM parameters are from:
8 * C. Shalem et. al. Nucl. Instr. Meth. A, 558, 475 (2006).
9 *
10*/
11#include <iostream>
12#include <cmath>
13
14#include <TCanvas.h>
15#include <TApplication.h>
16#include <TFile.h>
17
20#include "Garfield/Sensor.hh"
21#include "Garfield/ViewField.hh"
22#include "Garfield/Plotting.hh"
26#include "Garfield/Random.hh"
28
29using namespace Garfield;
30
31int main(int argc, char* argv[]) {
32
33 TApplication app("app", &argc, argv);
34
35 // Set relevant LEM parameters.
36 // LEM thickness in cm
37 const double lem_th = 0.04;
38 // Copper thickness
39 const double lem_cpth = 0.0035;
40 // LEM pitch in cm
41 const double lem_pitch = 0.07;
42 // X-width of drift simulation will cover between +/- axis_x
43 const double axis_x = 0.1;
44 // Y-width of drift simulation will cover between +/- axis_y
45 const double axis_y = 0.1;
46 const double axis_z = 0.25 + lem_th / 2 + lem_cpth;
47
48
49 // Define the medium (Ar/CO2 70:30).
50 MediumMagboltz gas("ar", 70., "co2", 30.);
51 // Set the temperature (K)
52 gas.SetTemperature(293.15);
53 // Set the pressure (Torr)
54 gas.SetPressure(740.);
55
56 // Import an Elmer-created field map.
58 "gemcell/mesh.header", "gemcell/mesh.elements", "gemcell/mesh.nodes",
59 "gemcell/dielectrics.dat", "gemcell/gemcell.result", "cm");
62 elm.SetGas(&gas);
63 // Import the weighting field for the readout electrode.
64 // elm.SetWeightingField("gemcell/gemcell_WTlel.result", "wtlel");
65
66 // Set up a sensor object.
67 Sensor sensor;
68 sensor.AddComponent(&elm);
69 sensor.SetArea(-axis_x, -axis_y, -axis_z, axis_x, axis_y, axis_z);
70 // sensor.AddElectrode(elm, "wtlel");
71 // Set the signal binning.
72 const double tEnd = 500.0;
73 const int nsBins = 500;
74 // sensor.SetTimeWindow(0., tEnd / nsBins, nsBins);
75
76 // Create an avalanche object
77 AvalancheMicroscopic aval(&sensor);
78
79 // Set up the object for drift line visualization.
80 ViewDrift viewDrift;
81 viewDrift.SetArea(-axis_x, -axis_y, -axis_z, axis_x, axis_y, axis_z);
82 aval.EnablePlotting(&viewDrift, 100);
83
84 // Set the electron start parameters.
85 // Starting z position for electron drift
86 const double zi = 0.5 * lem_th + lem_cpth + 0.1;
87 double ri = (lem_pitch / 2) * RndmUniform();
88 double thetai = RndmUniform() * TwoPi;
89 double xi = ri * cos(thetai);
90 double yi = ri * sin(thetai);
91 // Calculate the avalanche.
92 aval.AvalancheElectron(xi, yi, zi, 0., 0., 0., 0., 0.);
93 std::cout << "... avalanche complete with "
94 << aval.GetNumberOfElectronEndpoints() << " electron tracks.\n";
95
96 // Extract the calculated signal.
97 double bscale = tEnd / nsBins; // time per bin
98 double sum = 0.; // to keep a running sum of the integrated signal
99
100 // Create ROOT histograms of the signal and a file in which to store them.
101 TFile* f = new TFile("avalanche_signals.root", "RECREATE");
102 TH1F* hS = new TH1F("hh", "hh", nsBins, 0, tEnd); // total signal
103 TH1F* hInt = new TH1F("hInt", "hInt", nsBins, 0, tEnd); // integrated signal
104
105 // Fill the histograms with the signals.
106 // Note that the signals will be in C/(ns*binWidth), and we will divide by e
107 // to give a signal in e/(ns*binWidth).
108 // The total signal is then the integral over all bins multiplied by the bin
109 // width in ns.
110 for (int i = 0; i < nsBins; i++) {
111 double wt = sensor.GetSignal("wtlel", i) / ElementaryCharge;
112 sum += wt;
113 hS->Fill(i * bscale, wt);
114 hInt->Fill(i * bscale, sum);
115 }
116
117 // Write the histograms to the TFile.
118 hS->Write();
119 hInt->Write();
120 f->Close();
121
122 // Plot the signal.
123 const bool plotSignal = false;
124 if (plotSignal) {
125 TCanvas* cSignal = new TCanvas("signal", "Signal");
126 ViewSignal* vSignal = new ViewSignal();
127 vSignal->SetSensor(&sensor);
128 vSignal->SetCanvas(cSignal);
129 vSignal->PlotSignal("wtlel");
130 }
131
132 // Plot the geometry, field and drift lines.
133 TCanvas* cGeom = new TCanvas("geom", "Geometry/Avalanche/Fields");
134 cGeom->SetLeftMargin(0.14);
135 const bool plotContours = false;
136 if (plotContours) {
137 ViewField* vf = new ViewField();
138 vf->SetSensor(&sensor);
139 vf->SetCanvas(cGeom);
140 vf->SetArea(-axis_x, -axis_y, axis_x, axis_y);
141 vf->SetNumberOfContours(40);
142 vf->SetNumberOfSamples2d(30, 30);
143 vf->SetPlane(0, -1, 0, 0, 0, 0);
144 vf->PlotContour("v");
145 }
146
147 // Set up the object for FE mesh visualization.
148 ViewFEMesh vFE;
149 vFE.SetArea(-axis_x, -axis_z, -axis_y, axis_x, axis_z, axis_y);
150 vFE.SetCanvas(cGeom);
151 vFE.SetComponent(&elm);
152 vFE.SetPlane(0, -1, 0, 0, 0, 0);
153 vFE.SetFillMesh(true);
154 vFE.SetColor(1, kGray);
155 vFE.SetColor(2, kYellow + 3);
156 vFE.SetColor(3, kYellow + 3);
157 if (!plotContours) {
158 vFE.EnableAxes();
159 vFE.SetXaxisTitle("x (cm)");
160 vFE.SetYaxisTitle("z (cm)");
161 vFE.SetViewDrift(&viewDrift);
162 vFE.Plot();
163 }
164
165 app.Run();
166 return 0;
167}
int main(int argc, char *argv[])
Definition avalanche.cc:31
Calculate electron drift lines and avalanches using microscopic tracking.
bool AvalancheElectron(const double x, const double y, const double z, const double t, const double e, const double dx=0., const double dy=0., const double dz=0.)
Calculate an avalanche initiated by a given electron.
void EnablePlotting(ViewDrift *view, const size_t nColl=100)
Switch on drift line plotting.
Component for importing field maps computed by Elmer.
void EnablePeriodicityX(const bool on=true)
Enable simple periodicity in the direction.
Definition Component.hh:204
void EnableMirrorPeriodicityY(const bool on=true)
Enable mirror periodicity in the direction.
Definition Component.hh:231
void SetTemperature(const double t)
Set the temperature [K].
Definition Medium.cc:72
void SetPressure(const double p)
Definition Medium.cc:82
void AddComponent(Component *comp)
Add a component.
Definition Sensor.cc:355
double GetSignal(const std::string &label, const unsigned int bin)
Retrieve the total signal for a given electrode and time bin.
Definition Sensor.cc:976
bool SetArea(const bool verbose=false)
Set the user area to the default.
Definition Sensor.cc:187
void SetCanvas(TPad *pad)
Set the canvas to be painted on.
Definition ViewBase.hh:28
void SetArea(const double xmin, const double ymin, const double xmax, const double ymax)
Definition ViewBase.cc:109
virtual void SetPlane(const double fx, const double fy, const double fz, const double x0, const double y0, const double z0)
Definition ViewBase.cc:142
Visualize drift lines and tracks.
Definition ViewDrift.hh:19
Draw the mesh of a field-map component.
Definition ViewFEMesh.hh:22
bool Plot(const bool twod=true)
Plot method to be called by user.
Definition ViewFEMesh.cc:60
void SetYaxisTitle(const std::string &ytitle)
Definition ViewFEMesh.hh:42
void SetViewDrift(ViewDrift *vd)
Set the optional associated ViewDrift.
Definition ViewFEMesh.hh:64
void SetXaxisTitle(const std::string &xtitle)
Definition ViewFEMesh.hh:41
void SetPlane(const double fx, const double fy, const double fz, const double x0, const double y0, const double z0) override
void SetComponent(ComponentFieldMap *cmp)
Set the component from which to retrieve the mesh and field.
Definition ViewFEMesh.cc:49
void SetColor(int matID, int colorID)
Definition ViewFEMesh.hh:58
void SetFillMesh(const bool f)
Element fill switch; 2D only, set false for wireframe mesh.
Definition ViewFEMesh.hh:50
Visualize the potential or electric field of a component or sensor.
Definition ViewField.hh:15
void SetNumberOfSamples2d(const unsigned int nx, const unsigned int ny)
Set the number of points used for drawing 2D functions.
Definition ViewField.cc:123
void PlotContour(const std::string &option="v")
Definition ViewField.cc:129
void SetNumberOfContours(const unsigned int n)
Set the number of contour levels.
Definition ViewField.cc:115
void SetSensor(Sensor *s)
Set the sensor for which to plot the field.
Definition ViewField.cc:73
Plot the signal computed by a sensor as a ROOT histogram.
Definition ViewSignal.hh:19
void SetSensor(Sensor *s)
Set the sensor from which to retrieve the signal.
Definition ViewSignal.cc:18
void PlotSignal(const std::string &label, const std::string &optTotal="t", const std::string &optPrompt="", const std::string &optDelayed="", const bool same=false)
Definition ViewSignal.cc:61
double RndmUniform()
Draw a random number uniformly distributed in the range [0, 1).
Definition Random.hh:14