df90c77f4651f5b24d38d3f4dc46ade2aae6ef98
[u/mrichter/AliRoot.git] / PWG1 / TPC / AliMCTrackingTestTask.cxx
1 //
2 // This class is the task to check the 
3 // Propagation and Update method used in the 
4 //               1. AliExternalTrackParam 
5 //               2. AliTracker
6 //
7 // Pure Monte-Carlo data used, not influence of detectors
8
9 // Input - TParticle + Array of track references - (Points alogn track trajectories)
10 // Output - Trees with track references - no histograms
11 //          MC tree -  test for material budget correction 
12 //                     see function ProcessRefTracker
13 //          MCupdate tree - test for correctness of propagation and update
14 //                     see function AliMCTrackingTestTask::FitTrackRefs
15 //
16 // Principle - Creates AliExternalTrackParam form 1 Track Refernece - 
17 //             Propagate it to other
18 // Magnetic field and the geometry has to be created before using it 
19
20 //
21 //  
22 //
23
24 //
25 // ROOT includes
26 #include <TChain.h>
27 #include <TMath.h>
28 #include <TVectorD.h>
29 #include <TSystem.h>
30 #include <TFile.h>
31 #include <TList.h>
32 #include <TTree.h>
33 // ALIROOT includes
34 #include <TTreeStream.h>
35 #include <AliAnalysisManager.h>
36 #include <AliESDInputHandler.h>
37 #include "AliStack.h"
38 #include "AliMCEvent.h"
39 #include "AliMCEventHandler.h"
40
41 #include <AliESD.h>
42 #include "AliTrackComparison.h"
43 #include "AliMCTrackingTestTask.h"
44 #include "AliGenInfoMaker.h"
45 #include "AliHelix.h"
46 #include "AliTrackPointArray.h"
47 #include "AliESDCaloCluster.h"
48
49 //
50 #include "AliMCInfo.h"
51 #include "AliComparisonObject.h"
52 #include "AliESDRecInfo.h"
53 #include "AliTPCParamSR.h"
54 #include "AliTracker.h"
55 #include "AliTPCseed.h"
56
57 #include "AliCDBManager.h"
58 #include "AliGRPManager.h"
59 #include "AliGeomManager.h"
60
61 // STL includes
62 #include <iostream>
63
64 using namespace std;
65
66 ClassImp(AliMCTrackingTestTask)
67
68 #define USE_STREAMER 0
69 #define DEBUG 0
70
71 //________________________________________________________________________
72 AliMCTrackingTestTask::AliMCTrackingTestTask() : 
73   AliAnalysisTask(), 
74   fMCinfo(0),     //! MC event handler
75   fESD(0),
76   fCurrentRun(-1),
77   fDebugStreamer(0),
78   fStreamLevel(0),
79   fDebugLevel(0),
80   fDebugOutputPath(),
81   fOutList(NULL),
82   fPitList(NULL),
83   fCompList(NULL)
84 {
85   //
86   // Default constructor (should not be used)
87   //
88 }
89
90 AliMCTrackingTestTask::AliMCTrackingTestTask(const AliMCTrackingTestTask& /*info*/) : 
91   AliAnalysisTask(), 
92   fMCinfo(0),     //! MC event handler
93   fESD(0),
94   fCurrentRun(-1),
95   fDebugStreamer(0),
96   fStreamLevel(0),
97   fDebugLevel(),
98   fDebugOutputPath(),
99   fOutList(NULL),
100   fPitList(NULL),
101   fCompList(NULL)
102 {
103   //
104   // Default constructor 
105   //
106 }
107
108
109
110 //________________________________________________________________________
111 AliMCTrackingTestTask::AliMCTrackingTestTask(const char *name) : 
112   AliAnalysisTask(name, "AliMCTrackingTestTask"), 
113   fMCinfo(0),     //! MC event handler
114   fESD(0),
115   fCurrentRun(-1),
116   fDebugStreamer(0),
117   fStreamLevel(0),
118   fDebugLevel(0),
119   fDebugOutputPath(),
120   fOutList(NULL),
121   fPitList(NULL),
122   fCompList(NULL)
123 {
124   //
125   // Normal constructor
126   //
127   // Input slot #0 works with a TChain
128   DefineInput(0, TChain::Class());
129   DefineOutput(0, TList::Class());
130
131   // create the list for comparison objects
132   fCompList = new TList;
133   //
134   //
135 }
136
137 AliMCTrackingTestTask::~AliMCTrackingTestTask(){
138   //
139   //
140   //
141   if (fDebugLevel>0)  printf("AliMCTrackingTestTask::~AliMCTrackingTestTask\n");
142   if (fDebugStreamer) delete fDebugStreamer;
143   fDebugStreamer=0;
144   if (fOutList)    delete fOutList;   fOutList   = 0;
145   if (fCompList)   delete fCompList;  fCompList = 0; 
146 }
147
148
149 //_____________________________________________________________________________
150 Bool_t AliMCTrackingTestTask::AddComparisonObject(AliTrackComparison *cObj) 
151 {
152   // add comparison object to the list
153   if(cObj == 0) {
154     Printf("ERROR: Could not add comparison object");
155     return kFALSE;
156   }
157
158   // add object to the list
159   fCompList->AddLast(cObj);
160        
161   return kTRUE;
162 }
163 //________________________________________________________________________
164 void AliMCTrackingTestTask::ConnectInputData(Option_t *) 
165 {
166   //
167   // Connect the input data
168   //
169   if(fDebugLevel>3)
170     cout << "AnalysisTaskTPCCluster::ConnectInputData()" << endl;
171
172   TTree* tree=dynamic_cast<TTree*>(GetInputData(0));
173   if (!tree) {
174     //Printf("ERROR: Could not read chain from input slot 0");
175   }
176   else {
177     AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
178     if (!esdH) {
179       //Printf("ERROR: Could not get ESDInputHandler");
180     }
181     else {
182       fESD = esdH->GetEvent();
183       //Printf("*** CONNECTED NEW EVENT ****");
184     }  
185   }
186   AliMCEventHandler* mcinfo = (AliMCEventHandler*) (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());  
187   mcinfo->SetReadTR(kTRUE);
188   
189   fMCinfo = mcinfo->MCEvent();
190 }
191
192 //________________________________________________________________________
193 void AliMCTrackingTestTask::CreateOutputObjects() 
194 {
195   //
196   // Connect the output objects
197   //
198   if(fDebugLevel>3)
199     cout << "AnalysisTaskTPCCluster::CreateOutputObjects()" << endl;
200   
201   if (!fOutList)
202     fOutList = new TList;
203   fOutList->SetOwner(kTRUE);
204   fPitList = fOutList->MakeIterator();
205   
206   // create output list
207   
208   // add comparison objects to the output
209   AliTrackComparison *cObj=0;
210   Int_t count=0;
211   TIterator *pitCompList = fCompList->MakeIterator();
212   pitCompList->Reset();
213   while(( cObj = (AliTrackComparison*)pitCompList->Next()) != NULL) {
214     fOutList->Add(cObj);
215     count++;
216   }
217   Printf("UserCreateOutputObjects(): Number of output comparison objects: %d \n", count);
218   
219   PostData(0, fOutList);  
220 }
221
222
223 //________________________________________________________________________
224 void AliMCTrackingTestTask::Exec(Option_t *) {
225   //
226   // Execute analysis for current event 
227   //
228
229 #if DEBUG
230   printf("New event!\n");
231 #endif
232
233   if(fDebugLevel>3)
234     cout << "AliMCTrackingTestTask::Exec()" << endl;
235     
236
237
238   // If MC has been connected   
239   if (!fMCinfo){
240     cout << "Not MC info\n" << endl;
241   }else{
242     ProcessMCInfo();
243     // fMCinfo->Print();
244     //DumpInfo();
245   }
246
247   PostData(0, fOutList);
248 }      
249
250
251 //________________________________________________________________________
252 void AliMCTrackingTestTask::Terminate(Option_t *) {
253     //
254     // Terminate loop
255     //
256   if(fDebugLevel>3)
257     printf("AliMCTrackingTestTask: Terminate() \n");  
258   //
259   if (fDebugLevel>0) printf("AliMCtrackingTestTask::Terminate\n");
260   if (fDebugStreamer) delete fDebugStreamer;
261   fDebugStreamer = 0;
262
263 //   AliTrackComparison *cObj=0;
264 //   TIterator *pitCompList = fCompList->MakeIterator();
265 //   pitCompList->Reset();
266 //   while(( cObj = (AliTrackComparison*)pitCompList->Next()) != NULL) {
267 //     for(Int_t i=0; i<6; i++)
268 //       cObj->MakeDistortionMap(438,i);
269 //   }
270
271   return;
272 }
273
274
275 //________________________________________________________________________
276 TTreeSRedirector *AliMCTrackingTestTask::GetDebugStreamer(){
277   //
278   // Get Debug streamer
279   // In case debug streamer not yet initialized and StreamLevel>0 create new one
280   //
281   if (fStreamLevel==0) return 0;
282   if (fDebugStreamer) return fDebugStreamer;
283   TString dsName;
284   dsName=GetName();
285   dsName+="Debug.root";
286
287   printf(" get debug streamer \n");
288
289   dsName.ReplaceAll(" ","");
290   fDebugStreamer = new TTreeSRedirector(dsName.Data());
291   return fDebugStreamer;
292 }
293
294 //________________________________________________________________________
295 AliExternalTrackParam * AliMCTrackingTestTask::MakeTrack(const AliTrackReference* ref, TParticle*part)
296 {
297   //
298   // Make track out of the track ref
299   // part - TParticle used to determine chargr
300   // the covariance matrix - equal 0 - starting from ideal MC position
301   Double_t xyz[3]={ref->X(),ref->Y(),ref->Z()};
302   Double_t pxyz[3]={ref->Px(),ref->Py(),ref->Pz()};
303   Double_t cv[21];
304   for (Int_t i=0; i<21;i++) cv[i]=0;
305   if (!part->GetPDG()) return 0;
306   AliExternalTrackParam * param = new AliExternalTrackParam(xyz,pxyz,cv,TMath::Nint(part->GetPDG()->Charge()/3.));
307   return param;
308 }
309
310 //________________________________________________________________________
311 Bool_t  AliMCTrackingTestTask::PropagateToPoint(AliExternalTrackParam *param, Double_t *xyz, Double_t mass, Float_t step){
312   // 
313   // Propagate track to point xyz using 
314   // AliTracker::PropagateToBxByBz functionality
315   //
316   //  param - track parameters
317   //  xyz   - position to propagate
318   //  mass  - particle mass
319   //  step  - step to be used
320   Double_t radius=TMath::Sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]);
321   Double_t alpha = TMath::ATan2(xyz[1],xyz[0]);
322
323   AliTracker::PropagateTrackToBxByBz(param, radius+step, mass, step, kTRUE,0.99,-1);
324   param->Rotate(alpha);
325   Bool_t isOK = param->PropagateTo(radius,AliTracker::GetBz());
326
327   return isOK;
328 }
329
330 //________________________________________________________________________
331 void  AliMCTrackingTestTask::ProcessMCInfo(){
332   //
333   //
334   //
335    //
336 #if DEBUG
337   printf("ProcessMCInfo\n");
338 #endif
339
340   const AliESDVertex *pVertex = fESD->GetPrimaryVertex();
341   if(!pVertex) AliError("No primary vertex found!\n");
342   Double_t vPos[3];
343   pVertex->GetXYZ(vPos);
344
345   TParticle * particle= new TParticle();
346   TClonesArray * trefs = new TClonesArray("AliTrackReference");
347   const Double_t kPcut=0.1;
348   //
349   //
350   // Process tracks
351   //
352   Int_t npart = fMCinfo->GetNumberOfTracks();
353   if (npart==0) return;
354   Double_t vertex[4]={0,0,0,0};
355   fMCinfo->GetParticleAndTR(0, particle, trefs);
356   if (particle){
357     vertex[0]=particle->Vx();
358     vertex[1]=particle->Vy();
359     vertex[2]=particle->Vz();
360     vertex[3]=particle->R();
361   }
362   //
363   //
364
365
366   for (Int_t ipart=0;ipart<npart;ipart++){
367     Int_t status = fMCinfo->GetParticleAndTR(ipart, particle, trefs);
368     if (status<0 || !particle || !trefs) continue;
369     Int_t nref = trefs->GetEntries();
370     if (nref<5) continue;
371     FitTrackRefs(particle,trefs);
372
373     AliTrackReference * tpcIn=0;
374     AliTrackReference * tpcOut=0;
375     AliTrackReference * trdIn=0;
376     AliTrackReference * trdOut=0;
377     AliTrackReference * itsIn=0;
378     AliTrackReference * itsOut=0;
379
380     AliTrackReference * tofIn=0;
381     AliTrackReference * tofOut=0;
382     AliTrackReference * hmpidIn=0;
383     AliTrackReference * hmpidOut=0;
384     AliTrackReference * emcalIn=0;
385     AliTrackReference * emcalOut=0;
386
387     Double_t rmax=0;
388     Double_t rmin=1000;
389     for (Int_t iref=0;iref<nref;iref++){
390       AliTrackReference * ref = (AliTrackReference*)trefs->At(iref);
391       if (!ref) continue;
392       
393       Float_t dir = ref->X()*ref->Px()+ref->Y()*ref->Py();
394       
395       if (dir<0) break; // oposite direction - looping track - return back
396       if (ref->P()<kPcut) continue;
397       if (ref->R()<rmax) break;
398       //if (ref->R()<rmin)  break; 
399       //TPC
400       if (ref->DetectorId()==AliTrackReference::kTPC){
401         if (!tpcIn) {
402           tpcIn  = ref;
403         }else{
404           if (ref->R()>tpcIn->R()) tpcOut = ref;
405         }       
406       }
407       //ITS
408       if (ref->DetectorId()==AliTrackReference::kITS){
409         if (!itsIn) {
410           itsIn  = ref;
411         }else{
412           if (ref->R()>itsIn->R()) itsOut = ref;
413         }       
414       }
415       //TRD
416       if (ref->DetectorId()==AliTrackReference::kTRD){
417         if (!trdIn) {
418           trdIn  = ref;
419         }else{
420           if (ref->R()>trdIn->R()) trdOut = ref;
421         }       
422       }      
423       //TOF
424       if (ref->DetectorId()==AliTrackReference::kTOF){
425         if (!tofIn) {
426           tofIn  = ref;
427         }else{
428           if (ref->R()>tofIn->R()) tofOut = ref;
429         }       
430       }      
431
432       //HMPID
433       if (ref->DetectorId()==AliTrackReference::kHMPID){
434         if (!hmpidIn) {
435           hmpidIn  = ref;
436         }else{
437           if (ref->R()>hmpidIn->R()) hmpidOut = ref;
438         }       
439       }      
440
441
442       //EMCAL
443       if (ref->DetectorId()==AliTrackReference::kEMCAL){
444         if (!emcalIn) {
445           emcalIn  = ref;
446         }else{
447           if (ref->R()>emcalIn->R()) emcalOut = ref;
448         }       
449       }      
450
451       if (ref->R()<rmin) rmin=ref->R();
452       if (ref->R()>rmax) rmax=ref->R();
453     } // end ref loop
454
455     // -----------------------------------------------
456     // check for gammas
457     
458     // electron
459     if ( TMath::Abs(particle->GetPdgCode()) == 11 ) {
460
461       // findable
462       if (IsFindable(ipart,70)) {
463
464         // is from gamma conversion
465         Int_t motherId = particle->GetFirstMother();
466         if (motherId > 0) {
467           if (motherId < npart) {
468             TParticle* mother = (fMCinfo->Stack())->Particle(motherId);
469             if (mother && TMath::Abs(mother->GetPdgCode()) == 22) {
470               Int_t nDaughters = mother->GetNDaughters();
471               
472               for (Int_t idx=0; idx<nDaughters; ++idx) {
473                 Int_t daughterId = mother->GetDaughter(idx);
474                 if ( daughterId == ipart || daughterId >= npart )
475                   continue;
476
477                 TParticle* daughter = (fMCinfo->Stack())->Particle(daughterId);
478                 if (daughter && TMath::Abs(daughter->GetPdgCode()) == 11) {
479                   //Bool_t findable = IsFindable(daughterId,70);
480 #if USE_STREAMER
481                   TTreeSRedirector *pcstream = GetDebugStreamer();
482                   if (pcstream){
483                     (*pcstream)<<"MCgamma"<<
484                     "triggerP="<<particle<<      // trigger electron
485                       "motherP="<<mother<<         // mother gamma
486                       "daughterP="<<daughter<<     // daughter electron
487                       "isFindable="<<findable<<     // 2nd is findable
488                       "\n";
489                   }
490 #endif          
491                 } 
492               }
493             }  
494           }
495         }
496       }
497     }
498
499     // -----------------------------------------------
500     if (tpcIn && tpcOut) {
501       ProcessRefTracker(tpcIn,tpcOut,particle,1);
502       ProcessRefTracker(tpcIn,tpcOut,particle,3);
503     }
504     if (itsIn  && itsOut)  ProcessRefTracker(itsIn, itsOut, particle, 0);
505     if (trdIn  && trdOut)  ProcessRefTracker(trdIn, trdOut, particle, 2);
506
507     if (tpcOut && trdIn)   ProcessRefTracker(tpcOut,trdIn,  particle, 4);
508     if (tpcOut && tofIn)   ProcessRefTracker(tpcOut,tofIn,  particle, 5);
509     if (tpcOut && hmpidIn) ProcessRefTracker(tpcOut,hmpidIn,particle, 6);
510     if (tpcOut && emcalIn) ProcessRefTracker(tpcOut,emcalIn,particle, 7);
511
512     if (tpcOut && trdOut)  ProcessRefTracker(tpcOut,trdOut, particle, 8);
513     if (trdIn  && tofIn)   ProcessRefTracker(trdIn, tofIn,  particle, 9);
514     if (tofIn  && tofOut)  ProcessRefTracker(tofIn, tofOut, particle,10);
515
516
517     // -----------------------------------------------
518     //Test for new base tracking class
519           
520     AliTrackComparison *cObj=0;
521     AliTrackPoint *point=new AliTrackPoint();
522     AliESDCaloCluster *cluster=0;
523     AliExternalTrackParam *tpcOuter=0;
524
525     Double_t eMax=0;
526     Int_t clsIndex=-1;
527
528     for(Int_t iCluster=0; iCluster<fESD->GetNumberOfCaloClusters(); iCluster++)
529       {
530         cluster = fESD->GetCaloCluster(iCluster);
531         if(!cluster->IsEMCAL()) continue;
532         if(cluster->GetLabel()==ipart && cluster->E()>eMax)
533           {               
534             clsIndex=iCluster;
535             eMax=cluster->E();
536           }
537       }
538     
539     if(clsIndex>-1)
540       {
541         if(cluster) cluster=0;
542         cluster = fESD->GetCaloCluster(clsIndex);
543         Float_t clusterPos[3];
544         cluster->GetPosition(clusterPos);
545         point->SetXYZ(clusterPos[0],clusterPos[1],clusterPos[2],0);
546       }
547
548     if(tpcOut)
549       tpcOuter = MakeTrack(tpcOut,particle);
550       
551
552     Double_t mass = particle->GetMass();
553     Int_t charge = TMath::Nint(particle->GetPDG()->Charge()/3.);
554     fPitList->Reset();
555     while(( cObj = (AliTrackComparison *)fPitList->Next()) != NULL) {
556       TString objName(cObj->GetName());
557       if(!objName.CompareTo("TPCOutToEMCalInElecCls"))
558         { 
559           if(TMath::Abs(particle->GetPdgCode())==11 && tpcOuter && point && cluster && emcalIn)
560             {
561               printf("\n\nTPCOutToEMCalInElecCls: ");
562               cout<<cObj->AddTracks(tpcOuter,point,mass,cluster->E(),vPos)<<endl;
563             }
564         }
565           
566       if(!objName.CompareTo("TPCOutToEMCalInElec"))
567         {
568           if(TMath::Abs(particle->GetPdgCode())==11 && tpcOut && emcalIn)
569             {
570                printf("TPCOutToEMCalInElec: ");
571                cout<<cObj->AddTracks(tpcOut,emcalIn,mass,charge)<<endl;
572             }
573         }
574
575       if(!objName.CompareTo("TPCOutToEMCalInPion"))
576         {
577           if(TMath::Abs(particle->GetPdgCode())==211 && tpcOut && emcalIn)
578             cObj->AddTracks(tpcOut,emcalIn,mass,charge);
579         }
580
581       if(!objName.CompareTo("TPCOutToTOFIn"))
582         {
583           if(tpcOut && tofIn)
584             cObj->AddTracks(tpcOut,tofIn,mass,charge);
585         }
586
587       if(!objName.CompareTo("TPCOutToHMPIDIn"))
588         {
589           if(tpcOut && hmpidIn)
590             cObj->AddTracks(tpcOut,hmpidIn,mass,charge);
591         }
592     }   
593     //End of the test for new base tracking class
594     // -----------------------------------------------
595     delete point;
596
597   }
598
599   trefs->Clear("C");
600   //delete particle;
601   //delete tpcIn;
602
603 }
604
605 void AliMCTrackingTestTask::ProcessRefTracker(AliTrackReference* refIn,  AliTrackReference* refOut, TParticle*part,Int_t type){
606   //
607   // Test propagation from In to out
608   //
609
610 #if DEBUG
611   printf("ProcessRefTracker\n");
612 #endif
613
614   AliExternalTrackParam *param = 0;
615   AliExternalTrackParam *paramMC = 0;
616   AliExternalTrackParam *paramDebug = 0;
617
618   //  Double_t xyzIn[3]={refIn->X(),refIn->Y(), refIn->Z()};
619   Double_t xyzOut[3]={refOut->X(),refOut->Y(), refOut->Z()};
620   Double_t mass = part->GetMass();
621   Double_t step=1;
622   //
623   param=MakeTrack(refIn,part);
624   paramMC=MakeTrack(refOut,part);
625   paramDebug=MakeTrack(refIn,part);
626
627   if (!param) return;
628   if (type!=3) PropagateToPoint(param,xyzOut, mass, step);
629   //
630 #if 0
631   /*
632     if (type==3) {
633     AliTPCseed seed;
634     seed.Set(param->GetX(),param->GetAlpha(),param->GetParameter(),param->GetCovariance());
635     Float_t alpha= TMath::ATan2(refIn->Y(),refIn->X());
636     seed.Rotate(alpha-seed.GetAlpha());
637     seed.SetMass(mass);
638     for (Float_t xlayer= seed.GetX(); xlayer<refOut->R(); xlayer+=step){
639       seed.PropagateTo(xlayer);
640     }
641     seed.PropagateTo(refOut->R());
642     param->Set(seed.GetX(),seed.GetAlpha(),seed.GetParameter(),seed.GetCovariance());
643   }
644 */
645 #endif
646 #if USE_STREAMER
647   TTreeSRedirector *pcstream = GetDebugStreamer();
648   TVectorD gpos(3);
649   TVectorD gmom(3);
650   Bool_t isOK=kTRUE;
651   isOK&=param->Rotate(paramMC->GetAlpha());
652   isOK&=param->PropagateTo(paramMC->GetX(),AliTracker::GetBz());
653   param->GetXYZ(gpos.GetMatrixArray());
654   param->GetPxPyPz(gmom.GetMatrixArray());
655   if (pcstream){
656     (*pcstream)<<"MC"<<
657       "isOK="<<isOK<<
658       "type="<<type<<              // detector matching type
659       "step="<<step<<              // propagation step length
660       "refIn.="<<refIn<<           // starting track refernce
661       "refOut.="<<refOut<<         // outer track reference
662       "p.="<<part<<                // particle desription (TParticle)
663       "par.="<<param<<             // AliExternalTrackParam create at starting point propagated to outer track ref radius
664       "parMC.="<<paramMC<<         // AliExternalTrackParam created at the outer point  
665       "gpos.="<<&gpos<<            // global position
666       "gmom.="<<&gmom<<            // global momenta
667       "\n";
668   }
669 #endif
670   delete param;
671   delete paramMC;
672   delete paramDebug;
673 }
674
675  
676 Bool_t AliMCTrackingTestTask::IsFindable(Int_t label, Float_t minTrackLength ) {
677   //
678   // Find findable tracks
679   //
680   
681   AliMCParticle *mcParticle = (AliMCParticle*) fMCinfo->GetTrack(label);
682   if(!mcParticle) return kFALSE;
683   
684   Int_t counter; 
685   Float_t tpcTrackLength = mcParticle->GetTPCTrackLength(AliTracker::GetBz(),0.05,counter,3.0); 
686   //printf("tpcTrackLength %f \n", tpcTrackLength);
687   
688   return (tpcTrackLength>minTrackLength);    
689 }
690
691
692 void  AliMCTrackingTestTask::FitTrackRefs(TParticle * part, TClonesArray * trefs){
693   //
694   //
695   //
696   //
697
698 #if DEBUG
699   printf("FitTrackRefs\n");
700 #endif
701
702   const Int_t kMinRefs=6;
703   Int_t nrefs = trefs->GetEntries();
704   if (nrefs<kMinRefs) return; // we should have enough references
705   Int_t iref0 =-1;
706   Int_t iref1 =-1;
707   
708   for (Int_t iref=0; iref<nrefs; iref++){
709     AliTrackReference * ref = (AliTrackReference*)trefs->At(iref);
710     if (!ref) continue;    
711     Float_t dir = ref->X()*ref->Px()+ref->Y()*ref->Py();
712     if (dir<0) break;
713     if (ref->DetectorId()!=AliTrackReference::kTPC) continue;
714     if (iref0<0) iref0 = iref;
715     iref1 = iref;    
716   }
717   if (iref1-iref0<kMinRefs) return;
718   Double_t covar[15];
719   for (Int_t icov=0; icov<15; icov++) covar[icov]=0;
720   covar[0]=1; 
721   covar[2]=1; 
722   covar[5]=1;
723   covar[9]=1;
724   covar[14]=1;
725
726   AliTrackReference * refIn = (AliTrackReference*)trefs->At(iref0);
727   //AliTrackReference * refOut = (AliTrackReference*)trefs->At(iref1);
728   AliExternalTrackParam *paramPropagate= MakeTrack(refIn,part);
729   AliExternalTrackParam *paramUpdate   = MakeTrack(refIn,part);
730   paramUpdate->AddCovariance(covar);
731   //Double_t mass = part->GetMass();
732   //Double_t charge = part->GetPDG()->Charge()/3.;
733 /*
734   Float_t alphaIn= TMath::ATan2(refIn->Y(),refIn->X());
735   Float_t radiusIn= refIn->R();
736   Float_t alphaOut= TMath::ATan2(refOut->Y(),refOut->X());
737   Float_t radiusOut= refOut->R();
738 */
739   Bool_t isOKP=kTRUE;
740   Bool_t isOKU=kTRUE;
741   AliMagF * field = (AliMagF*) TGeoGlobalMagField::Instance()->GetField();
742   for (Int_t iref = iref0; iref<=iref1; iref++){
743     AliTrackReference * ref = (AliTrackReference*)trefs->At(iref);
744     Float_t alphaC= TMath::ATan2(ref->Y(),ref->X());
745     Double_t pos[3] = {ref->X(), ref->Y(), ref->Z()};
746     Double_t mag[3];
747     field->Field(pos,mag);
748     isOKP&=paramPropagate->Rotate(alphaC);
749     isOKU&=paramUpdate->Rotate(alphaC);
750     for (Float_t xref= paramPropagate->GetX(); xref<ref->R(); xref++){
751       isOKP&=paramPropagate->PropagateTo(xref, mag[2]);
752       isOKU&=paramUpdate->PropagateTo(xref, mag[2]);
753     }
754     isOKP&=paramPropagate->PropagateTo(ref->R(), mag[2]);
755     isOKU&=paramUpdate->PropagateTo(ref->R(), mag[2]);
756     Double_t clpos[2] = {0, ref->Z()};
757     Double_t clcov[3] = { 0.005,0,0.005};
758     isOKU&= paramUpdate->Update(clpos, clcov);  
759   }
760 #if USE_STREAMER
761   TTreeSRedirector *pcstream = GetDebugStreamer();
762   if (pcstream){
763     TVectorD gposU(3);
764     TVectorD gmomU(3);
765     TVectorD gposP(3);
766     TVectorD gmomP(3);
767     paramUpdate->GetXYZ(gposU.GetMatrixArray());
768     paramUpdate->GetPxPyPz(gmomU.GetMatrixArray());
769     paramPropagate->GetXYZ(gposP.GetMatrixArray());
770     paramPropagate->GetPxPyPz(gmomP.GetMatrixArray());
771
772      (*pcstream)<<"MCupdate"<<
773        "isOKU="<<isOKU<<
774        "isOKP="<<isOKP<<
775        "m="<<mass<<
776        "q="<<charge<<
777        "part.="<<part<<
778        "refIn.="<<refIn<<
779        "refOut.="<<refOut<<
780        "pP.="<<paramPropagate<<
781        "pU.="<<paramUpdate<<
782        "gposU.="<<&gposU<<
783        "gmomU.="<<&gmomU<<
784        "gposP.="<<&gposP<<
785        "gmomP.="<<&gmomP<<
786        "\n";
787    }
788 #endif
789   delete paramPropagate;
790   delete paramUpdate;
791 }
792
793
794
795
796 void AliMCTrackingTestTask::FinishTaskOutput()
797 {
798   //
799   // According description in AliAnalisysTask this method is call
800   // on the slaves before sending data
801   //
802   Terminate("slave");
803   gSystem->Exec("pwd");
804   RegisterDebugOutput();
805
806 }
807
808
809 void AliMCTrackingTestTask::RegisterDebugOutput(){
810   //
811   //
812   //
813   //
814   // store  - copy debug output to the destination position
815   // currently ONLY for local copy
816   TString dsName;
817   dsName=GetName();
818   dsName+="Debug.root";
819   dsName.ReplaceAll(" ","");
820   TString dsName2=fDebugOutputPath.Data();
821   gSystem->MakeDirectory(dsName2.Data());
822   dsName2+=gSystem->HostName();
823   gSystem->MakeDirectory(dsName2.Data());
824   dsName2+="/";
825   dsName2+=gSystem->BaseName(gSystem->pwd());
826   dsName2+="/";
827   gSystem->MakeDirectory(dsName2.Data());
828   dsName2+=dsName;
829   AliInfo(Form("copy %s\t%s\n",dsName.Data(),dsName2.Data()));
830   printf("copy %s\t%s\n",dsName.Data(),dsName2.Data());
831   TFile::Cp(dsName.Data(),dsName2.Data());
832 }
833
834
835 /*
836   gSystem->AddIncludePath("-I$ALICE_ROOT/TPC/macros");
837   gROOT->LoadMacro("$ALICE_ROOT/TPC/macros/AliXRDPROOFtoolkit.cxx+")
838   AliXRDPROOFtoolkit tool; 
839   TChain * chain = tool.MakeChain("mctracking.txt","MC",0,100);
840   chain->Lookup();
841   //
842   //
843   chain->SetAlias("pdg","(p.fPdgCode)");
844   chain->SetAlias("dPRec","(refOut.P()-par.P())/refIn.P()");
845   chain->SetAlias("dPMC","(refOut.P()-refIn.P())/refIn->P()");
846   chain->SetAlias("dPtRec","(refOut.Pt()-par.Pt())/refIn.Pt()");
847   chain->SetAlias("dPtMC","(refOut.Pt()-refIn.Pt())/refIn->Pt()");
848
849
850   // ITS
851   chain->Draw("-sqrt(dPRec):-sqrt(dPMC)","abs(pdg)!=11&&type==0&&p.Pt()<0.5&&abs(p.R())<1&&abs(refOut.fZ)<220");
852   htemp->SetYTitle("#sqrt{#DeltaP_{rec}/P}");
853   htemp->SetXTitle("#sqrt{#DeltaP_{mc}/P}");
854   gPad->SaveAs("picLoss/dPcorr_ITS_step1.gif");
855   gPad->SaveAs("picLoss/dPcorr_ITS_step1.eps");
856   // TPC
857   chain->Draw("-sqrt(dPRec):-sqrt(dPMC)","abs(pdg)!=11&&type==1&&p.Pt()<0.5&&abs(p.R())<1&&abs(refOut.fZ)<220");
858   htemp->SetYTitle("#sqrt{#DeltaP_{rec}/P}");
859   htemp->SetXTitle("#sqrt{#DeltaP_{mc}/P}");
860   gPad->SaveAs("picLoss/dPcorr_TPC_step1.gif");
861   gPad->SaveAs("picLoss/dPcorr_TPC_step1.eps");
862   //
863    // TPC
864   chain->Draw("-sqrt(dPRec):-sqrt(dPMC)","abs(pdg)!=11&&type==3&&p.Pt()<0.5&&abs(p.R())<1&&abs(refOut.fZ)<220");
865   htemp->SetYTitle("#sqrt{#DeltaP_{rec}/P}");
866   htemp->SetXTitle("#sqrt{#DeltaP_{mc}/P}");
867   gPad->SaveAs("picLoss/dPcorr_TPCseed_step1.gif");
868   gPad->SaveAs("picLoss/dPcorr_TPCseed_step1.eps");
869
870
871   // TRD
872   chain->Draw("-sqrt(dPRec):-sqrt(dPMC)","abs(pdg)!=11&&type==2&&p.Pt()<0.5&&abs(p.R())<1&&abs(refOut.fZ)<220");
873   htemp->SetYTitle("#sqrt{#DeltaP_{rec}/P}");
874   htemp->SetXTitle("#sqrt{#DeltaP_{mc}/P}");
875   gPad->SaveAs("picLoss/dPcorr_TRD_step1.gif");
876   gPad->SaveAs("picLoss/dPcorr_TRD_step1.eps");
877
878   //
879   //
880   //
881   chain->Draw("(par.Pt()-refIn.Pt())/refIn.Pt()>>his(100,-0.02,0.02)","abs(pdg)!=11&&type==3&&p.Pt()<0.5&&abs(p.R())<1&&abs(refOut.fZ)<220");
882   his->SetXTitle("(P_{trec}-P_{tmc})/P_{tmc}");
883   gPad->SaveAs("picLoss/dPtcorr_TPCseed_step1_1D.eps");
884   gPad->SaveAs("picLoss/dPtcorr_TPCseed_step1_1D.gif");
885
886   chain->Draw("(par.P()-refIn.P())/refIn.P()>>his(100,-0.02,0.02)","abs(pdg)!=11&&type==3&&p.Pt()<0.5&&abs(p.R())<1&&abs(refOut.fZ)<220");
887   his->SetXTitle("(P_{rec}-P_{mc})/P_{mc}");
888   gPad->SaveAs("picLoss/dPcorr_TPCseed_step1_1D.eps");
889   gPad->SaveAs("picLoss/dPcorr_TPCseed_step1_1D.gif");
890 */