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 "AliTRDpidUtil.h"
41 #include "AliTRDpidRefMakerNN.h"
42 #include "AliTRDpidUtil.h"
44 #include "Cal/AliTRDCalPID.h"
45 #include "Cal/AliTRDCalPIDNN.h"
46 #include "info/AliTRDtrackInfo.h"
47 #include "info/AliTRDv0Info.h"
48 #include "info/AliTRDpidInfo.h"
50 ClassImp(AliTRDpidRefMakerNN)
52 //________________________________________________________________________
53 AliTRDpidRefMakerNN::AliTRDpidRefMakerNN()
69 // Default constructor
71 SetNameTitle("PIDrefMakerNN", "PID(NN) Reference Maker");
73 memset(fTrain, 0, AliTRDCalPID::kNMom*sizeof(TEventList*));
74 memset(fTest, 0, AliTRDCalPID::kNMom*sizeof(TEventList*));
75 memset(fTrainData, 0, AliTRDCalPID::kNMom*sizeof(TTree*));
78 SetScaledEdx(Float_t(AliTRDCalPIDNN::kMLPscale));
80 fDate = datime.GetDate();
83 //________________________________________________________________________
84 AliTRDpidRefMakerNN::AliTRDpidRefMakerNN(const char *name)
85 :AliTRDpidRefMaker(name, "PID(NN) Reference Maker")
100 // Default constructor
103 memset(fTrain, 0, AliTRDCalPID::kNMom*sizeof(TEventList*));
104 memset(fTest, 0, AliTRDCalPID::kNMom*sizeof(TEventList*));
105 memset(fTrainData, 0, AliTRDCalPID::kNMom*sizeof(TTree*));
108 SetScaledEdx(Float_t(AliTRDCalPIDNN::kMLPscale));
110 fDate = datime.GetDate();
114 //________________________________________________________________________
115 AliTRDpidRefMakerNN::~AliTRDpidRefMakerNN()
120 //________________________________________________________________________
121 void AliTRDpidRefMakerNN::MakeTrainTestTrees()
123 // Create output file and tree
126 fRef = new TFile("TRD.CalibPIDrefMakerNN.root", "RECREATE");
127 for(Int_t ip = 0; ip < AliTRDCalPID::kNMom; ip++){
128 fTrainData[ip] = new TTree(Form("fTrainData_%d", ip), Form("NN Reference Data for MomBin %d", ip));
129 fTrainData[ip] -> Branch("fdEdx", fdEdx, Form("fdEdx[%d]/F", AliTRDpidUtil::kNNslices));
130 fTrainData[ip] -> Branch("fPID", fPID, Form("fPID[%d]/F", AliPID::kSPECIES));
131 fTrainData[ip] -> Branch("fLy", &fLy, "fLy/I");
132 fTrainData[ip] -> Branch("fNtrkl", &fNtrkl, "fNtrkl/I");
134 fTrain[ip] = new TEventList(Form("fTrainMom%d", ip), Form("Training list for momentum intervall %d", ip));
135 fTest[ip] = new TEventList(Form("fTestMom%d", ip), Form("Test list for momentum intervall %d", ip));
141 //________________________________________________________________________
142 Bool_t AliTRDpidRefMakerNN::PostProcess()
144 // Draw result to the screen
145 // Called once at the end of the query
147 TFile *fCalib = TFile::Open(Form("AnalysisResults.root"));
149 AliError("Calibration file not available");
152 TDirectoryFile *dCalib = (TDirectoryFile*)fCalib->Get("TRD.CalibPIDrefMaker");
154 AliError("Calibration directory not available");
157 fData = (TTree*)dCalib->Get("RefPID");
159 AliError("Calibration data not available");
163 if(!(o = (TObjArray*)dCalib->Get("MonitorNN"))) {
164 AliWarning("Missing monitoring container.");
167 fContainer = (TObjArray*)o->Clone("monitor");
172 AliDebug(2, "Loading file TRD.CalibPIDrefMakerNN.root");
173 LoadFile("TRD.CalibPIDrefMakerNN.root");
175 else AliDebug(2, "file available");
178 MakeTrainingSample();
180 else AliDebug(2, "file available");
183 // build the training and the test list for the neural networks
184 for(Int_t ip = 0; ip < AliTRDCalPID::kNMom; ip++){
185 MakeTrainingLists(ip);
187 if(!fDoTraining) return kTRUE;
191 // train the neural networks
192 gSystem->Exec(Form("mkdir ./Networks_%d/",fDate));
193 AliDebug(2, Form("TrainMomBin [%d] [%d]", fTrainMomBin, kAll));
195 // train single network for a single momentum (recommended)
196 if(!(fTrainMomBin == kAll)){
197 if(fTrain[fTrainMomBin] -> GetN() < fMinTrain){
198 AliError("Warning in AliTRDpidRefMakerNN::PostProcess : Not enough events for training available! Please check Data sample!");
201 MakeRefs(fTrainMomBin);
202 MonitorTraining(fTrainMomBin);
206 for(Int_t iMomBin = 0; iMomBin < AliTRDCalPID::kNMom; iMomBin++){
207 if(fTrain[iMomBin] -> GetN() < fMinTrain){
208 AliError(Form("Warning in AliTRDpidRefMakerNN::PostProcess : Not enough events for training available for momentum bin [%d]! Please check Data sample!", iMomBin));
212 MonitorTraining(iMomBin);
216 return kTRUE; // testing protection
220 //________________________________________________________________________
221 Bool_t AliTRDpidRefMakerNN::MakeTrainingSample()
223 // convert AnalysisResults.root to training file
224 TFile *fCalib = TFile::Open(Form("AnalysisResults.root"));
226 AliError("Calibration file not available");
229 TDirectoryFile *dCalib = (TDirectoryFile*)fCalib->Get("TRD.CalibPIDrefMaker");
231 AliError("Calibration directory not available");
234 fData = (TTree*)dCalib->Get("RefPID");
236 AliError("Calibration data not available");
240 if(!(o = (TObjArray*)dCalib->Get("MonitorNN"))) {
241 AliWarning("Missing monitoring container.");
244 fContainer = (TObjArray*)o->Clone("monitor");
247 MakeTrainTestTrees();
249 // Convert the CaliPIDrefMaker tree to 11 (different momentum bin) trees for NN training
252 for(Int_t ip=0; ip < AliTRDCalPID::kNMom; ip++){
253 for(Int_t is=0; is < AliPID::kSPECIES; is++) {
254 memset(fPID, 0, AliPID::kSPECIES*sizeof(Float_t));
256 Int_t n(0); // index of data
257 for(Int_t itrk = 0; itrk<fData->GetEntries() && n<kMaxStat; itrk++){
258 if(!(fData->GetEntry(itrk))) continue;
259 if(fPIDdataArray->GetPID()!=is) continue;
260 fNtrkl = fPIDdataArray->GetNtracklets();
261 for(Int_t ily=fPIDdataArray->GetNtracklets(); ily--;){
263 if(fPIDdataArray->GetData(ily)->Momentum()!= ip) continue;
264 memset(fdEdx, 0, AliTRDpidUtil::kNNslices*sizeof(Float_t));
265 for(Int_t islice=AliTRDCalPID::kNSlicesNN; islice--;){
266 fdEdx[islice]+=fPIDdataArray->GetData(ily)->fdEdx[islice];
267 fdEdx[islice]/=fScale;
269 fTrainData[ip] -> Fill();
273 AliDebug(2, Form("%d %d %d", ip, is, n));
279 for(Int_t ip = 0; ip < AliTRDCalPID::kNMom; ip++){
280 fTrainData[ip] -> Write();
285 else AliWarning("Training file available. No conversion done!");
290 //________________________________________________________________________
291 void AliTRDpidRefMakerNN::MakeTrainingLists(Int_t mombin)
294 // build the training lists for the neural networks
298 LoadFile(Form("TRD.Calib%s.root", GetName()));
302 AliError("ERROR file for building training list not available");
306 AliDebug(2, "\n Making training lists! \n");
308 Int_t nPart[AliPID::kSPECIES];
309 memset(nPart, 0, AliPID::kSPECIES*sizeof(Int_t));
311 // set needed branches
312 fTrainData[mombin] -> SetBranchAddress("fdEdx", fdEdx);
313 fTrainData[mombin] -> SetBranchAddress("fPID", fPID);
314 fTrainData[mombin] -> SetBranchAddress("fLy", &fLy);
315 fTrainData[mombin] -> SetBranchAddress("fNtrkl", &fNtrkl);
317 // start first loop to check total number of each particle type
318 for(Int_t iEv=0; iEv < fTrainData[mombin] -> GetEntries(); iEv++){
319 fTrainData[mombin] -> GetEntry(iEv);
321 // use only events with goes through 6 layers TRD
322 if(fNtrkl != AliTRDgeometry::kNlayer) continue;
324 if(fPID[AliPID::kElectron] == 1)
325 nPart[AliPID::kElectron]++;
326 else if(fPID[AliPID::kMuon] == 1)
327 nPart[AliPID::kMuon]++;
328 else if(fPID[AliPID::kPion] == 1)
329 nPart[AliPID::kPion]++;
330 else if(fPID[AliPID::kKaon] == 1)
331 nPart[AliPID::kKaon]++;
332 else if(fPID[AliPID::kProton] == 1)
333 nPart[AliPID::kProton]++;
336 AliDebug(2, "Particle multiplicities:");
337 AliDebug(2, Form("Momentum[%d] Elecs[%d] Muons[%d] Pions[%d] Kaons[%d] Protons[%d]", mombin, nPart[AliPID::kElectron], nPart[AliPID::kMuon], nPart[AliPID::kPion], nPart[AliPID::kKaon], nPart[AliPID::kProton]));
341 // // implement counter of training and test sample size
342 Int_t iTrain = 0, iTest = 0;
344 // set training sample size per momentum interval to 2/3
345 // of smallest particle counter and test sample to 1/3
347 for(Int_t iPart = 1; iPart < AliPID::kSPECIES; iPart++){
348 // exclude muons and kaons if not availyable
349 // this is neeeded since we do not have v0 candiates
350 if((iPart == AliPID::kMuon || iPart == AliPID::kKaon) && (nPart[AliPID::kMuon] == 0 || nPart[AliPID::kKaon] == 0)) continue;
351 if(iTrain > nPart[iPart])
352 iTrain = nPart[iPart];
354 iTest = Int_t( iTrain * (1-fFreq));
355 iTrain = Int_t(iTrain * fFreq);
356 AliDebug(2, Form("Momentum[%d] Train[%d] Test[%d]", mombin, iTrain, iTest));
361 memset(nPart, 0, AliPID::kSPECIES*sizeof(Int_t));
363 // start second loop to set the event lists
364 for(Int_t iEv = 0; iEv < fTrainData[mombin] -> GetEntries(); iEv++){
365 fTrainData[mombin] -> GetEntry(iEv);
368 for(Int_t is = 0; is < AliPID::kSPECIES; is++){
369 if(nPart[is] < iTrain && fPID[is] == 1){
370 fTrain[mombin] -> Enter(iEv);
372 } else if(nPart[is] < iTest+iTrain && fPID[is] == 1){
373 fTest[mombin] -> Enter(iEv);
379 AliDebug(2, "Particle multiplicities in both lists:");
380 AliDebug(2, Form("Momentum[%d] Elecs[%d] Muons[%d] Pions[%d] Kaons[%d] Protons[%d]", mombin, nPart[AliPID::kElectron], nPart[AliPID::kMuon], nPart[AliPID::kPion], nPart[AliPID::kKaon], nPart[AliPID::kProton]));
386 //________________________________________________________________________
387 void AliTRDpidRefMakerNN::MakeRefs(Int_t mombin)
390 // train the neural networks
394 if (!fTrainData[mombin]) LoadFile(Form("TRD.CalibPIDrefMakerNN.root"));
396 if (!fTrainData[mombin]) {
397 AliError("Tree for training list not available");
402 fDate = datime.GetDate();
404 AliDebug(2, Form("Training momentum bin %d", mombin));
406 // set variable to monitor the training and to save the development of the networks
407 Int_t nEpochs = fEpochs/kMoniTrain;
408 AliDebug(2, Form("Training %d times %d epochs", kMoniTrain, nEpochs));
410 // make directories to save the networks
411 gSystem->Exec(Form("rm -r ./Networks_%d/MomBin_%d",fDate, mombin));
412 gSystem->Exec(Form("mkdir ./Networks_%d/MomBin_%d",fDate, mombin));
414 // variable to check if network can load weights from previous training
415 Bool_t bFirstLoop = kTRUE;
417 // train networks over several loops and save them after each loop
418 for(Int_t iLoop = 0; iLoop < kMoniTrain; iLoop++){
419 fTrainData[mombin] -> SetEventList(fTrain[mombin]);
420 fTrainData[mombin] -> SetEventList(fTest[mombin]);
422 AliDebug(2, Form("Momentum[%d] Trainingloop[%d]", mombin, iLoop));
424 // check if network is already implemented
425 if(bFirstLoop == kTRUE){
426 fNet = 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]!",fTrainData[mombin],fTrain[mombin],fTest[mombin]);
427 fNet -> SetLearningMethod(TMultiLayerPerceptron::kStochastic); // set learning method
428 fNet -> TMultiLayerPerceptron::SetEta(0.001); // set learning speed
429 if(!fContinueTraining){
430 if(AliLog::GetDebugLevel("","AliTRDpidRefMakerNN")>=2) fNet -> Train(nEpochs,"text update=10, graph");
431 else fNet -> Train(nEpochs,"");
434 fNet -> LoadWeights(Form("./Networks_%d/MomBin_%d/Net_%d",fTrainPath, mombin, kMoniTrain - 1));
435 if(AliLog::GetDebugLevel("","AliTRDpidRefMakerNN")>=2) fNet -> Train(nEpochs,"text update=10, graph+");
436 else fNet -> Train(nEpochs,"+");
441 if(AliLog::GetDebugLevel("","AliTRDpidRefMakerNN")>=2) fNet -> Train(nEpochs,"text update=10, graph+");
442 else fNet -> Train(nEpochs,"+");
445 // save weights for monitoring of the training
446 fNet -> DumpWeights(Form("./Networks_%d/MomBin_%d/Net_%d",fDate, mombin, iLoop));
447 } // end training loop
452 //________________________________________________________________________
453 void AliTRDpidRefMakerNN::MonitorTraining(Int_t mombin)
456 // train the neural networks
459 if (!fTrainData[mombin]) LoadFile(Form("TRD.CalibPIDrefMakerNN.root"));
460 if (!fTrainData[mombin]) {
461 AliError("Tree for training list not available");
465 // init networks and set event list
466 for(Int_t iChamb = 0; iChamb < AliTRDgeometry::kNlayer; iChamb++){
467 fNet = 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]!",fTrainData[mombin],fTrain[mombin],fTest[mombin]);
468 fTrainData[mombin] -> SetEventList(fTrain[mombin]);
469 fTrainData[mombin] -> SetEventList(fTest[mombin]);
472 // implement variables for likelihoods
473 Float_t like[AliPID::kSPECIES][AliTRDgeometry::kNlayer];
474 memset(like, 0, AliPID::kSPECIES*AliTRDgeometry::kNlayer*sizeof(Float_t));
475 Float_t likeAll[AliPID::kSPECIES], totProb;
477 Double_t pionEffiTrain[kMoniTrain], pionEffiErrTrain[kMoniTrain];
478 Double_t pionEffiTest[kMoniTrain], pionEffiErrTest[kMoniTrain];
479 memset(pionEffiTrain, 0, kMoniTrain*sizeof(Double_t));
480 memset(pionEffiErrTrain, 0, kMoniTrain*sizeof(Double_t));
481 memset(pionEffiTest, 0, kMoniTrain*sizeof(Double_t));
482 memset(pionEffiErrTest, 0, kMoniTrain*sizeof(Double_t));
485 const Float_t epsilon = 1/(2*(AliTRDpidUtil::kBins-1)); // get nice histos with bin center at 0 and 1
486 TH1F *hElecs, *hPions;
487 hElecs = new TH1F("hElecs","Likelihood for electrons", AliTRDpidUtil::kBins, 0.-epsilon, 1.+epsilon);
488 hPions = new TH1F("hPions","Likelihood for pions", AliTRDpidUtil::kBins, 0.-epsilon, 1.+epsilon);
490 TGraphErrors *gEffisTrain = new TGraphErrors(kMoniTrain);
491 gEffisTrain -> SetLineColor(4);
492 gEffisTrain -> SetMarkerColor(4);
493 gEffisTrain -> SetMarkerStyle(29);
494 gEffisTrain -> SetMarkerSize(1);
496 TGraphErrors *gEffisTest = new TGraphErrors(kMoniTrain);
497 gEffisTest -> SetLineColor(2);
498 gEffisTest -> SetMarkerColor(2);
499 gEffisTest -> SetMarkerStyle(29);
500 gEffisTest -> SetMarkerSize(1);
502 AliTRDpidUtil *util = new AliTRDpidUtil();
504 // monitor training progress
505 for(Int_t iLoop = 0; iLoop < kMoniTrain; iLoop++){
508 fNet -> LoadWeights(Form("./Networks_%d/MomBin_%d/Net_%d",fDate, mombin, iLoop));
509 AliDebug(2, Form("./Networks_%d/MomBin_%d/Net_%d",fDate, mombin, iLoop));
511 // event loop training list
513 for(Int_t is = 0; is < AliPID::kSPECIES; is++){
515 if(!((is == AliPID::kElectron) || (is == AliPID::kPion))) continue;
518 // reset particle probabilities
519 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
520 likeAll[iPart] = 1./AliPID::kSPECIES;
524 AliDebug(2, Form("%d",fTrain[mombin] -> GetN()));
525 for(Int_t iEvent = 0; iEvent < fTrain[mombin] -> GetN(); iEvent++ ){
526 fTrainData[mombin] -> GetEntry(fTrain[mombin] -> GetEntry(iEvent));
527 // use event only if it is electron or pion
528 if(fPID[is] <1.e-5) continue;
529 // get the probabilities for each particle type in each chamber
530 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
531 like[iPart][iChamb] = fNet -> Result(fTrain[mombin] -> GetEntry(iEvent), iPart);
532 likeAll[iPart] *= like[iPart][iChamb];
537 // get total probability and normalize it
538 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
539 totProb += likeAll[iPart];
541 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
542 likeAll[iPart] /= totProb;
546 // fill likelihood distributions
547 if(fPID[AliPID::kElectron] == 1)
548 hElecs -> Fill(likeAll[AliPID::kElectron]);
549 if(fPID[AliPID::kPion] == 1)
550 hPions -> Fill(likeAll[AliPID::kElectron]);
552 // reset particle probabilities
553 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
554 likeAll[iPart] = 1./AliPID::kSPECIES;
559 } // end species loop
562 // calculate the pion efficiency and fill the graph
563 util -> CalculatePionEffi(hElecs, hPions);
564 pionEffiTrain[iLoop] = util -> GetPionEfficiency();
565 pionEffiErrTrain[iLoop] = util -> GetError();
567 gEffisTrain -> SetPoint(iLoop, iLoop+1, pionEffiTrain[iLoop]);
568 gEffisTrain -> SetPointError(iLoop, 0, pionEffiErrTrain[iLoop]);
571 AliDebug(2, Form("TrainingLoop[%d] PionEfficiency[%f +/- %f]", iLoop, pionEffiTrain[iLoop], pionEffiErrTrain[iLoop]));
576 // monitor validation progress
577 for(Int_t is = 0; is < AliPID::kSPECIES; is++){
579 if(!((is == AliPID::kElectron) || (is == AliPID::kPion))) continue;
582 // reset particle probabilities
583 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
584 likeAll[iPart] = 1./AliPID::kSPECIES;
588 for(Int_t iEvent = 0; iEvent < fTest[mombin] -> GetN(); iEvent++ ){
589 fTrainData[mombin] -> GetEntry(fTest[mombin] -> GetEntry(iEvent));
590 // use event only if it is electron or pion
591 if(TMath::Abs(fPID[is]- 1.)<1.e-5) continue;
593 // get the probabilities for each particle type in each chamber
594 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
595 like[iPart][iChamb] = fNet -> Result(fTest[mombin] -> GetEntry(iEvent), iPart);
596 likeAll[iPart] *= like[iPart][iChamb];
600 // get total probability and normalize it
601 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
602 totProb += likeAll[iPart];
604 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
605 likeAll[iPart] /= totProb;
609 // fill likelihood distributions
610 if(fPID[AliPID::kElectron] == 1)
611 hElecs -> Fill(likeAll[AliPID::kElectron]);
612 if(fPID[AliPID::kPion] == 1)
613 hPions -> Fill(likeAll[AliPID::kElectron]);
615 // reset particle probabilities
616 for(Int_t iPart = 0; iPart < AliPID::kSPECIES; iPart++){
617 likeAll[iPart] = 1./AliPID::kSPECIES;
622 } // end species loop
624 // calculate the pion efficiency and fill the graph
625 util -> CalculatePionEffi(hElecs, hPions);
626 pionEffiTest[iLoop] = util -> GetPionEfficiency();
627 pionEffiErrTest[iLoop] = util -> GetError();
629 gEffisTest -> SetPoint(iLoop, iLoop+1, pionEffiTest[iLoop]);
630 gEffisTest -> SetPointError(iLoop, 0, pionEffiErrTest[iLoop]);
633 AliDebug(2, Form("TestLoop[%d] PionEfficiency[%f +/- %f] \n", iLoop, pionEffiTest[iLoop], pionEffiErrTest[iLoop]));
635 } // end validation loop
639 gEffisTest -> Draw("PAL");
640 gEffisTrain -> Draw("PL");
645 //________________________________________________________________________
646 Bool_t AliTRDpidRefMakerNN::LoadFile(const Char_t *InFileNN)
649 // Loads the files and sets the event list
650 // for neural network training.
651 // Useable for training outside of the makeResults.C macro
654 fRef = TFile::Open(Form("%s", InFileNN));
656 for(Int_t ip = 0; ip < AliTRDCalPID::kNMom; ip++){
657 fTrainData[ip] = (TTree*)fRef -> Get(Form("fTrainData_%d", ip));
660 for(Int_t iMom = 0; iMom < AliTRDCalPID::kNMom; iMom++){
661 fTrain[iMom] = new TEventList(Form("fTrainMom%d", iMom), Form("Training list for momentum intervall %d", iMom));
662 fTest[iMom] = new TEventList(Form("fTestMom%d", iMom), Form("Test list for momentum intervall %d", iMom));