]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDtrigger.cxx
Add Raphaelles calibration class
[u/mrichter/AliRoot.git] / TRD / AliTRDtrigger.cxx
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
16 ///////////////////////////////////////////////////////////////////////////////
17 //                                                                           //
18 //  TRD trigger class                                                        //
19 //                                                                           //
20 //  Author:                                                                  //
21 //    Bogdan Vulpescu                                                        //
22 //                                                                           //
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #include <TTree.h>
26 #include <TBranch.h>
27 #include <TMatrixD.h>
28 #include <TClonesArray.h>
29 #include <TObjArray.h>
30
31 #include "AliLog.h"
32 #include "AliRun.h"
33 #include "AliLoader.h"
34
35 #include "AliTRDdigitsManager.h"
36 #include "AliTRDgeometry.h"
37 #include "AliTRDdataArrayI.h"
38 #include "AliTRDcalibDB.h"
39 #include "AliTRDCommonParam.h"
40 #include "AliTRDrawData.h"
41 #include "AliTRDtrigger.h"
42 #include "AliTRDmodule.h"
43 #include "AliTRDmcmTracklet.h"
44 #include "AliTRDgtuTrack.h"
45 #include "AliTRDtrigParam.h"
46 #include "AliTRDmcm.h"
47 #include "AliTRDzmaps.h"
48 #include "AliTRDCalibra.h"
49 #include "Cal/AliTRDCalPIDLQ.h"
50
51 ClassImp(AliTRDtrigger)
52
53 //_____________________________________________________________________________
54 AliTRDtrigger::AliTRDtrigger()
55   :TNamed()
56   ,fField(0)
57   ,fGeo(NULL)
58   ,fCalib(NULL)
59   ,fCParam(NULL)
60   ,fTrigParam(NULL)
61   ,fRunLoader(NULL)
62   ,fDigitsManager(NULL)
63   ,fTrackletTree(NULL)
64   ,fTracklets(NULL)
65   ,fNROB(0)
66   ,fMCM(NULL)
67   ,fTrk(NULL)
68   ,fTrkTest(NULL)
69   ,fModule(NULL)
70   ,fGTUtrk(NULL)
71   ,fNtracklets(0)
72   ,fDigits(NULL)
73   ,fTrack0(NULL)
74   ,fTrack1(NULL)
75   ,fTrack2(NULL)
76   ,fNPrimary(0)
77   ,fTracks(NULL)
78 {
79   //
80   // AliTRDtrigger default constructor
81   //
82
83 }
84
85 //_____________________________________________________________________________
86 AliTRDtrigger::AliTRDtrigger(const Text_t *name, const Text_t *title)
87   :TNamed(name,title)
88   ,fField(0)
89   ,fGeo(NULL)
90   ,fCalib(NULL)
91   ,fCParam(NULL)
92   ,fTrigParam(NULL)
93   ,fRunLoader(NULL)
94   ,fDigitsManager(new AliTRDdigitsManager())
95   ,fTrackletTree(NULL)
96   ,fTracklets(new TObjArray(400))
97   ,fNROB(0)
98   ,fMCM(NULL)
99   ,fTrk(NULL)
100   ,fTrkTest(NULL)
101   ,fModule(NULL)
102   ,fGTUtrk(NULL)
103   ,fNtracklets(0)
104   ,fDigits(NULL)
105   ,fTrack0(NULL)
106   ,fTrack1(NULL)
107   ,fTrack2(NULL)
108   ,fNPrimary(0)
109   ,fTracks(new TClonesArray("AliTRDgtuTrack",1000))
110 {
111   //
112   // AliTRDtrigger constructor
113   //
114
115 }
116
117 //_____________________________________________________________________________
118 AliTRDtrigger::AliTRDtrigger(const AliTRDtrigger &p)
119   :TNamed(p)
120   ,fField(p.fField)
121   ,fGeo(NULL)
122   ,fCalib(NULL)
123   ,fCParam(NULL)
124   ,fTrigParam(NULL)
125   ,fRunLoader(NULL)
126   ,fDigitsManager(NULL)
127   ,fTrackletTree(NULL)
128   ,fTracklets(NULL)
129   ,fNROB(p.fNROB)
130   ,fMCM(NULL)
131   ,fTrk(NULL)
132   ,fTrkTest(NULL)
133   ,fModule(NULL)
134   ,fGTUtrk(NULL)
135   ,fNtracklets(p.fNtracklets)
136   ,fDigits(NULL)
137   ,fTrack0(NULL)
138   ,fTrack1(NULL)
139   ,fTrack2(NULL)
140   ,fNPrimary(p.fNPrimary)
141   ,fTracks(NULL)
142 {
143   //
144   // AliTRDtrigger copy constructor
145   //
146
147 }
148
149 ///_____________________________________________________________________________
150 AliTRDtrigger::~AliTRDtrigger()
151 {
152   //
153   // AliTRDtrigger destructor
154   //
155
156   if (fTracklets) {
157     fTracklets->Delete();
158     delete fTracklets;
159   }
160
161   if (fTracks) {
162     fTracks->Delete();
163     delete fTracks;
164   }
165
166 }
167
168 //_____________________________________________________________________________
169 AliTRDtrigger &AliTRDtrigger::operator=(const AliTRDtrigger &p)
170 {
171   //
172   // Assignment operator
173   //
174
175   if (this != &p) ((AliTRDtrigger &) p).Copy(*this);
176   return *this;
177
178 }
179
180 //_____________________________________________________________________________
181 void AliTRDtrigger::Copy(TObject &) const
182 {
183   //
184   // Copy function
185   //
186
187   AliFatal("Not implemented");
188
189 }
190
191 //_____________________________________________________________________________
192 void AliTRDtrigger::Init()
193 {
194
195   fModule = new AliTRDmodule(fTrigParam); 
196   fTracks->Clear();
197
198   fField  = fTrigParam->GetField();
199   fGeo    = (AliTRDgeometry*)AliTRDgeometry::GetGeometry(fRunLoader);
200
201   fCalib  = AliTRDcalibDB::Instance();
202   if (!fCalib) {
203     AliError("No instance of AliTRDcalibDB.");
204     return;  
205   }
206
207   fCParam = AliTRDCommonParam::Instance();
208   if (!fCParam) {
209     AliError("No common parameters.");
210     return;
211   }
212
213 }
214
215 //_____________________________________________________________________________
216 Bool_t AliTRDtrigger::Open(const Char_t *name, Int_t nEvent)
217 {
218   //
219   // Opens the AliROOT file.
220   //
221
222   TString evfoldname = AliConfig::GetDefaultEventFolderName();
223   fRunLoader         = AliRunLoader::GetRunLoader(evfoldname);
224
225   if (!fRunLoader) {
226     fRunLoader = AliRunLoader::Open(name);
227   }
228   if (!fRunLoader) {
229     AliError(Form("Can not open session for file %s.",name));
230     return kFALSE;
231   }
232
233   // Open input
234   if (fRunLoader->GetAliRun() == 0x0) {
235     fRunLoader->LoadgAlice();
236   }
237   gAlice = fRunLoader->GetAliRun();
238   if (!(gAlice)) {
239     fRunLoader->LoadgAlice();
240     gAlice = fRunLoader->GetAliRun();
241     if (!(gAlice)) {
242       AliError("Could not find AliRun object.");
243       return kFALSE;
244     }
245   }
246
247   // Import the Trees for the event nEvent in the file
248   fRunLoader->GetEvent(nEvent);
249
250   // Open output
251   TObjArray *ioArray = 0;
252   AliLoader* loader  = fRunLoader->GetLoader("TRDLoader");
253   loader->MakeTree("T");
254   fTrackletTree = loader->TreeT();
255   fTrackletTree->Branch("TRDmcmTracklet","TObjArray",&ioArray,32000,0);
256   Init();
257
258   return kTRUE;
259
260 }
261
262 //_____________________________________________________________________________
263 Bool_t AliTRDtrigger::ReadDigits() 
264 {
265   //
266   // Reads the digits arrays from the input aliroot file
267   //
268
269   if (!fRunLoader) {
270     AliError("Can not find the Run Loader");
271     return kFALSE;
272   }
273
274   AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
275   if (!loader->TreeD()) {
276     loader->LoadDigits();
277   }
278   if (!loader->TreeD()) {
279      return kFALSE;
280   }
281
282   return (fDigitsManager->ReadDigits(loader->TreeD()));
283
284 }
285
286 //_____________________________________________________________________________
287 Bool_t AliTRDtrigger::ReadDigits(AliRawReader* rawReader)
288 {
289   //
290   // Reads the digits arrays from the ddl file
291   //
292
293   AliTRDrawData *raw = new AliTRDrawData();
294   fDigitsManager     = raw->Raw2Digits(rawReader);
295
296   return kTRUE;
297
298 }
299
300 //_____________________________________________________________________________
301 Bool_t AliTRDtrigger::ReadTracklets(AliRunLoader *rl) 
302 {
303   //
304   // Reads the tracklets find the tracks
305   //
306
307   Int_t idet;
308
309   AliLoader *loader = rl->GetLoader("TRDLoader");
310   loader->LoadTracks();
311   fTrackletTree     = loader->TreeT();
312
313   TBranch *branch   = fTrackletTree->GetBranch("TRDmcmTracklet");
314   if (!branch) {
315     AliError("Can't get the branch !");
316     return kFALSE;
317   }
318   TObjArray *tracklets = new TObjArray(400);
319   branch->SetAddress(&tracklets);
320
321   Int_t nEntries   = (Int_t) fTrackletTree->GetEntries();
322   Int_t iEntry;
323   Int_t itrk;
324   Int_t iStack;
325   Int_t iStackPrev = -1;
326   
327   for (iEntry = 0; iEntry < nEntries; iEntry++) {    
328
329     fTrackletTree->GetEvent(iEntry);
330     
331     for (itrk = 0; itrk < tracklets->GetEntriesFast(); itrk++) {
332
333       fTrk   = (AliTRDmcmTracklet*)tracklets->UncheckedAt(itrk);
334       idet   = fTrk->GetDetector();
335       iStack = idet / (AliTRDgeometry::Nplan());
336
337       if (iStackPrev != iStack) {
338         if (iStackPrev == -1) {
339           iStackPrev = iStack;
340         } 
341         else {
342           MakeTracks(idet - AliTRDgeometry::Nplan());
343           ResetTracklets();
344           iStackPrev = iStack;
345         }
346       }
347       
348       Tracklets()->Add(fTrk);
349
350       if ((iEntry == (nEntries-1)) && 
351           (itrk   == (tracklets->GetEntriesFast() - 1))) {
352         idet++;
353         MakeTracks(idet-AliTRDgeometry::Nplan());
354         ResetTracklets();
355       }
356
357     }
358
359   }
360
361   loader->UnloadTracks();
362
363   return kTRUE;
364
365 }
366
367 //_____________________________________________________________________________
368 Bool_t AliTRDtrigger::MakeTracklets(Bool_t makeTracks)
369 {
370   //
371   // Create tracklets from digits
372   //
373
374   Int_t chamBeg = 0;
375   Int_t chamEnd = AliTRDgeometry::Ncham();
376   Int_t planBeg = 0;
377   Int_t planEnd = AliTRDgeometry::Nplan();
378   Int_t sectBeg = 0;
379   Int_t sectEnd = AliTRDgeometry::Nsect();
380
381   fTrkTest = new AliTRDmcmTracklet(0,0,0);
382   fMCM     = new AliTRDmcm(fTrigParam,0);
383
384   Int_t   time;
385   Int_t   col;
386   Int_t   row;
387   Int_t   col1;
388   Int_t   col2;
389   Int_t   idet       = -1;
390   Int_t   iStack     = -1;
391   Int_t   iStackPrev = -1;
392   Float_t amp;
393
394   for (Int_t isect = sectBeg; isect < sectEnd; isect++) {
395
396     for (Int_t icham = chamBeg; icham < chamEnd; icham++) {
397
398       // Number of ROBs in the chamber
399       if(icham == 2) {
400         fNROB = 6;
401       } 
402       else {
403         fNROB = 8;
404       }
405
406       for (Int_t iplan = planBeg; iplan < planEnd; iplan++) {
407
408         idet = fGeo->GetDetector(iplan,icham,isect);
409         ResetTracklets();
410         
411         if (makeTracks) {
412           iStack = idet / (AliTRDgeometry::Nplan());
413           if (iStackPrev != iStack) {
414             if (iStackPrev == -1) {
415               iStackPrev = iStack;
416             } 
417             else {
418               MakeTracks(idet-AliTRDgeometry::Nplan());
419               ResetTracklets();
420               iStackPrev = iStack;
421             }
422           }
423         }
424
425         Int_t nRowMax    = fCParam->GetRowMax(iplan,icham,isect);
426         Int_t nColMax    = fCParam->GetColMax(iplan);
427         Int_t nTimeTotal = fCalib->GetNumberOfTimeBins();
428
429         // Get the digits
430         fDigits = fDigitsManager->GetDigits(idet);
431         if (!fDigits) return kFALSE;
432         // This is to take care of switched off super modules
433         if (fDigits->GetNtime() == 0) {
434           continue;
435         }
436         fDigits->Expand();
437         fTrack0 = fDigitsManager->GetDictionary(idet,0);
438         if (!fTrack0) return kFALSE;
439         fTrack0->Expand();
440         fTrack1 = fDigitsManager->GetDictionary(idet,1);
441         if (!fTrack1) return kFALSE;
442         fTrack1->Expand();
443         fTrack2 = fDigitsManager->GetDictionary(idet,2); 
444         if (!fTrack2) return kFALSE;
445         fTrack2->Expand();
446
447         for (Int_t iRob = 0; iRob < fNROB; iRob++) {
448
449           for (Int_t iMcm = 0; iMcm < kNMCM; iMcm++) {
450
451             fMCM->Reset();
452             fMCM->SetRobId(iRob);
453             fMCM->SetChaId(idet);
454
455             SetMCMcoordinates(iMcm);
456
457             row = fMCM->GetRow();
458
459             if ((row < 0) || (row >= nRowMax)) {
460               AliError("MCM row number out of range.");
461               continue;
462             }
463
464             fMCM->GetColRange(col1,col2);
465             
466             for (time = 0; time < nTimeTotal; time++) {
467               for (col = col1; col < col2; col++) {
468                 if ((col >= 0) && (col < nColMax)) {
469                   amp = TMath::Abs(fDigits->GetDataUnchecked(row,col,time));
470                 } 
471                 else {
472                   amp = 0.0;
473                 }
474                 fMCM->SetADC(col-col1,time,amp);
475               }
476             }
477
478             if (fTrigParam->GetTailCancelation()) {
479               fMCM->Filter(fTrigParam->GetNexponential(),fTrigParam->GetFilterType());
480             }
481             
482             if (fMCM->Run()) {
483
484               for (Int_t iSeed = 0; iSeed < kMaxTrackletsPerMCM; iSeed++) {
485                 
486                 if (fMCM->GetSeedCol()[iSeed] < 0) {
487                   continue;
488                 }
489
490                 if (fTrigParam->GetDebugLevel()   > 1) { 
491                   AliInfo(Form("Add tracklet %d in col %02d \n",fNtracklets,fMCM->GetSeedCol()[iSeed]));
492                 }
493
494                 if (TestTracklet(idet,row,iSeed,0)) {
495                   AddTracklet(idet,row,iSeed,fNtracklets++);
496                 }
497
498               }
499
500             }
501
502           }
503
504       
505         }
506
507         // Compress the arrays
508         fDigits->Compress(1,0);
509         fTrack0->Compress(1,0);
510         fTrack1->Compress(1,0);
511         fTrack2->Compress(1,0);
512
513         WriteTracklets(idet);
514
515      }
516     }
517   }
518
519   if (makeTracks) {
520     idet++;
521     MakeTracks(idet - AliTRDgeometry::Nplan());
522     ResetTracklets();
523   }
524
525   return kTRUE;
526
527 }
528
529 //_____________________________________________________________________________
530 void AliTRDtrigger::SetMCMcoordinates(Int_t imcm)
531 {
532   //
533   // Configure MCM position in the pad plane
534   //
535
536   Int_t robid = fMCM->GetRobId();
537
538   // setting the Row and Col range
539
540   const Int_t kNcolRob = 2;  // number of ROBs per chamber in column direction
541   const Int_t kNmcmRob = 4;  // number of MCMs per ROB in column/row direction
542
543   Int_t mcmid = imcm%(kNmcmRob*kNmcmRob);
544
545   if (robid%kNcolRob == 0) {
546
547     if (mcmid%kNmcmRob == 0) {
548       fMCM->SetColRange(18*0-1,18*1-1+2+1);
549     }
550     if (mcmid%kNmcmRob == 1) {
551       fMCM->SetColRange(18*1-1,18*2-1+2+1);
552     }
553     if (mcmid%kNmcmRob == 2) {
554       fMCM->SetColRange(18*2-1,18*3-1+2+1);
555     }
556     if (mcmid%kNmcmRob == 3) {
557       fMCM->SetColRange(18*3-1,18*4-1+2+1);
558     }
559
560   } 
561   else {
562
563     if (mcmid%kNmcmRob == 0) {
564       fMCM->SetColRange(18*4-1,18*5-1+2+1);
565     }
566     if (mcmid%kNmcmRob == 1) {
567       fMCM->SetColRange(18*5-1,18*6-1+2+1);
568     }
569     if (mcmid%kNmcmRob == 2) {
570       fMCM->SetColRange(18*6-1,18*7-1+2+1);
571     }
572     if (mcmid%kNmcmRob == 3) {
573       fMCM->SetColRange(18*7-1,18*8-1+2+1);
574     }
575
576   } 
577
578   fMCM->SetRow(kNmcmRob*(robid/kNcolRob)+mcmid/kNmcmRob);
579
580 }
581
582 //_____________________________________________________________________________
583 Bool_t AliTRDtrigger::TestTracklet(Int_t det, Int_t row, Int_t seed, Int_t n)
584 {
585   //
586   // Check first the tracklet pt
587   //
588
589   Int_t nTimeTotal  = fCalib->GetNumberOfTimeBins();
590
591   // Calibration fill 2D
592   AliTRDCalibra *calibra = AliTRDCalibra::Instance();
593   if (!calibra) {
594     AliInfo("Could not get Calibra instance\n");
595   }
596
597   fTrkTest->Reset();
598
599   fTrkTest->SetDetector(det);
600   fTrkTest->SetRow(row);
601   fTrkTest->SetN(n);
602
603   Int_t iCol, iCol1, iCol2, track[3];
604   iCol = fMCM->GetSeedCol()[seed];  // 0....20 (MCM)
605   fMCM->GetColRange(iCol1,iCol2);   // range in the pad plane
606             
607   Float_t amp[3];
608   for (Int_t iTime = 0; iTime < nTimeTotal; iTime++) {
609
610     amp[0] = fMCM->GetADC(iCol-1,iTime);
611     amp[1] = fMCM->GetADC(iCol  ,iTime);
612     amp[2] = fMCM->GetADC(iCol+1,iTime);
613
614     // extract track contribution only from the central pad
615     track[0] = fTrack0->GetDataUnchecked(row,iCol+iCol1,iTime);
616     track[1] = fTrack1->GetDataUnchecked(row,iCol+iCol1,iTime);
617     track[2] = fTrack2->GetDataUnchecked(row,iCol+iCol1,iTime);
618
619     if      (fMCM->IsCluster(iCol,iTime)) {
620
621       fTrkTest->AddCluster(iCol+iCol1,iTime,amp,track);
622
623     } 
624     else if ((iCol+1+1) < kMcmCol) {
625
626       amp[0] = fMCM->GetADC(iCol-1+1,iTime);
627       amp[1] = fMCM->GetADC(iCol  +1,iTime);
628       amp[2] = fMCM->GetADC(iCol+1+1,iTime);
629
630       if (fMCM->IsCluster(iCol+1,iTime)) {
631
632         // extract track contribution only from the central pad
633         track[0] = fTrack0->GetDataUnchecked(row,iCol+1+iCol1,iTime);
634         track[1] = fTrack1->GetDataUnchecked(row,iCol+1+iCol1,iTime);
635         track[2] = fTrack2->GetDataUnchecked(row,iCol+1+iCol1,iTime);
636
637         fTrkTest->AddCluster(iCol+1+iCol1,iTime,amp,track);
638
639       }
640
641     } 
642
643   }
644
645   fTrkTest->CookLabel(0.8);  
646   /*
647   if (fTrkTest->GetLabel() >= fNPrimary) {
648     Info("AddTracklet","Only primaries are stored!");
649     return;
650   }
651   */
652   // LTU Pt cut
653   fTrkTest->MakeTrackletGraph(fGeo,fField);
654
655   // TRD Online calibration
656   if (calibra->Getmcmtracking()) {
657     calibra->UpdateHistogramcm(fTrkTest);
658   }
659
660   fTrkTest->MakeClusAmpGraph();
661
662   if (TMath::Abs(fTrkTest->GetPt()) < fTrigParam->GetLtuPtCut()) {
663     return kFALSE;
664   }
665   
666   return kTRUE;  
667
668 }
669
670 //_____________________________________________________________________________
671 void AliTRDtrigger::AddTracklet(Int_t det, Int_t row, Int_t seed, Int_t n)
672 {
673   //
674   // Add a found tracklet
675   //
676
677   Int_t nTimeTotal  = fCalib->GetNumberOfTimeBins();
678
679   fTrk = new AliTRDmcmTracklet(det,row,n);
680
681   Int_t iCol, iCol1, iCol2, track[3];
682   iCol = fMCM->GetSeedCol()[seed];  // 0....20 (MCM)
683   fMCM->GetColRange(iCol1,iCol2);   // range in the pad plane
684             
685   Float_t amp[3];
686   for (Int_t iTime = 0; iTime < nTimeTotal; iTime++) {
687
688     amp[0] = fMCM->GetADC(iCol-1,iTime);
689     amp[1] = fMCM->GetADC(iCol  ,iTime);
690     amp[2] = fMCM->GetADC(iCol+1,iTime);
691
692     // extract track contribution only from the central pad
693     track[0] = fTrack0->GetDataUnchecked(row,iCol+iCol1,iTime);
694     track[1] = fTrack1->GetDataUnchecked(row,iCol+iCol1,iTime);
695     track[2] = fTrack2->GetDataUnchecked(row,iCol+iCol1,iTime);
696
697     if      (fMCM->IsCluster(iCol,iTime)) {
698
699       fTrk->AddCluster(iCol+iCol1,iTime,amp,track);
700
701     } 
702     else if ((iCol+1+1) < kMcmCol) {
703
704       amp[0] = fMCM->GetADC(iCol-1+1,iTime);
705       amp[1] = fMCM->GetADC(iCol  +1,iTime);
706       amp[2] = fMCM->GetADC(iCol+1+1,iTime);
707
708       if (fMCM->IsCluster(iCol+1,iTime)) {
709
710         // extract track contribution only from the central pad
711         track[0] = fTrack0->GetDataUnchecked(row,iCol+1+iCol1,iTime);
712         track[1] = fTrack1->GetDataUnchecked(row,iCol+1+iCol1,iTime);
713         track[2] = fTrack2->GetDataUnchecked(row,iCol+1+iCol1,iTime);
714
715         fTrk->AddCluster(iCol+1+iCol1,iTime,amp,track);
716
717       }
718
719     }
720
721   }
722
723   fTrk->CookLabel(0.8);  
724   /*
725   if (fTrk->GetLabel() >= fNPrimary) {
726     Info("AddTracklet","Only primaries are stored!");
727     return;
728   }
729   */
730   // LTU Pt cut
731   fTrk->MakeTrackletGraph(fGeo,fField);
732   fTrk->MakeClusAmpGraph();
733   if (TMath::Abs(fTrk->GetPt()) < fTrigParam->GetLtuPtCut()) {
734     return;
735   }
736       
737   Tracklets()->Add(fTrk);
738
739 }
740
741 //_____________________________________________________________________________
742 Bool_t AliTRDtrigger::WriteTracklets(Int_t det) 
743 {
744   //
745   // Fills TRDmcmTracklet branch in the tree with the Tracklets 
746   // found in detector = det. For det=-1 writes the tree. 
747   //
748
749   if ((det < -1) || (det >= AliTRDgeometry::Ndet())) {
750     AliError(Form("Unexpected detector index %d.",det));
751     return kFALSE;
752   }
753
754   TBranch *branch = fTrackletTree->GetBranch("TRDmcmTracklet");
755   if (!branch) {
756     TObjArray *ioArray = 0;
757     branch = fTrackletTree->Branch("TRDmcmTracklet","TObjArray",&ioArray,32000,0);
758   }
759
760   if ((det >= 0) && (det < AliTRDgeometry::Ndet())) {
761
762     Int_t nTracklets = Tracklets()->GetEntriesFast();
763     TObjArray *detTracklets = new TObjArray(400);
764
765     for (Int_t i = 0; i < nTracklets; i++) {
766
767       AliTRDmcmTracklet *trk = (AliTRDmcmTracklet *) Tracklets()->UncheckedAt(i);
768       
769       if (det == trk->GetDetector()) {
770         detTracklets->AddLast(trk);
771       }
772
773     }
774
775     branch->SetAddress(&detTracklets);
776     fTrackletTree->Fill();
777
778     delete detTracklets;
779
780     return kTRUE;
781
782   }
783
784   if (det == -1) {
785
786     AliInfo(Form("Writing the Tracklet tree %s for event %d."
787                 ,fTrackletTree->GetName(),fRunLoader->GetEventNumber()));
788
789     AliLoader* loader = fRunLoader->GetLoader("TRDLoader");
790     loader->WriteTracks("OVERWRITE");
791     
792     return kTRUE;  
793
794   }
795
796   return kFALSE;
797
798 }
799
800 //_____________________________________________________________________________
801 void AliTRDtrigger::MakeTracks(Int_t det)
802 {
803   //
804   // Create GTU tracks per module (stack of 6 chambers)
805   //
806   
807   fModule->Reset();
808
809   Int_t nRowMax, iplan, icham, isect, row;
810
811   if ((det < 0) || (det >= AliTRDgeometry::Ndet())) {
812     AliError(Form("Unexpected detector index %d.",det));
813     return;
814   }
815   
816   Int_t nTracklets = Tracklets()->GetEntriesFast();
817   
818   AliTRDmcmTracklet *trk;
819   for (Int_t i = 0; i < nTracklets; i++) {
820     
821     trk = (AliTRDmcmTracklet *) Tracklets()->UncheckedAt(i);
822     
823     iplan = fGeo->GetPlane(trk->GetDetector());
824     icham = fGeo->GetChamber(trk->GetDetector());
825     isect = fGeo->GetSector(trk->GetDetector());
826
827     nRowMax = fCParam->GetRowMax(iplan,icham,isect);
828     row = trk->GetRow();
829
830     fModule->AddTracklet(trk->GetDetector(),
831                          row,
832                          trk->GetRowz(),
833                          trk->GetSlope(),
834                          trk->GetOffset(),
835                          trk->GetTime0(),
836                          trk->GetNclusters(),
837                          trk->GetLabel(),
838                          trk->GetdQdl());
839     
840   }
841
842   fModule->SortTracklets();
843   fModule->RemoveMultipleTracklets();
844   fModule->SortZ((Int_t)fGeo->GetChamber(det));
845   fModule->FindTracks();
846   fModule->SortTracks();
847   fModule->RemoveMultipleTracks();
848
849   Int_t nModTracks = fModule->GetNtracks();
850   AliTRDgtuTrack *gtutrk;
851   for (Int_t i = 0; i < nModTracks; i++) {
852     gtutrk = (AliTRDgtuTrack*)fModule->GetTrack(i);
853     if (TMath::Abs(gtutrk->GetPt()) < fTrigParam->GetGtuPtCut()) continue;
854     gtutrk->CookLabel();
855     gtutrk->MakePID();
856     AddTrack(gtutrk,det);
857   }
858   
859 }
860
861 //_____________________________________________________________________________
862 void AliTRDtrigger::AddTrack(const AliTRDgtuTrack *t, Int_t det)  
863
864   //
865   // Add a track to the list
866   //
867
868   AliTRDgtuTrack *track = new(fTracks->operator[](fTracks->GetEntriesFast())) 
869                           AliTRDgtuTrack(*t);
870   track->SetDetector(det); 
871
872 }
873
874 //_____________________________________________________________________________
875 TObjArray* AliTRDtrigger::Tracklets()
876
877   //
878   // Returns list of tracklets
879   //
880
881   if (!fTracklets) {
882     fTracklets = new TObjArray(400); 
883   }
884   return fTracklets;       
885
886 }
887
888 //_____________________________________________________________________________
889 void AliTRDtrigger::ResetTracklets()                              
890 {
891   //
892   // Resets the list of tracklets
893   //
894  
895   if (fTracklets) {
896     fTracklets->Delete();             
897   }
898
899 }
900
901 //_____________________________________________________________________________
902 Int_t AliTRDtrigger::GetNumberOfTracks() const                     
903
904   //
905   // Returns number of tracks
906   //
907
908   return fTracks->GetEntriesFast(); 
909                 
910 }
911
912 //_____________________________________________________________________________
913 AliTRDgtuTrack* AliTRDtrigger::GetTrack(Int_t i) const               
914
915   //
916   // Returns a given track from the list
917   //
918
919   return (AliTRDgtuTrack *) fTracks->UncheckedAt(i); 
920
921 }