Project
Loading...
Searching...
No Matches
GEMAmplification.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
17#include <TStopwatch.h>
19#include <TFile.h>
21#include <fstream>
22#include "Framework/Logger.h"
23#include <filesystem>
24
25using namespace o2::tpc;
26using namespace o2::math_utils;
27using boost::format;
28
30 : mRandomGaus(RandomRing<>::RandomType::Gaus, rng),
31 mRandomFlat(RandomRing<>::RandomType::Flat, rng),
32 // the gain rings are refilled from the Polya distributions below; with an explicit generator they start empty
33 mGain{RandomRing<>(rng ? RandomRing<>::RandomType::CustomTF1 : RandomRing<>::RandomType::Gaus),
34 RandomRing<>(rng ? RandomRing<>::RandomType::CustomTF1 : RandomRing<>::RandomType::Gaus),
35 RandomRing<>(rng ? RandomRing<>::RandomType::CustomTF1 : RandomRing<>::RandomType::Gaus),
36 RandomRing<>(rng ? RandomRing<>::RandomType::CustomTF1 : RandomRing<>::RandomType::Gaus)},
37 mGainFullStack(rng ? RandomRing<>::RandomType::CustomTF1 : RandomRing<>::RandomType::Gaus)
38{
40
41 TStopwatch watch;
42 watch.Start();
43 const float sigmaOverMu = mGasParam->SigmaOverMu;
44 const float kappa = 1 / (sigmaOverMu * sigmaOverMu);
45 boost::format polya("1/(TMath::Gamma(%1%)*%2%) * TMath::Power(x/%3%, %4%) * TMath::Exp(-x/%5%)");
46
47 const char* polyaFileName = "tpc_polya.root";
48 TFile* outfile;
49 auto cacheexists = std::filesystem::exists(polyaFileName);
50 if (cacheexists) {
51 LOG(info) << "TPC: GEM setup from existing cache";
52 outfile = TFile::Open(polyaFileName);
53 } else {
54 LOG(info) << "TPC: GEM setup - initialization from scratch";
55 outfile = TFile::Open(polyaFileName, "CREATE");
56 }
57
59 for (int i = 0; i < 4; ++i) {
60 float s = mGEMParam->AbsoluteGain[i] / kappa;
61 polya % kappa % s % s % (kappa - 1) % s;
62 std::string name = polya.str();
63 o2::math_utils::CachingTF1* polyaDistribution = nullptr;
64 if (!cacheexists) {
65 polyaDistribution = new o2::math_utils::CachingTF1("polya", name.c_str(), 0, 10.f * mGEMParam->AbsoluteGain[i]);
68 polyaDistribution->SetNpx(100000);
69 } else {
70 polyaDistribution = (o2::math_utils::CachingTF1*)outfile->Get(TString::Format("func%d", i).Data());
71 // FIXME: verify that distribution corresponds to the parameters used here
72 }
73 mGain[i].initialize(*polyaDistribution, rng);
74
75 if (!cacheexists) {
76 outfile->WriteTObject(polyaDistribution, TString::Format("func%d", i).Data());
77 }
78 delete polyaDistribution;
79 }
80
82 const float gainStack = mGEMParam->TotalGainStack;
83 const float kappaStack = mGEMParam->KappaStack;
84 const float effStack = mGEMParam->EfficiencyStack;
85 // Correct for electron losses occurring when changing kappa and the efficiency
86 const float sStack = gainStack / (kappaStack * effStack);
87 polya % kappaStack % sStack % sStack % (kappaStack - 1) % sStack;
88 std::string name = polya.str();
89 o2::math_utils::CachingTF1* polyaDistribution = nullptr;
90 if (!cacheexists) {
91 polyaDistribution = new o2::math_utils::CachingTF1("polya", name.c_str(), 0, 25.f * gainStack);
92 polyaDistribution->SetNpx(50000);
93 } else {
94 polyaDistribution = (o2::math_utils::CachingTF1*)outfile->Get("polyaStack");
95 }
96 mGainFullStack.initialize(*polyaDistribution, rng);
97
98 if (!cacheexists) {
99 outfile->WriteTObject(polyaDistribution, "polyaStack");
100 }
101 delete polyaDistribution;
102
103 if (outfile) {
104 outfile->Close();
105 }
106 watch.Stop();
107 LOG(info) << "TPC: GEM setup (polya) took " << watch.CpuTime();
108}
109
111{
112 mRandomGaus.setRandomPosition(rng);
113 mRandomFlat.setRandomPosition(rng);
114 for (auto& gain : mGain) {
115 gain.setRandomPosition(rng);
116 }
117 mGainFullStack.setRandomPosition(rng);
118}
119
121{
122 auto& cdb = CDBInterface::instance();
123 mGEMParam = &(ParameterGEM::Instance());
124 mGasParam = &(ParameterGas::Instance());
125 mGainMap = &(cdb.getGainMap());
126}
127
129{
133 const int nElectronsGEM1 = getSingleGEMAmplification(nElectrons, 0);
134 const int nElectronsGEM2 = getSingleGEMAmplification(nElectronsGEM1, 1);
135 const int nElectronsGEM3 = getSingleGEMAmplification(nElectronsGEM2, 2);
136 const int nElectronsGEM4 = getSingleGEMAmplification(nElectronsGEM3, 3);
137 return nElectronsGEM4;
138}
139
141{
145 int nElectronsGEM = 0;
146 for (int i = 0; i < nElectrons; ++i) {
147 if (mRandomFlat.getNextValue() > mGEMParam->EfficiencyStack) {
148 continue;
149 }
150 nElectronsGEM += mGainFullStack.getNextValue();
151 }
152 return nElectronsGEM;
153}
154
156{
164 int collectionGEM = getElectronLosses(nElectrons, mGEMParam->CollectionEfficiency[GEM]);
165 int amplificationGEM = getGEMMultiplication(collectionGEM, GEM);
166 int extractionGEM = getElectronLosses(amplificationGEM, mGEMParam->ExtractionEfficiency[GEM]);
167 return extractionGEM;
168}
169
170int GEMAmplification::getGEMMultiplication(int nElectrons, int GEM)
171{
174 if (nElectrons < 1) {
176 return 0;
177 } else if (nElectrons > 500) {
181 return ((mRandomGaus.getNextValue() * std::sqrt(static_cast<float>(nElectrons)) *
182 mGasParam->SigmaOverMu) +
183 nElectrons) *
184 mGEMParam->AbsoluteGain[GEM];
185 } else {
188 int electronsOut = 0;
189 for (int i = 0; i < nElectrons; ++i) {
190 electronsOut += mGain[GEM].getNextValue();
191 }
192 return electronsOut;
193 }
194}
195
196int GEMAmplification::getElectronLosses(int nElectrons, float probability)
197{
199 float electronsFloat = static_cast<float>(nElectrons);
200 if (nElectrons < 1 || probability < 0.00001) {
202 return 0;
203 } else if (probability > 0.99999) {
205 return nElectrons;
206 } else if (electronsFloat * probability >= 5.f && electronsFloat * (1.f - probability) >= 5.f) {
209 return (mRandomGaus.getNextValue() * std::sqrt(electronsFloat * probability * (1 - probability))) +
210 electronsFloat * probability + 0.5;
211 } else {
213 int nElectronsOut = 0;
214 for (int i = 0; i < nElectrons; ++i) {
215 if (mRandomFlat.getNextValue() < probability) {
216 ++nElectronsOut;
217 }
218 }
219 return nElectronsOut;
220 }
221}
Simple interface to the CDB manager.
Definition of the GEM amplification.
int32_t i
static CDBInterface & instance()
void updateParameters()
Update the OCDB parameters cached in the class. To be called once per event.
int getElectronLosses(int nElectrons, float probability)
int getEffectiveStackAmplification(int nElectrons=1)
int getStackAmplification(int nElectrons=1)
int getSingleGEMAmplification(int nElectrons, int GEM)
void setRandomRingPositions(TRandom &rng)
Move the random rings to new positions drawn from rng.
GEMAmplification(TRandom *rng=nullptr)
int getGEMMultiplication(int nElectrons, int GEM)
GLuint const GLchar * name
Definition glcorearb.h:781
float & s
Definition Utils.h:110
Global TPC definitions and constants.
float AbsoluteGain[4]
Absolute gain.
float CollectionEfficiency[4]
Collection efficiency.
float EfficiencyStack
Variable steering the single electron efficiency of the full stack for the EffectiveMode.
float ExtractionEfficiency[4]
Extraction efficiency.
float KappaStack
Variable steering the energy resolution of the full stack for the EffectiveMode.
float TotalGainStack
Total gain of the stack for the EffectiveMode.
float SigmaOverMu
Sigma over mu, gives deviation from exponential gain fluctuations.
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"