CbmRoot
CbmTrdSetTracksPidLike.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- CbmTrdSetTracksPidLike source file -----
3 // ----- Created 25/02/07 by F.Uhlig -----
4 // ----- Updated 31/08/2016 by J. Book -----
5 // -------------------------------------------------------------------------
7 
8 #include "CbmTrdGas.h"
9 #include "CbmTrdHit.h"
10 #include "CbmTrdTrack.h"
11 
12 #include "CbmGlobalTrack.h"
13 #include "FairRootManager.h"
14 
15 #include "TClonesArray.h"
16 #include "TH1.h"
17 #include "TH2.h"
18 #include "TKey.h"
19 #include "TMath.h"
20 #include "TObjArray.h"
21 #include "TROOT.h"
22 #include "TString.h"
23 #include <TFile.h>
24 
25 #include <iostream>
26 
27 // ----- Default constructor -------------------------------------------
29  : CbmTrdSetTracksPidLike("TrdPidLI", "TrdPidLI") {}
30 // -------------------------------------------------------------------------
31 
32 
33 // ----- Standard constructor ------------------------------------------
34 CbmTrdSetTracksPidLike::CbmTrdSetTracksPidLike(const char* name, const char*)
35  : FairTask(name) {}
36 // -------------------------------------------------------------------------
37 
38 
39 // ----- Destructor ----------------------------------------------------
41 // -------------------------------------------------------------------------
42 
43 // ----- SetParContainers -------------------------------------------------
45 // -------------------------------------------------------------------------
46 
47 
48 // ----- RaedData -------------------------------------------------
50  //
51  // Read the TRD dEdx histograms.
52  //
53 
54  // Get the name of the input file from CbmTrdGas
55 
56  // This file stores all information about the gas layer of the TRD
57  // and can construct the required file name
58 
59  if (fFileName.IsNull()) {
60  CbmTrdGas* fTrdGas = CbmTrdGas::Instance();
61  if (fTrdGas == 0) {
62  fTrdGas = new CbmTrdGas();
63  fTrdGas->Init();
64  }
65  fFileName = fTrdGas->GetFileName("Like");
66  }
67 
68  // Open ROOT file with the histograms
69  TFile* histFile = new TFile(fFileName, "READ");
70  if (!histFile || !histFile->IsOpen()) {
71  Error("ReadData", "Could not open input file: %s", fFileName.Data());
72  return kFALSE;
73  } else {
74  Info("ReadData", "input file %s opened", fFileName.Data());
75  }
76 
77  gROOT->cd();
78 
79  TH2D* h[10];
80  TObjArray* inArr = NULL;
81 
82  if (fMCinput) {
83  if (fMomDep) {
84  std::vector<TString> histnames {"MC_electron_p_eloss",
85  "MC_pion_p_eloss",
86  "MC_kaon_p_eloss",
87  "MC_proton_p_eloss",
88  "MC_muon_p_eloss"};
89  inArr = new TObjArray(histnames.size());
90  for (size_t i = 0; i < histnames.size(); i++) {
91  h[i] = (TH2D*) histFile->Get(histnames[i]);
92  if (!h[i]) continue;
93 
94  //set name and title
95  h[i]->SetNameTitle(histnames[i], histnames[i]);
96 
97  //normalize each momentum bin to 1
98  for (Int_t x = 1; x <= h[i]->GetNbinsX(); x++) {
99  Double_t sum = 0.;
100  for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
101  sum += h[i]->GetBinContent(x, y);
102  for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
103  if (sum > 0)
104  h[i]->SetBinContent(x, y, h[i]->GetBinContent(x, y) / sum);
105  }
106 
107  inArr->Add(h[i]);
108  }
109  }
110  if (!fMomDep) {
111  std::vector<TString> histnames {"MC_electron_eloss",
112  "MC_pion_eloss",
113  "MC_kaon_eloss",
114  "MC_proton_eloss",
115  "MC_muon_eloss"};
116  inArr = new TObjArray(histnames.size());
117  for (size_t i = 0; i < histnames.size(); i++) {
118  h[i] = (TH2D*) histFile->Get(histnames[i]);
119  if (!h[i]) continue;
120 
121  //set name and title
122  h[i]->SetNameTitle(histnames[i], histnames[i]);
123 
124  //normalize spectrum to 1
125  h[i]->Scale(1. / h[i]->Integral());
126 
127  inArr->Add(h[i]);
128  }
129  }
130  } else {
131  if (fMomDep) {
132  std::vector<TString> histnames {"ELE_electron_p_eloss",
133  "PIO_pion_p_eloss",
134  "ELE_kaon_p_eloss",
135  "ELE_proton_p_eloss",
136  "ELE_muon_p_eloss"};
137  inArr = new TObjArray(histnames.size());
138  for (size_t i = 0; i < histnames.size(); i++) {
139  h[i] = (TH2D*) histFile->Get(histnames[i]);
140  h[i]->SetNameTitle(histnames[i], histnames[i]);
141  if (!h[i]) continue;
142 
143  //set name and title
144  h[i]->SetNameTitle(histnames[i], histnames[i]);
145 
146  //normalize each momentum bin to 1
147  for (Int_t x = 1; x <= h[i]->GetNbinsX(); x++) {
148  Double_t sum = 0.;
149  for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
150  sum += h[i]->GetBinContent(x, y);
151  for (Int_t y = 1; y <= h[i]->GetNbinsY(); y++)
152  if (sum > 0)
153  h[i]->SetBinContent(x, y, h[i]->GetBinContent(x, y) / sum);
154  }
155 
156  inArr->Add(h[i]);
157  }
158  }
159  if (!fMomDep) {
160  std::vector<TString> histnames {"ELE_electron_eloss",
161  "PIO_pion_eloss",
162  "ELE_kaon_eloss",
163  "ELE_proton_eloss",
164  "ELE_muon_eloss"};
165  inArr = new TObjArray(histnames.size());
166  for (size_t i = 0; i < histnames.size(); i++) {
167  h[i] = (TH2D*) histFile->Get(histnames[i]);
168  if (!h[i]) continue;
169 
170  //set name and title
171  h[i]->SetNameTitle(histnames[i], histnames[i]);
172 
173  //normalize spectrum to 1
174  h[i]->Scale(1. / h[i]->Integral());
175 
176  inArr->Add(h[i]);
177  }
178  }
179  }
180 
181  Int_t particle = 0;
182  for (Int_t i = 0; i < inArr->GetEntriesFast(); i++) {
183 
184  TH1* hist = (TH1*) inArr->At(i)->Clone();
185  TString name = hist->GetTitle();
186 
187  // check particles
188  if (name.Contains("electron"))
190  else if (name.Contains("pion"))
192  else if (name.Contains("kaon"))
194  else if (name.Contains("proton"))
196  else if (name.Contains("muon"))
198  else
199  continue;
200 
201  // add to hist array
202  Info("ReadData",
203  "particle histogram %s added to array at %d",
204  name.Data(),
205  particle);
206 
207  fHistdEdx->AddAt(hist, particle);
208  }
209 
211  histFile->Close();
212  delete histFile;
213 
214  return kTRUE;
215 }
216 
217 //_________________________________________________________________________
218 // ----- Public method Init (abstract in base class) --------------------
220 
221  //
222  // Initalize data members
223  //
224 
226  fHistdEdx = new TObjArray(fgkNParts);
227  fHistdEdx->SetOwner();
228 
229  // Read the data from ROOT file. In case of problems return kFATAL;
230  if (!ReadData()) return kFATAL;
231 
232  // Get and check FairRootManager
233  FairRootManager* ioman = FairRootManager::Instance();
234  if (!ioman) {
235  Error("Init", "RootManager not instantised!");
236  return kFATAL;
237  }
238 
239  // Get GlobalTack array
240  fglobalTrackArray = (TClonesArray*) ioman->GetObject("GlobalTrack");
241  if (!fglobalTrackArray) {
242  Error("Init", "No GlobalTack array!");
243  return kFATAL;
244  }
245 
246  // Get TrdTrack array
247  fTrackArray = (TClonesArray*) ioman->GetObject("TrdTrack"); //=>SG
248  if (!fTrackArray) {
249  Error("Init", "No TrdTrack array!");
250  return kFATAL;
251  }
252 
253  // Get TrdTrack array
254  fTrdHitArray = (TClonesArray*) ioman->GetObject("TrdHit"); //=>SG
255  if (!fTrdHitArray) {
256  Error("Init", "No TrdHit array!");
257  return kFATAL;
258  }
259 
260  return kSUCCESS;
261 }
262 // -------------------------------------------------------------------------
263 
264 
265 // ----- Public method Exec --------------------------------------------
267 
268  Double_t momentum;
269  Double_t prob[fgkNParts];
270  Double_t probTotal;
271 
272 
273  if (!fTrackArray) return;
274 
275  Int_t nTracks = fglobalTrackArray->GetEntriesFast();
277  for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
278 
279  CbmGlobalTrack* gTrack = (CbmGlobalTrack*) fglobalTrackArray->At(iTrack);
280 
281  Int_t trdTrackIndex = gTrack->GetTrdTrackIndex();
282  if (trdTrackIndex == -1) {
283  // cout <<" -W- CbmTrdSetTracksPidLike::Exec : no Trd track"<<endl;
284  continue;
285  }
286 
288  CbmTrdTrack* pTrack = (CbmTrdTrack*) fTrackArray->At(trdTrackIndex);
289  if (!pTrack) {
290  Warning("Exec", "No Trd track pointer");
291  continue;
292  }
293 
295  if (pTrack->GetNofHits() < 1)
296  continue;
297  else
298  fNofTracks++;
299 
300 
301  probTotal = 0.0;
302  for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
303  prob[iSpecies] = 1.0;
304  }
305 
307  if (TMath::Abs(pTrack->GetParamFirst()->GetQp()) > 0.) {
308  momentum = TMath::Abs(1. / (pTrack->GetParamFirst()->GetQp()));
309  } else if (TMath::Abs(pTrack->GetParamLast()->GetQp()) > 0.) {
310  momentum = TMath::Abs(1. / (pTrack->GetParamLast()->GetQp()));
311  } else {
312  Warning("Exec", "Could not assign any momentum to the track, use p=0.");
313  momentum = 0.;
314  }
315 
316 
317  Double_t dEdx = 0.;
318  Double_t dEsum = 0.;
319 
321  for (Int_t iTRD = 0; iTRD < pTrack->GetNofHits(); iTRD++) {
322  Int_t index = pTrack->GetHitIndex(iTRD);
323  CbmTrdHit* trdHit = (CbmTrdHit*) fTrdHitArray->At(index);
324 
325  dEdx = trdHit->GetELoss() * 1.e+6; //GeV->keV
326  dEsum += trdHit->GetELoss() * 1.e+6; //GeV->keV
327 
328  for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
329 
330  prob[iSpecies] *= GetProbability(iSpecies, momentum, dEdx);
331  // if(iSpecies==0) std::cout<<momentum<<" " << dEdx<<" " << GetProbability(iSpecies, momentum, dEdx)<<std::endl;
332  } //loop species
333  } // loop TRD hits
334 
336  for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
337  if (prob[iSpecies] >= 0. && prob[iSpecies] <= 1.)
338  probTotal += prob[iSpecies];
339  }
340 
342  for (Int_t iSpecies = 0; iSpecies < fgkNParts; iSpecies++) {
343  if (probTotal > 0) {
344  // std::cout<<iSpecies<<" " << probTotal<<" " << prob[iSpecies]<<std::endl;
345  prob[iSpecies] /= probTotal;
346  } else {
347  prob[iSpecies] = -1.5;
348  }
349  }
350 
351 
352  pTrack->SetELoss(dEsum);
353 
360  }
361 }
362 // -------------------------------------------------------------------------
363 
365  Double_t mom,
366  Double_t dedx) const {
367  //
368  // Gets the Probability of having dedx at a given momentum (mom)
369  // and particle type k from the precalculated de/dx distributions
370  //
371 
373  if (k < 0 || k > fgkNParts) { return -999.; }
374 
376  TH1* hist = (TH1*) fHistdEdx->At(k);
377  if (!hist) {
378  Info("GetProbability", "no input histogram");
379  return -999.;
380  }
381 
383  if (hist->GetEntries() < 1000.) {
384  Info("GetProbability", "input histogram is almost empty");
385  return -999.;
386  }
387 
388  Int_t ndim = hist->GetDimension();
389 
390  Float_t maxY = hist->GetYaxis()->GetXmax();
391  Float_t maxX = hist->GetXaxis()->GetXmax();
392 
394  Bool_t overflowY = (dedx > maxY);
395  Bool_t overflowX = (ndim == 1 ? (dedx > maxX) : (mom > maxX));
396 
398  Float_t binwidthY =
399  (ndim == 1 ? 0. : hist->GetYaxis()->GetBinWidth(hist->GetNbinsY()));
400  Float_t binwidthX = hist->GetXaxis()->GetBinWidth(hist->GetNbinsX());
401 
403  Int_t bin = 0;
404  if (ndim == 1) { // 1-dimensional input histograms
405  hist->FindBin((overflowX ? maxX - binwidthX : dedx));
406  } else { // 2-dimensional input histograms
407  hist->FindBin((overflowX ? maxX - binwidthX : mom),
408  (overflowY ? maxY - binwidthY : dedx));
409  }
410 
412  if (TMath::Abs(hist->GetBinContent(bin)) < 1.e-15) {
413  Double_t con = -999.;
414  if (ndim == 1) { // 1-dimensional input histograms
415  con = hist->Interpolate((overflowX ? maxX - binwidthX : dedx));
416  } else { // 2-dimensional input histograms
417  con = ((TH2*) hist)
418  ->Interpolate((overflowX ? maxX - binwidthX : mom),
419  (overflowY ? maxY - binwidthY : dedx));
420  }
421  return con;
422  } else {
423  return hist->GetBinContent(bin);
424  }
425 }
426 
427 // ----- Public method Finish ------------------------------------------
429 // -------------------------------------------------------------------------
430 
431 
CbmTrdGas::GetFileName
TString GetFileName(TString method) const
Definition: CbmTrdGas.cxx:161
CbmTrdSetTracksPidLike::~CbmTrdSetTracksPidLike
virtual ~CbmTrdSetTracksPidLike()
Definition: CbmTrdSetTracksPidLike.cxx:40
CbmTrack::GetParamLast
const FairTrackParam * GetParamLast() const
Definition: CbmTrack.h:62
CbmTrack::GetNofHits
virtual Int_t GetNofHits() const
Definition: CbmTrack.h:53
CbmTrdGas.h
Container for gas properties of TRD.
CbmTrdSetTracksPidLike::fglobalTrackArray
TClonesArray * fglobalTrackArray
Definition: CbmTrdSetTracksPidLike.h:81
CbmTrdHit::GetELoss
Double_t GetELoss() const
Definition: CbmTrdHit.h:79
CbmTrdTrack::SetPidLikePI
void SetPidLikePI(Double_t value)
Definition: CbmTrdTrack.h:45
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmTrdHit
data class for a reconstructed Energy-4D measurement in the TRD
Definition: CbmTrdHit.h:35
CbmTrdTrack::SetPidLikeEL
void SetPidLikeEL(Double_t value)
Definition: CbmTrdTrack.h:44
CbmTrdSetTracksPidLike::Exec
virtual void Exec(Option_t *opt)
Definition: CbmTrdSetTracksPidLike.cxx:266
CbmTrdSetTracksPidLike::Finish
virtual void Finish()
Definition: CbmTrdSetTracksPidLike.cxx:428
CbmTrdSetTracksPidLike::kElectron
@ kElectron
Definition: CbmTrdSetTracksPidLike.h:88
CbmGlobalTrack.h
CbmTrdSetTracksPidLike.h
CbmTrdSetTracksPidLike::fTrackArray
TClonesArray * fTrackArray
Definition: CbmTrdSetTracksPidLike.h:79
CbmTrdSetTracksPidLike::fMCinput
Bool_t fMCinput
Definition: CbmTrdSetTracksPidLike.h:77
CbmTrdSetTracksPidLike::fHistdEdx
TObjArray * fHistdEdx
Definition: CbmTrdSetTracksPidLike.h:83
h
Data class with information on a STS local track.
CbmTrdSetTracksPidLike::Init
virtual InitStatus Init()
Definition: CbmTrdSetTracksPidLike.cxx:219
CbmTrack::GetHitIndex
Int_t GetHitIndex(Int_t iHit) const
Definition: CbmTrack.h:54
CbmGlobalTrack::GetTrdTrackIndex
Int_t GetTrdTrackIndex() const
Definition: CbmGlobalTrack.h:39
CbmTrdSetTracksPidLike::kPion
@ kPion
Definition: CbmTrdSetTracksPidLike.h:89
CbmTrdHit.h
Class for hits in TRD detector.
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmTrdSetTracksPidLike::fMomDep
Bool_t fMomDep
Definition: CbmTrdSetTracksPidLike.h:78
CbmTrdTrack::SetPidLikeMU
void SetPidLikeMU(Double_t value)
Definition: CbmTrdTrack.h:48
CbmTrack::GetParamFirst
const FairTrackParam * GetParamFirst() const
Definition: CbmTrack.h:61
CbmTrdSetTracksPidLike::fFileName
TString fFileName
Definition: CbmTrdSetTracksPidLike.h:76
CbmTrdTrack
Definition: CbmTrdTrack.h:22
CbmTrdSetTracksPidLike::SetParContainers
virtual void SetParContainers()
Definition: CbmTrdSetTracksPidLike.cxx:44
CbmGlobalTrack
Definition: CbmGlobalTrack.h:26
CbmTrdTrack::SetPidLikeKA
void SetPidLikeKA(Double_t value)
Definition: CbmTrdTrack.h:46
CbmTrdTrack::SetELoss
void SetELoss(Double_t eLoss)
Definition: CbmTrdTrack.h:43
CbmTrdTrack::SetPidLikePR
void SetPidLikePR(Double_t value)
Definition: CbmTrdTrack.h:47
CbmTrdGas::Instance
static CbmTrdGas * Instance()
Definition: CbmTrdGas.h:29
CbmTrdSetTracksPidLike::fgkNParts
static const Int_t fgkNParts
Definition: CbmTrdSetTracksPidLike.h:86
x
Double_t x
Definition: CbmMvdSensorDigiToHitTask.cxx:68
CbmTrdSetTracksPidLike::fTrdHitArray
TClonesArray * fTrdHitArray
Definition: CbmTrdSetTracksPidLike.h:80
y
Double_t y
Definition: CbmMvdSensorDigiToHitTask.cxx:68
CbmTrdSetTracksPidLike::kKaon
@ kKaon
Definition: CbmTrdSetTracksPidLike.h:90
CbmTrdGas
Definition: CbmTrdGas.h:14
CbmTrdTrack.h
CbmTrdSetTracksPidLike::GetProbability
Double_t GetProbability(Int_t iType, Double_t mom, Double_t dedx) const
Definition: CbmTrdSetTracksPidLike.cxx:364
CbmTrdSetTracksPidLike::ReadData
Bool_t ReadData()
Definition: CbmTrdSetTracksPidLike.cxx:49
CbmTrdGas::Init
void Init()
Definition: CbmTrdGas.cxx:40
CbmTrdSetTracksPidLike::kProton
@ kProton
Definition: CbmTrdSetTracksPidLike.h:91
CbmTrdSetTracksPidLike::kMuon
@ kMuon
Definition: CbmTrdSetTracksPidLike.h:92
CbmTrdSetTracksPidLike::fNofTracks
Int_t fNofTracks
Definition: CbmTrdSetTracksPidLike.h:84
CbmTrdSetTracksPidLike
Definition: CbmTrdSetTracksPidLike.h:30
CbmTrdSetTracksPidLike::CbmTrdSetTracksPidLike
CbmTrdSetTracksPidLike()
Definition: CbmTrdSetTracksPidLike.cxx:28