CbmRoot
CbmTrdElectronsTrainAnn.cxx
Go to the documentation of this file.
1 
3 #include "CbmMCTrack.h"
4 #include "CbmTrackMatchNew.h"
5 #include "CbmTrdHit.h"
6 #include "CbmTrdPoint.h"
7 #include "CbmTrdTrack.h"
8 #include "TCanvas.h"
9 #include "TClonesArray.h"
10 #include "TCut.h"
11 #include "TFile.h"
12 #include "TGraph.h"
13 #include "TLine.h"
14 #include "TMVA/Config.h"
15 #include "TMVA/PDF.h"
16 #include "TMath.h"
17 #include "TRandom.h"
18 #include "TSystem.h"
19 
20 #include "CbmDrawHist.h"
21 
22 #include <boost/assign/list_of.hpp>
23 
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 using boost::assign::list_of;
30 using std::cout;
31 using std::endl;
32 using std::string;
33 using std::stringstream;
34 using std::vector;
35 
37  : FairTask()
38  , fMCTracks(NULL)
39  , fTrdPoints(NULL)
40  , fTrdTracks(NULL)
41  , fTrdTrackMatches(NULL)
42  , fTrdHits(NULL)
43  , fEloss()
44  , fHists()
45  , fhResults()
46  , fhMeanEloss()
47  , fhEloss()
48  , fhElossSort()
49  , fEventNum(0)
50  , fOutputDir("results/")
51  , fSigmaError(0.0)
52  , fIsDoTrain(true)
53  , fTransformType(1)
54  , fBeamDataFile("")
55  , fBeamDataPiHist("")
56  , fBeamDataElHist("")
57  , fAnnInput()
58  , fXOut(-1.)
59  , fNofTrdLayers(nofTrdLayers)
60  , fMaxEval(1.3)
61  , fMinEval(-1.3)
62  , fNN(NULL)
63  , fReader(NULL)
64  , fIdMethod(kANN)
65  , fNofAnnEpochs(50)
66  , fNofTrainSamples(2500)
67  , fElIdEfficiency(0.9)
68  , fhOutput()
69  , fhCumProbOutput()
70  , fhInput() {
71  fEloss.resize(2);
72  fhMeanEloss.resize(2);
73  fhEloss.resize(2);
74  string s;
75 
76  TH1::SetDefaultBufferSize(30000);
77 
78  fhResults = new TH1D("fhResults", "fhResults", 2, 0, 2);
79  fHists.push_back(fhResults);
80 
81  for (int i = 0; i < 2; i++) {
82  if (i == 0) s = "El";
83  if (i == 1) s = "Pi";
84  fhMeanEloss[i] = new TH1D(("fhMeanEloss" + s).c_str(),
85  "fhMeanEloss;Mean energy loss [a.u.];Yield",
86  100,
87  0.,
88  50e-6);
89  fHists.push_back(fhMeanEloss[i]);
90  fhEloss[i] = new TH1D(("fhEloss" + s).c_str(),
91  "fhEloss;Energy loss [a.u.];Yield",
92  100,
93  0.,
94  50e-6);
95  fHists.push_back(fhEloss[i]);
96  }
97 
98  Int_t nofSortBins = 200;
99  fhElossSort.resize(2);
100  for (int j = 0; j < 2; j++) {
101  fhElossSort[j].resize(fNofTrdLayers);
102  for (Int_t i = 0; i < fNofTrdLayers; i++) {
103  std::stringstream ss1;
104  if (j == 0) ss1 << "fhElossSortEl" << i;
105  if (j == 1) ss1 << "fhElossSortPi" << i;
106  fhElossSort[j][i] =
107  new TH1D(ss1.str().c_str(),
108  (ss1.str() + ";Energy loss [a.u.];Counters").c_str(),
109  nofSortBins,
110  0.,
111  0.);
112  }
113  }
114 
115  // Histograms for algorithm testing
116  Int_t nofBins = 10000;
117  fhOutput.resize(2);
118  fhCumProbOutput.resize(2);
119  fhInput.resize(2);
120  for (Int_t i = 0; i < 2; i++) {
121  s = (i == 0) ? "El" : "Pi";
122  fhOutput[i] = new TH1D(("fhOutput" + s).c_str(),
123  "fhOutput;Output;Counter",
124  nofBins,
125  fMinEval,
126  fMaxEval);
127  fhCumProbOutput[i] =
128  new TH1D(("fhCumProbOutput" + s).c_str(),
129  "fhCumProbOutput;Output;Cumulative probability",
130  nofBins,
131  fMinEval,
132  fMaxEval);
133 
134  fhInput[i].resize(fNofTrdLayers);
135  for (Int_t j = 0; j < fNofTrdLayers; j++) {
136  stringstream ss;
137  ss << s << j;
138  fhInput[i][j] =
139  new TH1D(("fhInput" + ss.str()).c_str(), "fhInput", 100, 0.0, 0.0);
140  }
141  }
142 }
143 
145 
147  FairRootManager* ioman = FairRootManager::Instance();
148  if (NULL == ioman) {
149  Fatal("-E- CbmTrdElectronsTrainAnn::Init", "RootManager not instantised!");
150  }
151 
152  fMCTracks = (TClonesArray*) ioman->GetObject("MCTrack");
153  if (NULL == fMCTracks) {
154  Fatal("-E- CbmTrdElectronsTrainAnn::Init", "No MCTrack array!");
155  }
156 
157  fTrdPoints = (TClonesArray*) ioman->GetObject("TrdPoint");
158  if (NULL == fTrdPoints) {
159  Fatal("-E- CbmTrdElectronsTrainAnn::Init", "No TRDPoint array!");
160  }
161 
162  fTrdTracks = (TClonesArray*) ioman->GetObject("TrdTrack");
163  if (NULL == fTrdTracks) {
164  Fatal("-E- CbmTrdElectronsTrainAnn::Init", "No TrdTrack array!");
165  }
166 
167  fTrdTrackMatches = (TClonesArray*) ioman->GetObject("TrdTrackMatch");
168  if (NULL == fTrdTrackMatches) {
169  Fatal("-E- CbmTrdElectronsTrainAnn::Init", "No TrdTrackMatch array!");
170  }
171 
172  fTrdHits = (TClonesArray*) ioman->GetObject("TrdHit");
173  if (NULL == fTrdHits) {
174  Fatal("-E- CbmTrdElectronsTrainAnn::Init", "No TRDHit array!");
175  }
176 
177  return kSUCCESS;
178 }
179 
181  fEventNum++;
182  cout << "CbmTrdElectronsTrainAnn, event " << fEventNum << endl;
183 
185  FillElossHist();
187 
188  cout << "Nof electrons = " << fEloss[0].size() << endl;
189  cout << "Nof pions = " << fEloss[1].size() << endl;
190 
191  // Finish();
192 }
193 
195  Run();
196  Draw();
197 
198  if (fOutputDir != "") { gSystem->mkdir(fOutputDir.c_str(), true); }
199  TFile* f =
200  new TFile(string(fOutputDir + "/trd_elid_hist.root").c_str(), "RECREATE");
201  for (unsigned int i = 0; i < fHists.size(); i++) {
202  fHists[i]->Write();
203  }
204  f->Close();
205 }
206 
209  FillElossHist();
211 
212  cout << "Nof electrons = " << fEloss[0].size() << endl;
213  cout << "Nof pions = " << fEloss[1].size() << endl;
214 
215  Finish();
216 }
217 
219  if (fBeamDataFile == "" || fBeamDataPiHist == "" || fBeamDataElHist == "") {
220  Fatal("-E- CbmTrdElectronsTrainAnn::FillElossVectorReal()",
221  "Set input file for beam data and histogram names!");
222  }
223 
224  TFile* file = new TFile(fBeamDataFile.c_str(), "READ");
225  TH1F* hPion = (TH1F*) file->Get(fBeamDataPiHist.c_str())->Clone();
226  TH1F* hElectron = (TH1F*) file->Get(fBeamDataElHist.c_str())->Clone();
227 
228  double scaleX = fhEloss[0]->GetXaxis()->GetBinUpEdge(fhEloss[0]->GetNbinsX())
229  / hPion->GetXaxis()->GetBinUpEdge(hPion->GetNbinsX());
230 
231  int nofSimulatedParticles = 1000000;
232  fEloss[0].resize(nofSimulatedParticles);
233  fEloss[1].resize(nofSimulatedParticles);
234 
235  for (int iP = 0; iP < 2; iP++) {
236  TH1F* hist = hElectron;
237  if (iP == 1) hist = hPion;
238  for (int iT = 0; iT < nofSimulatedParticles; iT++) {
239  fEloss[iP][iT].resize(fNofTrdLayers);
240  for (int iH = 0; iH < fNofTrdLayers; iH++) {
241  double eloss = scaleX * hist->GetRandom();
242  TrdEloss e(eloss);
243  fEloss[iP][iT][iH] = e;
244  }
245  }
246  }
247 }
248 
250  Int_t nofTrdTracks = fTrdTracks->GetEntries();
251  for (Int_t iTrdTrack = 0; iTrdTrack < nofTrdTracks; iTrdTrack++) {
252  CbmTrdTrack* trdtrack = (CbmTrdTrack*) fTrdTracks->At(iTrdTrack);
253  Int_t nHits = trdtrack->GetNofHits();
254 
255 
256  CbmTrackMatchNew* trdmatch =
257  (CbmTrackMatchNew*) fTrdTrackMatches->At(iTrdTrack);
258  Int_t iMC = trdmatch->GetMatchedLink().GetIndex();
259  if (iMC < 0 || iMC > fMCTracks->GetEntriesFast()) continue;
260 
261  CbmMCTrack* mctrack = (CbmMCTrack*) fMCTracks->At(iMC);
262  Int_t pdg = TMath::Abs(mctrack->GetPdgCode());
263  // Double_t momMC = mctrack->GetP();
264  Double_t motherId = mctrack->GetMotherId();
265 
267  if (motherId != -1) continue;
268 
269  int iP = 0; //[0] = electron
270  if (pdg == 211) iP = 1; //[1] = pion
271  vector<TrdEloss> v;
272 
273  if (nHits < fNofTrdLayers) continue;
274 
275  for (Int_t iHit = 0; iHit < nHits; iHit++) {
276  Int_t hitIndex = trdtrack->GetHitIndex(iHit);
277  CbmTrdHit* trdhit = (CbmTrdHit*) fTrdHits->At(hitIndex);
278  TrdEloss e(trdhit->GetELoss(),
279  trdhit->GetELoss(),
280  0 /*trdhit->GetELossdEdX(), trdhit->GetELossTR()*/);
281  if (static_cast<UInt_t>(fNofTrdLayers) == v.size()) break;
282  v.push_back(e);
283  } //iHit
284  fEloss[iP].push_back(v);
285  } //iTrdTrack
286 }
287 
289  // [0]=electron, [1]=pion
290  for (int i = 0; i < 2; i++) {
291  for (unsigned int iT = 0; iT < fEloss[i].size(); iT++) {
292  Int_t nHits = fEloss[i][iT].size();
293  double sumEloss = 0.;
294  for (int iH = 0; iH < nHits; iH++) {
295  sumEloss += fEloss[i][iT][iH].fEloss;
296  fhEloss[i]->Fill(fEloss[i][iT][iH].fEloss);
297  } // iH
298  fhMeanEloss[i]->Fill(sumEloss / nHits);
299  } //iT
300  }
301 }
302 
304  vector<double> v;
305  v.resize(fNofTrdLayers);
306  for (int iP = 0; iP < 2; iP++) {
307  for (unsigned int iT = 0; iT < fEloss[iP].size(); iT++) {
308  for (int iH = 0; iH < fNofTrdLayers; iH++) {
309  v[iH] = fEloss[iP][iT][iH].fEloss;
310  }
311  std::sort(v.begin(), v.end());
312  for (int iH = 0; iH < fNofTrdLayers; iH++) {
313  fhElossSort[iP][iH]->Fill(v[iH]);
314  }
315  } //iT
316 
317  for (int iH = 0; iH < fNofTrdLayers; iH++) {
318  fhElossSort[iP][iH]->Scale(1. / fhElossSort[iP][iH]->Integral());
319  }
320  }
321 }
322 
324  if (fIdMethod == kBDT || fIdMethod == kANN) {
325  if (fIsDoTrain) DoTrain();
326  DoTest();
327  } else if (fIdMethod == kMEDIAN || fIdMethod == kLIKELIHOOD
328  || fIdMethod == kMeanCut) {
329  DoTest();
330  }
331 }
332 
334  if (fIdMethod == kBDT || fIdMethod == kANN) {
335  if (fTransformType == 1) {
336  Transform1();
337  } else if (fTransformType == 2) {
338  Transform2();
339  }
340  }
341 }
342 
343 
345  Double_t ANNCoef1 = 1.06;
346  Double_t ANNCoef2 = 0.57;
347  //Double_t ANNCoef1[] = {1.04,1.105,1.154,1.277,1.333,1.394,1.47,1.50,1.54,1.58};
348  //Double_t ANNCoef2[] = {0.548,0.567,0.585,0.63,0.645,0.664,0.69,0.705,0.716,0.723};
349  for (UInt_t i = 0; i < fAnnInput.size(); i++) {
350  fAnnInput[i] = fAnnInput[i] * 1e6;
351  fAnnInput[i] = (fAnnInput[i] - ANNCoef1) / ANNCoef2 - 0.225;
352  }
353  sort(fAnnInput.begin(), fAnnInput.end());
354  for (UInt_t i = 0; i < fAnnInput.size(); i++) {
355  fAnnInput[i] = TMath::LandauI(fAnnInput[i]);
356  }
357 }
358 
360  sort(fAnnInput.begin(), fAnnInput.end());
361 
362  Int_t size = fAnnInput.size();
363  for (Int_t j = 0; j < size; j++) {
364  Int_t binNumEl = fhElossSort[0][j]->FindBin(fAnnInput[j]);
365  if (binNumEl > fhElossSort[0][j]->GetNbinsX())
366  binNumEl = fhElossSort[0][j]->GetNbinsX();
367  Double_t probEl = fhElossSort[0][j]->GetBinContent(binNumEl);
368 
369  Int_t binNumPi = fhElossSort[1][j]->FindBin(fAnnInput[j]);
370  if (binNumPi > fhElossSort[1][j]->GetNbinsX())
371  binNumPi = fhElossSort[1][j]->GetNbinsX();
372  Double_t probPi = fhElossSort[1][j]->GetBinContent(binNumPi);
373 
374  if (TMath::IsNaN(probPi / (probEl + probPi))) {
375  fAnnInput[j] = 0.;
376  } else {
377  fAnnInput[j] = probPi / (probEl + probPi);
378  }
379  }
380 }
381 
383  Double_t lPi = 1.;
384  Double_t lEl = 1.;
385  // sort(fAnnInput.begin(), fAnnInput.end());
386  for (UInt_t j = 0; j < fAnnInput.size(); j++) {
387  Int_t binNumEl = fhEloss[0]->FindBin(fAnnInput[j]);
388  if (binNumEl > fhEloss[0]->GetNbinsX()) binNumEl = fhEloss[0]->GetNbinsX();
389  Double_t probEl = fhEloss[0]->GetBinContent(binNumEl);
390  lEl = lEl * probEl;
391 
392  Int_t binNumPi = fhEloss[1]->FindBin(fAnnInput[j]);
393  if (binNumPi > fhEloss[1]->GetNbinsX()) binNumPi = fhEloss[1]->GetNbinsX();
394  Double_t probPi = fhEloss[1]->GetBinContent(binNumPi);
395  lPi = lPi * probPi;
396  }
397  return lEl / (lEl + lPi);
398 }
399 
401  sort(fAnnInput.begin(), fAnnInput.end());
402  double eval = 0.;
403  if (fNofTrdLayers % 2 == 0) {
404  eval =
405  (fAnnInput[fNofTrdLayers / 2] + fAnnInput[fNofTrdLayers / 2 + 1]) / 2.;
406  } else {
407  eval = fAnnInput[fNofTrdLayers / 2 + 1];
408  }
409  double scaleX =
410  1. / fhEloss[0]->GetXaxis()->GetBinUpEdge(fhEloss[0]->GetNbinsX());
411  return eval * scaleX;
412 }
413 
415  double eval = 0.;
416  for (int i = 0; i < fNofTrdLayers; i++) {
417  eval += fAnnInput[i];
418  }
419  eval = eval / fNofTrdLayers;
420  double scaleX =
421  1. / fhEloss[0]->GetXaxis()->GetBinUpEdge(fhEloss[0]->GetNbinsX());
422  return eval * scaleX;
423 }
424 
426  if (fIdMethod == kBDT) {
427  return fReader->EvaluateMVA("BDT");
428  } else if (fIdMethod == kANN) {
429  Double_t par[fNofTrdLayers];
430  for (UInt_t i = 0; i < fAnnInput.size(); i++)
431  par[i] = fAnnInput[i];
432  return fNN->Evaluate(0, par);
433  } else if (fIdMethod == kMEDIAN) {
434  return Median();
435  } else if (fIdMethod == kLIKELIHOOD) {
436  return Likelihood();
437  } else if (fIdMethod == kMeanCut) {
438  return MeanCut();
439  }
440  return -1.;
441 }
442 
443 
445  fAnnInput.clear();
446  fAnnInput.resize(fNofTrdLayers);
447 
448  TTree* simu = CreateTree();
449  for (Int_t i = 0; i < 2; i++) {
450  for (UInt_t iT = 0; iT < fEloss[i].size() - 2; iT++) {
451  for (int iH = 0; iH < fNofTrdLayers; iH++) {
452  fAnnInput[iH] = fEloss[i][iT][iH].fEloss;
453  }
454  fXOut = (i == 0) ? 1. : -1.;
455  Transform();
456  simu->Fill();
457  if (Int_t(iT) >= fNofTrainSamples) break;
458  } //iT
459  } //i
460 
461  if (fIdMethod == kANN) {
462  if (NULL != fNN) delete fNN;
463  TString mlpString = CreateAnnString();
464  cout << "-I- Create ANN: " << mlpString << endl;
465  cout << "-I- Number of training epochs = " << fNofAnnEpochs << endl;
466  fNN = new TMultiLayerPerceptron(mlpString, simu, "(Entry$+1)");
467  fNN->Train(fNofAnnEpochs, "+text,update=10");
468  stringstream ss;
469  ss << fOutputDir + "/ann_weights_" << fNofTrdLayers << ".txt";
470  fNN->DumpWeights(ss.str().c_str());
471  } else if (fIdMethod == kBDT) {
472  CreateFactory(simu);
473  (TMVA::gConfig().GetIONames()).fWeightFileDir = fOutputDir;
474  TCut mycuts = "";
475  TCut mycutb = "";
476  // factory->PrepareTrainingAndTestTree(mycuts, mycutb,"SplitMode=Random:NormMode=NumEvents:!V");
477  //factory->BookMethod(TMVA::Types::kTMlpANN, "TMlpANN","!H:!V:NCycles=50:HiddenLayers=N+1");
478  stringstream ss;
479  ss << "nTrain_Signal=" << fNofTrainSamples - 500
480  << ":nTrain_Background=" << fNofTrainSamples - 500
481  << ":nTest_Signal=0:nTest_Background=0";
482  //TMVA_API
483  // factory->PrepareTrainingAndTestTree("", ss.str().c_str());
484  // factory->BookMethod(TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=400:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=CostComplexity:PruneStrength=4.5");
485  // factory->TrainAllMethods();
486  }
487 }
488 
490  cout << "-I- Start pretesting " << endl;
491  if (fIdMethod == kBDT) {
492  cout << "-I- IdMethod = kBDT " << endl;
493  } else if (fIdMethod == kANN) {
494  cout << "-I- IdMethod = kANN " << endl;
495  } else if (fIdMethod == kMEDIAN) {
496  cout << "-I- IdMethod = kMEDIANA " << endl;
497  } else if (fIdMethod == kLIKELIHOOD) {
498  cout << "-I- IdMethod = kLIKELIHOOD " << endl;
499  }
500 
501  fAnnInput.clear();
502  fAnnInput.resize(fNofTrdLayers);
503 
504  if (fIdMethod == kBDT) {
506  fReader->BookMVA("BDT", fOutputDir + "/TMVAnalysis_BDT.weights.xml");
507  } else if (fIdMethod == kANN) {
508  if (fNN != NULL) delete fNN;
509  TTree* simu = CreateTree();
510  TString mlpString = CreateAnnString();
511  fNN = new TMultiLayerPerceptron(mlpString, simu, "(Entry$+1)");
512  stringstream ss;
513  ss << fOutputDir + "/ann_weights_" << fNofTrdLayers << ".txt";
514  fNN->LoadWeights(ss.str().c_str());
515  }
516 
517  for (Int_t i = 0; i < 2; i++) {
518  for (UInt_t iT = 0; iT < fEloss[i].size(); iT++) {
519  if (Int_t(iT) < fNofTrainSamples) continue; // exclude training samples
520  if (iT % 100000 == 0) cout << "-I- read number: " << iT << endl;
521 
522  //change electron hit to pion hit and vice versa
523  //int randPi = rand()%fElossPi.size();
524  //for (int i1 = 0; i1 < 5; i1++){
525  // fElossEl[iE][i1] = fElossPi[randPi][i1];
526  // }
527  for (int iH = 0; iH < fNofTrdLayers; iH++) {
528  if (fSigmaError != 0.)
529  fEloss[i][iT][iH].fEloss += gRandom->Gaus(0., fSigmaError);
530  fAnnInput[iH] = fEloss[i][iT][iH].fEloss;
531  }
532 
533  Transform();
534  Bool_t isEl = (i == 0) ? true : false;
535  FillAnnInputHist(isEl);
536  Double_t nnEval = Eval(isEl);
537  if (nnEval > fMaxEval) nnEval = fMaxEval - 0.01;
538  if (nnEval < fMinEval) nnEval = fMinEval + 0.01;
539  fhOutput[i]->Fill(nnEval);
540  }
541  }
543 }
544 
546  fAnnInput.clear();
547  fAnnInput.resize(fNofTrdLayers);
548 
549  DoPreTest();
550  double cut = FindOptimalCut();
551  cout << "-I- Optimal cut = " << cut << " for " << 100 * fElIdEfficiency
552  << "% electron efficiency" << endl;
553  cout << "-I- Start testing" << endl;
554  fAnnInput.clear();
555  fAnnInput.resize(fNofTrdLayers);
556 
557  if (fIdMethod == kBDT) {
559  //reader->BookMVA("TMlpANN", "weights/TMVAnalysis_TMlpANN.weights.txt");
560  fReader->BookMVA("BDT", fOutputDir + "/TMVAnalysis_BDT.weights.xml");
561  } else if (fIdMethod == kANN) {
562  if (fNN != NULL) delete fNN;
563  TTree* simu = CreateTree();
564  TString mlpString = CreateAnnString();
565  fNN = new TMultiLayerPerceptron(mlpString, simu, "(Entry$+1)");
566  stringstream ss;
567  ss << fOutputDir + "/ann_weights_" << fNofTrdLayers << ".txt";
568  fNN->LoadWeights(ss.str().c_str());
569  }
570 
571  int nofPiLikeEl = 0;
572  int nofElLikePi = 0;
573  int nofElTest = 0;
574  int nofPiTest = 0;
575 
576  for (Int_t i = 0; i < 2; i++) {
577  for (UInt_t iT = 0; iT < fEloss[i].size(); iT++) {
578  if (Int_t(iT) < fNofTrainSamples) continue; //exclude training samples
579  if (iT % 100000 == 0) cout << "-I- Read number: " << iT << endl;
580 
581  for (Int_t iH = 0; iH < fNofTrdLayers; iH++) {
582  fAnnInput[iH] = fEloss[i][iT][iH].fEloss;
583  }
584  Transform();
585 
586  Bool_t isEl = (i == 0) ? true : false;
587  Double_t nnEval = Eval(isEl);
588  if (nnEval > fMaxEval) nnEval = fMaxEval - 0.01;
589  if (nnEval < fMinEval) nnEval = fMinEval + 0.01;
590  if (i == 0) {
591  nofElTest++;
592  if (nnEval < cut) nofElLikePi++;
593  } else if (i == 1) {
594  nofPiTest++;
595  if (nnEval > cut) nofPiLikeEl++;
596  }
597  }
598  }
599  double piSupp = -1;
600  if (nofPiLikeEl != 0) piSupp = (double) nofPiTest / nofPiLikeEl;
601  double elEff = (double) nofElLikePi / nofElTest * 100.;
602 
603  cout << "Testing results:" << endl;
604  cout << "nof TRD layers " << fNofTrdLayers << endl;
605  cout << "cut = " << cut << endl;
606  cout << "nof El = " << nofElTest << endl;
607  cout << "nof Pi = " << nofPiTest << endl;
608  cout << "nof Pi identified as El = " << nofPiLikeEl << endl;
609  cout << "nof El identified as Pi = " << nofElLikePi << endl;
610  cout << "pion suppression = " << nofPiTest << "/" << nofPiLikeEl << " = "
611  << piSupp << endl;
612  cout << "electron efficiency loss in % = " << nofElLikePi << "/" << nofElTest
613  << " = " << elEff << endl;
614 
615  // write results to histogramm
616  fhResults->SetBinContent(1, piSupp);
617  fhResults->SetBinContent(2, elEff);
618 }
619 
621  for (Int_t i = 0; i < 2; i++) {
622  Double_t cumProb = 0.;
623  Double_t nofTr = fhOutput[i]->GetEntries();
624  for (Int_t iH = 1; iH <= fhOutput[i]->GetNbinsX(); iH++) {
625  cumProb += fhOutput[i]->GetBinContent(iH);
626  Double_t pr = (i == 0) ? 1. - cumProb / nofTr : cumProb / nofTr;
627  fhCumProbOutput[i]->SetBinContent(iH, pr);
628  }
629  }
630 }
631 
633  Int_t nBins = fhCumProbOutput[0]->GetNbinsX();
634  Double_t x[nBins], y[nBins];
635 
636  for (Int_t i = 1; i <= nBins; i++) {
637  Double_t cpi = 100. * fhCumProbOutput[1]->GetBinContent(i);
638  if (cpi == 100.) cpi = 100. - 0.0001;
639  y[i - 1] = 100. / (100. - cpi);
640  x[i - 1] = 100. * fhCumProbOutput[0]->GetBinContent(i);
641  }
642  TGraph* rocGraph = new TGraph(nBins, x, y);
643  rocGraph->SetTitle("ROC diagram");
644  rocGraph->GetXaxis()->SetTitle("Efficiency [%]");
645  rocGraph->GetYaxis()->SetTitle("Pion suppression");
646  rocGraph->SetLineWidth(3);
647  return rocGraph;
648 }
649 
651  Double_t optimalCut = -1;
652  for (Int_t i = 1; i <= fhCumProbOutput[0]->GetNbinsX(); i++) {
653  if (fhCumProbOutput[0]->GetBinContent(i) <= fElIdEfficiency) {
654  optimalCut = fhCumProbOutput[0]->GetBinCenter(i);
655  return optimalCut;
656  }
657  }
658  return optimalCut;
659 }
660 
662  fAnnInput.clear();
663  fAnnInput.resize(fNofTrdLayers);
664  TTree* simu = new TTree("MonteCarlo", "MontecarloData");
665  char txt1[100], txt2[100];
666  for (Int_t i = 0; i < fNofTrdLayers; i++) {
667  sprintf(txt1, "x%d", i);
668  sprintf(txt2, "x%d/F", i);
669  simu->Branch(txt1, &fAnnInput[i], txt2);
670  }
671  simu->Branch("xOut", &fXOut, "xOut/F");
672 
673  return simu;
674 }
675 
677  string st = "";
678  char txt[50];
679  for (Int_t i = 0; i < fNofTrdLayers - 1; i++) {
680  sprintf(txt, "x%d", i);
681  st = st + txt + ",";
682  }
683  sprintf(txt, "x%d", fNofTrdLayers - 1);
684  st = st + txt + "";
685  sprintf(txt, "%d", 2 * fNofTrdLayers);
686  st = st + ":" + txt + ":xOut";
687  return st;
688 }
689 
691  if (fOutputDir != "") gSystem->mkdir(fOutputDir.c_str(), true);
692  TFile* outputFile =
693  TFile::Open(string(fOutputDir + "/tmva_output.root").c_str(), "RECREATE");
694 
695  TMVA::Factory* factory = new TMVA::Factory("TMVAnalysis", outputFile);
696 
697  TCut piCut = "xOut<0";
698  TCut elCut = "xOut>0";
699  //TMVA_API
700  //factory->SetInputTrees(simu, elCut, piCut);
701  char txt1[100];
702  for (Int_t i = 0; i < fNofTrdLayers; i++) {
703  sprintf(txt1, "x%d", i);
705  //factory->AddVariable(txt1, 'F');
706  }
707 
708  return factory;
709 }
710 
712  if (!fReader) delete fReader;
713 
714  fAnnInput.clear();
715  fAnnInput.resize(fNofTrdLayers);
716 
717  TMVA::Reader* reader = new TMVA::Reader();
718 
719  char txt1[100];
720  for (Int_t i = 0; i < fNofTrdLayers; i++) {
721  sprintf(txt1, "x%d", i);
722  reader->AddVariable(txt1, &fAnnInput[i]);
723  }
724  return reader;
725 }
726 
728  for (UInt_t j = 0; j < fAnnInput.size(); j++) {
729  if (isEl) {
730  fhInput[0][j]->Fill(fAnnInput[j]);
731  } else {
732  fhInput[1][j]->Fill(fAnnInput[j]);
733  }
734  }
735 }
736 
739  TCanvas* cEloss = new TCanvas("trd_elid_eloss", "trd_elid_eloss", 1200, 600);
740  cEloss->Divide(2, 1);
741  cEloss->cd(1);
743  list_of("e^{#pm}")("#pi^{#pm}"),
744  kLinear,
745  kLog,
746  true,
747  0.8,
748  0.8,
749  0.99,
750  0.99);
751  cEloss->cd(2);
752  DrawH1(fhEloss,
753  list_of("e^{#pm}")("#pi^{#pm}"),
754  kLinear,
755  kLog,
756  true,
757  0.8,
758  0.8,
759  0.99,
760  0.99);
761 
762 
763  TCanvas* cElossSort =
764  new TCanvas("trd_elid_eloss_sort", "trd_elid_eloss_sort", 1200, 900);
765  cElossSort->Divide(4, 3);
766  for (int iL = 0; iL < fNofTrdLayers; iL++) {
767  cElossSort->cd(iL + 1);
768  DrawH1(list_of(fhElossSort[0][iL])(fhElossSort[1][iL]),
769  list_of("e^{#pm}")("#pi^{#pm}"),
770  kLinear,
771  kLog,
772  true,
773  0.8,
774  0.8,
775  0.99,
776  0.99);
777  }
778 
779  // TCanvas* cClassifierOutput = new TCanvas("trd_elid_classifier_output","trd_elid_classifier_output", 500, 500);
780  TH1D* out0 = (TH1D*) fhOutput[0]->Clone();
781  TH1D* out1 = (TH1D*) fhOutput[1]->Clone();
782  out0->Rebin(50);
783  out1->Rebin(50);
784  DrawH1(list_of(out0)(out1),
785  list_of("e^{#pm}")("#pi^{#pm}"),
786  kLinear,
787  kLog,
788  true,
789  0.8,
790  0.8,
791  0.99,
792  0.99);
793  out0->Scale(1. / out0->Integral());
794  out1->Scale(1. / out1->Integral());
795 
796  // TCanvas* cCumProbOutput = new TCanvas("trd_elid_cum_prob_output","trd_elid_cum_prob_output", 500,500);
798  list_of("e^{#pm}")("#pi^{#pm}"),
799  kLinear,
800  kLinear,
801  true,
802  0.8,
803  0.8,
804  0.99,
805  0.99);
806 
807  // TCanvas* cRoc = new TCanvas("trd_elid_roc","trd_elid_roc", 500, 500);
808  TGraph* rocGraph = CreateRocDiagramm();
809  DrawGraph(rocGraph);
810  gPad->SetLogy();
811 
812  TCanvas* cInput =
813  new TCanvas("trd_elid_input", "trd_elid_ann_input", 10, 10, 800, 800);
814  cInput->Divide(4, 3);
815  for (int i = 0; i < fNofTrdLayers; i++) {
816  cInput->cd(i + 1);
817  DrawH1(list_of(fhInput[0][i])(fhInput[1][i]),
818  list_of("e^{#pm}")("#pi^{#pm}"),
819  kLinear,
820  kLog,
821  true,
822  0.8,
823  0.8,
824  0.99,
825  0.99);
826  }
827 }
828 
CbmMCTrack::GetMotherId
Int_t GetMotherId() const
Definition: CbmMCTrack.h:71
CbmMatch::GetMatchedLink
const CbmLink & GetMatchedLink() const
Definition: CbmMatch.h:37
f
float f
Definition: L1/vectors/P4_F32vec4.h:24
CbmTrdElectronsTrainAnn::FillElossVectorSim
void FillElossVectorSim()
Fill vector with energy loss information for simulated data.
Definition: CbmTrdElectronsTrainAnn.cxx:249
CbmTrdElectronsTrainAnn::DoTrain
void DoTrain()
Definition: CbmTrdElectronsTrainAnn.cxx:444
CbmTrack::GetNofHits
virtual Int_t GetNofHits() const
Definition: CbmTrack.h:53
CbmTrdElectronsTrainAnn::FindOptimalCut
Double_t FindOptimalCut()
Definition: CbmTrdElectronsTrainAnn.cxx:650
CbmTrdElectronsTrainAnn::RunBeamData
void RunBeamData()
Definition: CbmTrdElectronsTrainAnn.cxx:207
CbmTrdElectronsTrainAnn::fAnnInput
std::vector< Float_t > fAnnInput
Definition: CbmTrdElectronsTrainAnn.h:213
CbmTrdElectronsTrainAnn::MeanCut
Double_t MeanCut()
Definition: CbmTrdElectronsTrainAnn.cxx:414
CbmTrdElectronsTrainAnn::FillElossVectorReal
void FillElossVectorReal()
Fill vector with energy loss information simulated from real data spectra.
Definition: CbmTrdElectronsTrainAnn.cxx:218
CbmTrdElectronsTrainAnn::fhCumProbOutput
std::vector< TH1 * > fhCumProbOutput
Definition: CbmTrdElectronsTrainAnn.h:232
CbmTrdHit::GetELoss
Double_t GetELoss() const
Definition: CbmTrdHit.h:79
CbmTrdElectronsTrainAnn::Finish
virtual void Finish()
Inherited from FairTask.
Definition: CbmTrdElectronsTrainAnn.cxx:194
CbmMCTrack::GetPdgCode
Int_t GetPdgCode() const
Definition: CbmMCTrack.h:70
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmTrdElectronsTrainAnn::fhEloss
std::vector< TH1 * > fhEloss
Definition: CbmTrdElectronsTrainAnn.h:194
CbmTrdElectronsTrainAnn::fOutputDir
std::string fOutputDir
Definition: CbmTrdElectronsTrainAnn.h:202
CbmTrdHit
data class for a reconstructed Energy-4D measurement in the TRD
Definition: CbmTrdHit.h:35
CbmTrdElectronsTrainAnn::fIsDoTrain
Bool_t fIsDoTrain
Definition: CbmTrdElectronsTrainAnn.h:204
CbmTrdElectronsTrainAnn::Run
void Run()
Definition: CbmTrdElectronsTrainAnn.cxx:323
CbmTrdElectronsTrainAnn::fNN
TMultiLayerPerceptron * fNN
Definition: CbmTrdElectronsTrainAnn.h:221
CbmTrdElectronsTrainAnn::CreateCumProbOutputHist
void CreateCumProbOutputHist()
Definition: CbmTrdElectronsTrainAnn.cxx:620
kLIKELIHOOD
@ kLIKELIHOOD
Definition: CbmTrdElectronsTrainAnn.h:29
CbmTrdElectronsTrainAnn::Transform2
void Transform2()
Definition: CbmTrdElectronsTrainAnn.cxx:359
CbmTrdElectronsTrainAnn::CreateRocDiagramm
TGraph * CreateRocDiagramm()
Definition: CbmTrdElectronsTrainAnn.cxx:632
CbmTrdElectronsTrainAnn::fReader
TMVA::Reader * fReader
Definition: CbmTrdElectronsTrainAnn.h:222
CbmTrdElectronsTrainAnn::Init
virtual InitStatus Init()
Inherited from FairTask.
Definition: CbmTrdElectronsTrainAnn.cxx:146
CbmDrawHist.h
Helper functions for drawing 1D and 2D histograms and graphs.
CbmTrdElectronsTrainAnn::~CbmTrdElectronsTrainAnn
virtual ~CbmTrdElectronsTrainAnn()
Destructor.
Definition: CbmTrdElectronsTrainAnn.cxx:144
CbmTrdElectronsTrainAnn::fTrdTrackMatches
TClonesArray * fTrdTrackMatches
Definition: CbmTrdElectronsTrainAnn.h:177
CbmTrdElectronsTrainAnn::DoTest
void DoTest()
Definition: CbmTrdElectronsTrainAnn.cxx:545
CbmTrdElectronsTrainAnn::fTransformType
Int_t fTransformType
Definition: CbmTrdElectronsTrainAnn.h:205
DrawGraph
void DrawGraph(TGraph *graph, HistScale logx, HistScale logy, const string &drawOpt, Int_t color, Int_t lineWidth, Int_t lineStyle, Int_t markerSize, Int_t markerStyle)
Definition: CbmDrawHist.cxx:151
CbmTrdElectronsTrainAnn::CreateFactory
TMVA::Factory * CreateFactory(TTree *simu)
Definition: CbmTrdElectronsTrainAnn.cxx:690
DrawH1
void DrawH1(TH1 *hist, HistScale logx, HistScale logy, const string &drawOpt, Int_t color, Int_t lineWidth, Int_t lineStyle, Int_t markerSize, Int_t markerStyle)
Definition: CbmDrawHist.cxx:49
CbmTrdElectronsTrainAnn::Transform1
void Transform1()
Definition: CbmTrdElectronsTrainAnn.cxx:344
CbmTrdElectronsTrainAnn::fhInput
std::vector< std::vector< TH1 * > > fhInput
Definition: CbmTrdElectronsTrainAnn.h:234
CbmTrdElectronsTrainAnn::Likelihood
Double_t Likelihood()
Definition: CbmTrdElectronsTrainAnn.cxx:382
CbmTrdElectronsTrainAnn::fBeamDataPiHist
std::string fBeamDataPiHist
Definition: CbmTrdElectronsTrainAnn.h:209
CbmTrdElectronsTrainAnn::fhElossSort
std::vector< std::vector< TH1 * > > fhElossSort
Definition: CbmTrdElectronsTrainAnn.h:199
kMEDIAN
@ kMEDIAN
Definition: CbmTrdElectronsTrainAnn.h:28
CbmTrdElectronsTrainAnn::fMCTracks
TClonesArray * fMCTracks
Definition: CbmTrdElectronsTrainAnn.h:174
CbmTrdElectronsTrainAnn::CreateAnnString
std::string CreateAnnString()
Definition: CbmTrdElectronsTrainAnn.cxx:676
CbmTrdElectronsTrainAnn::Draw
void Draw(Option_t *="")
Draw results.
Definition: CbmTrdElectronsTrainAnn.cxx:737
kLinear
@ kLinear
Definition: CbmDrawHist.h:79
CbmTrack::GetHitIndex
Int_t GetHitIndex(Int_t iHit) const
Definition: CbmTrack.h:54
CbmTrdElectronsTrainAnn::fXOut
Float_t fXOut
Definition: CbmTrdElectronsTrainAnn.h:214
CbmTrackMatchNew.h
CbmTrdElectronsTrainAnn::fMaxEval
Double_t fMaxEval
Definition: CbmTrdElectronsTrainAnn.h:218
CbmTrdElectronsTrainAnn::fHists
std::vector< TH1 * > fHists
Definition: CbmTrdElectronsTrainAnn.h:186
CbmTrdHit.h
Class for hits in TRD detector.
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmTrdElectronsTrainAnn::FillAnnInputHist
void FillAnnInputHist(Bool_t isEl)
Definition: CbmTrdElectronsTrainAnn.cxx:727
CbmTrdElectronsTrainAnn::fIdMethod
IdMethod fIdMethod
Definition: CbmTrdElectronsTrainAnn.h:223
CbmTrdElectronsTrainAnn::fEventNum
Int_t fEventNum
Definition: CbmTrdElectronsTrainAnn.h:201
CbmTrdElectronsTrainAnn::CreateTree
TTree * CreateTree()
Definition: CbmTrdElectronsTrainAnn.cxx:661
CbmTrdElectronsTrainAnn.h
CbmTrdElectronsTrainAnn::fBeamDataElHist
std::string fBeamDataElHist
Definition: CbmTrdElectronsTrainAnn.h:211
CbmTrdTrack
Definition: CbmTrdTrack.h:22
CbmTrdElectronsTrainAnn::DoPreTest
void DoPreTest()
Definition: CbmTrdElectronsTrainAnn.cxx:489
CbmTrdElectronsTrainAnn::Exec
virtual void Exec(Option_t *opt)
Inherited from FairTask.
Definition: CbmTrdElectronsTrainAnn.cxx:180
CbmMCTrack.h
CbmTrdElectronsTrainAnn::CreateTmvaReader
TMVA::Reader * CreateTmvaReader()
Definition: CbmTrdElectronsTrainAnn.cxx:711
v
__m128 v
Definition: L1/vectors/P4_F32vec4.h:1
CbmTrdElectronsTrainAnn::fNofTrainSamples
Int_t fNofTrainSamples
Definition: CbmTrdElectronsTrainAnn.h:225
CbmTrdElectronsTrainAnn::fhMeanEloss
std::vector< TH1 * > fhMeanEloss
Definition: CbmTrdElectronsTrainAnn.h:193
CbmMCTrack
Definition: CbmMCTrack.h:34
CbmTrdElectronsTrainAnn::fElIdEfficiency
double fElIdEfficiency
Definition: CbmTrdElectronsTrainAnn.h:226
x
Double_t x
Definition: CbmMvdSensorDigiToHitTask.cxx:68
CbmTrdElectronsTrainAnn::Transform
void Transform()
Definition: CbmTrdElectronsTrainAnn.cxx:333
CbmTrdElectronsTrainAnn
Definition: CbmTrdElectronsTrainAnn.h:54
CbmTrdElectronsTrainAnn::fNofAnnEpochs
Int_t fNofAnnEpochs
Definition: CbmTrdElectronsTrainAnn.h:224
kMeanCut
@ kMeanCut
Definition: CbmTrdElectronsTrainAnn.h:30
TrdEloss
Represents information about energy losses in one layer.
Definition: CbmTrdElectronsTrainAnn.h:41
CbmTrdElectronsTrainAnn::fBeamDataFile
std::string fBeamDataFile
Definition: CbmTrdElectronsTrainAnn.h:208
y
Double_t y
Definition: CbmMvdSensorDigiToHitTask.cxx:68
CbmTrdElectronsTrainAnn::fTrdPoints
TClonesArray * fTrdPoints
Definition: CbmTrdElectronsTrainAnn.h:175
CbmTrdPoint.h
CbmTrdElectronsTrainAnn::Median
Double_t Median()
Definition: CbmTrdElectronsTrainAnn.cxx:400
kANN
@ kANN
Definition: CbmTrdElectronsTrainAnn.h:26
CbmTrackMatchNew
Definition: CbmTrackMatchNew.h:19
CbmTrdElectronsTrainAnn::fTrdTracks
TClonesArray * fTrdTracks
Definition: CbmTrdElectronsTrainAnn.h:176
SetDefaultDrawStyle
void SetDefaultDrawStyle()
Definition: CbmDrawHist.cxx:33
CbmTrdTrack.h
kBDT
@ kBDT
Definition: CbmTrdElectronsTrainAnn.h:27
CbmTrdElectronsTrainAnn::CbmTrdElectronsTrainAnn
CbmTrdElectronsTrainAnn(int nofTrdLayers)
Default constructor.
CbmTrdElectronsTrainAnn::fhOutput
std::vector< TH1 * > fhOutput
Definition: CbmTrdElectronsTrainAnn.h:230
kLog
@ kLog
Definition: CbmDrawHist.h:78
CbmTrdElectronsTrainAnn::fEloss
std::vector< std::vector< std::vector< TrdEloss > > > fEloss
Definition: CbmTrdElectronsTrainAnn.h:184
CbmTrdElectronsTrainAnn::Eval
Double_t Eval(Bool_t isEl)
Definition: CbmTrdElectronsTrainAnn.cxx:425
CbmTrdElectronsTrainAnn::fhResults
TH1 * fhResults
Definition: CbmTrdElectronsTrainAnn.h:189
CbmTrdElectronsTrainAnn::fSigmaError
Double_t fSigmaError
Definition: CbmTrdElectronsTrainAnn.h:203
CbmTrdElectronsTrainAnn::fTrdHits
TClonesArray * fTrdHits
Definition: CbmTrdElectronsTrainAnn.h:178
CbmTrdElectronsTrainAnn::fNofTrdLayers
Int_t fNofTrdLayers
Definition: CbmTrdElectronsTrainAnn.h:216
CbmTrdElectronsTrainAnn::SortElossAndFillHist
void SortElossAndFillHist()
Sort energy losses and fill histograms.
Definition: CbmTrdElectronsTrainAnn.cxx:303
CbmTrdElectronsTrainAnn::FillElossHist
void FillElossHist()
Fill histograms with energy loss information.
Definition: CbmTrdElectronsTrainAnn.cxx:288
CbmTrdElectronsTrainAnn::fMinEval
Double_t fMinEval
Definition: CbmTrdElectronsTrainAnn.h:219