]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TRD/AliTRDpid.cxx
Transition to NewIO
[u/mrichter/AliRoot.git] / TRD / AliTRDpid.cxx
CommitLineData
a2b90f83 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial 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 **************************************************************************/
15
88cb7938 16/* $Id$ */
a2b90f83 17
18///////////////////////////////////////////////////////////////////////////////
19// //
16bf9884 20// The TRD particle identification base class //
a2b90f83 21// //
22// Its main purposes are: //
16bf9884 23// - Provide I/O framework for all neccessary files //
a2b90f83 24// - Assignment of a e/pi propability to a given track //
25// //
26///////////////////////////////////////////////////////////////////////////////
27
28#include <stdlib.h>
29#include <math.h>
30
31#include <TROOT.h>
32#include <TH1.h>
33#include <TObjArray.h>
34#include <TTree.h>
35#include <TFile.h>
36#include <TParticle.h>
37
38#include "AliRun.h"
39#include "AliTRD.h"
40#include "AliTRDpid.h"
41#include "AliTRDcluster.h"
42#include "AliTRDtrack.h"
43#include "AliTRDtracker.h"
44#include "AliTRDgeometry.h"
45
46ClassImp(AliTRDpid)
47
48//_____________________________________________________________________________
49AliTRDpid::AliTRDpid():TNamed()
50{
51 //
52 // AliTRDpid default constructor
53 //
54
16bf9884 55 fTrackArray = NULL;
56 fClusterArray = NULL;
57 fGeometry = NULL;
58 fFileKine = NULL;
a2b90f83 59
16bf9884 60 fPIDratioMin = 0.0;
61 fPIDpurePoints = kFALSE;
62 fPIDindexMin = 0;
63 fPIDindexMax = 0;
64
abaf1f1d 65 fEvent = 0;
66
16bf9884 67 fThreePadOnly = kFALSE;
a2b90f83 68
69}
70
71//_____________________________________________________________________________
72AliTRDpid::AliTRDpid(const char* name, const char* title):TNamed(name,title)
73{
74 //
75 // AliTRDpid constructor
76 //
77
a2b90f83 78 fTrackArray = NULL;
79 fClusterArray = NULL;
80 fGeometry = NULL;
16bf9884 81 fFileKine = NULL;
a2b90f83 82
abaf1f1d 83 fEvent = 0;
84
a2b90f83 85 Init();
86
87}
88
89//_____________________________________________________________________________
90AliTRDpid::AliTRDpid(const AliTRDpid &p)
91{
92 //
93 // AliTRDpid copy constructor
94 //
95
96 ((AliTRDpid &) p).Copy(*this);
97
98}
99
100//_____________________________________________________________________________
101AliTRDpid::~AliTRDpid()
102{
103 //
104 // AliTRDpid destructor
105 //
106
107 if (fClusterArray) {
108 fClusterArray->Delete();
109 delete fClusterArray;
abaf1f1d 110 fClusterArray = NULL;
a2b90f83 111 }
112
113 if (fTrackArray) {
114 fTrackArray->Delete();
115 delete fTrackArray;
abaf1f1d 116 fTrackArray = NULL;
a2b90f83 117 }
118
a1c3aded 119 if (fFileKine) {
120 delete fFileKine;
121 fFileKine = NULL;
122 }
a2b90f83 123
124}
125
126//_____________________________________________________________________________
127AliTRDpid &AliTRDpid::operator=(const AliTRDpid &p)
128{
129 //
130 // Assignment operator
131 //
132
133 if (this != &p) ((AliTRDpid &) p).Copy(*this);
134 return *this;
135
136}
137
138//_____________________________________________________________________________
139void AliTRDpid::Copy(TObject &p)
140{
141 //
142 // Copy function
143 //
144
16bf9884 145 ((AliTRDpid &) p).fTrackArray = NULL;
146 ((AliTRDpid &) p).fClusterArray = NULL;
147 ((AliTRDpid &) p).fGeometry = NULL;
148 ((AliTRDpid &) p).fFileKine = NULL;
149 ((AliTRDpid &) p).fPIDratioMin = fPIDratioMin;
150 ((AliTRDpid &) p).fPIDpurePoints = fPIDpurePoints;
151 ((AliTRDpid &) p).fPIDindexMin = fPIDindexMin;
152 ((AliTRDpid &) p).fPIDindexMax = fPIDindexMax;
153 ((AliTRDpid &) p).fThreePadOnly = fThreePadOnly;
abaf1f1d 154 ((AliTRDpid &) p).fEvent = fEvent;
a2b90f83 155
156}
157
158//_____________________________________________________________________________
159Bool_t AliTRDpid::Init()
160{
161 //
162 // Initializes the PID object
163 //
164
16bf9884 165 fClusterArray = new TObjArray();
166 fTrackArray = new TObjArray();
167
168 fPIDratioMin = 0.7;
169 fPIDpurePoints = kTRUE;
170 fPIDindexMin = -1;
171 fPIDindexMax = -1;
172
173 fThreePadOnly = kFALSE;
a2b90f83 174
175 return kTRUE;
176
177}
178
179//_____________________________________________________________________________
16bf9884 180Bool_t AliTRDpid::AssignLikelihood()
a2b90f83 181{
182 //
16bf9884 183 // Assigns the e / pi likelihood to all tracks.
a2b90f83 184 //
185
16bf9884 186 return AssignLikelihood(fTrackArray);
a2b90f83 187
188}
189
190//_____________________________________________________________________________
16bf9884 191Bool_t AliTRDpid::AssignLikelihood(TObjArray *tarray)
a2b90f83 192{
193 //
16bf9884 194 // Assigns the e / pi likelihood to all tracks in the array <tarray>.
a2b90f83 195 //
196
16bf9884 197 Bool_t status = kTRUE;
a2b90f83 198
16bf9884 199 AliTRDtrack *track;
a2b90f83 200
16bf9884 201 TIter nextTrack(tarray);
202 while ((track = (AliTRDtrack *) nextTrack())) {
203 if (!AssignLikelihood(track)) {
204 status = kFALSE;
205 continue;
206 }
207 }
a2b90f83 208
16bf9884 209 return status;
a2b90f83 210
211}
212
213//_____________________________________________________________________________
16bf9884 214Bool_t AliTRDpid::FillSpectra()
a2b90f83 215{
216 //
16bf9884 217 // Fills the energy loss distributions with all tracks.
a2b90f83 218 //
219
16bf9884 220 return FillSpectra(fTrackArray);
a2b90f83 221
222}
16bf9884 223
a2b90f83 224//_____________________________________________________________________________
16bf9884 225Bool_t AliTRDpid::FillSpectra(TObjArray *tarray)
a2b90f83 226{
227 //
16bf9884 228 // Fills the energy loss distributions with all tracks in <tarray>.
a2b90f83 229 //
230
231 Bool_t status = kTRUE;
232
233 AliTRDtrack *track;
234
16bf9884 235 TIter nextTrack(tarray);
a2b90f83 236 while ((track = (AliTRDtrack *) nextTrack())) {
16bf9884 237 if (!FillSpectra(track)) {
a2b90f83 238 status = kFALSE;
16bf9884 239 continue;
a2b90f83 240 }
241 }
242
243 return kTRUE;
244
245}
246
247//_____________________________________________________________________________
248Bool_t AliTRDpid::Open(const Char_t *name, Int_t event)
249{
250 //
16bf9884 251 // Opens and reads the kine tree, the geometry, the cluster array
252 // and the track array from the file <name>.
a2b90f83 253 //
254
255 Bool_t status = kTRUE;
256
a2b90f83 257 status = ReadCluster(name);
258 status = ReadTracks(name);
16bf9884 259 status = ReadKine(name,event);
a2b90f83 260
261 return status;
262
263}
264
265//_____________________________________________________________________________
266Bool_t AliTRDpid::Open(const Char_t *namekine
267 , const Char_t *namecluster
268 , const Char_t *nametracks
269 , Int_t event)
270{
271 //
272 // Opens and reads the kine tree and the geometry from file <namekine>,
273 // the cluster array from file <namecluster>,
16bf9884 274 // and the track array from the file <nametracks>.
a2b90f83 275 //
276
277 Bool_t status = kTRUE;
278
a2b90f83 279 status = ReadCluster(namecluster);
280 status = ReadTracks(nametracks);
16bf9884 281 status = ReadKine(namekine,event);
a2b90f83 282
283 return status;
284
285}
286
287//_____________________________________________________________________________
288Bool_t AliTRDpid::ReadKine(const Char_t *name, Int_t event)
289{
290 //
291 // Opens and reads the kine tree and the geometry from the file <name>.
292 //
293
16bf9884 294 TFile *fFileKine = (TFile *) gROOT->GetListOfFiles()->FindObject(name);
295 if (!fFileKine) {
a2b90f83 296 printf("AliTRDpid::ReadKine -- ");
297 printf("Open file %s\n",name);
16bf9884 298 fFileKine = new TFile(name);
299 if (!fFileKine) {
a2b90f83 300 printf("AliTRDpid::ReadKine -- ");
301 printf("Cannot open file %s\n",name);
302 return kFALSE;
303 }
304 }
305
16bf9884 306 gAlice = (AliRun *) fFileKine->Get("gAlice");
a2b90f83 307 if (!gAlice) {
308 printf("AliTRDpid::ReadKine -- ");
309 printf("No AliRun object found\n");
310 return kFALSE;
311 }
312 gAlice->GetEvent(event);
313
314 AliTRD *trd = (AliTRD *) gAlice->GetDetector("TRD");
315 if (!trd) {
316 printf("AliTRDpid::ReadKine -- ");
317 printf("No TRD object found\n");
318 return kFALSE;
319 }
320
321 fGeometry = trd->GetGeometry();
322 if (!fGeometry) {
323 printf("AliTRDpid::ReadKine -- ");
324 printf("No TRD geometry found\n");
325 return kFALSE;
326 }
327
abaf1f1d 328 fEvent = event;
329
a2b90f83 330 return kTRUE;
331
332}
333
334//_____________________________________________________________________________
335Bool_t AliTRDpid::ReadCluster(const Char_t *name)
336{
337 //
338 // Opens and reads the cluster array from the file <name>.
339 //
16bf9884 340
341 TDirectory *savedir = gDirectory;
a2b90f83 342
16bf9884 343 if (fClusterArray) {
344 fClusterArray->Delete();
345 }
346 else {
347 fClusterArray = new TObjArray();
348 }
a2b90f83 349
350 printf("AliTRDpid::ReadCluster -- ");
351 printf("Open file %s\n",name);
352
5443e65e 353 AliTRDtracker *tracker = new AliTRDtracker();
a2b90f83 354 tracker->ReadClusters(fClusterArray,name);
355
356 if (!fClusterArray) {
357 printf("AliTRDpid::ReadCluster -- ");
358 printf("Error reading the cluster array from file %s\n",name);
359 return kFALSE;
360 }
361
362 delete tracker;
363
16bf9884 364 savedir->cd();
365
a2b90f83 366 return kTRUE;
367
368}
369
370//_____________________________________________________________________________
371Bool_t AliTRDpid::ReadTracks(const Char_t *name)
372{
373 //
374 // Opens and reads the track array from the file <name>.
375 //
16bf9884 376
377 TDirectory *savedir = gDirectory;
a2b90f83 378
16bf9884 379 if (fTrackArray) {
380 fTrackArray->Delete();
381 }
382 else {
383 fTrackArray = new TObjArray();
384 }
a2b90f83 385
386 TFile *file = (TFile *) gROOT->GetListOfFiles()->FindObject(name);
387 if (!file) {
388 printf("AliTRDpid::ReadTracks -- ");
389 printf("Open file %s\n",name);
390 file = new TFile(name);
391 if (!file) {
392 printf("AliTRDpid::ReadTracks -- ");
393 printf("Cannot open file %s\n",name);
394 return kFALSE;
395 }
396 }
397
abaf1f1d 398 Char_t treeName[12];
399 sprintf(treeName,"TreeT%d_TRD",fEvent);
400 TTree *trackTree = (TTree *) file->Get(treeName);
a2b90f83 401 TBranch *trackBranch = trackTree->GetBranch("tracks");
402
403 Int_t nEntry = ((Int_t) trackTree->GetEntries());
404 for (Int_t iEntry = 0; iEntry < nEntry; iEntry++) {
405 AliTRDtrack *track = new AliTRDtrack();
406 trackBranch->SetAddress(&track);
407 trackTree->GetEvent(iEntry);
408 fTrackArray->AddLast(track);
409 }
410
411 file->Close();
412
16bf9884 413 savedir->cd();
a2b90f83 414
16bf9884 415 return kTRUE;
a2b90f83 416
417}
418
419//_____________________________________________________________________________
16bf9884 420Int_t AliTRDpid::MCpid(const AliTRDtrack *t)
a2b90f83 421{
422 //
16bf9884 423 // Determines the pid of the MC track <t>.
a2b90f83 424 //
425
426 // PDG codes
427 const Int_t kPdgEl = 11;
428 const Int_t kPdgPi = 211;
429
a2b90f83 430 AliTRDcluster *cluster;
431 TParticle *particle;
432
16bf9884 433 Int_t nClusterEl = 0;
434 Int_t nClusterPi = 0;
435 Int_t nClusterPure = 0;
436 Int_t nClusterMix = 0;
a2b90f83 437
16bf9884 438 Float_t ratioEl = 0;
439 Float_t ratioPi = 0;
a2b90f83 440
16bf9884 441 Int_t ipid = -1;
a2b90f83 442
443 if (!fClusterArray) {
16bf9884 444 printf("AliTRDpid::MCpid -- ");
a2b90f83 445 printf("ClusterArray not defined. Initialize the PID object first\n");
446 return -1;
447 }
448
449 // Loop through all clusters associated to this track
5443e65e 450 Int_t nCluster = t->GetNumberOfClusters();
a2b90f83 451 for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) {
452
453 // Get a cluster
454 Int_t index = t->GetClusterIndex(iCluster);
455 if (!(cluster = (AliTRDcluster *) fClusterArray->UncheckedAt(index))) {
456 break;
457 }
458
459 // Get the first two MC track indices
5443e65e 460 Int_t track0 = cluster->GetLabel(0);
461 Int_t track1 = cluster->GetLabel(1);
16bf9884 462
463 // Check on the track index to find the right primaries
464 if ((track0 > fPIDindexMin) &&
465 (track0 <= fPIDindexMax)) {
466
467 // Check whether it is a pure cluster, i.e. not overlapping
468 Bool_t accept = kTRUE;
469 if ((fPIDpurePoints) && (track1 != -1)) {
470 accept = kFALSE;
471 }
472 if (accept) {
473
474 particle = gAlice->Particle(track0);
475 if (particle->GetFirstMother() == -1) {
476 switch (TMath::Abs(particle->GetPdgCode())) {
477 case kPdgEl:
478 nClusterEl++;
479 break;
480 case kPdgPi:
481 nClusterPi++;
482 break;
483 };
484 nClusterPure++;
485 }
486
487 }
488 else {
489
490 nClusterMix++;
491
492 }
a2b90f83 493
494 }
495
496 }
497
498 if (nCluster) {
499 ratioEl = ((Float_t) nClusterEl) / ((Float_t) nCluster);
500 ratioPi = ((Float_t) nClusterPi) / ((Float_t) nCluster);
16bf9884 501 if (ratioEl > fPIDratioMin) {
a2b90f83 502 ipid = kElectron;
503 }
16bf9884 504 else if (ratioPi > fPIDratioMin) {
a2b90f83 505 ipid = kPion;
506 }
507 }
16bf9884 508// printf("AliTRDpid::MCpid -- ");
509// printf("nCluster = %d, nClusterEl = %d, nClusterPi = %d\n"
510// ,nCluster,nClusterEl,nClusterPi);
511// printf("AliTRDpid::MCpid -- ");
512// printf("nClusterPure = %d, nClusterMix = %d\n"
513// ,nClusterPure,nClusterMix);
514// printf("AliTRDpid::MCpid -- ");
515// printf("ratioEl = %f, ratioPi = %f, ipid = %d\n",ratioEl,ratioPi,ipid);
a2b90f83 516
517 return ipid;
518
519}
520
521//_____________________________________________________________________________
16bf9884 522Int_t AliTRDpid::MCpid(const AliTRDtrack *t, Int_t *pdg
523 , Int_t *nFound
524 , Int_t *indices)
525{
526 //
527 // Determines the pid of the MC track <t>.
528 // Returns the number of different MC particles that contribute to this
529 // track. <pdg> contains their PDG code, ordered by their frequency.
530 // <nFound> contains the corresponding number of cluster that contain
531 // contributions to this cluster.
532 //
533
534 const Int_t kNtrack = 3;
535 const Int_t kNpart = 200;
536
537 AliTRDcluster *cluster;
538 TParticle *particle;
539
540 Int_t nPart = 0;
541 Int_t iPart = 0;
542 Int_t iTrack = 0;
543 Int_t iCluster = 0;
544
545 if (!fClusterArray) {
546 printf("AliTRDpid::MCpid -- ");
547 printf("ClusterArray not defined. Initialize the PID object first\n");
548 return -1;
549 }
550
551 Int_t sort[kNpart];
552 Int_t pdgTmp[kNpart];
553 Int_t nFoundTmp[kNpart];
554 Int_t indicesTmp[kNpart];
555 for (iPart = 0; iPart < kNpart; iPart++) {
556 pdg[iPart] = 0;
557 nFound[iPart] = 0;
558 indices[iPart] = 0;
559 pdgTmp[iPart] = 0;
560 nFoundTmp[iPart] = 0;
561 indicesTmp[iPart] = 0;
562 sort[iPart] = 0;
563 }
564
565 // Loop through all clusters associated to this track
5443e65e 566 Int_t nCluster = t->GetNumberOfClusters();
16bf9884 567 for (iCluster = 0; iCluster < nCluster; iCluster++) {
568
569 // Get a cluster
570 Int_t index = t->GetClusterIndex(iCluster);
571 if (!(cluster = (AliTRDcluster *) fClusterArray->UncheckedAt(index))) {
572 break;
573 }
574
575 // Get the MC track indices
576 for (iTrack = 0; iTrack < kNtrack; iTrack++) {
577
5443e65e 578 Int_t trackIndex = cluster->GetLabel(iTrack);
16bf9884 579 if (trackIndex >= 0) {
580 particle = gAlice->Particle(trackIndex);
581 Int_t pdgCode = particle->GetPdgCode();
582 Bool_t newPart = kTRUE;
583 for (iPart = 0; iPart < nPart; iPart++) {
584 if (trackIndex == indicesTmp[iPart]) {
585 nFoundTmp[iPart]++;
586 newPart = kFALSE;
587 break;
588 }
589 }
590 if ((newPart) && (nPart < kNpart)) {
591 indicesTmp[nPart] = trackIndex;
592 pdgTmp[nPart] = pdgCode;
593 nFoundTmp[nPart]++;
594 nPart++;
595 }
596 }
597
598 }
599
600 }
601
602 // Sort the arrays by the frequency of the appearance of a MC track
603 TMath::Sort(nPart,nFoundTmp,sort,kTRUE);
604 for (iPart = 0; iPart < nPart; iPart++) {
605 pdg[iPart] = pdgTmp[sort[iPart]];
606 nFound[iPart] = nFoundTmp[sort[iPart]];
607 indices[iPart] = indicesTmp[sort[iPart]];
608// printf(" iPart = %d, pdg = %d, nFound = %d, indices = %d\n"
609// ,iPart,pdg[iPart],nFound[iPart],indices[iPart]);
610 }
611
612 return nPart;
613
614}
615
616//_____________________________________________________________________________
617Bool_t AliTRDpid::SumCharge(const AliTRDtrack *t
618 , Float_t *charge
619 , Int_t *nCluster)
a2b90f83 620{
621 //
622 // Sums up the charge in each plane for track <t>.
623 //
624
625 Bool_t status = kTRUE;
626
627 AliTRDcluster *cluster;
628
629 const Int_t kNplane = AliTRDgeometry::Nplan();
630 for (Int_t iPlane = 0; iPlane < kNplane; iPlane++) {
16bf9884 631 charge[iPlane] = 0.0;
632 nCluster[iPlane] = 0;
a2b90f83 633 }
634
635 if (!fClusterArray) {
636 printf("AliTRDpid::SumCharge -- ");
637 printf("ClusterArray not defined. Initialize the PID object first\n");
638 return kFALSE;
639 }
640 if (!fGeometry) {
641 printf("AliTRDpid::SumCharge -- ");
642 printf("TRD geometry not defined. Initialize the PID object first\n");
643 return kFALSE;
644 }
645
646 // Loop through all clusters associated to this track
5443e65e 647 Int_t nClus = t->GetNumberOfClusters();
16bf9884 648 for (Int_t iClus = 0; iClus < nClus; iClus++) {
a2b90f83 649
650 // Get a cluster
16bf9884 651 Int_t index = t->GetClusterIndex(iClus);
a2b90f83 652 if (!(cluster = (AliTRDcluster *) fClusterArray->UncheckedAt(index))) {
653 status = kFALSE;
654 break;
655 }
656
16bf9884 657 if (fThreePadOnly) {
658 if ((!cluster->From2pad()) &&
659 (!cluster->From3pad())) continue;
660 }
661
a2b90f83 662 // Sum up the charge
663 Int_t plane = fGeometry->GetPlane(cluster->GetDetector());
16bf9884 664 //charge[plane] += cluster->GetQ();
665 // Corrected charge
666 charge[plane] += t->GetClusterdQdl(iClus);
667 nCluster[plane]++;
a2b90f83 668
669 }
16bf9884 670// printf("AliTRDpid::SumCharge -- ");
671// printf("charge = %d, %d, %d, %d, %d, %d\n"
672// ,(Int_t) charge[0]
673// ,(Int_t) charge[1]
674// ,(Int_t) charge[2]
675// ,(Int_t) charge[3]
676// ,(Int_t) charge[4]
677// ,(Int_t) charge[5]);
678// printf("AliTRDpid::SumCharge -- ");
679// printf("nCluster = %d, %d, %d, %d, %d, %d\n"
680// ,nCluster[0]
681// ,nCluster[1]
682// ,nCluster[2]
683// ,nCluster[3]
684// ,nCluster[4]
685// ,nCluster[5]);
a2b90f83 686
687 return status;
688
689}