]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDinfoGen.cxx
change systematic and resolution plots
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDinfoGen.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 /* $Id: AliTRDinfoGen.cxx 27496 2008-07-22 08:35:45Z cblume $ */
17
18 ////////////////////////////////////////////////////////////////////////////
19 //
20 //  Tender wagon for TRD performance/calibration train
21 //
22 // In this wagon the information from
23 //   - ESD
24 //   - Friends [if available]
25 //   - MC [if available]
26 // are grouped into AliTRDtrackInfo objects and fed to worker tasks
27 //
28 //  Authors:
29 //    Markus Fasel <M.Fasel@gsi.de>
30 //    Alexandru Bercuci <A.Bercuci@gsi.de>
31 //
32 ////////////////////////////////////////////////////////////////////////////
33
34 #include <TClonesArray.h>
35 #include <TObjArray.h>
36 #include <TObject.h>
37 #include <TString.h>
38 #include <TH1S.h>
39 #include <TPad.h>
40 #include <TFile.h>
41 #include <TTree.h>
42 #include <TROOT.h>
43 #include <TChain.h>
44 #include <TParticle.h>
45
46 #include "AliLog.h"
47 #include "AliAnalysisManager.h"
48 #include "AliGeomManager.h"
49 #include "AliCDBManager.h"
50 #include "AliCDBEntry.h"
51 #include "AliCDBPath.h"
52 #include "AliESDEvent.h"
53 #include "AliMCEvent.h"
54 #include "AliESDInputHandler.h"
55 #include "AliMCEventHandler.h"
56
57 #include "AliESDfriend.h"
58 #include "AliESDfriendTrack.h"
59 #include "AliESDHeader.h"
60 #include "AliESDtrack.h"
61 #include "AliESDv0.h"
62 #include "AliESDtrackCuts.h"
63 #include "AliMCParticle.h"
64 #include "AliPID.h"
65 #include "AliStack.h"
66 #include "AliTrackReference.h"
67 #include "TTreeStream.h"
68
69 #include <cstdio>
70 #include <climits>
71 #include <cstring>
72 #include <iostream>
73 #include <memory>
74
75 #include "AliTRDReconstructor.h"
76 #include "AliTRDrecoParam.h"
77 #include "AliTRDcalibDB.h"
78 #include "AliTRDtrackerV1.h"
79 #include "AliTRDgeometry.h"
80 #include "AliTRDtrackV1.h"
81 #include "AliTRDseedV1.h"
82 #include "AliTRDcluster.h"
83 #include "AliTRDinfoGen.h"
84 #include "AliTRDpwg1Helper.h"
85 #include "info/AliTRDtrackInfo.h"
86 #include "info/AliTRDeventInfo.h"
87 #include "info/AliTRDv0Info.h"
88 #include "info/AliTRDeventCuts.h"
89
90 ClassImp(AliTRDinfoGen)
91
92 const Float_t AliTRDinfoGen::fgkITS = 100.; // to be checked
93 const Float_t AliTRDinfoGen::fgkTPC = 290.;
94 const Float_t AliTRDinfoGen::fgkTRD = 365.;
95
96 const Float_t AliTRDinfoGen::fgkEvVertexZ = 15.;
97 const Int_t   AliTRDinfoGen::fgkEvVertexN = 1;
98
99 const Float_t AliTRDinfoGen::fgkTrkDCAxy  = 3.;
100 const Float_t AliTRDinfoGen::fgkTrkDCAz   = 10.;
101 const Int_t   AliTRDinfoGen::fgkNclTPC    = 70;
102 const Float_t AliTRDinfoGen::fgkPt        = 0.2;
103 const Float_t AliTRDinfoGen::fgkEta       = 0.9;
104 AliTRDReconstructor* AliTRDinfoGen::fgReconstructor(NULL);
105 AliTRDgeometry* AliTRDinfoGen::fgGeo(NULL);
106 //____________________________________________________________________
107 AliTRDinfoGen::AliTRDinfoGen()
108   :AliAnalysisTaskSE()
109   ,fEvTrigger(NULL)
110   ,fESDev(NULL)
111   ,fMCev(NULL)
112   ,fEventCut(NULL)
113   ,fTrackCut(NULL)
114   ,fV0Cut(NULL)
115   ,fOCDB("local://$ALICE_ROOT/OCDB")
116   ,fTrackInfo(NULL)
117   ,fEventInfo(NULL)
118   ,fV0Info(NULL)
119   ,fTracksBarrel(NULL)
120   ,fTracksSA(NULL)
121   ,fTracksKink(NULL)
122   ,fV0List(NULL)
123   ,fContainer(NULL)
124   ,fDebugStream(NULL)
125 {
126   //
127   // Default constructor
128   //
129   SetNameTitle("TRDinfoGen", "MC-REC TRD-track list generator");
130 }
131
132 //____________________________________________________________________
133 AliTRDinfoGen::AliTRDinfoGen(char* name)
134   :AliAnalysisTaskSE(name)
135   ,fEvTrigger(NULL)
136   ,fESDev(NULL)
137   ,fMCev(NULL)
138   ,fEventCut(NULL)
139   ,fTrackCut(NULL)
140   ,fV0Cut(NULL)
141   ,fOCDB("local://$ALICE_ROOT/OCDB")
142   ,fTrackInfo(NULL)
143   ,fEventInfo(NULL)
144   ,fV0Info(NULL)
145   ,fTracksBarrel(NULL)
146   ,fTracksSA(NULL)
147   ,fTracksKink(NULL)
148   ,fV0List(NULL)
149   ,fContainer(NULL)
150   ,fDebugStream(NULL)
151 {
152   //
153   // Default constructor
154   //
155   SetTitle("MC-REC TRD-track list generator");
156   DefineOutput(AliTRDpwg1Helper::kTracksBarrel, TObjArray::Class());
157   DefineOutput(AliTRDpwg1Helper::kTracksSA, TObjArray::Class());
158   DefineOutput(AliTRDpwg1Helper::kTracksKink, TObjArray::Class());
159   DefineOutput(AliTRDpwg1Helper::kEventInfo, AliTRDeventInfo::Class());
160   DefineOutput(AliTRDpwg1Helper::kV0List, TObjArray::Class());
161   DefineOutput(AliTRDpwg1Helper::kMonitor, TObjArray::Class()); // histogram list
162 }
163
164 //____________________________________________________________________
165 AliTRDinfoGen::~AliTRDinfoGen()
166 {
167 // Destructor
168   if(fgGeo) delete fgGeo;
169   if(fgReconstructor) delete fgReconstructor;
170   if(fDebugStream) delete fDebugStream;
171   if(fEvTrigger) delete fEvTrigger;
172   if(fV0Cut) delete fV0Cut;
173   if(fTrackCut) delete fTrackCut;
174   if(fEventCut) delete fEventCut;
175   if(fTrackInfo) delete fTrackInfo; fTrackInfo = NULL;
176   if(fEventInfo) delete fEventInfo; fEventInfo = NULL;
177   if(fV0Info) delete fV0Info; fV0Info = NULL;
178   if(fTracksBarrel){ 
179     fTracksBarrel->Delete(); delete fTracksBarrel;
180     fTracksBarrel = NULL;
181   }
182   if(fTracksSA){ 
183     fTracksSA->Delete(); delete fTracksSA;
184     fTracksSA = NULL;
185   }
186   if(fTracksKink){ 
187     fTracksKink->Delete(); delete fTracksKink;
188     fTracksKink = NULL;
189   }
190   if(fV0List){ 
191     fV0List->Delete(); 
192     delete fV0List;
193     fV0List = NULL;
194   }
195   if(fContainer){ 
196     fContainer->Delete(); 
197     delete fContainer;
198     fContainer = NULL;
199   }
200 }
201
202 //____________________________________________________________________
203 Bool_t AliTRDinfoGen::GetRefFigure(Int_t)
204 {
205 // General graphs for PWG1/TRD train
206   if(!gPad){
207     AliWarning("Please provide a canvas to draw results.");
208     return kFALSE;
209   }
210   fContainer->At(0)->Draw("bar");
211   return kTRUE;
212 }
213
214 //____________________________________________________________________
215 void AliTRDinfoGen::UserCreateOutputObjects()
216 {       
217   //
218   // Create Output Containers (TObjectArray containing 1D histograms)
219   //
220  
221   fTrackInfo = new AliTRDtrackInfo();
222   fEventInfo = new AliTRDeventInfo();
223   fV0Info    = new AliTRDv0Info();
224   fTracksBarrel = new TObjArray(200); fTracksBarrel->SetOwner(kTRUE);
225   fTracksSA = new TObjArray(20); fTracksSA->SetOwner(kTRUE);
226   fTracksKink = new TObjArray(20); fTracksKink->SetOwner(kTRUE);
227   fV0List = new TObjArray(10); fV0List->SetOwner(kTRUE);
228
229   // define general monitor
230   fContainer = new TObjArray(1); fContainer->SetOwner(kTRUE);
231   TH1 *h=new TH1S("hStat", "Run statistics;Observable;Entries", 12, -0.5, 11.5);
232   TAxis *ax(h->GetXaxis());
233   ax->SetBinLabel( 1, "ESD");
234   ax->SetBinLabel( 2, "MC");
235   ax->SetBinLabel( 3, "V0");
236   ax->SetBinLabel( 4, "TPC");
237   ax->SetBinLabel( 5, "TRDin");
238   ax->SetBinLabel( 6, "TRDout");
239   ax->SetBinLabel( 7, "Barrel");
240   ax->SetBinLabel( 8, "BarrelMC");
241   ax->SetBinLabel( 9, "SA");
242   ax->SetBinLabel(10, "SAMC");
243   ax->SetBinLabel(11, "Kink");
244   ax->SetBinLabel(12, "KinkMC");
245   fContainer->AddAt(h, 0);
246   PostData(AliTRDpwg1Helper::kMonitor, fContainer);
247 }
248
249 //____________________________________________________________________
250 Bool_t AliTRDinfoGen::Load(const Char_t *file, const Char_t *dir, const Char_t *name)
251 {
252 // Load data from performance file
253
254   if(!TFile::Open(file)){
255     AliWarning(Form("Couldn't open file %s.", file));
256     return kFALSE;
257   }
258   if(dir){
259     if(!gFile->cd(dir)){
260       AliWarning(Form("Couldn't cd to %s in %s.", dir, file));
261       return kFALSE;
262     }
263   }
264   TObjArray *o(NULL);
265   const Char_t *tn=(name ? name : GetName());
266   if(!(o = (TObjArray*)gDirectory->Get(tn))){
267     AliWarning(Form("Missing histogram container %s.", tn));
268     return kFALSE;
269   }
270   fContainer = (TObjArray*)o->Clone(GetName());
271   gFile->Close();
272   return kTRUE;
273 }
274
275 //____________________________________________________________________
276 void AliTRDinfoGen::UserExec(Option_t *){
277   //
278   // Run the Analysis
279   //
280
281   fTracksBarrel->Delete();
282   fTracksSA->Delete();
283   fTracksKink->Delete();
284   fV0List->Delete();
285   fEventInfo->Delete("");
286
287   fESDev = dynamic_cast<AliESDEvent*>(InputEvent());
288   if(!fESDev){
289     AliError("Failed retrieving ESD event");
290     return;
291   }
292   // WARNING
293   // This part may conflict with other detectors !!
294   if(!IsInitOCDB()){ 
295     AliInfo("Initializing OCDB ...");
296     // prepare OCDB access
297     AliCDBManager* ocdb = AliCDBManager::Instance();
298     ocdb->SetDefaultStorage(fOCDB.Data());
299     ocdb->SetRun(fESDev->GetRunNumber());
300     // create geo manager
301     AliCDBEntry* obj = ocdb->Get(AliCDBPath("GRP", "Geometry", "Data"));
302     AliGeomManager::SetGeometry((TGeoManager*)obj->GetObject());
303     AliGeomManager::GetNalignable("TRD");
304     AliGeomManager::ApplyAlignObjsFromCDB("TRD");
305     fgGeo = new AliTRDgeometry;
306     //init magnetic field
307     if(!TGeoGlobalMagField::Instance()->IsLocked() &&
308        !fESDev->InitMagneticField()){
309       AliWarning("Magnetic field failed initialization.");
310     }
311
312     // set no of time bins
313     AliTRDtrackerV1::SetNTimeBins(AliTRDcalibDB::Instance()->GetNumberOfTimeBinsDCS());
314     AliInfo(Form("OCDB :  Loc[%s] Run[%d] TB[%d]", fOCDB.Data(), ocdb->GetRun(), AliTRDtrackerV1::GetNTimeBins()));
315
316     // set reco param valid for this run/event
317     AliInfo(Form("Initializing TRD reco params for EventSpecie[%d]...",
318       fESDev->GetEventSpecie()));
319     fgReconstructor = new AliTRDReconstructor();
320     obj = ocdb->Get(AliCDBPath("TRD", "Calib", "RecoParam"));
321     obj->PrintMetaData();
322     TObjArray *recos((TObjArray*)obj->GetObject());
323     for(Int_t ireco(0); ireco<recos->GetEntriesFast(); ireco++){
324       AliTRDrecoParam *reco((AliTRDrecoParam*)recos->At(ireco));
325       Int_t es(reco->GetEventSpecie());
326       if(!(es&fESDev->GetEventSpecie())) continue;
327       fgReconstructor->SetRecoParam(reco);
328       TString s;
329       if(es&AliRecoParam::kLowMult) s="LowMult";
330       else if(es&AliRecoParam::kHighMult) s="HighMult";
331       else if(es&AliRecoParam::kCosmic) s="Cosmic";
332       else if(es&AliRecoParam::kCalib) s="Calib";
333       else s="Unknown";
334       AliInfo(Form("Using reco params for %s", s.Data()));
335       break;
336     }
337     SetInitOCDB();
338   }
339
340   // link MC if available
341   fMCev = MCEvent();
342
343   // event selection : trigger cut
344   if(UseLocalEvSelection() && fEvTrigger){ 
345     Bool_t kTRIGGERED(kFALSE);
346     std::auto_ptr<TObjArray> trig(fEvTrigger->Tokenize(" "));
347     for(Int_t itrig=trig->GetEntriesFast(); itrig--;){
348       const Char_t *trigClass(((TObjString*)(*trig)[itrig])->GetName());
349       if(fESDev->IsTriggerClassFired(trigClass)) {
350         AliDebug(2, Form("Ev[%4d] Trigger[%s]", fESDev->GetEventNumberInFile(), trigClass));
351         kTRIGGERED = kTRUE;
352         break; 
353       }
354     }
355     if(!kTRIGGERED){ 
356       AliDebug(2, Form("Reject Ev[%4d] Trigger", fESDev->GetEventNumberInFile()));
357       return;
358     }
359     // select only physical events
360     if(fESDev->GetEventType() != 7){ 
361       AliDebug(2, Form("Reject Ev[%4d] EvType[%d]", fESDev->GetEventNumberInFile(), fESDev->GetEventType()));
362       return;
363     }
364   }
365
366   // if the required trigger is a collision trigger then apply event vertex cut
367   if(UseLocalEvSelection() && IsCollision()){
368     const AliESDVertex *vertex = fESDev->GetPrimaryVertex();
369     if(TMath::Abs(vertex->GetZv())<1.e-10 || 
370        TMath::Abs(vertex->GetZv())>fgkEvVertexZ || 
371        vertex->GetNContributors()<fgkEvVertexN) {
372       AliDebug(2, Form("Reject Ev[%4d] Vertex Zv[%f] Nv[%d]", fESDev->GetEventNumberInFile(), TMath::Abs(vertex->GetZv()), vertex->GetNContributors()));
373       return;
374     }
375   }
376
377   if(fEventCut && !fEventCut->IsSelected(fESDev, IsCollision())) return;
378
379   if(!fESDfriend){
380     AliError("Failed retrieving ESD friend event");
381     return;
382   }
383   if(HasMCdata() && !fMCev){
384     AliError("Failed retrieving MC event");
385     return;
386   }
387
388   new(fEventInfo)AliTRDeventInfo(fESDev->GetHeader(), const_cast<AliESDRun *>(fESDev->GetESDRun()));
389   
390   Bool_t *trackMap(NULL);
391   AliStack * mStack(NULL);
392   Int_t nTracksMC = HasMCdata() ? fMCev->GetNumberOfTracks() : 0, nTracksESD = fESDev->GetNumberOfTracks();
393   if(HasMCdata()){
394     mStack = fMCev->Stack();
395     if(!mStack){
396       AliError("Failed retrieving MC Stack");
397       return;
398     }
399     trackMap = new Bool_t[nTracksMC];
400     memset(trackMap, 0, sizeof(Bool_t) * nTracksMC);
401   }
402   
403   Double32_t dedx[100]; Int_t nSlices(0);
404   Int_t nTRDout(0), nTRDin(0), nTPC(0)
405        ,nclsTrklt
406        ,nBarrel(0), nSA(0), nKink(0)
407        ,nBarrelMC(0), nSAMC(0), nKinkMC(0);
408   AliESDtrack *esdTrack = NULL;
409   AliESDfriendTrack *esdFriendTrack = NULL;
410   TObject *calObject = NULL;
411   AliTRDtrackV1 *track = NULL;
412   AliTRDseedV1 *tracklet = NULL;
413   AliTRDcluster *cl = NULL;
414
415
416   // LOOP 0 - over ESD v0s
417   Float_t bField(fESDev->GetMagneticField());
418   AliESDv0 *v0(NULL);
419   Int_t v0pid[AliPID::kSPECIES];
420   for(Int_t iv0(0); iv0<fESDev->GetNumberOfV0s(); iv0++){
421     if(!(v0 = fESDev->GetV0(iv0))) continue;
422     // register v0
423     if(fV0Cut) new(fV0Info) AliTRDv0Info(*fV0Cut);
424     else new(fV0Info) AliTRDv0Info();
425     fV0Info->SetMagField(bField);
426     fV0Info->SetV0tracks(fESDev->GetTrack(v0->GetPindex()), fESDev->GetTrack(v0->GetNindex()));
427     fV0Info->SetV0Info(v0);
428     fV0List->Add(new AliTRDv0Info(*fV0Info));//  kFOUND=kFALSE;
429   }
430
431
432   // LOOP 1 - over ESD tracks
433   AliTRDv0Info *v0info=NULL;
434   for(Int_t itrk = 0; itrk < nTracksESD; itrk++){
435     new(fTrackInfo) AliTRDtrackInfo();
436     esdTrack = fESDev->GetTrack(itrk);
437     AliDebug(3, Form("\n%3d ITS[%d] TPC[%d] TRD[%d]\n", itrk, esdTrack->GetNcls(0), esdTrack->GetNcls(1), esdTrack->GetNcls(2)));
438
439     if(esdTrack->GetStatus()&AliESDtrack::kTPCout) nTPC++;
440     if(esdTrack->GetStatus()&AliESDtrack::kTRDout) nTRDout++;
441     if(esdTrack->GetStatus()&AliESDtrack::kTRDin) nTRDin++;
442
443     // look at external track param
444     const AliExternalTrackParam *op = esdTrack->GetOuterParam();
445     Double_t xyz[3];
446     if(op){
447       op->GetXYZ(xyz);
448       op->Global2LocalPosition(xyz, op->GetAlpha());
449       AliDebug(3, Form("op @ X[%7.3f]\n", xyz[0]));
450     }
451
452     // read MC info
453     Int_t fPdg = -1;
454     Int_t label = -1; UInt_t alab=UINT_MAX;
455     if(HasMCdata()){
456       label = esdTrack->GetLabel(); 
457       alab = TMath::Abs(label);
458       // register the track
459       if(alab < UInt_t(nTracksMC)){ 
460         trackMap[alab] = kTRUE; 
461       } else { 
462         AliError(Form("MC label[%d] outside scope for Ev[%d] Trk[%d].", label, (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), itrk));
463         continue; 
464       }
465       AliMCParticle *mcParticle = NULL; 
466       if(!(mcParticle = (AliMCParticle*) fMCev->GetTrack(alab))){
467         AliError(Form("MC particle label[%d] missing for Ev[%d] Trk[%d].", label, (Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), itrk));
468         continue;
469       }
470       fPdg = mcParticle->Particle()->GetPdgCode();
471       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
472       Int_t iref = 0; AliTrackReference *ref = NULL; 
473       while(iref<nRefs){
474         ref = mcParticle->GetTrackReference(iref);
475         if(ref->LocalX() > fgkTPC) break;
476         iref++;
477       }
478
479       fTrackInfo->SetMC();
480       fTrackInfo->SetPDG(fPdg);
481       fTrackInfo->SetPrimary(mcParticle->Particle()->IsPrimary());
482       fTrackInfo->SetLabel(label);
483       Int_t jref = iref;//, kref = 0;
484       while(jref<nRefs){
485         ref = mcParticle->GetTrackReference(jref);
486         if(ref->LocalX() > fgkTRD) break;
487         AliDebug(4, Form("  trackRef[%2d (%2d)] @ %7.3f OK", jref-iref, jref, ref->LocalX()));
488         fTrackInfo->AddTrackRef(ref);
489         jref++;
490       }
491       AliDebug(3, Form("NtrackRefs[%d(%d)]", fTrackInfo->GetNTrackRefs(), nRefs));
492     }
493
494     // copy some relevant info to TRD track info
495     fTrackInfo->SetStatus(esdTrack->GetStatus());
496     fTrackInfo->SetTrackId(esdTrack->GetID());
497     Double_t p[AliPID::kSPECIES]; esdTrack->GetTRDpid(p);
498     fTrackInfo->SetESDpid(p);
499     fTrackInfo->SetESDpidQuality(esdTrack->GetTRDntrackletsPID());
500     if(!nSlices) nSlices = esdTrack->GetNumberOfTRDslices();
501     memset(dedx, 0, 100*sizeof(Double32_t));
502     Int_t in(0);
503     for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++)
504       for(Int_t is=0; is<nSlices; is++) 
505         dedx[in++]=esdTrack->GetTRDslice(il, is);
506     for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++) dedx[in++]=esdTrack->GetTRDmomentum(il);
507     fTrackInfo->SetSlices(in, dedx);
508     fTrackInfo->SetNumberOfClustersRefit(esdTrack->GetNcls(2));
509     // some other Informations which we may wish to store in order to find problematic cases
510     fTrackInfo->SetKinkIndex(esdTrack->GetKinkIndex(0));
511     fTrackInfo->SetTPCncls(static_cast<UShort_t>(esdTrack->GetNcls(1)));
512     nclsTrklt = 0;
513   
514     // set V0pid info
515     for(Int_t iv(0); iv<fV0List->GetEntriesFast(); iv++){
516       if(!(v0info = (AliTRDv0Info*)fV0List->At(iv))) continue;
517       if(!v0info->GetV0Daughter(1) && !v0info->GetV0Daughter(-1)) continue;
518       if(!v0info->HasTrack(fTrackInfo)) continue;
519       memset(v0pid, 0, AliPID::kSPECIES*sizeof(Int_t));
520       fTrackInfo->SetV0();
521       for(Int_t is=AliPID::kSPECIES; is--;){v0pid[is] = v0info->GetPID(is, fTrackInfo);}
522       fTrackInfo->SetV0pid(v0pid);
523       fTrackInfo->SetV0();
524       //const AliTRDtrackInfo::AliESDinfo *ei = fTrackInfo->GetESDinfo();
525       break;
526     }
527
528     // read REC info
529     esdFriendTrack = fESDfriend->GetTrack(itrk);
530     if(esdFriendTrack){
531       Int_t icalib = 0;
532       while((calObject = esdFriendTrack->GetCalibObject(icalib++))){
533         if(strcmp(calObject->IsA()->GetName(),"AliTRDtrackV1") != 0) continue; // Look for the TRDtrack
534         if(!(track = dynamic_cast<AliTRDtrackV1*>(calObject))) break;
535         AliDebug(4, Form("TRD track OK"));
536         // Set the clusters to unused
537         for(Int_t ipl = 0; ipl < AliTRDgeometry::kNlayer; ipl++){
538           if(!(tracklet = track->GetTracklet(ipl))) continue;
539           tracklet->ResetClusterIter();
540           while((cl = tracklet->NextCluster())) cl->Use(0);
541         }
542         fTrackInfo->SetTrack(track);
543         break;
544       }
545       AliDebug(3, Form("Ntracklets[%d]\n", fTrackInfo->GetNTracklets()));
546     } else AliDebug(3, "No ESD friends");
547     if(op) fTrackInfo->SetOuterParam(op);
548
549     if(DebugLevel() >= 1){
550       AliTRDtrackInfo info(*fTrackInfo);
551       (*DebugStream()) << "trackInfo"
552       << "TrackInfo.=" << &info
553       << "\n";
554       info.Delete("");
555     }
556
557     ULong_t status(esdTrack->GetStatus());
558     if((status&AliESDtrack::kTPCout)){
559       if(!esdTrack->GetKinkIndex(0)){ // Barrel  Track Selection
560         Bool_t selected(kTRUE);
561         if(UseLocalTrkSelection()){
562           if(esdTrack->Pt() < fgkPt){ 
563             AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Pt[%5.2f]", fESDev->GetEventNumberInFile(), itrk, esdTrack->Pt()));
564             selected = kFALSE;
565           }
566           if(selected && TMath::Abs(esdTrack->Eta()) > fgkEta){
567             AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Eta[%5.2f]", fESDev->GetEventNumberInFile(), itrk, TMath::Abs(esdTrack->Eta())));
568             selected = kFALSE;
569           }
570           if(selected && esdTrack->GetTPCNcls() < fgkNclTPC){ 
571             AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] NclTPC[%d]", fESDev->GetEventNumberInFile(), itrk, esdTrack->GetTPCNcls()));
572             selected = kFALSE;
573           }
574           Float_t par[2], cov[3];
575           esdTrack->GetImpactParameters(par, cov);
576           if(IsCollision()){ // cuts on DCA
577             if(selected && TMath::Abs(par[0]) > fgkTrkDCAxy){ 
578               AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] DCAxy[%f]", fESDev->GetEventNumberInFile(), itrk, TMath::Abs(par[0])));
579               selected = kFALSE;
580             }
581             if(selected && TMath::Abs(par[1]) > fgkTrkDCAz){ 
582               AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] DCAz[%f]", fESDev->GetEventNumberInFile(), itrk, TMath::Abs(par[1])));
583               selected = kFALSE;
584             }
585           } else if(selected && fMCev && !fMCev->IsPhysicalPrimary(alab)){;
586             AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Primary", fESDev->GetEventNumberInFile(), itrk));
587             selected = kFALSE;
588           }
589         }
590         if(fTrackCut && !fTrackCut->IsSelected(esdTrack)) selected = kFALSE;
591         if(selected){ 
592           fTracksBarrel->Add(new AliTRDtrackInfo(*fTrackInfo));
593           nBarrel++;
594         }
595       } else {
596         fTracksKink->Add(new AliTRDtrackInfo(*fTrackInfo));
597         nKink++;
598       }
599     } else if((status&AliESDtrack::kTRDout) && !(status&AliESDtrack::kTRDin)){ 
600       fTracksSA->Add(new AliTRDtrackInfo(*fTrackInfo));
601       nSA++;
602     }
603     fTrackInfo->Delete("");
604   }
605
606   // LOOP 2 - over MC tracks which are passing TRD where the track is not reconstructed
607   if(HasMCdata()){
608     AliDebug(10, "Output of the MC track map:");
609     for(Int_t itk = 0; itk < nTracksMC;  itk++) AliDebug(10, Form("trackMap[%d] = %s", itk, trackMap[itk] == kTRUE ? "TRUE" : "kFALSE"));
610   
611     for(Int_t itk = 0; itk < nTracksMC; itk++){
612       if(trackMap[itk]) continue;
613       AliMCParticle *mcParticle =  (AliMCParticle*) fMCev->GetTrack(TMath::Abs(itk));
614       Int_t fPdg = mcParticle->Particle()->GetPdgCode();
615       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
616       Int_t iref = 0; AliTrackReference *ref = NULL; 
617       Int_t nRefsTRD = 0;
618       new(fTrackInfo) AliTRDtrackInfo();
619       fTrackInfo->SetMC();
620       fTrackInfo->SetPDG(fPdg);
621       while(iref<nRefs){ // count TRD TR
622         Bool_t kIN(kFALSE);
623         ref = mcParticle->GetTrackReference(iref);
624         if(ref->LocalX() > fgkTPC && ref->LocalX() < fgkTRD){
625           fTrackInfo->AddTrackRef(ref);
626           nRefsTRD++;kIN=kTRUE;
627         }
628         AliDebug(4, Form("  trackRef[%2d] @ x[%7.3f] %s", iref, ref->LocalX(), kIN?"IN":"OUT"));
629         iref++;
630       }
631       if(!nRefsTRD){
632         // In this stage we at least require 1 hit inside TRD. What will be done with this tracks is a task for the 
633         // analysis job
634         fTrackInfo->Delete("");
635         continue;
636       }
637       fTrackInfo->SetPrimary(mcParticle->Particle()->IsPrimary());
638       fTrackInfo->SetLabel(itk);
639       if(DebugLevel() >= 1){
640         AliTRDtrackInfo info(*fTrackInfo);
641         (*DebugStream()) << "trackInfo"
642         << "TrackInfo.=" << &info
643         << "\n";
644         info.Delete("");
645       }
646       AliDebug(3, Form("Add MC track @ label[%d] nTRDrefs[%d].", itk, nRefsTRD));
647       // check where the track starts
648       ref = mcParticle->GetTrackReference(0);
649       if(ref->LocalX() < fgkITS){ 
650         fTracksBarrel->Add(new AliTRDtrackInfo(*fTrackInfo));
651         nBarrelMC++;
652       } else if(ref->LocalX() < fgkTPC) {
653         fTracksKink->Add(new AliTRDtrackInfo(*fTrackInfo));
654         nKinkMC++;
655       } else if(nRefsTRD>6){
656         fTracksSA->Add(new AliTRDtrackInfo(*fTrackInfo));
657         nSAMC++;
658       }
659       fTrackInfo->Delete("");
660     }
661     delete[] trackMap;
662   }
663   AliDebug(1, Form(
664     "\nEv[%3d] Tracks: ESD[%d] MC[%d] V0[%d]\n"
665     "        TPCout[%d] TRDin[%d] TRDout[%d]\n"
666     "        Barrel[%3d+%3d=%3d] SA[%2d+%2d=%2d] Kink[%2d+%2d=%2d]"
667     ,(Int_t)AliAnalysisManager::GetAnalysisManager()->GetCurrentEntry(), nTracksESD, nTracksMC, fV0List->GetEntries()
668     , nTPC, nTRDin, nTRDout
669     ,nBarrel, nBarrelMC, fTracksBarrel->GetEntries()
670     ,nSA, nSAMC, fTracksSA->GetEntries()
671     ,nKink, nKinkMC, fTracksKink->GetEntries()
672   ));
673   // save track statistics
674   TH1 *h((TH1S*)fContainer->At(0));
675   h->Fill( 0., nTracksESD);
676   h->Fill( 1., nTracksMC);
677   h->Fill( 2., fV0List->GetEntries());
678   h->Fill( 3., nTPC);
679   h->Fill( 4., nTRDin);
680   h->Fill( 5., nTRDout);
681   h->Fill( 6., nBarrel);
682   h->Fill( 7., nBarrelMC);
683   h->Fill( 8., nSA);
684   h->Fill( 9., nSAMC);
685   h->Fill(10., nKink);
686   h->Fill(11., nKinkMC);
687
688   PostData(AliTRDpwg1Helper::kTracksBarrel, fTracksBarrel);
689   PostData(AliTRDpwg1Helper::kTracksSA, fTracksSA);
690   PostData(AliTRDpwg1Helper::kTracksKink, fTracksKink);
691   PostData(AliTRDpwg1Helper::kEventInfo, fEventInfo);
692   PostData(AliTRDpwg1Helper::kV0List, fV0List);
693 }
694
695 //____________________________________________________________________
696 void AliTRDinfoGen::SetLocalV0Selection(const AliTRDv0Info *v0)
697 {
698 // Set V0 cuts from outside
699
700   if(!fV0Cut) fV0Cut = new AliTRDv0Info(*v0);
701   else new(fV0Cut) AliTRDv0Info(*v0);
702 }
703
704 //____________________________________________________________________
705 void AliTRDinfoGen::SetTrigger(const Char_t *trigger)
706 {
707   if(!fEvTrigger) fEvTrigger = new TString(trigger);
708   else (*fEvTrigger) = trigger;
709 }
710
711 //____________________________________________________________________
712 TTreeSRedirector* AliTRDinfoGen::DebugStream()
713 {
714 // Manage debug stream for task
715   if(!fDebugStream){
716     TDirectory *savedir = gDirectory;
717     fDebugStream = new TTreeSRedirector("TRD.DebugInfoGen.root");
718     savedir->cd();
719   }
720   return fDebugStream;
721 }
722
723
724