17 #include "TRestDetectorPositionMappingProcess.h"
20 #include <TPaveText.h>
27 TRestDetectorPositionMappingProcess::TRestDetectorPositionMappingProcess() { Initialize(); }
29 TRestDetectorPositionMappingProcess::~TRestDetectorPositionMappingProcess() {}
32 SetSectionName(this->ClassName());
33 SetLibraryVersion(LIBRARY_VERSION);
41 fReadout = GetMetadata<TRestDetectorReadout>();
42 fCalib = GetMetadata<TRestDetectorGainMap>();
43 fGas = GetMetadata<TRestDetectorGas>();
44 if (fReadout ==
nullptr) {
46 RESTError <<
"You must set a TRestDetectorReadout metadata object to create gain map!"
51 double xmin, xmax, ymin, ymax;
52 fReadout->GetReadoutPlane(0)->GetBoundaries(xmin, xmax, ymin, ymax);
53 fAreaGainMap =
new TH2F(
"areaGainMap",
"areaGainMap", fNBinsX, xmin, xmax, fNBinsY, ymin, ymax);
54 fAreaCounts =
new TH2D(
"areaCounts",
"areaCounts", fNBinsX, xmin, xmax, fNBinsY, ymin, ymax);
56 new TH2D(
"areaThrIntegralSum",
"areaThrIntegralSum", fNBinsX, xmin, xmax, fNBinsY, ymin, ymax);
59 if (fApplyGainCorrection) {
60 if (fCalib ==
nullptr || fCalib->f2DGainMapping ==
nullptr) {
61 RESTError <<
"You must set a TRestDetectorGainMap metadata object to apply gain correction!"
71 Double_t newEnergy = 0;
73 SetObservableValue(
"AreaCorrEnergy", newEnergy);
75 double x = fHitsEvent->GetMeanPositionX();
76 double y = fHitsEvent->GetMeanPositionY();
77 double z = fHitsEvent->GetMeanPositionZ();
79 double e = fHitsEvent->GetTotalEnergy();
80 double n = fHitsEvent->GetNumberOfHits();
84 if (TMath::IsNaN(x) || TMath::IsNaN(y)) {
89 if (e > fEnergyCutRange.X() && e < fEnergyCutRange.Y() && n > fNHitsCutRange.X() &&
90 n < fNHitsCutRange.Y()) {
92 int bin = fAreaThrIntegralSum->FindBin(x, y);
93 fAreaThrIntegralSum->AddBinContent(bin, e);
94 fAreaCounts->AddBinContent(bin, 1);
98 else if (fApplyGainCorrection) {
99 double correction = GetCorrection2(x, y);
100 correction *= GetCorrection3(x, y, z);
102 newEnergy = e * correction;
105 SetObservableValue(
"AreaCorrEnergy", newEnergy);
110 double TRestDetectorPositionMappingProcess::GetCorrection2(
double x,
double y) {
111 int bin = fCalib->f2DGainMapping->FindBin(x, y);
112 return fCalib->f2DGainMapping->GetBinContent(bin);
115 double TRestDetectorPositionMappingProcess::GetCorrection3(
double x,
double y,
double z) {
117 if (fCalib->f3DGainMapping !=
nullptr && fCalib->f3DGainMapping->GetEntries() > 0) {
118 int bin = fCalib->f3DGainMapping->FindBin(x, y, z);
119 result *= fCalib->f2DGainMapping->GetBinContent(bin);
121 if (fGas !=
nullptr && fGas->GetElectronLifeTime() != 0) {
122 double dt = z / fGas->GetDriftVelocity();
123 result *= exp(dt / fGas->GetElectronLifeTime());
129 if (fCreateGainMap) {
133 for (
int i = 1; i <= fAreaGainMap->GetNbinsX(); i++) {
134 for (
int j = 1; j <= fAreaGainMap->GetNbinsY(); j++) {
135 if (fAreaCounts->GetBinContent(i, j) > 100) {
136 double meanthrintegral =
137 fAreaThrIntegralSum->GetBinContent(i, j) / fAreaCounts->GetBinContent(i, j);
138 fAreaGainMap->SetBinContent(i, j, meanthrintegral);
139 sum += meanthrintegral;
146 double meanmean = sum / n;
149 for (
int i = 1; i <= fAreaGainMap->GetNbinsX(); i++) {
150 for (
int j = 1; j <= fAreaGainMap->GetNbinsY(); j++) {
151 if (fAreaGainMap->GetBinContent(i, j) == 0) {
152 fAreaGainMap->SetBinContent(i, j, 1);
154 fAreaGainMap->SetBinContent(i, j, meanmean / fAreaGainMap->GetBinContent(i, j));
159 if (fCalib ==
nullptr) {
162 fCalib->f2DGainMapping = fAreaGainMap;
163 fCalib->SetName(
"PositionCalibration");
166 r->SetOutputFileName(fMappingSave);
170 if (fAreaGainMap !=
nullptr) fAreaGainMap->Write();
179 string mode = GetParameter(
"mode",
"apply");
180 if (mode ==
"create") {
181 fCreateGainMap =
true;
182 fApplyGainCorrection =
false;
183 fSingleThreadOnly =
true;
184 }
else if (mode ==
"apply") {
185 fCreateGainMap =
false;
186 fApplyGainCorrection =
true;
187 fSingleThreadOnly =
false;
189 RESTError <<
"illegal mode definition! supported: create, apply" << RESTendl;
195 fMappingSave = GetParameter(
"save",
"calib.root");
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
void Initialize() override
Making default settings.
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.
A base class for any REST event.
Data provider and manager in REST.
TFile * FormOutputFile()
Create a new TFile as REST output file. Writing metadata objects into it.
void AddMetadata(TRestMetadata *meta)
add metadata object to the metadata list
Double_t StringToDouble(std::string in)
Gets a double from a string.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.