ROOT logo
/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/* $Id$ */

////////////////////////////////////////////////////////////////////////////
//                                                                        //
// Container of the distributions for the neural network                  //
//                                                                        //
// Author:                                                                //
// Alex Wilk <wilka@uni-muenster.de>                                      //
//                                                                        //
////////////////////////////////////////////////////////////////////////////

#include <TFile.h>
#include <TROOT.h>
#include <TMultiLayerPerceptron.h>

#include "AliPID.h"
#include "AliLog.h"

#include "AliTRDgeometry.h"
#include "AliTRDCalPIDNN.h"

ClassImp(AliTRDCalPIDNN)

//_________________________________________________________________________
AliTRDCalPIDNN::AliTRDCalPIDNN()
  :AliTRDCalPID("pid", "NN PID references for TRD")
{
  //
  //  The Default constructor
  //

  Init();

}

//_________________________________________________________________________
AliTRDCalPIDNN::AliTRDCalPIDNN(const Text_t *name, const Text_t *title) 
  :AliTRDCalPID(name,title)
{
  //
  //  The main constructor
  //
  
  Init();

}

// //_____________________________________________________________________________
// AliTRDCalPIDNN::AliTRDCalPIDNN(const AliTRDCalPIDNN &c) 
//   :TNamed(c)
//   ,fMeanChargeRatio(c.fMeanChargeRatio)
//   ,fModel(0x0)
// {
//   //
//   // Copy constructor
//   //
// 
//   if (this != &c) ((AliTRDCalPIDNN &) c).Copy(*this);
//   
// }

//_________________________________________________________________________
AliTRDCalPIDNN::~AliTRDCalPIDNN()
{
  //
  // Destructor
  //

  //if (fModel) delete fModel;
  
}

//_________________________________________________________________________
Bool_t AliTRDCalPIDNN::LoadReferences(Char_t *refFile)
{
  //
  // Read the TRD Neural Networks
  //

  // Read NN Root file  
  TFile *nnFile = TFile::Open(refFile,"READ");
  if (!nnFile || !nnFile->IsOpen()) {
    AliError(Form("Opening TRD histgram file %s failed",refFile));
    return kFALSE;
  }
  gROOT->cd();

  // Read Networks
  for (Int_t imom = 0; imom < kNMom; imom++) {
    for (Int_t iplane = 0; iplane < AliTRDgeometry::kNlayer; iplane++) {
      TMultiLayerPerceptron *nn = (TMultiLayerPerceptron *)
         nnFile->Get(Form("NN_Mom%d_Plane%d",imom,iplane));
      fModel->AddAt(nn,GetModelID(imom,0,iplane));
    }
  }

  nnFile->Close();
  delete nnFile;

  return kTRUE;

}

//_________________________________________________________________________
TObject *AliTRDCalPIDNN::GetModel(Int_t ip, Int_t, Int_t iplane) const
{
  //
  // Returns one selected TMultiLayerPerceptron. iType not used.
  //

  if (ip<0 || ip>= kNMom) return 0x0;
  
  AliInfo(Form("Retrive MultiLayerPerceptron for %5.2f GeV/c for plane %d" 
         ,fgTrackMomentum[ip]
         ,iplane));
  
  return fModel->At(GetModelID(ip, 0, iplane));

}

//_________________________________________________________________________
Double_t AliTRDCalPIDNN::GetProbability(Int_t spec, Float_t mom
                                      , const Float_t * const dedx
                                      , Float_t, Int_t iplane) const
{
  //
  // Core function of AliTRDCalPID class for calculating the
  // likelihood for species "spec" (see AliTRDtrack::kNspecie) of a
  // given momentum "mom", a given dE/dx (slice "dedx") yield per
  // layer in a given layer (iplane)
  //

  if (spec < 0 || spec >= AliPID::kSPECIES) return 0.;

  // find the interval in momentum and track segment length which applies for this data
  
  Int_t imom = 1;
  while (imom<AliTRDCalPID::kNMom-1 && mom>fgTrackMomentum[imom]) imom++;
  Double_t lNN1, lNN2;
  Double_t mom1 = fgTrackMomentum[imom-1], mom2 = fgTrackMomentum[imom];

  TMultiLayerPerceptron *nn = 0x0;
  if(!(nn = (TMultiLayerPerceptron *) fModel->At(GetModelID(imom-1, spec, iplane/*, ilength*/)))){
    AliInfo(Form("Looking for mom(%f) plane(%d)", mom-1, iplane));
    AliError(Form("NN id %d not found in DB.", GetModelID(imom-1, spec, iplane)));
    return 0.;
  }

  Double_t ddedx[AliTRDCalPID::kNSlicesNN];

  for (int inode=0; inode<AliTRDCalPID::kNSlicesNN; inode++) {
    ddedx[inode] = (Double_t) dedx[inode]/kMLPscale;
  }

  lNN1 = nn->Evaluate(spec, ddedx);
  
  if(!(nn = (TMultiLayerPerceptron*)fModel->At(GetModelID(imom, spec, iplane/*, ilength*/)))){
    AliInfo(Form("Looking for mom(%f) plane(%d)", mom, iplane));
    AliError(Form("NN id %d not found in DB.", GetModelID(imom, spec, iplane)));
    return lNN1;
  }
  lNN2 = nn->Evaluate(spec, ddedx);
  
  // return interpolation over momentum binning
  if      (mom < fgTrackMomentum[0]) {
    return lNN1;
  }
  else if (mom > fgTrackMomentum[AliTRDCalPID::kNMom-1]) {
    return lNN2;
  }
  else {
    return lNN1 + (lNN2 - lNN1)*(mom - mom1)/(mom2 - mom1);
  }
  
}

//_________________________________________________________________________
void AliTRDCalPIDNN::Init()
{
  //
  // Initialization
  //

  fModel = new TObjArray(AliTRDgeometry::kNlayer * AliTRDCalPID::kNMom);
  fModel->SetOwner();
  
}

//_________________________________________________________________________
Int_t AliTRDCalPIDNN::GetModelID(Int_t mom, Int_t /*ii*/, Int_t plane)
{
  
  // returns the ID of the NN distribution (66 MLPs, ID from 56 to 121)

  return /*AliPID::kSPECIES * AliTRDCalPID::kNMom + */
    plane * AliTRDCalPID::kNMom + mom;  
  
}
 AliTRDCalPIDNN.cxx:1
 AliTRDCalPIDNN.cxx:2
 AliTRDCalPIDNN.cxx:3
 AliTRDCalPIDNN.cxx:4
 AliTRDCalPIDNN.cxx:5
 AliTRDCalPIDNN.cxx:6
 AliTRDCalPIDNN.cxx:7
 AliTRDCalPIDNN.cxx:8
 AliTRDCalPIDNN.cxx:9
 AliTRDCalPIDNN.cxx:10
 AliTRDCalPIDNN.cxx:11
 AliTRDCalPIDNN.cxx:12
 AliTRDCalPIDNN.cxx:13
 AliTRDCalPIDNN.cxx:14
 AliTRDCalPIDNN.cxx:15
 AliTRDCalPIDNN.cxx:16
 AliTRDCalPIDNN.cxx:17
 AliTRDCalPIDNN.cxx:18
 AliTRDCalPIDNN.cxx:19
 AliTRDCalPIDNN.cxx:20
 AliTRDCalPIDNN.cxx:21
 AliTRDCalPIDNN.cxx:22
 AliTRDCalPIDNN.cxx:23
 AliTRDCalPIDNN.cxx:24
 AliTRDCalPIDNN.cxx:25
 AliTRDCalPIDNN.cxx:26
 AliTRDCalPIDNN.cxx:27
 AliTRDCalPIDNN.cxx:28
 AliTRDCalPIDNN.cxx:29
 AliTRDCalPIDNN.cxx:30
 AliTRDCalPIDNN.cxx:31
 AliTRDCalPIDNN.cxx:32
 AliTRDCalPIDNN.cxx:33
 AliTRDCalPIDNN.cxx:34
 AliTRDCalPIDNN.cxx:35
 AliTRDCalPIDNN.cxx:36
 AliTRDCalPIDNN.cxx:37
 AliTRDCalPIDNN.cxx:38
 AliTRDCalPIDNN.cxx:39
 AliTRDCalPIDNN.cxx:40
 AliTRDCalPIDNN.cxx:41
 AliTRDCalPIDNN.cxx:42
 AliTRDCalPIDNN.cxx:43
 AliTRDCalPIDNN.cxx:44
 AliTRDCalPIDNN.cxx:45
 AliTRDCalPIDNN.cxx:46
 AliTRDCalPIDNN.cxx:47
 AliTRDCalPIDNN.cxx:48
 AliTRDCalPIDNN.cxx:49
 AliTRDCalPIDNN.cxx:50
 AliTRDCalPIDNN.cxx:51
 AliTRDCalPIDNN.cxx:52
 AliTRDCalPIDNN.cxx:53
 AliTRDCalPIDNN.cxx:54
 AliTRDCalPIDNN.cxx:55
 AliTRDCalPIDNN.cxx:56
 AliTRDCalPIDNN.cxx:57
 AliTRDCalPIDNN.cxx:58
 AliTRDCalPIDNN.cxx:59
 AliTRDCalPIDNN.cxx:60
 AliTRDCalPIDNN.cxx:61
 AliTRDCalPIDNN.cxx:62
 AliTRDCalPIDNN.cxx:63
 AliTRDCalPIDNN.cxx:64
 AliTRDCalPIDNN.cxx:65
 AliTRDCalPIDNN.cxx:66
 AliTRDCalPIDNN.cxx:67
 AliTRDCalPIDNN.cxx:68
 AliTRDCalPIDNN.cxx:69
 AliTRDCalPIDNN.cxx:70
 AliTRDCalPIDNN.cxx:71
 AliTRDCalPIDNN.cxx:72
 AliTRDCalPIDNN.cxx:73
 AliTRDCalPIDNN.cxx:74
 AliTRDCalPIDNN.cxx:75
 AliTRDCalPIDNN.cxx:76
 AliTRDCalPIDNN.cxx:77
 AliTRDCalPIDNN.cxx:78
 AliTRDCalPIDNN.cxx:79
 AliTRDCalPIDNN.cxx:80
 AliTRDCalPIDNN.cxx:81
 AliTRDCalPIDNN.cxx:82
 AliTRDCalPIDNN.cxx:83
 AliTRDCalPIDNN.cxx:84
 AliTRDCalPIDNN.cxx:85
 AliTRDCalPIDNN.cxx:86
 AliTRDCalPIDNN.cxx:87
 AliTRDCalPIDNN.cxx:88
 AliTRDCalPIDNN.cxx:89
 AliTRDCalPIDNN.cxx:90
 AliTRDCalPIDNN.cxx:91
 AliTRDCalPIDNN.cxx:92
 AliTRDCalPIDNN.cxx:93
 AliTRDCalPIDNN.cxx:94
 AliTRDCalPIDNN.cxx:95
 AliTRDCalPIDNN.cxx:96
 AliTRDCalPIDNN.cxx:97
 AliTRDCalPIDNN.cxx:98
 AliTRDCalPIDNN.cxx:99
 AliTRDCalPIDNN.cxx:100
 AliTRDCalPIDNN.cxx:101
 AliTRDCalPIDNN.cxx:102
 AliTRDCalPIDNN.cxx:103
 AliTRDCalPIDNN.cxx:104
 AliTRDCalPIDNN.cxx:105
 AliTRDCalPIDNN.cxx:106
 AliTRDCalPIDNN.cxx:107
 AliTRDCalPIDNN.cxx:108
 AliTRDCalPIDNN.cxx:109
 AliTRDCalPIDNN.cxx:110
 AliTRDCalPIDNN.cxx:111
 AliTRDCalPIDNN.cxx:112
 AliTRDCalPIDNN.cxx:113
 AliTRDCalPIDNN.cxx:114
 AliTRDCalPIDNN.cxx:115
 AliTRDCalPIDNN.cxx:116
 AliTRDCalPIDNN.cxx:117
 AliTRDCalPIDNN.cxx:118
 AliTRDCalPIDNN.cxx:119
 AliTRDCalPIDNN.cxx:120
 AliTRDCalPIDNN.cxx:121
 AliTRDCalPIDNN.cxx:122
 AliTRDCalPIDNN.cxx:123
 AliTRDCalPIDNN.cxx:124
 AliTRDCalPIDNN.cxx:125
 AliTRDCalPIDNN.cxx:126
 AliTRDCalPIDNN.cxx:127
 AliTRDCalPIDNN.cxx:128
 AliTRDCalPIDNN.cxx:129
 AliTRDCalPIDNN.cxx:130
 AliTRDCalPIDNN.cxx:131
 AliTRDCalPIDNN.cxx:132
 AliTRDCalPIDNN.cxx:133
 AliTRDCalPIDNN.cxx:134
 AliTRDCalPIDNN.cxx:135
 AliTRDCalPIDNN.cxx:136
 AliTRDCalPIDNN.cxx:137
 AliTRDCalPIDNN.cxx:138
 AliTRDCalPIDNN.cxx:139
 AliTRDCalPIDNN.cxx:140
 AliTRDCalPIDNN.cxx:141
 AliTRDCalPIDNN.cxx:142
 AliTRDCalPIDNN.cxx:143
 AliTRDCalPIDNN.cxx:144
 AliTRDCalPIDNN.cxx:145
 AliTRDCalPIDNN.cxx:146
 AliTRDCalPIDNN.cxx:147
 AliTRDCalPIDNN.cxx:148
 AliTRDCalPIDNN.cxx:149
 AliTRDCalPIDNN.cxx:150
 AliTRDCalPIDNN.cxx:151
 AliTRDCalPIDNN.cxx:152
 AliTRDCalPIDNN.cxx:153
 AliTRDCalPIDNN.cxx:154
 AliTRDCalPIDNN.cxx:155
 AliTRDCalPIDNN.cxx:156
 AliTRDCalPIDNN.cxx:157
 AliTRDCalPIDNN.cxx:158
 AliTRDCalPIDNN.cxx:159
 AliTRDCalPIDNN.cxx:160
 AliTRDCalPIDNN.cxx:161
 AliTRDCalPIDNN.cxx:162
 AliTRDCalPIDNN.cxx:163
 AliTRDCalPIDNN.cxx:164
 AliTRDCalPIDNN.cxx:165
 AliTRDCalPIDNN.cxx:166
 AliTRDCalPIDNN.cxx:167
 AliTRDCalPIDNN.cxx:168
 AliTRDCalPIDNN.cxx:169
 AliTRDCalPIDNN.cxx:170
 AliTRDCalPIDNN.cxx:171
 AliTRDCalPIDNN.cxx:172
 AliTRDCalPIDNN.cxx:173
 AliTRDCalPIDNN.cxx:174
 AliTRDCalPIDNN.cxx:175
 AliTRDCalPIDNN.cxx:176
 AliTRDCalPIDNN.cxx:177
 AliTRDCalPIDNN.cxx:178
 AliTRDCalPIDNN.cxx:179
 AliTRDCalPIDNN.cxx:180
 AliTRDCalPIDNN.cxx:181
 AliTRDCalPIDNN.cxx:182
 AliTRDCalPIDNN.cxx:183
 AliTRDCalPIDNN.cxx:184
 AliTRDCalPIDNN.cxx:185
 AliTRDCalPIDNN.cxx:186
 AliTRDCalPIDNN.cxx:187
 AliTRDCalPIDNN.cxx:188
 AliTRDCalPIDNN.cxx:189
 AliTRDCalPIDNN.cxx:190
 AliTRDCalPIDNN.cxx:191
 AliTRDCalPIDNN.cxx:192
 AliTRDCalPIDNN.cxx:193
 AliTRDCalPIDNN.cxx:194
 AliTRDCalPIDNN.cxx:195
 AliTRDCalPIDNN.cxx:196
 AliTRDCalPIDNN.cxx:197
 AliTRDCalPIDNN.cxx:198
 AliTRDCalPIDNN.cxx:199
 AliTRDCalPIDNN.cxx:200
 AliTRDCalPIDNN.cxx:201
 AliTRDCalPIDNN.cxx:202
 AliTRDCalPIDNN.cxx:203
 AliTRDCalPIDNN.cxx:204
 AliTRDCalPIDNN.cxx:205
 AliTRDCalPIDNN.cxx:206
 AliTRDCalPIDNN.cxx:207
 AliTRDCalPIDNN.cxx:208
 AliTRDCalPIDNN.cxx:209
 AliTRDCalPIDNN.cxx:210
 AliTRDCalPIDNN.cxx:211
 AliTRDCalPIDNN.cxx:212
 AliTRDCalPIDNN.cxx:213