CbmRoot
Loading...
Searching...
No Matches
CbmTrdSetTracksPidLike.cxx
Go to the documentation of this file.
1/* Copyright (C) 2007-2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Florian Uhlig [committer], Etienne Bechtel */
4
5// -------------------------------------------------------------------------
6// ----- CbmTrdSetTracksPidLike source file -----
7// ----- Created 25/02/07 by F.Uhlig -----
8// ----- Updated 31/08/2016 by J. Book -----
9// -------------------------------------------------------------------------
11
12#include "CbmGlobalTrack.h"
13#include "CbmTrdGas.h"
14#include "CbmTrdHit.h"
15#include "CbmTrdTrack.h"
16#include "FairParamList.h"
17#include "FairRootManager.h"
18#include "FairRunAna.h"
19#include "FairRuntimeDb.h"
20#include "TClonesArray.h"
21#include "TH1.h"
22#include "TH2.h"
23#include "TKey.h"
24#include "TMath.h"
25#include "TObjArray.h"
26#include "TROOT.h"
27#include "TString.h"
28
29#include <TFile.h>
30
31#include <iostream>
32
33// ----- Default constructor -------------------------------------------
35// -------------------------------------------------------------------------
36
37
38// ----- Standard constructor ------------------------------------------
39CbmTrdSetTracksPidLike::CbmTrdSetTracksPidLike(const char* name, const char*) : FairTask(name) {}
40// -------------------------------------------------------------------------
41
42
43// ----- Destructor ----------------------------------------------------
45// -------------------------------------------------------------------------
46
47// ----- SetParContainers -------------------------------------------------
49{
50 fGasPar = static_cast<CbmTrdParSetGas*>(FairRunAna::Instance()->GetRuntimeDb()->getContainer("CbmTrdParSetGas"));
51}
52// -------------------------------------------------------------------------
53
54
55// ----- ReadData -------------------------------------------------
57{
58 //
59 // Read the TRD dEdx histograms.
60 //
61
62 // Get the name of the input file from CbmTrdGas
63
64 // This file stores all information about the gas layer of the TRD
65 // and can construct the required file name
66
67 if (fFileName.IsNull()) {
68
69 FairParamList* parlist = new FairParamList();
70 fGasPar->putParams(parlist);
71 FairParamObj* filenamepar = parlist->find("RepoPid");
72 fFileName.Form("%s/%s", getenv("VMCWORKDIR"), filenamepar->getParamValue());
73 //Whitespace added on some mac versions somehow to the filename resulting in fatal error, chop away here
74 while (!fFileName.EndsWith(".root"))
75 fFileName.Chop();
76 }
77
78
80 TFile* oldFile = gFile;
81 TDirectory* oldDir = gDirectory;
82
83 // Open ROOT file with the histograms
84 TFile* histFile = new TFile(fFileName, "READ");
85 if (!histFile || !histFile->IsOpen()) {
86 LOG(error) << "Could not open input file: " << fFileName;
87 return kFALSE;
88 }
89 else {
90 LOG(info) << "Input file " << fFileName << " open";
91 }
92
93 gROOT->cd();
94
95 TH1* h[10];
96 TObjArray* inArr = nullptr;
97
98 if (fMCinput) {
99 if (fMomDep) {
100 std::vector<TString> histnames{"MC_electron_p_eloss", "MC_pion_p_eloss", "MC_kaon_p_eloss", "MC_proton_p_eloss",
101 "MC_muon_p_eloss"};
102 inArr = new TObjArray(histnames.size());
103 for (size_t i = 0; i < histnames.size(); i++) {
104 h[i] = histFile->Get<TH2F>(histnames[i]);
105 if (!h[i]) {
106 LOG(info) << "No input histogram " << histnames[i].Data();
107 continue;
108 }
109
110 //set name and title
111 h[i]->SetNameTitle(histnames[i], histnames[i]);
112
113 //normalize each momentum bin to 1
114 for (Int_t x = 1; x <= h[i]->GetNbinsX(); x++) {
115 Double_t sum = 0.;
116 for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
117 sum += h[i]->GetBinContent(x, y);
118 for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
119 if (sum > 0) h[i]->SetBinContent(x, y, h[i]->GetBinContent(x, y) / sum);
120 }
121
122 inArr->Add(h[i]);
123 }
124 }
125 if (!fMomDep) {
126 std::vector<TString> histnames{"MC_electron_eloss", "MC_pion_eloss", "MC_kaon_eloss", "MC_proton_eloss",
127 "MC_muon_eloss"};
128 inArr = new TObjArray(histnames.size());
129 for (size_t i = 0; i < histnames.size(); i++) {
130 h[i] = histFile->Get<TH1F>(histnames[i]);
131 if (!h[i]) {
132 LOG(info) << "No input histogram " << histnames[i].Data();
133 continue;
134 }
135
136 //set name and title
137 h[i]->SetNameTitle(histnames[i], histnames[i]);
138
139 //normalize spectrum to 1
140 h[i]->Scale(1. / h[i]->Integral());
141
142 inArr->Add(h[i]);
143 }
144 }
145 }
146 else {
147 if (fMomDep) {
148 std::vector<TString> histnames{"ELE_electron_p_eloss", "PIO_pion_p_eloss", "ELE_kaon_p_eloss",
149 "ELE_proton_p_eloss", "ELE_muon_p_eloss"};
150 inArr = new TObjArray(histnames.size());
151 for (size_t i = 0; i < histnames.size(); i++) {
152 h[i] = histFile->Get<TH2F>(histnames[i]);
153 h[i]->SetNameTitle(histnames[i], histnames[i]);
154 if (!h[i]) {
155 LOG(info) << "No input histogram " << histnames[i].Data();
156 continue;
157 }
158
159 //set name and title
160 h[i]->SetNameTitle(histnames[i], histnames[i]);
161
162 //normalize each momentum bin to 1
163 for (Int_t x = 1; x <= h[i]->GetNbinsX(); x++) {
164 Double_t sum = 0.;
165 for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
166 sum += h[i]->GetBinContent(x, y);
167 for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
168 if (sum > 0) h[i]->SetBinContent(x, y, h[i]->GetBinContent(x, y) / sum);
169 }
170
171 inArr->Add(h[i]);
172 }
173 }
174 if (!fMomDep) {
175 std::vector<TString> histnames{"ELE_electron_eloss", "PIO_pion_eloss", "ELE_kaon_eloss", "ELE_proton_eloss",
176 "ELE_muon_eloss"};
177 inArr = new TObjArray(histnames.size());
178 for (size_t i = 0; i < histnames.size(); i++) {
179 h[i] = histFile->Get<TH1F>(histnames[i]);
180 if (!h[i]) {
181 LOG(info) << "No input histogram " << histnames[i].Data();
182 continue;
183 }
184
185 //set name and title
186 h[i]->SetNameTitle(histnames[i], histnames[i]);
187
188 //normalize spectrum to 1
189 h[i]->Scale(1. / h[i]->Integral());
190
191 inArr->Add(h[i]);
192 }
193 }
194 }
195
196 Int_t particle = 0;
197 for (Int_t i = 0; i < inArr->GetEntriesFast(); i++) {
198
199 TH1* hist = (TH1*) inArr->At(i)->Clone();
200 TString name = hist->GetTitle();
201
202 LOG_IF(info, hist->GetEntries() < 1000) << "Input histogram is almost empty for" << name.Data();
203
204 // check particles
205 if (name.Contains("electron"))
207 else if (name.Contains("pion"))
209 else if (name.Contains("kaon"))
211 else if (name.Contains("proton"))
213 else if (name.Contains("muon"))
215 else
216 continue;
217
218 // add to hist array
219 LOG(info) << "Particle histogram " << name.Data() << " added to array at " << particle;
220
221 fHistdEdx->AddAt(hist, particle);
222 }
223
225 gFile = oldFile;
226 gDirectory = oldDir;
227
229 histFile->Close();
230 delete histFile;
231
232 return kTRUE;
233}
234
235//_________________________________________________________________________
236// ----- Public method Init (abstract in base class) --------------------
238{
239
240 //
241 // Initalize data members
242 //
243
245 fHistdEdx = new TObjArray(fgkNParts);
246 fHistdEdx->SetOwner();
247
248 // Read the data from ROOT file. In case of problems return kFATAL;
249 if (!ReadData()) return kFATAL;
250
251 // Get and check FairRootManager
252 FairRootManager* ioman = FairRootManager::Instance();
253 if (!ioman) {
254 Error("Init", "RootManager not instantised!");
255 return kFATAL;
256 }
257
258 // Get GlobalTack array
259 fglobalTrackArray = (TClonesArray*) ioman->GetObject("GlobalTrack");
260 if (!fglobalTrackArray) {
261 Error("Init", "No GlobalTack array!");
262 return kFATAL;
263 }
264
265 // Get TrdTrack array
266 fTrackArray = (TClonesArray*) ioman->GetObject("TrdTrack"); //=>SG
267 if (!fTrackArray) {
268 Error("Init", "No TrdTrack array!");
269 return kFATAL;
270 }
271
272 // Get TrdTrack array
273 fTrdHitArray = (TClonesArray*) ioman->GetObject("TrdHit"); //=>SG
274 if (!fTrdHitArray) {
275 Error("Init", "No TrdHit array!");
276 return kFATAL;
277 }
278
279 return kSUCCESS;
280}
281// -------------------------------------------------------------------------
282
283
284// ----- Public method Exec --------------------------------------------
286{
287
288 Double_t momentum;
289 Double_t prob[fgkNParts];
290 Double_t probTotal;
291
292
293 if (!fTrackArray) return;
294
295 Int_t nTracks = fglobalTrackArray->GetEntriesFast();
297 for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
298
299 CbmGlobalTrack* gTrack = (CbmGlobalTrack*) fglobalTrackArray->At(iTrack);
300
301 Int_t trdTrackIndex = gTrack->GetTrdTrackIndex();
302 if (trdTrackIndex == -1) {
303 // cout <<" -W- CbmTrdSetTracksPidLike::Exec : no Trd track"<<endl;
304 continue;
305 }
306
308 CbmTrdTrack* pTrack = (CbmTrdTrack*) fTrackArray->At(trdTrackIndex);
309 if (!pTrack) {
310 Warning("Exec", "No Trd track pointer");
311 continue;
312 }
313
315 if (pTrack->GetNofHits() < 1)
316 continue;
317 else
318 fNofTracks++;
319
320
321 probTotal = 0.0;
322 for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
323 prob[iSpecies] = 1.0;
324 }
325
327 if (TMath::Abs(pTrack->GetParamFirst()->GetQp()) > 0.) {
328 momentum = TMath::Abs(1. / (pTrack->GetParamFirst()->GetQp()));
329 }
330 else if (TMath::Abs(pTrack->GetParamLast()->GetQp()) > 0.) {
331 momentum = TMath::Abs(1. / (pTrack->GetParamLast()->GetQp()));
332 }
333 else {
334 Warning("Exec", "Could not assign any momentum to the track, use p=0.");
335 momentum = 0.;
336 }
337
338
339 Double_t dEdx = 0.;
340 Double_t dEsum = 0.;
341
343 for (Int_t iTRD = 0; iTRD < pTrack->GetNofHits(); iTRD++) {
344 Int_t index = pTrack->GetHitIndex(iTRD);
345 CbmTrdHit* trdHit = (CbmTrdHit*) fTrdHitArray->At(index);
346 if (trdHit->GetELoss() < 0.) continue;
347 dEdx = trdHit->GetELoss() * 1.e+6; //GeV->keV
348 dEsum += trdHit->GetELoss() * 1.e+6; //GeV->keV
349
350 for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
351
352 prob[iSpecies] *= GetProbability(iSpecies, momentum, dEdx);
353 // if(iSpecies==0) std::cout<<momentum<<" " << dEdx<<" " << GetProbability(iSpecies, momentum, dEdx)<<std::endl;
354 } //loop species
355 } // loop TRD hits
356
358 for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
359 if (prob[iSpecies] >= 0. && prob[iSpecies] <= 1.) probTotal += prob[iSpecies];
360 }
361
363 for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
364 if (probTotal > 0) {
365 // std::cout<<iSpecies<<" " << probTotal<<" " << prob[iSpecies]<<std::endl;
366 prob[iSpecies] /= probTotal;
367 }
368 else {
369 prob[iSpecies] = -1.5;
370 }
371 }
372
373
374 pTrack->SetELoss(dEsum);
375
382 }
383}
384// -------------------------------------------------------------------------
385
386Double_t CbmTrdSetTracksPidLike::GetProbability(Int_t k, Double_t mom, Double_t dedx) const
387{
388 //
389 // Gets the Probability of having dedx at a given momentum (mom)
390 // and particle type k from the precalculated de/dx distributions
391 //
392
394 if (dedx < 0.) {
395 return -999.;
396 }
397
399 if (k < 0 || k > fgkNParts) {
400 return -999.;
401 }
402
404 TH1* hist = (TH1*) fHistdEdx->At(k);
405 if (!hist) {
406 return -999.;
407 }
408
410 if (hist->GetEntries() < 1000.) {
411 return -999.;
412 }
413
414 Int_t ndim = hist->GetDimension();
415
416 Float_t maxY = hist->GetYaxis()->GetXmax();
417 Float_t maxX = hist->GetXaxis()->GetXmax();
418
420 Bool_t overflowY = (dedx > maxY);
421 Bool_t overflowX = (ndim == 1 ? (dedx > maxX) : (mom > maxX));
422
424 Float_t binwidthY = (ndim == 1 ? 0. : hist->GetYaxis()->GetBinWidth(hist->GetNbinsY()));
425 Float_t binwidthX = hist->GetXaxis()->GetBinWidth(hist->GetNbinsX());
426
428 Int_t bin = 0;
429 if (ndim == 1) { // 1-dimensional input histograms
430 hist->FindBin((overflowX ? maxX - binwidthX : dedx));
431 }
432 else { // 2-dimensional input histograms
433 hist->FindBin((overflowX ? maxX - binwidthX : mom), (overflowY ? maxY - binwidthY : dedx));
434 }
435
437 if (TMath::Abs(hist->GetBinContent(bin)) < 1.e-15) {
438 Double_t con = -999.;
439 if (ndim == 1) { // 1-dimensional input histograms
440 con = hist->Interpolate((overflowX ? maxX - binwidthX : dedx));
441 }
442 else { // 2-dimensional input histograms
443 con = ((TH2*) hist)->Interpolate((overflowX ? maxX - binwidthX : mom), (overflowY ? maxY - binwidthY : dedx));
444 }
445 return con;
446 }
447 else {
448 return hist->GetBinContent(bin);
449 }
450}
451
452// ----- Public method Finish ------------------------------------------
454// -------------------------------------------------------------------------
455
456
ClassImp(CbmConverterManager)
Container for gas properties of TRD.
Class for hits in TRD detector.
int32_t GetTrdTrackIndex() const
Data class with information on a STS local track.
const FairTrackParam * GetParamLast() const
Definition CbmTrack.h:69
virtual int32_t GetNofHits() const
Definition CbmTrack.h:58
const FairTrackParam * GetParamFirst() const
Definition CbmTrack.h:68
int32_t GetHitIndex(int32_t iHit) const
Definition CbmTrack.h:59
data class for a reconstructed Energy-4D measurement in the TRD
Definition CbmTrdHit.h:40
double GetELoss() const
Definition CbmTrdHit.h:79
Describe TRD module working settings (HV, etc)
void putParams(FairParamList *)
Double_t GetProbability(Int_t iType, Double_t mom, Double_t dedx) const
virtual void Exec(Option_t *opt)
void SetPidLikeMU(double value)
Definition CbmTrdTrack.h:57
void SetPidLikePR(double value)
Definition CbmTrdTrack.h:56
void SetELoss(double eLoss)
Definition CbmTrdTrack.h:52
void SetPidLikeKA(double value)
Definition CbmTrdTrack.h:55
void SetPidLikePI(double value)
Definition CbmTrdTrack.h:54
void SetPidLikeEL(double value)
Definition CbmTrdTrack.h:53