CbmRoot
CbmTrdElectronsTrainAnn.h
Go to the documentation of this file.
1 
7 #ifndef CBM_TRD_ELECTRONS_TRAIN_ANN
8 #define CBM_TRD_ELECTRONS_TRAIN_ANN
9 
10 #include "FairTask.h"
11 
12 #include "TMVA/Factory.h"
13 #include "TMVA/Reader.h"
14 #include "TMultiLayerPerceptron.h"
15 
16 #include <string>
17 #include <vector>
18 
19 class TH1D;
20 class TTree;
21 class TGraph;
22 class TRandom;
23 //class TMultiLayerPerceptron;
24 
25 enum IdMethod {
26  kANN = 0,
27  kBDT = 1,
28  kMEDIAN = 2,
30  kMeanCut = 4
31 };
32 
41 class TrdEloss {
42 public:
43  TrdEloss() : TrdEloss(0., 0., 0.) {}
44 
45  TrdEloss(double eloss, double dedx = 0., double tr = 0.)
46  : fEloss(eloss), fdEdX(dedx), fTR(tr) {}
47 
48  double fEloss;
49  double fdEdX;
50  double fTR;
51 };
52 
53 
54 class CbmTrdElectronsTrainAnn : public FairTask {
55 public:
59  CbmTrdElectronsTrainAnn(int nofTrdLayers);
60 
64  virtual ~CbmTrdElectronsTrainAnn();
65 
69  virtual InitStatus Init();
70 
74  virtual void Exec(Option_t* opt);
75 
79  virtual void Finish();
80 
84  void Draw(Option_t* = "");
85 
86 private:
90  void FillElossVectorSim();
91 
95  void FillElossVectorReal();
96 
100  void FillElossHist();
101 
105  void SortElossAndFillHist();
106 
107  void Run();
108 
109  void DoTrain();
110 
111  void DoTest();
112 
113  void DoPreTest();
114 
115  TTree* CreateTree();
116 
117  std::string CreateAnnString();
118 
119  TMVA::Factory* CreateFactory(TTree* simu);
120 
121  TMVA::Reader* CreateTmvaReader();
122 
123  void Transform();
124 
125  void Transform1();
126 
127  void Transform2();
128 
129  Double_t FindOptimalCut();
130 
132 
133  TGraph* CreateRocDiagramm();
134 
135  Double_t Likelihood();
136 
137  Double_t Median();
138 
139  Double_t MeanCut();
140 
141  Double_t Eval(Bool_t isEl);
142 
143 public:
144  void SetIsDoTrain(Bool_t doTrain) { fIsDoTrain = doTrain; }
145 
146  void SetTransformType(Int_t type) { fTransformType = type; }
147 
148  void SetIdMethod(IdMethod idMethod) { fIdMethod = idMethod; }
149 
150  void SetOutputDir(const std::string& outputDir) { fOutputDir = outputDir; }
151 
152  void SetNofAnnEpochs(Int_t nofAnnEpochs) { fNofAnnEpochs = nofAnnEpochs; }
153 
154  void SetNofTrainSamples(Int_t nofTr) { fNofTrainSamples = nofTr; }
155 
156  void SetSigmaError(Double_t sigma) { fSigmaError = sigma; }
157 
158  void SetBeamDataFile(std::string beamDataFile) {
159  fBeamDataFile = beamDataFile;
160  }
161  void SetBeamDataPiHist(std::string beamDataPiHist) {
162  fBeamDataPiHist = beamDataPiHist;
163  }
164  void SetBeamDataElHist(std::string beamDataElHist) {
165  fBeamDataElHist = beamDataElHist;
166  }
167 
168  void RunBeamData();
169 
170 private:
171  void FillAnnInputHist(Bool_t isEl);
172 
173  // Input arrays of simulated events
174  TClonesArray* fMCTracks;
175  TClonesArray* fTrdPoints;
176  TClonesArray* fTrdTracks;
177  TClonesArray* fTrdTrackMatches;
178  TClonesArray* fTrdHits;
179 
180  // store energy loss information from TrdTrack
181  // 1st index -> [0] = electrons, [1] = pions
182  // 2nd index -> track index
183  // 3rd index -> hit index in track
184  std::vector<std::vector<std::vector<TrdEloss>>> fEloss;
185 
186  std::vector<TH1*> fHists; //store all pointers to histograms
187 
188  TH1*
189  fhResults; // histograms for the results storing: pi suppression, el efficiency etc.
190 
191  // [0] = electrons, [1] = pions
192  std::vector<TH1*>
193  fhMeanEloss; // sum of energy losses in all layers divided by number of layers
194  std::vector<TH1*> fhEloss; // energy losses in one layer
195 
196  // store sorted energy losses and cumulative probability for each TRD layer
197  // 1st index -> [0] = electrons, [1] = pions
198  // 2nd index -> layer number
199  std::vector<std::vector<TH1*>> fhElossSort;
200 
201  Int_t fEventNum; // event number
202  std::string fOutputDir; // output directory
203  Double_t fSigmaError; // additional sigma error for energy loss measurements
204  Bool_t fIsDoTrain; // do you want to run training procedure?
205  Int_t fTransformType; // Energy loss transformation type
206 
207  std::string
208  fBeamDataFile; // path to file with beamtime data for energy losses
209  std::string fBeamDataPiHist; // histogram name with energy losses for pions
210  std::string
211  fBeamDataElHist; // histogram name with energy losses for electrons
212 
213  std::vector<Float_t> fAnnInput; // input vector for ANN
214  Float_t fXOut; // output value from ANN
215 
216  Int_t fNofTrdLayers; // number of TRD layers
217 
218  Double_t fMaxEval;
219  Double_t fMinEval;
220 
221  TMultiLayerPerceptron* fNN;
222  TMVA::Reader* fReader;
227 
228  // Histograms for testing
229  // [0] = electron, [1] = pion
230  std::vector<TH1*> fhOutput; // algorithm output
231  std::vector<TH1*>
232  fhCumProbOutput; // Cumulative probabilities for algorithm's output
233  std::vector<std::vector<TH1*>>
234  fhInput; // Input data for algorithm for each input
235 
238 
240 };
241 
242 #endif
TrdEloss::TrdEloss
TrdEloss(double eloss, double dedx=0., double tr=0.)
Definition: CbmTrdElectronsTrainAnn.h:45
CbmTrdElectronsTrainAnn::SetNofTrainSamples
void SetNofTrainSamples(Int_t nofTr)
Definition: CbmTrdElectronsTrainAnn.h:154
CbmTrdElectronsTrainAnn::SetBeamDataFile
void SetBeamDataFile(std::string beamDataFile)
Definition: CbmTrdElectronsTrainAnn.h:158
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
CbmTrdElectronsTrainAnn::CbmTrdElectronsTrainAnn
CbmTrdElectronsTrainAnn(const CbmTrdElectronsTrainAnn &)
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
CbmTrdElectronsTrainAnn::Finish
virtual void Finish()
Inherited from FairTask.
Definition: CbmTrdElectronsTrainAnn.cxx:194
CbmTrdElectronsTrainAnn::fhEloss
std::vector< TH1 * > fhEloss
Definition: CbmTrdElectronsTrainAnn.h:194
CbmTrdElectronsTrainAnn::fOutputDir
std::string fOutputDir
Definition: CbmTrdElectronsTrainAnn.h:202
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::SetOutputDir
void SetOutputDir(const std::string &outputDir)
Definition: CbmTrdElectronsTrainAnn.h:150
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
TrdEloss::fTR
double fTR
Definition: CbmTrdElectronsTrainAnn.h:50
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
IdMethod
IdMethod
Definition: CbmTrdElectronsTrainAnn.h:25
CbmTrdElectronsTrainAnn::CreateFactory
TMVA::Factory * CreateFactory(TTree *simu)
Definition: CbmTrdElectronsTrainAnn.cxx:690
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::SetIsDoTrain
void SetIsDoTrain(Bool_t doTrain)
Definition: CbmTrdElectronsTrainAnn.h:144
CbmTrdElectronsTrainAnn::fMCTracks
TClonesArray * fMCTracks
Definition: CbmTrdElectronsTrainAnn.h:174
CbmTrdElectronsTrainAnn::CreateAnnString
std::string CreateAnnString()
Definition: CbmTrdElectronsTrainAnn.cxx:676
CbmTrdElectronsTrainAnn::SetNofAnnEpochs
void SetNofAnnEpochs(Int_t nofAnnEpochs)
Definition: CbmTrdElectronsTrainAnn.h:152
CbmTrdElectronsTrainAnn::Draw
void Draw(Option_t *="")
Draw results.
Definition: CbmTrdElectronsTrainAnn.cxx:737
CbmTrdElectronsTrainAnn::ClassDef
ClassDef(CbmTrdElectronsTrainAnn, 1)
CbmTrdElectronsTrainAnn::fXOut
Float_t fXOut
Definition: CbmTrdElectronsTrainAnn.h:214
CbmTrdElectronsTrainAnn::SetIdMethod
void SetIdMethod(IdMethod idMethod)
Definition: CbmTrdElectronsTrainAnn.h:148
CbmTrdElectronsTrainAnn::fMaxEval
Double_t fMaxEval
Definition: CbmTrdElectronsTrainAnn.h:218
CbmTrdElectronsTrainAnn::fHists
std::vector< TH1 * > fHists
Definition: CbmTrdElectronsTrainAnn.h:186
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::fBeamDataElHist
std::string fBeamDataElHist
Definition: CbmTrdElectronsTrainAnn.h:211
CbmTrdElectronsTrainAnn::SetBeamDataPiHist
void SetBeamDataPiHist(std::string beamDataPiHist)
Definition: CbmTrdElectronsTrainAnn.h:161
CbmTrdElectronsTrainAnn::DoPreTest
void DoPreTest()
Definition: CbmTrdElectronsTrainAnn.cxx:489
CbmTrdElectronsTrainAnn::Exec
virtual void Exec(Option_t *opt)
Inherited from FairTask.
Definition: CbmTrdElectronsTrainAnn.cxx:180
TrdEloss::fEloss
double fEloss
Definition: CbmTrdElectronsTrainAnn.h:48
CbmTrdElectronsTrainAnn::CreateTmvaReader
TMVA::Reader * CreateTmvaReader()
Definition: CbmTrdElectronsTrainAnn.cxx:711
TrdEloss::TrdEloss
TrdEloss()
Definition: CbmTrdElectronsTrainAnn.h:43
CbmTrdElectronsTrainAnn::fNofTrainSamples
Int_t fNofTrainSamples
Definition: CbmTrdElectronsTrainAnn.h:225
CbmTrdElectronsTrainAnn::fhMeanEloss
std::vector< TH1 * > fhMeanEloss
Definition: CbmTrdElectronsTrainAnn.h:193
CbmTrdElectronsTrainAnn::fElIdEfficiency
double fElIdEfficiency
Definition: CbmTrdElectronsTrainAnn.h:226
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
CbmTrdElectronsTrainAnn::fTrdPoints
TClonesArray * fTrdPoints
Definition: CbmTrdElectronsTrainAnn.h:175
CbmTrdElectronsTrainAnn::Median
Double_t Median()
Definition: CbmTrdElectronsTrainAnn.cxx:400
kANN
@ kANN
Definition: CbmTrdElectronsTrainAnn.h:26
CbmTrdElectronsTrainAnn::fTrdTracks
TClonesArray * fTrdTracks
Definition: CbmTrdElectronsTrainAnn.h:176
TrdEloss::fdEdX
double fdEdX
Definition: CbmTrdElectronsTrainAnn.h:49
kBDT
@ kBDT
Definition: CbmTrdElectronsTrainAnn.h:27
CbmTrdElectronsTrainAnn::SetSigmaError
void SetSigmaError(Double_t sigma)
Definition: CbmTrdElectronsTrainAnn.h:156
CbmTrdElectronsTrainAnn::SetTransformType
void SetTransformType(Int_t type)
Definition: CbmTrdElectronsTrainAnn.h:146
CbmTrdElectronsTrainAnn::CbmTrdElectronsTrainAnn
CbmTrdElectronsTrainAnn(int nofTrdLayers)
Default constructor.
CbmTrdElectronsTrainAnn::fhOutput
std::vector< TH1 * > fhOutput
Definition: CbmTrdElectronsTrainAnn.h:230
CbmTrdElectronsTrainAnn::operator=
CbmTrdElectronsTrainAnn & operator=(const CbmTrdElectronsTrainAnn &)
CbmTrdElectronsTrainAnn::SetBeamDataElHist
void SetBeamDataElHist(std::string beamDataElHist)
Definition: CbmTrdElectronsTrainAnn.h:164
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