1 /*************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercialf purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
16 /* $Id: AliTRDpidRefMakerNN.cxx 27496 2008-07-22 08:35:45Z cblume $ */
18 ////////////////////////////////////////////////////////////////////////////
20 // Builds the reference tree for the training of neural networks //
22 ////////////////////////////////////////////////////////////////////////////
30 #include "TGraphErrors.h"
32 #include "TEventList.h"
33 #include "TMultiLayerPerceptron.h"
36 #include "AliESDtrack.h"
37 #include "AliTrackReference.h"
39 #include "AliTRDtrackV1.h"
40 #include "AliTRDReconstructor.h"
41 #include "AliTRDpidUtil.h"
42 #include "AliTRDpidRefMakerNN.h"
43 #include "AliTRDpidUtil.h"
45 #include "../Cal/AliTRDCalPID.h"
46 #include "../Cal/AliTRDCalPIDNN.h"
47 #include "info/AliTRDtrackInfo.h"
48 #include "info/AliTRDv0Info.h"
50 ClassImp(AliTRDpidRefMakerNN)
52 //________________________________________________________________________
53 AliTRDpidRefMakerNN::AliTRDpidRefMakerNN()
54 :AliTRDpidRefMaker("PidRefMakerNN", "PID(NN) Reference Maker")
55 // :AliTRDrecoTask("PidRefMakerNN", "PID(NN) Reference Maker")
66 // Default constructor
69 memset(fTrain, 0, AliTRDCalPID::kNMom*AliTRDgeometry::kNlayer*sizeof(TEventList*));
70 memset(fTest, 0, AliTRDCalPID::kNMom*AliTRDgeometry::kNlayer*sizeof(TEventList*));
71 memset(fNet, 0, AliTRDgeometry::kNlayer*sizeof(TMultiLayerPerceptron*));
74 SetScaledEdx(Float_t(AliTRDCalPIDNN::kMLPscale));
76 fDate = datime.GetDate();
78 DefineInput(1, TObjArray::Class());
79 DefineOutput(1, TTree::Class());
83 //________________________________________________________________________
84 AliTRDpidRefMakerNN::~AliTRDpidRefMakerNN()
89 //________________________________________________________________________
90 void AliTRDpidRefMakerNN::CreateOutputObjects()
95 AliTRDpidRefMaker::CreateOutputObjects();
96 TGraphErrors *gEffisTrain = new TGraphErrors(kMoniTrain);
97 gEffisTrain -> SetLineColor(4);
98 gEffisTrain -> SetMarkerColor(4);
99 gEffisTrain -> SetMarkerStyle(29);
100 gEffisTrain -> SetMarkerSize(1);
102 TGraphErrors *gEffisTest = new TGraphErrors(kMoniTrain);
103 gEffisTest -> SetLineColor(2);
104 gEffisTest -> SetMarkerColor(2);
105 gEffisTest -> SetMarkerStyle(29);
106 gEffisTest -> SetMarkerSize(1);
108 fContainer -> AddAt(gEffisTrain,kGraphTrain);
109 fContainer -> AddAt(gEffisTest,kGraphTest);
114 //________________________________________________________________________
115 Bool_t AliTRDpidRefMakerNN::PostProcess()
117 // Draw result to the screen
118 // Called once at the end of the query
120 // build the training andthe test list for the neural networks
122 if(!fDoTraining) return kTRUE;
124 // train the neural networks and build the refrence histos for 2-dim LQ
125 gSystem->Exec(Form("mkdir ./Networks_%d/",fDate));
126 AliDebug(2, Form("TrainMomBin [%d] [%d]", fTrainMomBin, kAll));
128 // train single network for a single momentum (recommended)
129 if(!(fTrainMomBin == kAll)){
130 if(fTrain[fTrainMomBin][0] -> GetN() < fMinTrain){
131 AliDebug(2, Form("Warning in AliTRDpidRefMakerNN::PostProcess : Not enough events for training available! Please check Data sample!"));
134 MakeRefs(fTrainMomBin);
135 // TrainNetworks(fTrainMomBin);
136 MonitorTraining(fTrainMomBin);
140 for(Int_t iMomBin = 0; iMomBin < AliTRDCalPID::kNMom; iMomBin++){
141 if(fTrain[iMomBin][0] -> GetN() < fMinTrain){
142 AliDebug(2, Form("Warning in AliTRDpidRefMakerNN::PostProcess : Not enough events for training available for momentum bin [%d]! Please check Data sample!", iMomBin));
145 MakeRefs(fTrainMomBin);
146 // TrainNetworks(iMomBin);
147 MonitorTraining(iMomBin);
151 return kTRUE; // testing protection
155 //________________________________________________________________________
156 void AliTRDpidRefMakerNN::MakeTrainingLists()
159 // build the training lists for the neural networks
163 LoadFile("TRD.CalibPidRefMakerNN.root");
167 Printf("ERROR tree for training list not available");
171 AliDebug(2, " Making training lists! \n");
173 Int_t nPart[AliPID::kSPECIES][AliTRDCalPID::kNMom];
174 memset(nPart, 0, AliPID::kSPECIES*AliTRDCalPID::kNMom*sizeof(Int_t));
176 // set needed branches
179 // start first loop to check total number of each particle type
180 for(Int_t iEv=0; iEv < fData -> GetEntries(); iEv++){
181 fData -> GetEntry(iEv);
183 // use only events with goes through 6 layers TRD
184 if(fPIDdataArray->fNtracklets != AliTRDgeometry::kNlayer) continue;
186 for(Int_t ily=AliTRDgeometry::kNlayer; ily--;) nPart[fPIDbin][fPIDdataArray->fData[ily].fPLbin & 0xf]++;
189 AliDebug(2, "Particle multiplicities:");
190 for(Int_t iMomBin = 0; iMomBin <AliTRDCalPID::kNMom; iMomBin++)
191 AliDebug(2, Form("Momentum[%d] Elecs[%d] Muons[%d] Pions[%d] Kaons[%d] Protons[%d]", iMomBin, nPart[AliPID::kElectron][iMomBin], nPart[AliPID::kMuon][iMomBin], nPart[AliPID::kPion][iMomBin], nPart[AliPID::kKaon][iMomBin], nPart[AliPID::kProton][iMomBin]));
193 // implement counter of training and test sample size
194 Int_t iTrain[AliTRDCalPID::kNMom], iTest[AliTRDCalPID::kNMom];
195 memset(iTrain, 0, AliTRDCalPID::kNMom*sizeof(Int_t));
196 memset(iTest, 0, AliTRDCalPID::kNMom*sizeof(Int_t));
198 // set training sample size per momentum interval to 2/3
199 // of smallest particle counter and test sample to 1/3
200 for(Int_t iMomBin = 0; iMomBin < AliTRDCalPID::kNMom; iMomBin++){
201 iTrain[iMomBin] = nPart[0][iMomBin];
202 for(Int_t iPart = 1; iPart < AliPID::kSPECIES; iPart++){
203 if(iTrain[iMomBin] > nPart[iPart][iMomBin])
204 iTrain[iMomBin] = nPart[iPart][iMomBin];
206 iTrain[iMomBin] = Int_t(iTrain[iMomBin] * .66);
207 iTest[iMomBin] = Int_t( iTrain[iMomBin] * .5);
208 AliDebug(2, Form("Momentum[%d] Train[%d] Test[%d]", iMomBin, iTrain[iMomBin], iTest[iMomBin]));
213 memset(nPart, 0, AliPID::kSPECIES*AliTRDCalPID::kNMom*sizeof(Int_t));
215 // start second loop to set the event lists
216 for(Int_t iEv = 0; iEv < fData -> GetEntries(); iEv++){
217 fData -> GetEntry(iEv);
219 // use only events with goes through 6 layers TRD
220 if(fPIDdataArray->fNtracklets != AliTRDgeometry::kNlayer) continue;
222 for(Int_t ily=AliTRDgeometry::kNlayer; ily--;){
223 Int_t iMomBin = fPIDdataArray->fData[ily].fPLbin & 0xf;
226 if(nPart[fPIDbin][iMomBin] < iTrain[iMomBin]){
227 fTrain[iMomBin][ily] -> Enter(iEv + ily);
228 nPart[fPIDbin][iMomBin]++;
229 } else if(nPart[fPIDbin][iMomBin] < iTest[iMomBin]+iTrain[iMomBin]){
230 fTest[iMomBin][ily] -> Enter(iEv + ily);
231 nPart[fPIDbin][iMomBin]++;
236 AliDebug(2, "Particle multiplicities in both lists:");
237 for(Int_t iMomBin = 0; iMomBin <AliTRDCalPID::kNMom; iMomBin++)
238 AliDebug(2, Form("Momentum[%d] Elecs[%d] Muons[%d] Pions[%d] Kaons[%d] Protons[%d]", iMomBin, nPart[AliPID::kElectron][iMomBin], nPart[AliPID::kMuon][iMomBin], nPart[AliPID::kPion][iMomBin], nPart[AliPID::kKaon][iMomBin], nPart[AliPID::kProton][iMomBin]));
242 //________________________________________________________________________
243 void AliTRDpidRefMakerNN::MakeRefs(Int_t mombin)
246 // train the neural networks
250 if (!fData) LoadFile(Form("TRD.Calib%s.root", GetName()));
253 AliError("Tree for training list not available");
258 fDate = datime.GetDate();
260 AliDebug(2, Form("Training momentum bin %d", mombin));
262 // set variable to monitor the training and to save the development of the networks
263 Int_t nEpochs = fEpochs/kMoniTrain;
264 AliDebug(2, Form("Training %d times %d epochs", kMoniTrain, nEpochs));
266 // make directories to save the networks
267 gSystem->Exec(Form("rm -r ./Networks_%d/MomBin_%d",fDate, mombin));
268 gSystem->Exec(Form("mkdir ./Networks_%d/MomBin_%d",fDate, mombin));
270 // variable to check if network can load weights from previous training
271 Bool_t bFirstLoop[AliTRDgeometry::kNlayer];
272 memset(bFirstLoop, kTRUE, AliTRDgeometry::kNlayer*sizeof(Bool_t));
274 // train networks over several loops and save them after each loop
275 for(Int_t iLoop = 0; iLoop < kMoniTrain; iLoop++){
276 // loop over chambers
277 for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
278 // set the event lists
279 fData -> SetEventList(fTrain[mombin][iChamb]);
280 fData -> SetEventList(fTest[mombin][iChamb]);
282 AliDebug(2, Form("Trainingloop[%d] Chamber[%d]", iLoop, iChamb));
284 // check if network is already implemented
285 if(bFirstLoop[iChamb] == kTRUE){
286 fNet[iChamb] = new TMultiLayerPerceptron("fdEdx[0],fdEdx[1],fdEdx[2],fdEdx[3],fdEdx[4],fdEdx[5],fdEdx[6],fdEdx[7]:15:7:fPID[0],fPID[1],fPID[2],fPID[3],fPID[4]!",fData,fTrain[mombin][iChamb],fTest[mombin][iChamb]);
287 fNet[iChamb] -> SetLearningMethod(TMultiLayerPerceptron::kStochastic); // set learning method
288 fNet[iChamb] -> TMultiLayerPerceptron::SetEta(0.001); // set learning speed
289 if(!fContinueTraining){
290 if(DebugLevel()>=2) fNet[iChamb] -> Train(nEpochs,"text update=10, graph");
291 else fNet[iChamb] -> Train(nEpochs,"");
294 fNet[iChamb] -> LoadWeights(Form("./Networks_%d/MomBin_%d/Net%d_%d",fTrainPath, mombin, iChamb, kMoniTrain - 1));
295 if(DebugLevel()>=2) fNet[iChamb] -> Train(nEpochs,"text update=10, graph+");
296 else fNet[iChamb] -> Train(nEpochs,"+");
298 bFirstLoop[iChamb] = kFALSE;
301 if(DebugLevel()>=2) fNet[iChamb] -> Train(nEpochs,"text update=10, graph+");
302 else fNet[iChamb] -> Train(nEpochs,"+");
305 // save weights for monitoring of the training
306 fNet[iChamb] -> DumpWeights(Form("./Networks_%d/MomBin_%d/Net%d_%d",fDate, mombin, iChamb, iLoop));
307 } // end chamber loop
308 } // end training loop
313 //________________________________________________________________________
314 void AliTRDpidRefMakerNN::MonitorTraining(Int_t mombin)
317 // train the neural networks
320 if (!fData) LoadFile(Form("TRD.Calib%s.root", GetName()));
322 AliError("Tree for training list not available");
326 // init networks and set event list
327 for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
328 fNet[iChamb] = new TMultiLayerPerceptron("fdEdx[0],fdEdx[1],fdEdx[2],fdEdx[3],fdEdx[4],fdEdx[5],fdEdx[6],fdEdx[7]:15:7:fPID[0],fPID[1],fPID[2],fPID[3],fPID[4]!",fData,fTrain[mombin][iChamb],fTest[mombin][iChamb]);
329 fData -> SetEventList(fTrain[mombin][iChamb]);
330 fData -> SetEventList(fTest[mombin][iChamb]);
333 // implement variables for likelihoods
334 Float_t like[AliPID::kSPECIES][AliTRDgeometry::kNlayer];
335 memset(like, 0, AliPID::kSPECIES*AliTRDgeometry::kNlayer*sizeof(Float_t));
336 Float_t likeAll[AliPID::kSPECIES], totProb;
338 Double_t pionEffiTrain[kMoniTrain], pionEffiErrTrain[kMoniTrain];
339 Double_t pionEffiTest[kMoniTrain], pionEffiErrTest[kMoniTrain];
340 memset(pionEffiTrain, 0, kMoniTrain*sizeof(Double_t));
341 memset(pionEffiErrTrain, 0, kMoniTrain*sizeof(Double_t));
342 memset(pionEffiTest, 0, kMoniTrain*sizeof(Double_t));
343 memset(pionEffiErrTest, 0, kMoniTrain*sizeof(Double_t));
346 const Float_t epsilon = 1/(2*(AliTRDpidUtil::kBins-1)); // get nice histos with bin center at 0 and 1
347 TH1F *hElecs, *hPions;
348 hElecs = new TH1F("hElecs","Likelihood for electrons", AliTRDpidUtil::kBins, 0.-epsilon, 1.+epsilon);
349 hPions = new TH1F("hPions","Likelihood for pions", AliTRDpidUtil::kBins, 0.-epsilon, 1.+epsilon);
351 TGraphErrors *gEffisTrain=0x0, *gEffisTest=0x0;
352 gEffisTrain = (TGraphErrors*)fContainer->At(kGraphTrain);
353 gEffisTest = (TGraphErrors*)fContainer->At(kGraphTest);
355 AliTRDpidUtil *util = new AliTRDpidUtil();
357 // monitor training progress
358 for(Int_t iLoop = 0; iLoop < kMoniTrain; iLoop++){
361 for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
362 fNet[iChamb] -> LoadWeights(Form("./Networks_%d/MomBin_%d/Net%d_%d",fDate, mombin, iChamb, iLoop));
365 // event loop training list
366 for(Int_t iEvent = 0; iEvent < fTrain[mombin][0] -> GetN(); iEvent++ ){
368 // reset particle probabilities
369 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
370 likeAll[iPart] = 1./AliPID::kSPECIES;
374 fData -> GetEntry(fTrain[mombin][0] -> GetEntry(iEvent));
375 // use event only if it is electron or pion
376 if(!((fPID[AliPID::kElectron] == 1.0) || (fPID[AliPID::kPion] == 1.0))) continue;
378 // get the probabilities for each particle type in each chamber
379 for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
380 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
381 like[iPart][iChamb] = fNet[iChamb] -> Result(fTrain[mombin][iChamb] -> GetEntry(iEvent), iPart);
382 likeAll[iPart] *= like[iPart][iChamb];
386 // get total probability and normalize it
387 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
388 totProb += likeAll[iPart];
390 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
391 likeAll[iPart] /= totProb;
394 // fill likelihood distributions
395 if(fPID[AliPID::kElectron] == 1)
396 hElecs -> Fill(likeAll[AliPID::kElectron]);
397 if(fPID[AliPID::kPion] == 1)
398 hPions -> Fill(likeAll[AliPID::kElectron]);
402 // calculate the pion efficiency and fill the graph
403 util -> CalculatePionEffi(hElecs, hPions);
404 pionEffiTrain[iLoop] = util -> GetPionEfficiency();
405 pionEffiErrTrain[iLoop] = util -> GetError();
407 gEffisTrain -> SetPoint(iLoop, iLoop+1, pionEffiTrain[iLoop]);
408 gEffisTrain -> SetPointError(iLoop, 0, pionEffiErrTrain[iLoop]);
411 AliDebug(2, Form("TrainingLoop[%d] PionEfficiency[%f +/- %f]", iLoop, pionEffiTrain[iLoop], pionEffiErrTrain[iLoop]));
416 // event loop test list
417 for(Int_t iEvent = 0; iEvent < fTest[mombin][0] -> GetN(); iEvent++ ){
419 // reset particle probabilities
420 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
421 likeAll[iPart] = 1./AliTRDgeometry::kNlayer;
425 fData -> GetEntry(fTest[mombin][0] -> GetEntry(iEvent));
426 // use event only if it is electron or pion
427 if(!((fPID[AliPID::kElectron] == 1.0) || (fPID[AliPID::kPion] == 1.0))) continue;
429 // get the probabilities for each particle type in each chamber
430 for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
431 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
432 like[iPart][iChamb] = fNet[iChamb] -> Result(fTest[mombin][iChamb] -> GetEntry(iEvent), iPart);
433 likeAll[iPart] *= like[iPart][iChamb];
437 // get total probability and normalize it
438 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
439 totProb += likeAll[iPart];
441 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
442 likeAll[iPart] /= totProb;
445 // fill likelihood distributions
446 if(fPID[AliPID::kElectron] == 1)
447 hElecs -> Fill(likeAll[AliPID::kElectron]);
448 if(fPID[AliPID::kPion] == 1)
449 hPions -> Fill(likeAll[AliPID::kElectron]);
452 // calculate the pion efficiency and fill the graph
453 util -> CalculatePionEffi(hElecs, hPions);
454 pionEffiTest[iLoop] = util -> GetPionEfficiency();
455 pionEffiErrTest[iLoop] = util -> GetError();
457 gEffisTest -> SetPoint(iLoop, iLoop+1, pionEffiTest[iLoop]);
458 gEffisTest -> SetPointError(iLoop, 0, pionEffiErrTest[iLoop]);
461 AliDebug(2, Form("TestLoop[%d] PionEfficiency[%f +/- %f] \n", iLoop, pionEffiTest[iLoop], pionEffiErrTest[iLoop]));
463 } // end training loop
467 gEffisTest -> Draw("PAL");
468 gEffisTrain -> Draw("PL");
473 //________________________________________________________________________
474 void AliTRDpidRefMakerNN::LoadFile(const Char_t *InFileNN)
477 // Loads the files and sets the event list
478 // for neural network training and
479 // building of the 2-dim reference histograms.
480 // Useable for training outside of the makeResults.C macro
484 fInFileNN = new TFile(InFileNN, "READ");
485 fData = (TTree*)fInFileNN -> Get("NN");
487 for(Int_t iMom = 0; iMom < AliTRDCalPID::kNMom; iMom++){
488 for(Int_t ily = 0; ily < AliTRDgeometry::kNlayer; ily++){
489 fTrain[iMom][ily] = new TEventList(Form("fTrainMom%d_%d", iMom, ily), Form("Training list for momentum intervall %d and plane %d", iMom, ily));
490 fTest[iMom][ily] = new TEventList(Form("fTestMom%d_%d", iMom, ily), Form("Test list for momentum intervall %d and plane %d", iMom, ily));
496 // //________________________________________________________________________
497 // void AliTRDpidRefMakerNN::LoadContainer(const Char_t *InFileCont)
501 // // Loads the container if no container is there.
502 // // Useable for training outside of the makeResults.C macro
505 // TFile *fInFileCont;
506 // fInFileCont = new TFile(InFileCont, "READ");
507 // fContainer = (TObjArray*)fInFileCont -> Get("PidRefMaker");
512 // //________________________________________________________________________
513 // void AliTRDpidRefMakerNN::CreateGraphs()
515 // // Create histograms
518 // OpenFile(0, "RECREATE");
519 // fContainer = new TObjArray();
520 // fContainer->AddAt(new TH1F("hPDG","hPDG",AliPID::kSPECIES,-0.5,5.5),0);
522 // TGraphErrors *gEffisTrain = new TGraphErrors(kMoniTrain);
523 // gEffisTrain -> SetLineColor(4);
524 // gEffisTrain -> SetMarkerColor(4);
525 // gEffisTrain -> SetMarkerStyle(29);
526 // gEffisTrain -> SetMarkerSize(2);
528 // TGraphErrors *gEffisTest = new TGraphErrors(kMoniTrain);
529 // gEffisTest -> SetLineColor(2);
530 // gEffisTest -> SetMarkerColor(2);
531 // gEffisTest -> SetMarkerSize(2);
533 // fContainer -> AddAt(gEffisTrain,kGraphTrain);
534 // fContainer -> AddAt(gEffisTest,kGraphTest);