]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliReconstruction.cxx
Creating the magnetic field map out of the information stored in GRP.
[u/mrichter/AliRoot.git] / STEER / AliReconstruction.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$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // class for running the reconstruction                                      //
21 //                                                                           //
22 // Clusters and tracks are created for all detectors and all events by       //
23 // typing:                                                                   //
24 //                                                                           //
25 //   AliReconstruction rec;                                                  //
26 //   rec.Run();                                                              //
27 //                                                                           //
28 // The Run method returns kTRUE in case of successful execution.             //
29 //                                                                           //
30 // If the input to the reconstruction are not simulated digits but raw data, //
31 // this can be specified by an argument of the Run method or by the method   //
32 //                                                                           //
33 //   rec.SetInput("...");                                                    //
34 //                                                                           //
35 // The input formats and the corresponding argument are:                     //
36 // - DDL raw data files: directory name, ends with "/"                       //
37 // - raw data root file: root file name, extension ".root"                   //
38 // - raw data DATE file: DATE file name, any other non-empty string          //
39 // - MC root files     : empty string, default                               //
40 //                                                                           //
41 // By default all events are reconstructed. The reconstruction can be        //
42 // limited to a range of events by giving the index of the first and the     //
43 // last event as an argument to the Run method or by calling                 //
44 //                                                                           //
45 //   rec.SetEventRange(..., ...);                                            //
46 //                                                                           //
47 // The index -1 (default) can be used for the last event to indicate no      //
48 // upper limit of the event range.                                           //
49 //                                                                           //
50 // In case of raw-data reconstruction the user can modify the default        //
51 // number of events per digits/clusters/tracks file. In case the option      //
52 // is not used the number is set 1. In case the user provides 0, than        //
53 // the number of events is equal to the number of events inside the          //
54 // raw-data file (i.e. one digits/clusters/tracks file):                     //
55 //                                                                           //
56 //   rec.SetNumberOfEventsPerFile(...);                                      //
57 //                                                                           //
58 //                                                                           //
59 // The name of the galice file can be changed from the default               //
60 // "galice.root" by passing it as argument to the AliReconstruction          //
61 // constructor or by                                                         //
62 //                                                                           //
63 //   rec.SetGAliceFile("...");                                               //
64 //                                                                           //
65 // The local reconstruction can be switched on or off for individual         //
66 // detectors by                                                              //
67 //                                                                           //
68 //   rec.SetRunLocalReconstruction("...");                                   //
69 //                                                                           //
70 // The argument is a (case sensitive) string with the names of the           //
71 // detectors separated by a space. The special string "ALL" selects all      //
72 // available detectors. This is the default.                                 //
73 //                                                                           //
74 // The reconstruction of the primary vertex position can be switched off by  //
75 //                                                                           //
76 //   rec.SetRunVertexFinder(kFALSE);                                         //
77 //                                                                           //
78 // The tracking and the creation of ESD tracks can be switched on for        //
79 // selected detectors by                                                     //
80 //                                                                           //
81 //   rec.SetRunTracking("...");                                              //
82 //                                                                           //
83 // Uniform/nonuniform field tracking switches (default: uniform field)       //
84 //                                                                           //
85 //   rec.SetUniformFieldTracking(); ( rec.SetUniformFieldTracking(kFALSE); ) //
86 //                                                                           //
87 // The filling of additional ESD information can be steered by               //
88 //                                                                           //
89 //   rec.SetFillESD("...");                                                  //
90 //                                                                           //
91 // Again, for both methods the string specifies the list of detectors.       //
92 // The default is "ALL".                                                     //
93 //                                                                           //
94 // The call of the shortcut method                                           //
95 //                                                                           //
96 //   rec.SetRunReconstruction("...");                                        //
97 //                                                                           //
98 // is equivalent to calling SetRunLocalReconstruction, SetRunTracking and    //
99 // SetFillESD with the same detector selecting string as argument.           //
100 //                                                                           //
101 // The reconstruction requires digits or raw data as input. For the creation //
102 // of digits and raw data have a look at the class AliSimulation.            //
103 //                                                                           //
104 // The input data of a detector can be replaced by the corresponding HLT     //
105 // data by calling (usual detector string)                                   //
106 // SetUseHLTData("...");                                                     //
107 //                                                                           //
108 // For debug purposes the method SetCheckPointLevel can be used. If the      //
109 // argument is greater than 0, files with ESD events will be written after   //
110 // selected steps of the reconstruction for each event:                      //
111 //   level 1: after tracking and after filling of ESD (final)                //
112 //   level 2: in addition after each tracking step                           //
113 //   level 3: in addition after the filling of ESD for each detector         //
114 // If a final check point file exists for an event, this event will be       //
115 // skipped in the reconstruction. The tracking and the filling of ESD for    //
116 // a detector will be skipped as well, if the corresponding check point      //
117 // file exists. The ESD event will then be loaded from the file instead.     //
118 //                                                                           //
119 ///////////////////////////////////////////////////////////////////////////////
120
121 #include <TArrayF.h>
122 #include <TFile.h>
123 #include <TList.h>
124 #include <TSystem.h>
125 #include <TROOT.h>
126 #include <TPluginManager.h>
127 #include <TGeoManager.h>
128 #include <TLorentzVector.h>
129 #include <TArrayS.h>
130 #include <TArrayD.h>
131 #include <TObjArray.h>
132 #include <TMap.h>
133
134 #include "AliReconstruction.h"
135 #include "AliCodeTimer.h"
136 #include "AliReconstructor.h"
137 #include "AliLog.h"
138 #include "AliRunLoader.h"
139 #include "AliRun.h"
140 #include "AliRawReaderFile.h"
141 #include "AliRawReaderDate.h"
142 #include "AliRawReaderRoot.h"
143 #include "AliRawEventHeaderBase.h"
144 #include "AliESDEvent.h"
145 #include "AliESDMuonTrack.h"
146 #include "AliESDfriend.h"
147 #include "AliESDVertex.h"
148 #include "AliESDcascade.h"
149 #include "AliESDkink.h"
150 #include "AliESDtrack.h"
151 #include "AliESDCaloCluster.h"
152 #include "AliESDCaloCells.h"
153 #include "AliMultiplicity.h"
154 #include "AliTracker.h"
155 #include "AliVertexer.h"
156 #include "AliVertexerTracks.h"
157 #include "AliV0vertexer.h"
158 #include "AliCascadeVertexer.h"
159 #include "AliHeader.h"
160 #include "AliGenEventHeader.h"
161 #include "AliPID.h"
162 #include "AliESDpid.h"
163 #include "AliESDtrack.h"
164 #include "AliESDPmdTrack.h"
165
166 #include "AliESDTagCreator.h"
167 #include "AliAODTagCreator.h"
168
169 #include "AliGeomManager.h"
170 #include "AliTrackPointArray.h"
171 #include "AliCDBManager.h"
172 #include "AliCDBStorage.h"
173 #include "AliCDBEntry.h"
174 #include "AliAlignObj.h"
175
176 #include "AliCentralTrigger.h"
177 #include "AliTriggerConfiguration.h"
178 #include "AliTriggerClass.h"
179 #include "AliCTPRawStream.h"
180
181 #include "AliQADataMakerRec.h" 
182 #include "AliGlobalQADataMaker.h" 
183 #include "AliQA.h"
184 #include "AliQADataMakerSteer.h"
185
186 #include "AliPlaneEff.h"
187
188 #include "AliSysInfo.h" // memory snapshots
189 #include "AliRawHLTManager.h"
190
191 #include "AliMagWrapCheb.h"
192
193 ClassImp(AliReconstruction)
194
195
196 //_____________________________________________________________________________
197 const char* AliReconstruction::fgkDetectorName[AliReconstruction::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
198
199 //_____________________________________________________________________________
200 AliReconstruction::AliReconstruction(const char* gAliceFilename,
201                                      const char* name, const char* title) :
202   TNamed(name, title),
203
204   fUniformField(kFALSE),
205   fForcedFieldMap(0x0),
206   fRunVertexFinder(kTRUE),
207   fRunVertexFinderTracks(kTRUE),
208   fRunHLTTracking(kFALSE),
209   fRunMuonTracking(kFALSE),
210   fRunV0Finder(kTRUE),
211   fRunCascadeFinder(kTRUE),
212   fStopOnError(kFALSE),
213   fWriteAlignmentData(kFALSE),
214   fWriteESDfriend(kFALSE),
215   fWriteAOD(kFALSE),
216   fFillTriggerESD(kTRUE),
217
218   fCleanESD(kTRUE),
219   fV0DCAmax(3.),
220   fV0CsPmin(0.),
221   fDmax(50.),
222   fZmax(50.),
223
224   fRunLocalReconstruction("ALL"),
225   fRunTracking("ALL"),
226   fFillESD("ALL"),
227   fUseTrackingErrorsForAlignment(""),
228   fGAliceFileName(gAliceFilename),
229   fInput(""),
230   fEquipIdMap(""),
231   fFirstEvent(0),
232   fLastEvent(-1),
233   fNumberOfEventsPerFile(1),
234   fCheckPointLevel(0),
235   fOptions(),
236   fLoadAlignFromCDB(kTRUE),
237   fLoadAlignData("ALL"),
238   fESDPar(""),
239   fUseHLTData(),
240
241   fRunLoader(NULL),
242   fRawReader(NULL),
243   fParentRawReader(NULL),
244
245   fVertexer(NULL),
246   fDiamondProfile(NULL),
247   fDiamondProfileTPC(NULL),
248   fMeanVertexConstraint(kTRUE),
249
250   fGRPData(NULL),
251
252   fAlignObjArray(NULL),
253   fCDBUri(),
254   fSpecCDBUri(), 
255   fInitCDBCalled(kFALSE),
256   fSetRunNumberFromDataCalled(kFALSE),
257   fQADetectors("ALL"), 
258   fQATasks("ALL"), 
259   fRunQA(kTRUE),  
260   fRunGlobalQA(kTRUE),
261   fInLoopQA(kFALSE),
262   fSameQACycle(kFALSE),
263
264   fRunPlaneEff(kFALSE),
265
266   fesd(NULL),
267   fhltesd(NULL),
268   fesdf(NULL),
269   ffile(NULL),
270   ftree(NULL),
271   fhlttree(NULL),
272   ffileOld(NULL),
273   ftreeOld(NULL),
274   fhlttreeOld(NULL),
275   ftVertexer(NULL),
276   fIsNewRunLoader(kFALSE),
277   fRunAliEVE(kFALSE)
278 {
279 // create reconstruction object with default parameters
280   
281   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
282     fReconstructor[iDet] = NULL;
283     fLoader[iDet] = NULL;
284     fTracker[iDet] = NULL;
285     fQADataMaker[iDet] = NULL;
286         fQACycles[iDet] = 999999;       
287   }
288   fQADataMaker[fgkNDetectors]=NULL;  //Global QA
289   AliPID pid;
290 }
291
292 //_____________________________________________________________________________
293 AliReconstruction::AliReconstruction(const AliReconstruction& rec) :
294   TNamed(rec),
295
296   fUniformField(rec.fUniformField),
297   fForcedFieldMap(0x0),
298   fRunVertexFinder(rec.fRunVertexFinder),
299   fRunVertexFinderTracks(rec.fRunVertexFinderTracks),
300   fRunHLTTracking(rec.fRunHLTTracking),
301   fRunMuonTracking(rec.fRunMuonTracking),
302   fRunV0Finder(rec.fRunV0Finder),
303   fRunCascadeFinder(rec.fRunCascadeFinder),
304   fStopOnError(rec.fStopOnError),
305   fWriteAlignmentData(rec.fWriteAlignmentData),
306   fWriteESDfriend(rec.fWriteESDfriend),
307   fWriteAOD(rec.fWriteAOD),
308   fFillTriggerESD(rec.fFillTriggerESD),
309
310   fCleanESD(rec.fCleanESD),
311   fV0DCAmax(rec.fV0DCAmax),
312   fV0CsPmin(rec.fV0CsPmin),
313   fDmax(rec.fDmax),
314   fZmax(rec.fZmax),
315
316   fRunLocalReconstruction(rec.fRunLocalReconstruction),
317   fRunTracking(rec.fRunTracking),
318   fFillESD(rec.fFillESD),
319   fUseTrackingErrorsForAlignment(rec.fUseTrackingErrorsForAlignment),
320   fGAliceFileName(rec.fGAliceFileName),
321   fInput(rec.fInput),
322   fEquipIdMap(rec.fEquipIdMap),
323   fFirstEvent(rec.fFirstEvent),
324   fLastEvent(rec.fLastEvent),
325   fNumberOfEventsPerFile(rec.fNumberOfEventsPerFile),
326   fCheckPointLevel(0),
327   fOptions(),
328   fLoadAlignFromCDB(rec.fLoadAlignFromCDB),
329   fLoadAlignData(rec.fLoadAlignData),
330   fESDPar(rec.fESDPar),
331   fUseHLTData(rec.fUseHLTData),
332
333   fRunLoader(NULL),
334   fRawReader(NULL),
335   fParentRawReader(NULL),
336
337   fVertexer(NULL),
338   fDiamondProfile(NULL),
339   fDiamondProfileTPC(NULL),
340   fMeanVertexConstraint(rec.fMeanVertexConstraint),
341
342   fGRPData(NULL),
343
344   fAlignObjArray(rec.fAlignObjArray),
345   fCDBUri(rec.fCDBUri),
346   fSpecCDBUri(), 
347   fInitCDBCalled(rec.fInitCDBCalled),
348   fSetRunNumberFromDataCalled(rec.fSetRunNumberFromDataCalled),
349   fQADetectors(rec.fQADetectors), 
350   fQATasks(rec.fQATasks), 
351   fRunQA(rec.fRunQA),  
352   fRunGlobalQA(rec.fRunGlobalQA),
353   fInLoopQA(rec.fInLoopQA),
354   fSameQACycle(rec.fSameQACycle),
355   fRunPlaneEff(rec.fRunPlaneEff),
356
357   fesd(NULL),
358   fhltesd(NULL),
359   fesdf(NULL),
360   ffile(NULL),
361   ftree(NULL),
362   fhlttree(NULL),
363   ffileOld(NULL),
364   ftreeOld(NULL),
365   fhlttreeOld(NULL),
366   ftVertexer(NULL),
367   fIsNewRunLoader(rec.fIsNewRunLoader),
368   fRunAliEVE(kFALSE)
369 {
370 // copy constructor
371
372   for (Int_t i = 0; i < rec.fOptions.GetEntriesFast(); i++) {
373     if (rec.fOptions[i]) fOptions.Add(rec.fOptions[i]->Clone());
374   }
375   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
376     fReconstructor[iDet] = NULL;
377     fLoader[iDet] = NULL;
378     fTracker[iDet] = NULL;
379     fQADataMaker[iDet] = NULL;
380         fQACycles[iDet] = rec.fQACycles[iDet];  
381   }
382   fQADataMaker[fgkNDetectors]=NULL;  //Global QA
383   for (Int_t i = 0; i < rec.fSpecCDBUri.GetEntriesFast(); i++) {
384     if (rec.fSpecCDBUri[i]) fSpecCDBUri.Add(rec.fSpecCDBUri[i]->Clone());
385   }
386
387   fForcedFieldMap=new AliMagWrapCheb(*((AliMagWrapCheb*)rec.fForcedFieldMap));
388 }
389
390 //_____________________________________________________________________________
391 AliReconstruction& AliReconstruction::operator = (const AliReconstruction& rec)
392 {
393 // assignment operator
394
395   this->~AliReconstruction();
396   new(this) AliReconstruction(rec);
397   return *this;
398 }
399
400 //_____________________________________________________________________________
401 AliReconstruction::~AliReconstruction()
402 {
403 // clean up
404
405   CleanUp();
406   fOptions.Delete();
407   fSpecCDBUri.Delete();
408   delete fForcedFieldMap;
409
410   AliCodeTimer::Instance()->Print();
411 }
412
413 //_____________________________________________________________________________
414 void AliReconstruction::InitCDB()
415 {
416 // activate a default CDB storage
417 // First check if we have any CDB storage set, because it is used 
418 // to retrieve the calibration and alignment constants
419
420   if (fInitCDBCalled) return;
421   fInitCDBCalled = kTRUE;
422
423   AliCDBManager* man = AliCDBManager::Instance();
424   if (man->IsDefaultStorageSet())
425   {
426     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
427     AliWarning("Default CDB storage has been already set !");
428     AliWarning(Form("Ignoring the default storage declared in AliReconstruction: %s",fCDBUri.Data()));
429     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
430     fCDBUri = man->GetDefaultStorage()->GetURI();
431   }
432   else {
433     if (fCDBUri.Length() > 0) 
434     {
435         AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
436         AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
437         AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
438     } else {
439         fCDBUri="local://$ALICE_ROOT";
440         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
441         AliWarning("Default CDB storage not yet set !!!!");
442         AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
443         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
444                 
445     }
446     man->SetDefaultStorage(fCDBUri);
447   }
448
449   // Now activate the detector specific CDB storage locations
450   for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
451     TObject* obj = fSpecCDBUri[i];
452     if (!obj) continue;
453     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
454     AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
455     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
456     man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
457   }
458   
459 }
460
461 //_____________________________________________________________________________
462 void AliReconstruction::SetDefaultStorage(const char* uri) {
463 // Store the desired default CDB storage location
464 // Activate it later within the Run() method
465
466   fCDBUri = uri;
467
468 }
469
470 //_____________________________________________________________________________
471 void AliReconstruction::SetSpecificStorage(const char* calibType, const char* uri) {
472 // Store a detector-specific CDB storage location
473 // Activate it later within the Run() method
474
475   AliCDBPath aPath(calibType);
476   if(!aPath.IsValid()){
477         // if calibType is not wildcard but it is a valid detector, add "/*" to make it a valid path
478         for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
479                 if(!strcmp(calibType, fgkDetectorName[iDet])) {
480                         aPath.SetPath(Form("%s/*", calibType));
481                         AliInfo(Form("Path for specific storage set to %s", aPath.GetPath().Data()));
482                         break;
483                 }
484         }
485         if(!aPath.IsValid()){
486                 AliError(Form("Not a valid path or detector: %s", calibType));
487                 return;
488         }
489   }
490
491 //  // check that calibType refers to a "valid" detector name
492 //  Bool_t isDetector = kFALSE;
493 //  for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
494 //    TString detName = fgkDetectorName[iDet];
495 //    if(aPath.GetLevel0() == detName) {
496 //      isDetector = kTRUE;
497 //      break;
498 //    }
499 //  }
500 //
501 //  if(!isDetector) {
502 //      AliError(Form("Not a valid detector: %s", aPath.GetLevel0().Data()));
503 //      return;
504 //  }
505
506   TObject* obj = fSpecCDBUri.FindObject(aPath.GetPath().Data());
507   if (obj) fSpecCDBUri.Remove(obj);
508   fSpecCDBUri.Add(new TNamed(aPath.GetPath().Data(), uri));
509
510 }
511
512 //_____________________________________________________________________________
513 Bool_t AliReconstruction::SetRunNumberFromData()
514 {
515   // The method is called in Run() in order
516   // to set a correct run number.
517   // In case of raw data reconstruction the
518   // run number is taken from the raw data header
519
520   if (fSetRunNumberFromDataCalled) return kTRUE;
521   fSetRunNumberFromDataCalled = kTRUE;
522   
523   AliCDBManager* man = AliCDBManager::Instance();
524   
525   if(man->GetRun() > 0) {
526         AliWarning("Run number is taken from raw-event header! Ignoring settings in AliCDBManager!");
527   } 
528   
529   if (!fRunLoader) {
530       AliError("No run loader is found !"); 
531       return kFALSE;
532     }
533     // read run number from gAlice
534     if(fRunLoader->GetAliRun())
535       AliCDBManager::Instance()->SetRun(fRunLoader->GetHeader()->GetRun());
536     else {
537       if(fRawReader) {
538         if(fRawReader->NextEvent()) {
539           AliCDBManager::Instance()->SetRun(fRawReader->GetRunNumber());
540           fRawReader->RewindEvents();
541         }
542         else {
543           if(man->GetRun() > 0) {
544             AliWarning("No raw events is found ! Using settings in AliCDBManager !");
545             man->Print();  
546             return kTRUE;
547           }
548           else {
549             AliWarning("Neither raw events nor settings in AliCDBManager are found !");
550             return kFALSE;
551           }
552         }
553       }
554       else {
555         AliError("Neither gAlice nor RawReader objects are found !");
556         return kFALSE;
557       }
558   }
559
560   man->Print();  
561   
562   return kTRUE;
563 }
564
565 //_____________________________________________________________________________
566 void AliReconstruction::SetCDBLock() {
567   // Set CDB lock: from now on it is forbidden to reset the run number
568   // or the default storage or to activate any further storage!
569   
570   AliCDBManager::Instance()->SetLock(1);
571 }
572
573 //_____________________________________________________________________________
574 Bool_t AliReconstruction::MisalignGeometry(const TString& detectors)
575 {
576   // Read the alignment objects from CDB.
577   // Each detector is supposed to have the
578   // alignment objects in DET/Align/Data CDB path.
579   // All the detector objects are then collected,
580   // sorted by geometry level (starting from ALIC) and
581   // then applied to the TGeo geometry.
582   // Finally an overlaps check is performed.
583
584   // Load alignment data from CDB and fill fAlignObjArray 
585   if(fLoadAlignFromCDB){
586         
587     TString detStr = detectors;
588     TString loadAlObjsListOfDets = "";
589     
590     for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
591       if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
592       loadAlObjsListOfDets += fgkDetectorName[iDet];
593       loadAlObjsListOfDets += " ";
594     } // end loop over detectors
595     loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
596     AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
597   }else{
598     // Check if the array with alignment objects was
599     // provided by the user. If yes, apply the objects
600     // to the present TGeo geometry
601     if (fAlignObjArray) {
602       if (gGeoManager && gGeoManager->IsClosed()) {
603         if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
604           AliError("The misalignment of one or more volumes failed!"
605                    "Compare the list of simulated detectors and the list of detector alignment data!");
606           return kFALSE;
607         }
608       }
609       else {
610         AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
611         return kFALSE;
612       }
613     }
614   }
615   
616   delete fAlignObjArray; fAlignObjArray=0;
617
618   return kTRUE;
619 }
620
621 //_____________________________________________________________________________
622 void AliReconstruction::SetGAliceFile(const char* fileName)
623 {
624 // set the name of the galice file
625
626   fGAliceFileName = fileName;
627 }
628
629 //_____________________________________________________________________________
630 void AliReconstruction::SetInput(const char* input) 
631 {
632   // In case the input string starts with 'mem://', we run in an online mode
633   // and AliRawReaderDateOnline object is created. In all other cases a raw-data
634   // file is assumed. One can give as an input:
635   // mem://: - events taken from DAQ monitoring libs online
636   //  or
637   // mem://<filename> - emulation of the above mode (via DATE monitoring libs)
638   fInput = input;
639 }
640
641 //_____________________________________________________________________________
642 void AliReconstruction::SetOption(const char* detector, const char* option)
643 {
644 // set options for the reconstruction of a detector
645
646   TObject* obj = fOptions.FindObject(detector);
647   if (obj) fOptions.Remove(obj);
648   fOptions.Add(new TNamed(detector, option));
649 }
650
651 //_____________________________________________________________________________
652 Bool_t AliReconstruction::ForceFieldMap(Float_t l3Current, Float_t diCurrent, Float_t factor, Char_t *path) {
653   //------------------------------------------------
654   // The magnetic field map, defined externally...
655   // L3 current 30000 A  -> 0.5 T
656   // L3 current 12000 A  -> 0.2 T
657   // dipole current 6000 A
658   // The polarities must be the same
659   //------------------------------------------------
660   const Float_t l3NominalCurrent1=30000.; // (A)
661   const Float_t l3NominalCurrent2=12000.; // (A)
662   const Float_t diNominalCurrent =6000. ; // (A)
663
664   const Float_t tolerance=0.03; // relative current tolerance
665   const Float_t zero=77.;       // "zero" current (A)
666
667   Int_t map=0;
668   Bool_t dipoleON=kFALSE;
669
670   TString s=(l3Current < 0) ? "L3: -" : "L3: +";
671
672   if (TMath::Abs(l3Current-l3NominalCurrent1)/l3NominalCurrent1 < tolerance) {
673     map=AliMagWrapCheb::k5kG;
674     s+="0.5 T;  ";
675   } else
676   if (TMath::Abs(l3Current-l3NominalCurrent2)/l3NominalCurrent2 < tolerance) {
677     map=AliMagWrapCheb::k2kG;
678     s+="0.2 T;  ";
679   } else
680   if (TMath::Abs(l3Current) < zero) {
681     map=AliMagWrapCheb::k2kG;
682     s+="0.0 T;  ";
683     factor=0.;                  // in fact, this is a global factor...
684   } else {
685     AliError("Wrong L3 current !");
686     return kFALSE;
687   }
688
689   if (TMath::Abs(diCurrent-diNominalCurrent)/diNominalCurrent < tolerance) {
690     // 3% current tolerance...
691     dipoleON=kTRUE;
692     s+="Dipole ON";
693   } else
694   if (TMath::Abs(diCurrent) < zero) { // some small current..
695     dipoleON=kFALSE;
696     s+="Dipole OFF";
697   } else {
698     AliError("Wrong dipole current !");
699     return kFALSE;
700   }
701
702   delete fForcedFieldMap;
703   fForcedFieldMap=
704     new AliMagWrapCheb("B field map  ",s,2,factor,10.,map,dipoleON,path);
705
706   fForcedFieldMap->Print();
707
708   AliTracker::SetFieldMap(fForcedFieldMap,fUniformField);    
709
710   return kTRUE;
711 }
712
713
714 Bool_t AliReconstruction::InitGRP() {
715   //------------------------------------
716   // Initialization of the GRP entry 
717   //------------------------------------
718   AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/GRP/Data");
719
720   if (entry) fGRPData = dynamic_cast<TMap*>(entry->GetObject());  
721
722   if (!fGRPData) {
723      AliError("No GRP entry found in OCDB!");
724      return kFALSE;
725   }
726
727
728   //*** Dealing with the magnetic field map
729   if (AliTracker::GetFieldMap()) {
730     AliInfo("Running with the externally set B field !");
731   } else {
732     // Construct the field map out of the information retrieved from GRP.
733
734     // L3
735     TObjString *l3Current=
736        dynamic_cast<TObjString*>(fGRPData->GetValue("fL3Current"));
737     if (!l3Current) {
738       AliError("GRP/GRP/Data entry:  missing value for the L3 current !");
739       return kFALSE;
740     }
741     TObjString *l3Polarity=
742        dynamic_cast<TObjString*>(fGRPData->GetValue("fL3Polarity"));
743     if (!l3Polarity) {
744       AliError("GRP/GRP/Data entry:  missing value for the L3 polarity !");
745       return kFALSE;
746     }
747
748     // Dipole
749     TObjString *diCurrent=
750        dynamic_cast<TObjString*>(fGRPData->GetValue("fDipoleCurrent"));
751     if (!diCurrent) {
752       AliError("GRP/GRP/Data entry:  missing value for the dipole current !");
753       return kFALSE;
754     }
755     TObjString *diPolarity=
756        dynamic_cast<TObjString*>(fGRPData->GetValue("fDipolePolarity"));
757     if (!diPolarity) {
758       AliError("GRP/GRP/Data entry:  missing value for the dipole polarity !");
759       return kFALSE;
760     }
761
762     Float_t l3Cur=atof(l3Current->GetName());
763     Float_t diCur=atof(diCurrent->GetName());
764
765     if (!ForceFieldMap(l3Cur, diCur)) {
766        AliFatal("Failed to creat a B field map ! Crashing...");
767     }
768
769     AliInfo("Running with the B field constructed out of GRP !");
770   }
771
772
773   //*** Get the diamond profile from OCDB
774   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertex");
775   if (entry) {
776      fDiamondProfile = dynamic_cast<AliESDVertex*> (entry->GetObject());  
777   } else {
778      AliError("No diamond profile found in OCDB!");
779   }
780
781   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexTPC");
782   if (entry) {
783      fDiamondProfileTPC = dynamic_cast<AliESDVertex*> (entry->GetObject());  
784   } else {
785      AliError("No diamond profile found in OCDB!");
786   }
787
788   return kTRUE;
789
790
791 //_____________________________________________________________________________
792 Bool_t AliReconstruction::Run(const char* input)
793 {
794   // Run Run Run
795   AliCodeTimerAuto("");
796
797   if (!InitRun(input)) return kFALSE;
798   //******* The loop over events
799   Int_t iEvent = 0;
800   while ((iEvent < fRunLoader->GetNumberOfEvents()) ||
801          (fRawReader && fRawReader->NextEvent())) {
802     if (!RunEvent(iEvent)) return kFALSE;
803     iEvent++;
804   }
805
806   if (!FinishRun()) return kFALSE;
807
808   return kTRUE;
809 }
810
811 //_____________________________________________________________________________
812 Bool_t AliReconstruction::InitRun(const char* input)
813 {
814   // Initialize all the stuff before
815   // going into the event loop
816   // If the second argument is given, the first one is ignored and
817   // the reconstruction works in an online mode
818   AliCodeTimerAuto("");
819
820   // Overwrite the previous setting
821   if (input) fInput = input;
822
823   // set the input in case of raw data
824   fRawReader = AliRawReader::Create(fInput.Data());
825   if (!fRawReader)
826     AliInfo("Reconstruction will run over digits");
827
828   if (!fEquipIdMap.IsNull() && fRawReader)
829     fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
830
831   if (!fUseHLTData.IsNull()) {
832     // create the RawReaderHLT which performs redirection of HLT input data for
833     // the specified detectors
834     AliRawReader* pRawReader=AliRawHLTManager::CreateRawReaderHLT(fRawReader, fUseHLTData.Data());
835     if (pRawReader) {
836       fParentRawReader=fRawReader;
837       fRawReader=pRawReader;
838     } else {
839       AliError(Form("can not create Raw Reader for HLT input %s", fUseHLTData.Data()));
840     }
841   }
842
843    AliSysInfo::AddStamp("Start");
844   // get the run loader
845   if (!InitRunLoader()) return kFALSE;
846    AliSysInfo::AddStamp("LoadLoader");
847
848   // Initialize the CDB storage
849   InitCDB();
850   
851   AliSysInfo::AddStamp("LoadCDB");
852
853   // Set run number in CDBManager (if it is not already set by the user)
854   if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
855   
856   // Set CDB lock: from now on it is forbidden to reset the run number
857   // or the default storage or to activate any further storage!
858   SetCDBLock();
859   
860   // Import ideal TGeo geometry and apply misalignment
861   if (!gGeoManager) {
862     TString geom(gSystem->DirName(fGAliceFileName));
863     geom += "/geometry.root";
864     AliGeomManager::LoadGeometry(geom.Data());
865
866     TString detsToCheck=fRunLocalReconstruction;
867     if(!AliGeomManager::CheckSymNamesLUT(detsToCheck.Data()))
868           AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
869     if (!gGeoManager) if (fStopOnError) return kFALSE;
870   }
871
872   if (!MisalignGeometry(fLoadAlignData)) if (fStopOnError) return kFALSE;
873    AliSysInfo::AddStamp("LoadGeom");
874
875
876   if (!InitGRP()) return kFALSE;
877
878
879   ftVertexer = new AliVertexerTracks(AliTracker::GetBz());
880   if(fDiamondProfile && fMeanVertexConstraint) ftVertexer->SetVtxStart(fDiamondProfile);
881
882   // get vertexer
883   if (fRunVertexFinder && !CreateVertexer()) {
884     if (fStopOnError) {
885       CleanUp(); 
886       return kFALSE;
887     }
888   }
889    AliSysInfo::AddStamp("Vertexer");
890
891   // get trackers
892   if (!fRunTracking.IsNull() && !CreateTrackers(fRunTracking)) {
893     if (fStopOnError) {
894       CleanUp(); 
895       return kFALSE;
896     }      
897   }
898    AliSysInfo::AddStamp("LoadTrackers");
899
900   // get the possibly already existing ESD file and tree
901   fesd = new AliESDEvent(); fhltesd = new AliESDEvent();
902   if (!gSystem->AccessPathName("AliESDs.root")){
903     gSystem->CopyFile("AliESDs.root", "AliESDs.old.root", kTRUE);
904     ffileOld = TFile::Open("AliESDs.old.root");
905     if (ffileOld && ffileOld->IsOpen()) {
906       ftreeOld = (TTree*) ffileOld->Get("esdTree");
907       if (ftreeOld)fesd->ReadFromTree(ftreeOld);
908       fhlttreeOld = (TTree*) ffileOld->Get("HLTesdTree");
909       if (fhlttreeOld)  fhltesd->ReadFromTree(fhlttreeOld);
910     }
911   }
912
913   // create the ESD output file and tree
914   ffile = TFile::Open("AliESDs.root", "RECREATE");
915   ffile->SetCompressionLevel(2);
916   if (!ffile->IsOpen()) {
917     AliError("opening AliESDs.root failed");
918     if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}    
919   }
920
921   ftree = new TTree("esdTree", "Tree with ESD objects");
922   fesd = new AliESDEvent();
923   fesd->CreateStdContent();
924   fesd->WriteToTree(ftree);
925
926   fhlttree = new TTree("HLTesdTree", "Tree with HLT ESD objects");
927   fhltesd = new AliESDEvent();
928   fhltesd->CreateStdContent();
929   fhltesd->WriteToTree(fhlttree);
930
931
932   if (fWriteESDfriend) {
933     fesdf = new AliESDfriend();
934     TBranch *br=ftree->Branch("ESDfriend.","AliESDfriend", &fesdf);
935     br->SetFile("AliESDfriends.root");
936     fesd->AddObject(fesdf);
937   }
938
939
940
941   if (fRawReader) fRawReader->RewindEvents();
942
943   ProcInfo_t ProcInfo;
944   gSystem->GetProcInfo(&ProcInfo);
945   AliInfo(Form("Current memory usage %d %d", ProcInfo.fMemResident, ProcInfo.fMemVirtual));
946   
947   //QA
948   if (fRunQA && fRawReader && fQATasks.Contains(Form("%d", AliQA::kRAWS))) { 
949                 AliQADataMakerSteer qas ; 
950                 qas.Run(fRunLocalReconstruction, fRawReader) ; 
951                 fSameQACycle = kTRUE ; 
952   }
953   //Initialize the QA and start of cycle for out-of-cycle QA
954   if (fRunQA) {
955           TString detStr(fQADetectors) ; 
956       for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
957          if (!IsSelected(fgkDetectorName[iDet], detStr)) 
958                          continue;
959          AliQADataMakerRec *qadm = GetQADataMaker(iDet);  
960          if (!qadm) 
961                          continue;
962          AliInfo(Form("Initializing the QA data maker for %s", 
963                 fgkDetectorName[iDet]));
964                  if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) 
965                          qadm->Init(AliQA::kRECPOINTS, AliCDBManager::Instance()->GetRun());
966                  if (fQATasks.Contains(Form("%d", AliQA::kESDS)))  
967                          qadm->Init(AliQA::kESDS, AliCDBManager::Instance()->GetRun());
968          if (!fInLoopQA) {
969                          if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
970                                  qadm->StartOfCycle(AliQA::kRECPOINTS, fSameQACycle);
971                                  fSameQACycle = kTRUE;
972                          }
973                          if (fQATasks.Contains(Form("%d", AliQA::kESDS))) {  
974                                  qadm->StartOfCycle(AliQA::kESDS, fSameQACycle);
975                                  fSameQACycle = kTRUE;
976                          }
977                  }
978       }
979           if (fRunGlobalQA) {
980                   AliQADataMakerRec *qadm = GetQADataMaker(AliQA::kGLOBAL);
981                   AliInfo(Form("Initializing the global QA data maker"));
982                   if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
983                           TObjArray *arr=
984                                 qadm->Init(AliQA::kRECPOINTS, AliCDBManager::Instance()->GetRun());
985                           AliTracker::SetResidualsArray(arr);
986                   }
987                   if (fQATasks.Contains(Form("%d", AliQA::kESDS))) {
988                           qadm->Init(AliQA::kESDS, AliCDBManager::Instance()->GetRun());
989                   }
990                   if (!fInLoopQA) {
991                           fSameQACycle = kFALSE;
992                           if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) {                                 
993                                   qadm->StartOfCycle(AliQA::kRECPOINTS, fSameQACycle);
994                                   fSameQACycle = kTRUE;
995                           }
996                           if (fQATasks.Contains(Form("%d", AliQA::kESDS))) { 
997                                   qadm->StartOfCycle(AliQA::kESDS, fSameQACycle);
998                                   fSameQACycle = kTRUE; 
999                           }
1000                   }
1001           }
1002   }
1003
1004   //Initialize the Plane Efficiency framework
1005   if (fRunPlaneEff && !InitPlaneEff()) {
1006     if(fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1007   }
1008
1009   if (strcmp(gProgName,"alieve") == 0)
1010     fRunAliEVE = InitAliEVE();
1011
1012   return kTRUE;
1013 }
1014
1015 //_____________________________________________________________________________
1016 Bool_t AliReconstruction::RunEvent(Int_t iEvent)
1017 {
1018   // run the reconstruction over a single event
1019   // The event loop is steered in Run method
1020
1021   AliCodeTimerAuto("");
1022
1023   if (iEvent >= fRunLoader->GetNumberOfEvents()) {
1024     fRunLoader->SetEventNumber(iEvent);
1025     fRunLoader->GetHeader()->Reset(fRawReader->GetRunNumber(), 
1026                                    iEvent, iEvent);
1027     //??      fRunLoader->MakeTree("H");
1028     fRunLoader->TreeE()->Fill();
1029   }
1030
1031   if ((iEvent < fFirstEvent) || ((fLastEvent >= 0) && (iEvent > fLastEvent))) {
1032     // copy old ESD to the new one
1033     if (ftreeOld) {
1034       fesd->ReadFromTree(ftreeOld);
1035       ftreeOld->GetEntry(iEvent);
1036       ftree->Fill();
1037     }
1038     if (fhlttreeOld) {
1039       fhltesd->ReadFromTree(fhlttreeOld);
1040       fhlttreeOld->GetEntry(iEvent);
1041       fhlttree->Fill();
1042     }
1043     return kTRUE;
1044   }
1045
1046   AliInfo(Form("processing event %d", iEvent));
1047
1048     //Start of cycle for the in-loop QA
1049     if (fInLoopQA) {
1050        if (fRunQA) {
1051                   fSameQACycle = kFALSE ;
1052           TString detStr(fQADetectors); 
1053           for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1054              if (!IsSelected(fgkDetectorName[iDet], detStr)) 
1055                                  continue;
1056              AliQADataMakerRec *qadm = GetQADataMaker(iDet);  
1057              if (!qadm) 
1058                                  continue;
1059                           if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
1060                                   qadm->StartOfCycle(AliQA::kRECPOINTS, fSameQACycle);
1061                                   fSameQACycle = kTRUE;
1062                           }
1063                           if (fQATasks.Contains(Form("%d", AliQA::kESDS))) {
1064                                   qadm->StartOfCycle(AliQA::kESDS, fSameQACycle) ;
1065                                   fSameQACycle = kTRUE;
1066                           }
1067           }
1068                    if (fRunGlobalQA) {
1069                            fSameQACycle = kFALSE;
1070                            AliQADataMakerRec *qadm = GetQADataMaker(AliQA::kGLOBAL);
1071                            if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) {
1072                                    qadm->StartOfCycle(AliQA::kRECPOINTS, fSameQACycle);
1073                                    fSameQACycle = kTRUE;
1074                            }
1075                            if (fQATasks.Contains(Form("%d", AliQA::kESDS))) {
1076                                    qadm->StartOfCycle(AliQA::kESDS, fSameQACycle);
1077                                    fSameQACycle = kTRUE;
1078                            }
1079                    }               
1080            }
1081     }
1082
1083     fRunLoader->GetEvent(iEvent);
1084
1085     char aFileName[256];
1086     sprintf(aFileName, "ESD_%d.%d_final.root", 
1087             fRunLoader->GetHeader()->GetRun(), 
1088             fRunLoader->GetHeader()->GetEventNrInRun());
1089     if (!gSystem->AccessPathName(aFileName)) return kTRUE;
1090
1091     // local single event reconstruction
1092     if (!fRunLocalReconstruction.IsNull()) {
1093       TString detectors=fRunLocalReconstruction;
1094       // run HLT event reconstruction first
1095       // ;-( IsSelected changes the string
1096       if (IsSelected("HLT", detectors) &&
1097           !RunLocalEventReconstruction("HLT")) {
1098         if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1099       }
1100       detectors=fRunLocalReconstruction;
1101       detectors.ReplaceAll("HLT", "");
1102       if (!RunLocalEventReconstruction(detectors)) {
1103         if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1104       }
1105     }
1106
1107     fesd->SetRunNumber(fRunLoader->GetHeader()->GetRun());
1108     fhltesd->SetRunNumber(fRunLoader->GetHeader()->GetRun());
1109     fesd->SetEventNumberInFile(fRunLoader->GetHeader()->GetEventNrInRun());
1110     fhltesd->SetEventNumberInFile(fRunLoader->GetHeader()->GetEventNrInRun());
1111     
1112     // Set magnetic field from the tracker
1113     fesd->SetMagneticField(AliTracker::GetBz());
1114     fhltesd->SetMagneticField(AliTracker::GetBz());
1115
1116     
1117     
1118     // Fill raw-data error log into the ESD
1119     if (fRawReader) FillRawDataErrorLog(iEvent,fesd);
1120
1121     // vertex finder
1122     if (fRunVertexFinder) {
1123       if (!ReadESD(fesd, "vertex")) {
1124         if (!RunVertexFinder(fesd)) {
1125           if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1126         }
1127         if (fCheckPointLevel > 0) WriteESD(fesd, "vertex");
1128       }
1129     }
1130
1131     // Muon tracking
1132     if (!fRunTracking.IsNull()) {
1133       if (fRunMuonTracking) {
1134         if (!RunMuonTracking(fesd)) {
1135           if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1136         }
1137       }
1138     }
1139
1140     // barrel tracking
1141     if (!fRunTracking.IsNull()) {
1142       if (!ReadESD(fesd, "tracking")) {
1143         if (!RunTracking(fesd)) {
1144           if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1145         }
1146         if (fCheckPointLevel > 0) WriteESD(fesd, "tracking");
1147       }
1148     }
1149
1150     // fill ESD
1151     if (!fFillESD.IsNull()) {
1152       TString detectors=fFillESD;
1153       // run HLT first and on hltesd
1154       // ;-( IsSelected changes the string
1155       if (IsSelected("HLT", detectors) &&
1156           !FillESD(fhltesd, "HLT")) {
1157         if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1158       }
1159       detectors=fFillESD;
1160       // Temporary fix to avoid problems with HLT that overwrites the offline ESDs
1161       if (detectors.Contains("ALL")) {
1162         detectors="";
1163         for (Int_t idet=0; idet<fgkNDetectors; ++idet){
1164           detectors += fgkDetectorName[idet];
1165           detectors += " ";
1166         }
1167       }
1168       detectors.ReplaceAll("HLT", "");
1169       if (!FillESD(fesd, detectors)) {
1170         if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1171       }
1172     }
1173   
1174     // fill Event header information from the RawEventHeader
1175     if (fRawReader){FillRawEventHeaderESD(fesd);}
1176
1177     // combined PID
1178     AliESDpid::MakePID(fesd);
1179     if (fCheckPointLevel > 1) WriteESD(fesd, "PID");
1180
1181     if (fFillTriggerESD) {
1182       if (!ReadESD(fesd, "trigger")) {
1183         if (!FillTriggerESD(fesd)) {
1184           if (fStopOnError) {CleanUp(ffile, ffileOld); return kFALSE;}
1185         }
1186         if (fCheckPointLevel > 1) WriteESD(fesd, "trigger");
1187       }
1188     }
1189
1190     ffile->cd();
1191
1192     //
1193     // Propagate track to the beam pipe  (if not already done by ITS)
1194     //
1195     const Int_t ntracks = fesd->GetNumberOfTracks();
1196     const Double_t kBz = fesd->GetMagneticField();
1197     const Double_t kRadius  = 2.8; //something less than the beam pipe radius
1198
1199     TObjArray trkArray;
1200     UShort_t *selectedIdx=new UShort_t[ntracks];
1201
1202     for (Int_t itrack=0; itrack<ntracks; itrack++){
1203       const Double_t kMaxStep = 5;   //max step over the material
1204       Bool_t ok;
1205
1206       AliESDtrack *track = fesd->GetTrack(itrack);
1207       if (!track) continue;
1208
1209       AliExternalTrackParam *tpcTrack =
1210            (AliExternalTrackParam *)track->GetTPCInnerParam();
1211       ok = kFALSE;
1212       if (tpcTrack)
1213         ok = AliTracker::
1214           PropagateTrackTo(tpcTrack,kRadius,track->GetMass(),kMaxStep,kTRUE);
1215
1216       if (ok) {
1217         Int_t n=trkArray.GetEntriesFast();
1218         selectedIdx[n]=track->GetID();
1219         trkArray.AddLast(tpcTrack);
1220       }
1221
1222       //Tracks refitted by ITS should already be at the SPD vertex
1223       if (track->IsOn(AliESDtrack::kITSrefit)) continue;
1224
1225       AliTracker::
1226          PropagateTrackTo(track,kRadius,track->GetMass(),kMaxStep,kTRUE);
1227       track->RelateToVertex(fesd->GetPrimaryVertexSPD(), kBz, kVeryBig);
1228
1229     }
1230
1231     //
1232     // Improve the reconstructed primary vertex position using the tracks
1233     //
1234     TObject *obj = fOptions.FindObject("ITS");
1235     if (obj) {
1236       TString optITS = obj->GetTitle();
1237       if (optITS.Contains("cosmics") || optITS.Contains("COSMICS")) 
1238         fRunVertexFinderTracks=kFALSE;
1239     }
1240     if (fRunVertexFinderTracks) {
1241        // TPC + ITS primary vertex
1242        ftVertexer->SetITSrefitRequired();
1243        if(fDiamondProfile && fMeanVertexConstraint) {
1244          ftVertexer->SetVtxStart(fDiamondProfile);
1245        } else {
1246          ftVertexer->SetConstraintOff();
1247        }
1248        AliESDVertex *pvtx=ftVertexer->FindPrimaryVertex(fesd);
1249        if (pvtx) {
1250           if (pvtx->GetStatus()) {
1251              fesd->SetPrimaryVertex(pvtx);
1252              for (Int_t i=0; i<ntracks; i++) {
1253                  AliESDtrack *t = fesd->GetTrack(i);
1254                  t->RelateToVertex(pvtx, kBz, kVeryBig);
1255              } 
1256           }
1257        }
1258
1259        // TPC-only primary vertex
1260        ftVertexer->SetITSrefitNotRequired();
1261        if(fDiamondProfileTPC && fMeanVertexConstraint) {
1262          ftVertexer->SetVtxStart(fDiamondProfileTPC);
1263        } else {
1264          ftVertexer->SetConstraintOff();
1265        }
1266        pvtx=ftVertexer->FindPrimaryVertex(&trkArray,selectedIdx);
1267        if (pvtx) {
1268           if (pvtx->GetStatus()) {
1269              fesd->SetPrimaryVertexTPC(pvtx);
1270              for (Int_t i=0; i<ntracks; i++) {
1271                  AliESDtrack *t = fesd->GetTrack(i);
1272                  t->RelateToVertexTPC(pvtx, kBz, kVeryBig);
1273              } 
1274           }
1275        }
1276
1277     }
1278     delete[] selectedIdx;
1279
1280     if(fDiamondProfile) fesd->SetDiamond(fDiamondProfile);
1281     
1282
1283     if (fRunV0Finder) {
1284        // V0 finding
1285        AliV0vertexer vtxer;
1286        vtxer.Tracks2V0vertices(fesd);
1287
1288        if (fRunCascadeFinder) {
1289           // Cascade finding
1290           AliCascadeVertexer cvtxer;
1291           cvtxer.V0sTracks2CascadeVertices(fesd);
1292        }
1293     }
1294  
1295     // write ESD
1296     if (fCleanESD) CleanESD(fesd);
1297
1298         if (fRunQA) {
1299                 if (fRunGlobalQA) {
1300                         AliQADataMakerRec *qadm = GetQADataMaker(AliQA::kGLOBAL);
1301                         if (qadm && fQATasks.Contains(Form("%d", AliQA::kESDS)))
1302                                 qadm->Exec(AliQA::kESDS, fesd);
1303                 }
1304         }
1305
1306     if (fWriteESDfriend) {
1307       fesdf->~AliESDfriend();
1308       new (fesdf) AliESDfriend(); // Reset...
1309       fesd->GetESDfriend(fesdf);
1310     }
1311     ftree->Fill();
1312
1313     // write HLT ESD
1314     fhlttree->Fill();
1315
1316     // call AliEVE
1317     if (fRunAliEVE) RunAliEVE();
1318
1319     if (fCheckPointLevel > 0)  WriteESD(fesd, "final"); 
1320     fesd->Reset();
1321     fhltesd->Reset();
1322     if (fWriteESDfriend) {
1323       fesdf->~AliESDfriend();
1324       new (fesdf) AliESDfriend(); // Reset...
1325     }
1326  
1327     ProcInfo_t ProcInfo;
1328     gSystem->GetProcInfo(&ProcInfo);
1329     AliInfo(Form("Event %d -> Current memory usage %d %d",iEvent, ProcInfo.fMemResident, ProcInfo.fMemVirtual));
1330   
1331
1332   // End of cycle for the in-loop  
1333      if (fInLoopQA) {
1334         if (fRunQA) {
1335            RunQA(fesd);
1336            for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1337                            if (!IsSelected(fgkDetectorName[iDet], fQADetectors)) 
1338                                    continue;
1339                            AliQADataMakerRec * qadm = GetQADataMaker(iDet);
1340                            if (!qadm)
1341                                    continue;
1342                            if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) 
1343                                    qadm->EndOfCycle(AliQA::kRECPOINTS);
1344                            if (fQATasks.Contains(Form("%d", AliQA::kESDS))) 
1345                                    qadm->EndOfCycle(AliQA::kESDS);
1346                            qadm->Finish();
1347                    }
1348         }
1349         if (fRunGlobalQA) {
1350            AliQADataMakerRec *qadm = GetQADataMaker(AliQA::kGLOBAL);
1351            if (qadm) {
1352                            if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) 
1353                                    qadm->EndOfCycle(AliQA::kRECPOINTS);
1354                            if (fQATasks.Contains(Form("%d", AliQA::kESDS))) 
1355                                    qadm->EndOfCycle(AliQA::kESDS);
1356                            qadm->Finish();
1357                    }
1358         }
1359      }
1360
1361      return kTRUE;
1362 }
1363
1364 //_____________________________________________________________________________
1365 Bool_t AliReconstruction::FinishRun()
1366 {
1367   // Finalize the run
1368   // Called after the exit
1369   // from the event loop
1370   AliCodeTimerAuto("");
1371
1372   if (fIsNewRunLoader) { // galice.root didn't exist
1373     fRunLoader->WriteHeader("OVERWRITE");
1374     fRunLoader->CdGAFile();
1375     fRunLoader->Write(0, TObject::kOverwrite);
1376   }
1377
1378   ftree->GetUserInfo()->Add(fesd);
1379   fhlttree->GetUserInfo()->Add(fhltesd);
1380   
1381   const TMap *cdbMap = AliCDBManager::Instance()->GetStorageMap();       
1382   const TList *cdbList = AliCDBManager::Instance()->GetRetrievedIds();   
1383                  
1384    TMap *cdbMapCopy = new TMap(cdbMap->GetEntries());    
1385    cdbMapCopy->SetOwner(1);      
1386    cdbMapCopy->SetName("cdbMap");        
1387    TIter iter(cdbMap->GetTable());       
1388          
1389    TPair* pair = 0;      
1390    while((pair = dynamic_cast<TPair*> (iter.Next()))){   
1391          TObjString* keyStr = dynamic_cast<TObjString*> (pair->Key());   
1392          TObjString* valStr = dynamic_cast<TObjString*> (pair->Value());         
1393          cdbMapCopy->Add(new TObjString(keyStr->GetName()), new TObjString(valStr->GetName()));  
1394    }     
1395          
1396    TList *cdbListCopy = new TList();     
1397    cdbListCopy->SetOwner(1);     
1398    cdbListCopy->SetName("cdbList");      
1399          
1400    TIter iter2(cdbList);         
1401          
1402    AliCDBId* id=0;       
1403    while((id = dynamic_cast<AliCDBId*> (iter2.Next()))){         
1404          cdbListCopy->Add(new TObjString(id->ToString().Data()));        
1405    }     
1406          
1407    ftree->GetUserInfo()->Add(cdbMapCopy);        
1408    ftree->GetUserInfo()->Add(cdbListCopy);
1409
1410
1411   if(fESDPar.Contains("ESD.par")){
1412     AliInfo("Attaching ESD.par to Tree");
1413     TNamed *fn = CopyFileToTNamed(fESDPar.Data(),"ESD.par");
1414     ftree->GetUserInfo()->Add(fn);
1415   }
1416
1417
1418   ffile->cd();
1419
1420   if (fWriteESDfriend)
1421     ftree->SetBranchStatus("ESDfriend*",0);
1422   // we want to have only one tree version number
1423   ftree->Write(ftree->GetName(),TObject::kOverwrite);
1424   fhlttree->Write();
1425
1426 // Finish with Plane Efficiency evaluation: before of CleanUp !!!
1427   if (fRunPlaneEff && !FinishPlaneEff()) {
1428    AliWarning("Finish PlaneEff evaluation failed");
1429   }
1430
1431   gROOT->cd();
1432   CleanUp(ffile, ffileOld);
1433     
1434   if (fWriteAOD) {
1435     AliWarning("AOD creation not supported anymore during reconstruction. See ANALYSIS/AliAnalysisTaskESDfilter.cxx instead.");
1436   }
1437
1438   // Create tags for the events in the ESD tree (the ESD tree is always present)
1439   // In case of empty events the tags will contain dummy values
1440   AliESDTagCreator *esdtagCreator = new AliESDTagCreator();
1441   esdtagCreator->CreateESDTags(fFirstEvent,fLastEvent,fGRPData);
1442   if (fWriteAOD) {
1443     AliWarning("AOD tag creation not supported anymore during reconstruction.");
1444   }
1445
1446   //Finish QA and end of cycle for out-of-loop QA
1447   if (!fInLoopQA) {
1448           if (fRunQA) {
1449                   AliQADataMakerSteer qas;
1450                   if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) 
1451                           qas.Run(fRunLocalReconstruction.Data(), AliQA::kRECPOINTS, fSameQACycle);
1452                   //qas.Reset() ;
1453                   if (fQATasks.Contains(Form("%d", AliQA::kESDS))) 
1454                           qas.Run(fRunLocalReconstruction.Data(), AliQA::kESDS, fSameQACycle);
1455                   if (fRunGlobalQA) {
1456                          AliQADataMakerRec *qadm = GetQADataMaker(AliQA::kGLOBAL);
1457                           if (qadm) {
1458                                   if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) 
1459                                           qadm->EndOfCycle(AliQA::kRECPOINTS);
1460                                   if (fQATasks.Contains(Form("%d", AliQA::kESDS))) 
1461                                           qadm->EndOfCycle(AliQA::kESDS);
1462                                   qadm->Finish();
1463                           }
1464                   }
1465           }
1466   }
1467   
1468   // Cleanup of CDB manager: cache and active storages!
1469   AliCDBManager::Instance()->ClearCache();
1470   
1471   return kTRUE;
1472 }
1473
1474
1475 //_____________________________________________________________________________
1476 Bool_t AliReconstruction::RunLocalReconstruction(const TString& /*detectors*/)
1477 {
1478 // run the local reconstruction
1479   static Int_t eventNr=0;
1480   AliCodeTimerAuto("")
1481
1482  //  AliCDBManager* man = AliCDBManager::Instance();
1483 //   Bool_t origCache = man->GetCacheFlag();
1484
1485 //   TString detStr = detectors;
1486 //   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1487 //     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
1488 //     AliReconstructor* reconstructor = GetReconstructor(iDet);
1489 //     if (!reconstructor) continue;
1490 //     if (reconstructor->HasLocalReconstruction()) continue;
1491
1492 //     AliCodeTimerStart(Form("running reconstruction for %s", fgkDetectorName[iDet]));
1493 //     AliInfo(Form("running reconstruction for %s", fgkDetectorName[iDet]));
1494     
1495 //     AliCodeTimerStart(Form("Loading calibration data from OCDB for %s", fgkDetectorName[iDet]));                          
1496 //     AliInfo(Form("Loading calibration data from OCDB for %s", fgkDetectorName[iDet]));
1497
1498 //     man->SetCacheFlag(kTRUE);
1499 //     TString calibPath = Form("%s/Calib/*", fgkDetectorName[iDet]);
1500 //     man->GetAll(calibPath); // entries are cached!
1501
1502 //     AliCodeTimerStop(Form("Loading calibration data from OCDB for %s", fgkDetectorName[iDet]));
1503      
1504 //     if (fRawReader) {
1505 //       fRawReader->RewindEvents();
1506 //       reconstructor->Reconstruct(fRunLoader, fRawReader);
1507 //     } else {
1508 //       reconstructor->Reconstruct(fRunLoader);
1509 //     }
1510      
1511 //      AliCodeTimerStop(Form("running reconstruction for %s", fgkDetectorName[iDet]));
1512     // AliSysInfo::AddStamp(Form("LRec%s_%d",fgkDetectorName[iDet],eventNr));
1513
1514 //     // unload calibration data
1515 //     man->UnloadFromCache(calibPath);
1516 //     //man->ClearCache();
1517 //   }
1518
1519 //   man->SetCacheFlag(origCache);
1520
1521 //   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1522 //     AliError(Form("the following detectors were not found: %s",
1523 //                   detStr.Data()));
1524 //     if (fStopOnError) return kFALSE;
1525 //   }
1526
1527           eventNr++;
1528   return kTRUE;
1529 }
1530
1531 //_____________________________________________________________________________
1532 Bool_t AliReconstruction::RunLocalEventReconstruction(const TString& detectors)
1533 {
1534 // run the local reconstruction
1535
1536   static Int_t eventNr=0;
1537   AliCodeTimerAuto("")
1538
1539   TString detStr = detectors;
1540   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1541     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
1542     AliReconstructor* reconstructor = GetReconstructor(iDet);
1543     if (!reconstructor) continue;
1544     AliLoader* loader = fLoader[iDet];
1545     // Matthias April 2008: temporary fix to run HLT reconstruction
1546     // although the HLT loader is missing
1547     if (strcmp(fgkDetectorName[iDet], "HLT")==0) {
1548       if (fRawReader) {
1549         reconstructor->Reconstruct(fRawReader, NULL);
1550       } else {
1551         TTree* dummy=NULL;
1552         reconstructor->Reconstruct(dummy, NULL);
1553       }
1554       continue;
1555     }
1556     if (!loader) {
1557       AliWarning(Form("No loader is defined for %s!",fgkDetectorName[iDet]));
1558       continue;
1559     }
1560     // conversion of digits
1561     if (fRawReader && reconstructor->HasDigitConversion()) {
1562       AliInfo(Form("converting raw data digits into root objects for %s", 
1563                    fgkDetectorName[iDet]));
1564       AliCodeTimerAuto(Form("converting raw data digits into root objects for %s", 
1565                             fgkDetectorName[iDet]));
1566       loader->LoadDigits("update");
1567       loader->CleanDigits();
1568       loader->MakeDigitsContainer();
1569       TTree* digitsTree = loader->TreeD();
1570       reconstructor->ConvertDigits(fRawReader, digitsTree);
1571       loader->WriteDigits("OVERWRITE");
1572       loader->UnloadDigits();
1573     }
1574     // local reconstruction
1575     AliInfo(Form("running reconstruction for %s", fgkDetectorName[iDet]));
1576     AliCodeTimerAuto(Form("running reconstruction for %s", fgkDetectorName[iDet]));
1577     loader->LoadRecPoints("update");
1578     loader->CleanRecPoints();
1579     loader->MakeRecPointsContainer();
1580     TTree* clustersTree = loader->TreeR();
1581     if (fRawReader && !reconstructor->HasDigitConversion()) {
1582       reconstructor->Reconstruct(fRawReader, clustersTree);
1583     } else {
1584       loader->LoadDigits("read");
1585       TTree* digitsTree = loader->TreeD();
1586       if (!digitsTree) {
1587         AliError(Form("Can't get the %s digits tree", fgkDetectorName[iDet]));
1588         if (fStopOnError) return kFALSE;
1589       } else {
1590         reconstructor->Reconstruct(digitsTree, clustersTree);
1591       }
1592       loader->UnloadDigits();
1593     }
1594
1595     // In-loop QA for local reconstrucion 
1596     if (fRunQA && fInLoopQA) {
1597        AliQADataMakerRec * qadm = GetQADataMaker(iDet);
1598        if (qadm) {
1599           //AliCodeTimerStart
1600           //(Form("Running QA data maker for %s", fgkDetectorName[iDet]));
1601           //AliInfo
1602           //(Form("Running QA data maker for %s", fgkDetectorName[iDet]));
1603
1604                    if (fQATasks.Contains(Form("%d", AliQA::kRECPOINTS))) 
1605                            qadm->Exec(AliQA::kRECPOINTS, clustersTree) ;
1606           //AliCodeTimerStop
1607           //(Form("Running QA data maker for %s", fgkDetectorName[iDet]));
1608        }
1609     }
1610
1611     loader->WriteRecPoints("OVERWRITE");
1612     loader->UnloadRecPoints();
1613     AliSysInfo::AddStamp(Form("LRec%s_%d",fgkDetectorName[iDet],eventNr), iDet,1,eventNr);
1614   }
1615
1616   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1617     AliError(Form("the following detectors were not found: %s",
1618                   detStr.Data()));
1619     if (fStopOnError) return kFALSE;
1620   }
1621   eventNr++;
1622   return kTRUE;
1623 }
1624
1625 //_____________________________________________________________________________
1626 Bool_t AliReconstruction::RunVertexFinder(AliESDEvent*& esd)
1627 {
1628 // run the barrel tracking
1629
1630   AliCodeTimerAuto("")
1631
1632   AliESDVertex* vertex = NULL;
1633   Double_t vtxPos[3] = {0, 0, 0};
1634   Double_t vtxErr[3] = {0.07, 0.07, 0.1};
1635   TArrayF mcVertex(3); 
1636   if (fRunLoader->GetHeader() && fRunLoader->GetHeader()->GenEventHeader()) {
1637     fRunLoader->GetHeader()->GenEventHeader()->PrimaryVertex(mcVertex);
1638     for (Int_t i = 0; i < 3; i++) vtxPos[i] = mcVertex[i];
1639   }
1640
1641   if (fVertexer) {
1642     AliInfo("running the ITS vertex finder");
1643     if (fLoader[0]) {
1644       fLoader[0]->LoadRecPoints();
1645       TTree* cltree = fLoader[0]->TreeR();
1646       if (cltree) {
1647         if(fDiamondProfile) fVertexer->SetVtxStart(fDiamondProfile);
1648         vertex = fVertexer->FindVertexForCurrentEvent(cltree);
1649       }
1650       else {
1651         AliError("Can't get the ITS cluster tree");
1652       }
1653       fLoader[0]->UnloadRecPoints();
1654     }
1655     else {
1656       AliError("Can't get the ITS loader");
1657     }
1658     if(!vertex){
1659       AliWarning("Vertex not found");
1660       vertex = new AliESDVertex();
1661       vertex->SetName("default");
1662     }
1663     else {
1664       vertex->SetName("reconstructed");
1665     }
1666
1667   } else {
1668     AliInfo("getting the primary vertex from MC");
1669     vertex = new AliESDVertex(vtxPos, vtxErr);
1670   }
1671
1672   if (vertex) {
1673     vertex->GetXYZ(vtxPos);
1674     vertex->GetSigmaXYZ(vtxErr);
1675   } else {
1676     AliWarning("no vertex reconstructed");
1677     vertex = new AliESDVertex(vtxPos, vtxErr);
1678   }
1679   esd->SetPrimaryVertexSPD(vertex);
1680   // if SPD multiplicity has been determined, it is stored in the ESD
1681   AliMultiplicity *mult = fVertexer->GetMultiplicity();
1682   if(mult)esd->SetMultiplicity(mult);
1683
1684   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1685     if (fTracker[iDet]) fTracker[iDet]->SetVertex(vtxPos, vtxErr);
1686   }  
1687   delete vertex;
1688
1689   return kTRUE;
1690 }
1691
1692 //_____________________________________________________________________________
1693 Bool_t AliReconstruction::RunHLTTracking(AliESDEvent*& esd)
1694 {
1695 // run the HLT barrel tracking
1696
1697   AliCodeTimerAuto("")
1698
1699   if (!fRunLoader) {
1700     AliError("Missing runLoader!");
1701     return kFALSE;
1702   }
1703
1704   AliInfo("running HLT tracking");
1705
1706   // Get a pointer to the HLT reconstructor
1707   AliReconstructor *reconstructor = GetReconstructor(fgkNDetectors-1);
1708   if (!reconstructor) return kFALSE;
1709
1710   // TPC + ITS
1711   for (Int_t iDet = 1; iDet >= 0; iDet--) {
1712     TString detName = fgkDetectorName[iDet];
1713     AliDebug(1, Form("%s HLT tracking", detName.Data()));
1714     reconstructor->SetOption(detName.Data());
1715     AliTracker *tracker = reconstructor->CreateTracker();
1716     if (!tracker) {
1717       AliWarning(Form("couldn't create a HLT tracker for %s", detName.Data()));
1718       if (fStopOnError) return kFALSE;
1719       continue;
1720     }
1721     Double_t vtxPos[3];
1722     Double_t vtxErr[3]={0.005,0.005,0.010};
1723     const AliESDVertex *vertex = esd->GetVertex();
1724     vertex->GetXYZ(vtxPos);
1725     tracker->SetVertex(vtxPos,vtxErr);
1726     if(iDet != 1) {
1727       fLoader[iDet]->LoadRecPoints("read");
1728       TTree* tree = fLoader[iDet]->TreeR();
1729       if (!tree) {
1730         AliError(Form("Can't get the %s cluster tree", detName.Data()));
1731         return kFALSE;
1732       }
1733       tracker->LoadClusters(tree);
1734     }
1735     if (tracker->Clusters2Tracks(esd) != 0) {
1736       AliError(Form("HLT %s Clusters2Tracks failed", fgkDetectorName[iDet]));
1737       return kFALSE;
1738     }
1739     if(iDet != 1) {
1740       tracker->UnloadClusters();
1741     }
1742     delete tracker;
1743   }
1744
1745   return kTRUE;
1746 }
1747
1748 //_____________________________________________________________________________
1749 Bool_t AliReconstruction::RunMuonTracking(AliESDEvent*& esd)
1750 {
1751 // run the muon spectrometer tracking
1752
1753   AliCodeTimerAuto("")
1754
1755   if (!fRunLoader) {
1756     AliError("Missing runLoader!");
1757     return kFALSE;
1758   }
1759   Int_t iDet = 7; // for MUON
1760
1761   AliInfo("is running...");
1762
1763   // Get a pointer to the MUON reconstructor
1764   AliReconstructor *reconstructor = GetReconstructor(iDet);
1765   if (!reconstructor) return kFALSE;
1766
1767   
1768   TString detName = fgkDetectorName[iDet];
1769   AliDebug(1, Form("%s tracking", detName.Data()));
1770   AliTracker *tracker =  reconstructor->CreateTracker();
1771   if (!tracker) {
1772     AliWarning(Form("couldn't create a tracker for %s", detName.Data()));
1773     return kFALSE;
1774   }
1775      
1776   // read RecPoints
1777   fLoader[iDet]->LoadRecPoints("read");  
1778
1779   tracker->LoadClusters(fLoader[iDet]->TreeR());
1780   
1781   Int_t rv = tracker->Clusters2Tracks(esd);
1782   
1783   if ( rv )
1784   {
1785     AliError(Form("%s Clusters2Tracks failed", fgkDetectorName[iDet]));
1786     return kFALSE;
1787   }
1788   
1789   fLoader[iDet]->UnloadRecPoints();
1790
1791   tracker->UnloadClusters();
1792   
1793   delete tracker;
1794   
1795   return kTRUE;
1796 }
1797
1798
1799 //_____________________________________________________________________________
1800 Bool_t AliReconstruction::RunTracking(AliESDEvent*& esd)
1801 {
1802 // run the barrel tracking
1803   static Int_t eventNr=0;
1804   AliCodeTimerAuto("")
1805
1806   AliInfo("running tracking");
1807
1808   //Fill the ESD with the T0 info (will be used by the TOF) 
1809   if (fReconstructor[11] && fLoader[11]) {
1810     fLoader[11]->LoadRecPoints("READ");
1811     TTree *treeR = fLoader[11]->TreeR();
1812     GetReconstructor(11)->FillESD((TTree *)NULL,treeR,esd);
1813   }
1814
1815   // pass 1: TPC + ITS inwards
1816   for (Int_t iDet = 1; iDet >= 0; iDet--) {
1817     if (!fTracker[iDet]) continue;
1818     AliDebug(1, Form("%s tracking", fgkDetectorName[iDet]));
1819
1820     // load clusters
1821     fLoader[iDet]->LoadRecPoints("read");
1822     AliSysInfo::AddStamp(Form("RLoadCluster%s_%d",fgkDetectorName[iDet],eventNr),iDet,1, eventNr);
1823     TTree* tree = fLoader[iDet]->TreeR();
1824     if (!tree) {
1825       AliError(Form("Can't get the %s cluster tree", fgkDetectorName[iDet]));
1826       return kFALSE;
1827     }
1828     fTracker[iDet]->LoadClusters(tree);
1829     AliSysInfo::AddStamp(Form("TLoadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,2, eventNr);
1830     // run tracking
1831     if (fTracker[iDet]->Clusters2Tracks(esd) != 0) {
1832       AliError(Form("%s Clusters2Tracks failed", fgkDetectorName[iDet]));
1833       return kFALSE;
1834     }
1835     if (fCheckPointLevel > 1) {
1836       WriteESD(esd, Form("%s.tracking", fgkDetectorName[iDet]));
1837     }
1838     // preliminary PID in TPC needed by the ITS tracker
1839     if (iDet == 1) {
1840       GetReconstructor(1)->FillESD((TTree*)NULL, (TTree*)NULL, esd);
1841       AliESDpid::MakePID(esd);
1842     } 
1843     AliSysInfo::AddStamp(Form("Tracking0%s_%d",fgkDetectorName[iDet],eventNr), iDet,3,eventNr);
1844   }
1845
1846   // pass 2: ALL backwards
1847
1848   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1849     if (!fTracker[iDet]) continue;
1850     AliDebug(1, Form("%s back propagation", fgkDetectorName[iDet]));
1851
1852     // load clusters
1853     if (iDet > 1) {     // all except ITS, TPC
1854       TTree* tree = NULL;
1855       fLoader[iDet]->LoadRecPoints("read");
1856       AliSysInfo::AddStamp(Form("RLoadCluster0%s_%d",fgkDetectorName[iDet],eventNr), iDet,1, eventNr);
1857       tree = fLoader[iDet]->TreeR();
1858       if (!tree) {
1859         AliError(Form("Can't get the %s cluster tree", fgkDetectorName[iDet]));
1860         return kFALSE;
1861       }
1862       fTracker[iDet]->LoadClusters(tree); 
1863       AliSysInfo::AddStamp(Form("TLoadCluster0%s_%d",fgkDetectorName[iDet],eventNr), iDet,2, eventNr);
1864     }
1865
1866     // run tracking
1867     if (iDet>1) // start filling residuals for the "outer" detectors
1868     if (fRunGlobalQA) AliTracker::SetFillResiduals(kTRUE);     
1869
1870     if (fTracker[iDet]->PropagateBack(esd) != 0) {
1871       AliError(Form("%s backward propagation failed", fgkDetectorName[iDet]));
1872       //      return kFALSE;
1873     }
1874     if (fCheckPointLevel > 1) {
1875       WriteESD(esd, Form("%s.back", fgkDetectorName[iDet]));
1876     }
1877
1878     // unload clusters
1879     if (iDet > 2) {     // all except ITS, TPC, TRD
1880       fTracker[iDet]->UnloadClusters();
1881       fLoader[iDet]->UnloadRecPoints();
1882     }
1883     // updated PID in TPC needed by the ITS tracker -MI
1884     if (iDet == 1) {
1885       GetReconstructor(1)->FillESD((TTree*)NULL, (TTree*)NULL, esd);
1886       AliESDpid::MakePID(esd);
1887     }
1888     AliSysInfo::AddStamp(Form("Tracking1%s_%d",fgkDetectorName[iDet],eventNr), iDet,3, eventNr);
1889   }
1890   //stop filling residuals for the "outer" detectors
1891   if (fRunGlobalQA) AliTracker::SetFillResiduals(kFALSE);     
1892
1893   // write space-points to the ESD in case alignment data output
1894   // is switched on
1895   if (fWriteAlignmentData)
1896     WriteAlignmentData(esd);
1897
1898   // pass 3: TRD + TPC + ITS refit inwards
1899
1900   for (Int_t iDet = 2; iDet >= 0; iDet--) {
1901     if (!fTracker[iDet]) continue;
1902     AliDebug(1, Form("%s inward refit", fgkDetectorName[iDet]));
1903
1904     // run tracking
1905     if (iDet<2) // start filling residuals for TPC and ITS
1906     if (fRunGlobalQA) AliTracker::SetFillResiduals(kTRUE);     
1907
1908     if (fTracker[iDet]->RefitInward(esd) != 0) {
1909       AliError(Form("%s inward refit failed", fgkDetectorName[iDet]));
1910       //      return kFALSE;
1911     }
1912     // run postprocessing
1913     if (fTracker[iDet]->PostProcess(esd) != 0) {
1914       AliError(Form("%s postprocessing failed", fgkDetectorName[iDet]));
1915       //      return kFALSE;
1916     }
1917     if (fCheckPointLevel > 1) {
1918       WriteESD(esd, Form("%s.refit", fgkDetectorName[iDet]));
1919     }
1920     AliSysInfo::AddStamp(Form("Tracking2%s_%d",fgkDetectorName[iDet],eventNr), iDet,3, eventNr);
1921     // unload clusters
1922     fTracker[iDet]->UnloadClusters();
1923     AliSysInfo::AddStamp(Form("TUnloadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,4, eventNr);
1924     fLoader[iDet]->UnloadRecPoints();
1925     AliSysInfo::AddStamp(Form("RUnloadCluster%s_%d",fgkDetectorName[iDet],eventNr), iDet,5, eventNr);
1926   }
1927   // stop filling residuals for TPC and ITS
1928   if (fRunGlobalQA) AliTracker::SetFillResiduals(kFALSE);     
1929
1930   eventNr++;
1931   return kTRUE;
1932 }
1933
1934 //_____________________________________________________________________________
1935 Bool_t AliReconstruction::CleanESD(AliESDEvent *esd){
1936   //
1937   // Remove the data which are not needed for the physics analysis.
1938   //
1939
1940   Int_t nTracks=esd->GetNumberOfTracks();
1941   Int_t nV0s=esd->GetNumberOfV0s();
1942   AliInfo
1943   (Form("Number of ESD tracks and V0s before cleaning: %d %d",nTracks,nV0s));
1944
1945   Float_t cleanPars[]={fV0DCAmax,fV0CsPmin,fDmax,fZmax};
1946   Bool_t rc=esd->Clean(cleanPars);
1947
1948   nTracks=esd->GetNumberOfTracks();
1949   nV0s=esd->GetNumberOfV0s();
1950   AliInfo
1951   (Form("Number of ESD tracks and V0s after cleaning %d %d",nTracks,nV0s));
1952
1953   return rc;
1954 }
1955
1956 //_____________________________________________________________________________
1957 Bool_t AliReconstruction::FillESD(AliESDEvent*& esd, const TString& detectors)
1958 {
1959 // fill the event summary data
1960
1961   AliCodeTimerAuto("")
1962     static Int_t eventNr=0; 
1963   TString detStr = detectors;
1964   
1965   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
1966   if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
1967     AliReconstructor* reconstructor = GetReconstructor(iDet);
1968     if (!reconstructor) continue;
1969     if (!ReadESD(esd, fgkDetectorName[iDet])) {
1970       AliDebug(1, Form("filling ESD for %s", fgkDetectorName[iDet]));
1971       TTree* clustersTree = NULL;
1972       if (fLoader[iDet]) {
1973         fLoader[iDet]->LoadRecPoints("read");
1974         clustersTree = fLoader[iDet]->TreeR();
1975         if (!clustersTree) {
1976           AliError(Form("Can't get the %s clusters tree", 
1977                         fgkDetectorName[iDet]));
1978           if (fStopOnError) return kFALSE;
1979         }
1980       }
1981       if (fRawReader && !reconstructor->HasDigitConversion()) {
1982         reconstructor->FillESD(fRawReader, clustersTree, esd);
1983       } else {
1984         TTree* digitsTree = NULL;
1985         if (fLoader[iDet]) {
1986           fLoader[iDet]->LoadDigits("read");
1987           digitsTree = fLoader[iDet]->TreeD();
1988           if (!digitsTree) {
1989             AliError(Form("Can't get the %s digits tree", 
1990                           fgkDetectorName[iDet]));
1991             if (fStopOnError) return kFALSE;
1992           }
1993         }
1994         reconstructor->FillESD(digitsTree, clustersTree, esd);
1995         if (fLoader[iDet]) fLoader[iDet]->UnloadDigits();
1996       }
1997       if (fLoader[iDet]) {
1998         fLoader[iDet]->UnloadRecPoints();
1999       }
2000
2001       if (fCheckPointLevel > 2) WriteESD(esd, fgkDetectorName[iDet]);
2002     }
2003   }
2004
2005   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
2006     AliError(Form("the following detectors were not found: %s", 
2007                   detStr.Data()));
2008     if (fStopOnError) return kFALSE;
2009   }
2010   AliSysInfo::AddStamp(Form("FillESD%d",eventNr), 0,1, eventNr);
2011   eventNr++;
2012   return kTRUE;
2013 }
2014
2015 //_____________________________________________________________________________
2016 Bool_t AliReconstruction::FillTriggerESD(AliESDEvent*& esd)
2017 {
2018   // Reads the trigger decision which is
2019   // stored in Trigger.root file and fills
2020   // the corresponding esd entries
2021
2022   AliCodeTimerAuto("")
2023   
2024   AliInfo("Filling trigger information into the ESD");
2025
2026   AliCentralTrigger *aCTP = NULL;
2027
2028   if (fRawReader) {
2029     AliCTPRawStream input(fRawReader);
2030     if (!input.Next()) {
2031       AliWarning("No valid CTP (trigger) DDL raw data is found ! The trigger mask will be taken from the event header, trigger cluster mask will be empty !");
2032       ULong64_t mask = (((ULong64_t)fRawReader->GetTriggerPattern()[1]) << 32) +
2033         fRawReader->GetTriggerPattern()[0];
2034       esd->SetTriggerMask(mask);
2035       esd->SetTriggerCluster(0);
2036     }
2037     else {
2038       esd->SetTriggerMask(input.GetClassMask());
2039       esd->SetTriggerCluster(input.GetClusterMask());
2040     }
2041
2042     aCTP = new AliCentralTrigger();
2043     TString configstr("");
2044     if (!aCTP->LoadConfiguration(configstr)) { // Load CTP config from OCDB
2045       AliError("No trigger configuration found in OCDB! The trigger classes information will no be stored in ESD!");
2046       delete aCTP;
2047       return kFALSE;
2048     }
2049   }
2050   else {
2051     AliRunLoader *runloader = AliRunLoader::GetRunLoader();
2052     if (runloader) {
2053       if (!runloader->LoadTrigger()) {
2054         aCTP = runloader->GetTrigger();
2055         esd->SetTriggerMask(aCTP->GetClassMask());
2056         esd->SetTriggerCluster(aCTP->GetClusterMask());
2057       }
2058       else {
2059         AliWarning("No trigger can be loaded! The trigger information is not stored in the ESD !");
2060         return kFALSE;
2061       }
2062     }
2063     else {
2064       AliError("No run loader is available! The trigger information is not stored in the ESD !");
2065       return kFALSE;
2066     }
2067   }
2068
2069   // Now fill the trigger class names into AliESDRun object
2070   AliTriggerConfiguration *config = aCTP->GetConfiguration();
2071   if (!config) {
2072     AliError("No trigger configuration has been found! The trigger classes information will not be stored in ESD!");
2073     if (fRawReader) delete aCTP;
2074     return kFALSE;
2075   }
2076
2077   const TObjArray& classesArray = config->GetClasses();
2078   Int_t nclasses = classesArray.GetEntriesFast();
2079   for( Int_t j=0; j<nclasses; j++ ) {
2080     AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
2081     Int_t trindex = (Int_t)TMath::Log2(trclass->GetMask());
2082     esd->SetTriggerClass(trclass->GetName(),trindex);
2083   }
2084
2085   if (fRawReader) delete aCTP;
2086   return kTRUE;
2087 }
2088
2089
2090
2091
2092
2093 //_____________________________________________________________________________
2094 Bool_t AliReconstruction::FillRawEventHeaderESD(AliESDEvent*& esd)
2095 {
2096   // 
2097   // Filling information from RawReader Header
2098   // 
2099
2100   AliInfo("Filling information from RawReader Header");
2101   esd->SetBunchCrossNumber(0);
2102   esd->SetOrbitNumber(0);
2103   esd->SetPeriodNumber(0);
2104   esd->SetTimeStamp(0);
2105   esd->SetEventType(0);
2106   const AliRawEventHeaderBase * eventHeader = fRawReader->GetEventHeader();
2107   if (eventHeader){
2108
2109     const UInt_t *id = eventHeader->GetP("Id");
2110     esd->SetBunchCrossNumber((id)[1]&0x00000fff);
2111     esd->SetOrbitNumber((((id)[0]<<20)&0xf00000)|(((id)[1]>>12)&0xfffff));
2112     esd->SetPeriodNumber(((id)[0]>>4)&0x0fffffff);
2113
2114     esd->SetTimeStamp((eventHeader->Get("Timestamp")));  
2115     esd->SetEventType((eventHeader->Get("Type")));
2116   }
2117
2118   return kTRUE;
2119 }
2120
2121
2122 //_____________________________________________________________________________
2123 Bool_t AliReconstruction::IsSelected(TString detName, TString& detectors) const
2124 {
2125 // check whether detName is contained in detectors
2126 // if yes, it is removed from detectors
2127
2128   // check if all detectors are selected
2129   if ((detectors.CompareTo("ALL") == 0) ||
2130       detectors.BeginsWith("ALL ") ||
2131       detectors.EndsWith(" ALL") ||
2132       detectors.Contains(" ALL ")) {
2133     detectors = "ALL";
2134     return kTRUE;
2135   }
2136
2137   // search for the given detector
2138   Bool_t result = kFALSE;
2139   if ((detectors.CompareTo(detName) == 0) ||
2140       detectors.BeginsWith(detName+" ") ||
2141       detectors.EndsWith(" "+detName) ||
2142       detectors.Contains(" "+detName+" ")) {
2143     detectors.ReplaceAll(detName, "");
2144     result = kTRUE;
2145   }
2146
2147   // clean up the detectors string
2148   while (detectors.Contains("  ")) detectors.ReplaceAll("  ", " ");
2149   while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
2150   while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
2151
2152   return result;
2153 }
2154
2155 //_____________________________________________________________________________
2156 Bool_t AliReconstruction::InitRunLoader()
2157 {
2158 // get or create the run loader
2159
2160   if (gAlice) delete gAlice;
2161   gAlice = NULL;
2162
2163   if (!gSystem->AccessPathName(fGAliceFileName.Data())) { // galice.root exists
2164     // load all base libraries to get the loader classes
2165     TString libs = gSystem->GetLibraries();
2166     for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
2167       TString detName = fgkDetectorName[iDet];
2168       if (detName == "HLT") continue;
2169       if (libs.Contains("lib" + detName + "base.so")) continue;
2170       gSystem->Load("lib" + detName + "base.so");
2171     }
2172     fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
2173     if (!fRunLoader) {
2174       AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
2175       CleanUp();
2176       return kFALSE;
2177     }
2178
2179     fRunLoader->CdGAFile();
2180     fRunLoader->LoadgAlice();
2181
2182     //PH This is a temporary fix to give access to the kinematics
2183     //PH that is needed for the labels of ITS clusters
2184     fRunLoader->LoadHeader();
2185     fRunLoader->LoadKinematics();
2186
2187   } else {               // galice.root does not exist
2188     if (!fRawReader) {
2189       AliError(Form("the file %s does not exist", fGAliceFileName.Data()));
2190       CleanUp();
2191       return kFALSE;
2192     }
2193     fRunLoader = AliRunLoader::Open(fGAliceFileName.Data(),
2194                                     AliConfig::GetDefaultEventFolderName(),
2195                                     "recreate");
2196     if (!fRunLoader) {
2197       AliError(Form("could not create run loader in file %s", 
2198                     fGAliceFileName.Data()));
2199       CleanUp();
2200       return kFALSE;
2201     }
2202     fIsNewRunLoader = kTRUE;
2203     fRunLoader->MakeTree("E");
2204
2205     if (fNumberOfEventsPerFile > 0)
2206       fRunLoader->SetNumberOfEventsPerFile(fNumberOfEventsPerFile);
2207     else
2208       fRunLoader->SetNumberOfEventsPerFile((UInt_t)-1);
2209   }
2210
2211   return kTRUE;
2212 }
2213
2214 //_____________________________________________________________________________
2215 AliReconstructor* AliReconstruction::GetReconstructor(Int_t iDet)
2216 {
2217 // get the reconstructor object and the loader for a detector
2218
2219   if (fReconstructor[iDet]) return fReconstructor[iDet];
2220
2221   // load the reconstructor object
2222   TPluginManager* pluginManager = gROOT->GetPluginManager();
2223   TString detName = fgkDetectorName[iDet];
2224   TString recName = "Ali" + detName + "Reconstructor";
2225
2226   if (!fIsNewRunLoader && !fRunLoader->GetLoader(detName+"Loader") && (detName != "HLT")) return NULL;
2227
2228   AliReconstructor* reconstructor = NULL;
2229   // first check if a plugin is defined for the reconstructor
2230   TPluginHandler* pluginHandler = 
2231     pluginManager->FindHandler("AliReconstructor", detName);
2232   // if not, add a plugin for it
2233   if (!pluginHandler) {
2234     AliDebug(1, Form("defining plugin for %s", recName.Data()));
2235     TString libs = gSystem->GetLibraries();
2236     if (libs.Contains("lib" + detName + "base.so") ||
2237         (gSystem->Load("lib" + detName + "base.so") >= 0)) {
2238       pluginManager->AddHandler("AliReconstructor", detName, 
2239                                 recName, detName + "rec", recName + "()");
2240     } else {
2241       pluginManager->AddHandler("AliReconstructor", detName, 
2242                                 recName, detName, recName + "()");
2243     }
2244     pluginHandler = pluginManager->FindHandler("AliReconstructor", detName);
2245   }
2246   if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
2247     reconstructor = (AliReconstructor*) pluginHandler->ExecPlugin(0);
2248   }
2249   if (reconstructor) {
2250     TObject* obj = fOptions.FindObject(detName.Data());
2251     if (obj) reconstructor->SetOption(obj->GetTitle());
2252     reconstructor->Init();
2253     fReconstructor[iDet] = reconstructor;
2254   }
2255
2256   // get or create the loader
2257   if (detName != "HLT") {
2258     fLoader[iDet] = fRunLoader->GetLoader(detName + "Loader");
2259     if (!fLoader[iDet]) {
2260       AliConfig::Instance()
2261         ->CreateDetectorFolders(fRunLoader->GetEventFolder(), 
2262                                 detName, detName);
2263       // first check if a plugin is defined for the loader
2264       pluginHandler = 
2265         pluginManager->FindHandler("AliLoader", detName);
2266       // if not, add a plugin for it
2267       if (!pluginHandler) {
2268         TString loaderName = "Ali" + detName + "Loader";
2269         AliDebug(1, Form("defining plugin for %s", loaderName.Data()));
2270         pluginManager->AddHandler("AliLoader", detName, 
2271                                   loaderName, detName + "base", 
2272                                   loaderName + "(const char*, TFolder*)");
2273         pluginHandler = pluginManager->FindHandler("AliLoader", detName);
2274       }
2275       if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
2276         fLoader[iDet] = 
2277           (AliLoader*) pluginHandler->ExecPlugin(2, detName.Data(), 
2278                                                  fRunLoader->GetEventFolder());
2279       }
2280       if (!fLoader[iDet]) {   // use default loader
2281         fLoader[iDet] = new AliLoader(detName, fRunLoader->GetEventFolder());
2282       }
2283       if (!fLoader[iDet]) {
2284         AliWarning(Form("couldn't get loader for %s", detName.Data()));
2285         if (fStopOnError) return NULL;
2286       } else {
2287         fRunLoader->AddLoader(fLoader[iDet]);
2288         fRunLoader->CdGAFile();
2289         if (gFile && !gFile->IsWritable()) gFile->ReOpen("UPDATE");
2290         fRunLoader->Write(0, TObject::kOverwrite);
2291       }
2292     }
2293   }
2294       
2295   return reconstructor;
2296 }
2297
2298 //_____________________________________________________________________________
2299 Bool_t AliReconstruction::CreateVertexer()
2300 {
2301 // create the vertexer
2302
2303   fVertexer = NULL;
2304   AliReconstructor* itsReconstructor = GetReconstructor(0);
2305   if (itsReconstructor) {
2306     fVertexer = itsReconstructor->CreateVertexer();
2307   }
2308   if (!fVertexer) {
2309     AliWarning("couldn't create a vertexer for ITS");
2310     if (fStopOnError) return kFALSE;
2311   }
2312
2313   return kTRUE;
2314 }
2315
2316 //_____________________________________________________________________________
2317 Bool_t AliReconstruction::CreateTrackers(const TString& detectors)
2318 {
2319 // create the trackers
2320
2321   TString detStr = detectors;
2322   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
2323     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
2324     AliReconstructor* reconstructor = GetReconstructor(iDet);
2325     if (!reconstructor) continue;
2326     TString detName = fgkDetectorName[iDet];
2327     if (detName == "HLT") {
2328       fRunHLTTracking = kTRUE;
2329       continue;
2330     }
2331     if (detName == "MUON") {
2332       fRunMuonTracking = kTRUE;
2333       continue;
2334     }
2335
2336
2337     fTracker[iDet] = reconstructor->CreateTracker();
2338     if (!fTracker[iDet] && (iDet < 7)) {
2339       AliWarning(Form("couldn't create a tracker for %s", detName.Data()));
2340       if (fStopOnError) return kFALSE;
2341     }
2342     AliSysInfo::AddStamp(Form("LTracker%s",fgkDetectorName[iDet]), iDet,0);
2343   }
2344
2345   return kTRUE;
2346 }
2347
2348 //_____________________________________________________________________________
2349 void AliReconstruction::CleanUp(TFile* file, TFile* fileOld)
2350 {
2351 // delete trackers and the run loader and close and delete the file
2352
2353   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
2354     delete fReconstructor[iDet];
2355     fReconstructor[iDet] = NULL;
2356     fLoader[iDet] = NULL;
2357     delete fTracker[iDet];
2358     fTracker[iDet] = NULL;
2359 //    delete fQADataMaker[iDet];
2360 //    fQADataMaker[iDet] = NULL;
2361   }
2362   delete fVertexer;
2363   fVertexer = NULL;
2364
2365   if (ftVertexer) delete ftVertexer;
2366   ftVertexer = NULL;
2367   
2368   if(!(AliCDBManager::Instance()->GetCacheFlag())) {
2369         delete fDiamondProfile;
2370         fDiamondProfile = NULL;
2371         delete fDiamondProfileTPC;
2372         fDiamondProfileTPC = NULL;
2373         delete fGRPData;
2374         fGRPData = NULL;
2375   }
2376
2377
2378   delete fRunLoader;
2379   fRunLoader = NULL;
2380   delete fRawReader;
2381   fRawReader = NULL;
2382   if (fParentRawReader) delete fParentRawReader;
2383   fParentRawReader=NULL;
2384
2385   if (file) {
2386     file->Close();
2387     delete file;
2388   }
2389
2390   if (fileOld) {
2391     fileOld->Close();
2392     delete fileOld;
2393     gSystem->Unlink("AliESDs.old.root");
2394   }
2395
2396 }
2397
2398 //_____________________________________________________________________________
2399
2400 Bool_t AliReconstruction::ReadESD(AliESDEvent*& esd, const char* recStep) const
2401 {
2402 // read the ESD event from a file
2403
2404   if (!esd) return kFALSE;
2405   char fileName[256];
2406   sprintf(fileName, "ESD_%d.%d_%s.root", 
2407           esd->GetRunNumber(), esd->GetEventNumberInFile(), recStep);
2408   if (gSystem->AccessPathName(fileName)) return kFALSE;
2409
2410   AliInfo(Form("reading ESD from file %s", fileName));
2411   AliDebug(1, Form("reading ESD from file %s", fileName));
2412   TFile* file = TFile::Open(fileName);
2413   if (!file || !file->IsOpen()) {
2414     AliError(Form("opening %s failed", fileName));
2415     delete file;
2416     return kFALSE;
2417   }
2418
2419   gROOT->cd();
2420   delete esd;
2421   esd = (AliESDEvent*) file->Get("ESD");
2422   file->Close();
2423   delete file;
2424   return kTRUE;
2425
2426 }
2427
2428
2429
2430 //_____________________________________________________________________________
2431 void AliReconstruction::WriteESD(AliESDEvent* esd, const char* recStep) const
2432 {
2433 // write the ESD event to a file
2434
2435   if (!esd) return;
2436   char fileName[256];
2437   sprintf(fileName, "ESD_%d.%d_%s.root", 
2438           esd->GetRunNumber(), esd->GetEventNumberInFile(), recStep);
2439
2440   AliDebug(1, Form("writing ESD to file %s", fileName));
2441   TFile* file = TFile::Open(fileName, "recreate");
2442   if (!file || !file->IsOpen()) {
2443     AliError(Form("opening %s failed", fileName));
2444   } else {
2445     esd->Write("ESD");
2446     file->Close();
2447   }
2448   delete file;
2449 }
2450
2451
2452 void AliReconstruction::WriteAlignmentData(AliESDEvent* esd)
2453 {
2454   // Write space-points which are then used in the alignment procedures
2455   // For the moment only ITS, TRD and TPC
2456
2457   // Load TOF clusters
2458   if (fTracker[3]){
2459     fLoader[3]->LoadRecPoints("read");
2460     TTree* tree = fLoader[3]->TreeR();
2461     if (!tree) {
2462       AliError(Form("Can't get the %s cluster tree", fgkDetectorName[3]));
2463       return;
2464     }
2465     fTracker[3]->LoadClusters(tree);
2466   }
2467   Int_t ntracks = esd->GetNumberOfTracks();
2468   for (Int_t itrack = 0; itrack < ntracks; itrack++)
2469     {
2470       AliESDtrack *track = esd->GetTrack(itrack);
2471       Int_t nsp = 0;
2472       Int_t idx[200];
2473       for (Int_t iDet = 3; iDet >= 0; iDet--)
2474         nsp += track->GetNcls(iDet);
2475       if (nsp) {
2476         AliTrackPointArray *sp = new AliTrackPointArray(nsp);
2477         track->SetTrackPointArray(sp);
2478         Int_t isptrack = 0;
2479         for (Int_t iDet = 3; iDet >= 0; iDet--) {
2480           AliTracker *tracker = fTracker[iDet];
2481           if (!tracker) continue;
2482           Int_t nspdet = track->GetNcls(iDet);
2483           if (nspdet <= 0) continue;
2484           track->GetClusters(iDet,idx);
2485           AliTrackPoint p;
2486           Int_t isp = 0;
2487           Int_t isp2 = 0;
2488           while (isp2 < nspdet) {
2489             Bool_t isvalid;
2490             TString dets = fgkDetectorName[iDet];
2491             if ((fUseTrackingErrorsForAlignment.CompareTo(dets) == 0) ||
2492             fUseTrackingErrorsForAlignment.BeginsWith(dets+" ") ||
2493             fUseTrackingErrorsForAlignment.EndsWith(" "+dets) ||
2494             fUseTrackingErrorsForAlignment.Contains(" "+dets+" ")) {
2495               isvalid = tracker->GetTrackPointTrackingError(idx[isp2],p,track);
2496             } else {
2497               isvalid = tracker->GetTrackPoint(idx[isp2],p); 
2498             } 
2499             isp2++;
2500             const Int_t kNTPCmax = 159;
2501             if (iDet==1 && isp2>kNTPCmax) break;   // to be fixed
2502             if (!isvalid) continue;
2503             sp->AddPoint(isptrack,&p); isptrack++; isp++;
2504           }
2505         }       
2506       }
2507     }
2508   if (fTracker[3]){
2509     fTracker[3]->UnloadClusters();
2510     fLoader[3]->UnloadRecPoints();
2511   }
2512 }
2513
2514 //_____________________________________________________________________________
2515 void AliReconstruction::FillRawDataErrorLog(Int_t iEvent, AliESDEvent* esd)
2516 {
2517   // The method reads the raw-data error log
2518   // accumulated within the rawReader.
2519   // It extracts the raw-data errors related to
2520   // the current event and stores them into
2521   // a TClonesArray inside the esd object.
2522
2523   if (!fRawReader) return;
2524
2525   for(Int_t i = 0; i < fRawReader->GetNumberOfErrorLogs(); i++) {
2526
2527     AliRawDataErrorLog *log = fRawReader->GetErrorLog(i);
2528     if (!log) continue;
2529     if (iEvent != log->GetEventNumber()) continue;
2530
2531     esd->AddRawDataErrorLog(log);
2532   }
2533
2534 }
2535
2536 TNamed* AliReconstruction::CopyFileToTNamed(TString fPath,TString pName){
2537   // Dump a file content into a char in TNamed
2538   ifstream in;
2539   in.open(fPath.Data(),ios::in | ios::binary|ios::ate);
2540   Int_t kBytes = (Int_t)in.tellg();
2541   printf("Size: %d \n",kBytes);
2542   TNamed *fn = 0;
2543   if(in.good()){
2544     char* memblock = new char [kBytes];
2545     in.seekg (0, ios::beg);
2546     in.read (memblock, kBytes);
2547     in.close();
2548     TString fData(memblock,kBytes);
2549     fn = new TNamed(pName,fData);
2550     printf("fData Size: %d \n",fData.Sizeof());
2551     printf("pName Size: %d \n",pName.Sizeof());
2552     printf("fn    Size: %d \n",fn->Sizeof());
2553     delete[] memblock;
2554   }
2555   else{
2556     AliInfo(Form("Could not Open %s\n",fPath.Data()));
2557   }
2558
2559   return fn;
2560 }
2561
2562 void AliReconstruction::TNamedToFile(TTree* fTree, TString pName){
2563   // This is not really needed in AliReconstruction at the moment
2564   // but can serve as a template
2565
2566   TList *fList = fTree->GetUserInfo();
2567   TNamed *fn = (TNamed*)fList->FindObject(pName.Data());
2568   printf("fn Size: %d \n",fn->Sizeof());
2569
2570   TString fTmp(fn->GetName()); // to be 100% sure in principle pName also works
2571   const char* cdata = fn->GetTitle();
2572   printf("fTmp Size %d\n",fTmp.Sizeof());
2573
2574   int size = fn->Sizeof()-fTmp.Sizeof()-sizeof(UChar_t)-sizeof(Int_t); // see dfinition of TString::SizeOf()...
2575   printf("calculated size %d\n",size);
2576   ofstream out(pName.Data(),ios::out | ios::binary);
2577   out.write(cdata,size);
2578   out.close();
2579
2580 }
2581   
2582 //_____________________________________________________________________________
2583 AliQADataMakerRec * AliReconstruction::GetQADataMaker(Int_t iDet)
2584 {
2585  // get the quality assurance data maker object and the loader for a detector
2586
2587   if (fQADataMaker[iDet]) 
2588     return fQADataMaker[iDet];
2589
2590   AliQADataMakerRec * qadm = NULL;
2591   if (iDet == fgkNDetectors) { //Global QA
2592      qadm = new AliGlobalQADataMaker();
2593      fQADataMaker[iDet] = qadm;
2594      return qadm;
2595   }
2596
2597   // load the QA data maker object
2598   TPluginManager* pluginManager = gROOT->GetPluginManager();
2599   TString detName = fgkDetectorName[iDet];
2600   TString qadmName = "Ali" + detName + "QADataMakerRec";
2601   if (!fIsNewRunLoader && !fRunLoader->GetLoader(detName+"Loader") && (detName != "HLT")) 
2602     return NULL;
2603
2604   // first check if a plugin is defined for the quality assurance data maker
2605   TPluginHandler* pluginHandler = pluginManager->FindHandler("AliQADataMakerRec", detName);
2606   // if not, add a plugin for it
2607   if (!pluginHandler) {
2608     AliDebug(1, Form("defining plugin for %s", qadmName.Data()));
2609     TString libs = gSystem->GetLibraries();
2610     if (libs.Contains("lib" + detName + "base.so") ||
2611         (gSystem->Load("lib" + detName + "base.so") >= 0)) {
2612       pluginManager->AddHandler("AliQADataMakerRec", detName, 
2613                                 qadmName, detName + "qadm", qadmName + "()");
2614     } else {
2615       pluginManager->AddHandler("AliQADataMakerRec", detName, 
2616                                 qadmName, detName, qadmName + "()");
2617     }
2618     pluginHandler = pluginManager->FindHandler("AliQADataMakerRec", detName);
2619   }
2620   if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
2621     qadm = (AliQADataMakerRec *) pluginHandler->ExecPlugin(0);
2622   }
2623
2624   fQADataMaker[iDet] = qadm;
2625
2626   return qadm;
2627 }
2628
2629 //_____________________________________________________________________________
2630 Bool_t AliReconstruction::RunQA(AliESDEvent *& esd)
2631 {
2632   // run the Quality Assurance data producer
2633
2634   AliCodeTimerAuto("")
2635   TString detStr = fQADetectors ;
2636   for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
2637    if (!IsSelected(fgkDetectorName[iDet], detStr)) 
2638      continue;
2639    AliQADataMakerRec * qadm = GetQADataMaker(iDet);
2640    if (!qadm) 
2641      continue;
2642    AliCodeTimerStart(Form("running quality assurance data maker for %s", fgkDetectorName[iDet]));
2643    AliInfo(Form("running quality assurance data maker for %s", fgkDetectorName[iDet]));
2644     
2645    if (fQATasks.Contains(Form("%d", AliQA::kESDS))) {
2646            qadm->Exec(AliQA::kESDS, esd) ; 
2647            qadm->Increment() ; 
2648    }
2649    AliCodeTimerStop(Form("running quality assurance data maker for %s", fgkDetectorName[iDet]));
2650  }
2651  if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
2652    AliError(Form("the following detectors were not found: %s",
2653                  detStr.Data()));
2654    if (fStopOnError) 
2655      return kFALSE;
2656  }
2657  
2658  return kTRUE;
2659   
2660 }
2661
2662 //_____________________________________________________________________________
2663 void AliReconstruction::CheckQA()
2664 {
2665 // check the QA of SIM for this run and remove the detectors 
2666 // with status Fatal
2667   
2668         TString newRunLocalReconstruction ; 
2669         TString newRunTracking ;
2670         TString newFillESD ;
2671          
2672         for (Int_t iDet = 0; iDet < AliQA::kNDET; iDet++) {
2673                 TString detName(AliQA::GetDetName(iDet)) ;
2674                 AliQA * qa = AliQA::Instance(AliQA::DETECTORINDEX_t(iDet)) ; 
2675                 if ( qa->IsSet(AliQA::DETECTORINDEX_t(iDet), AliQA::kSIM, AliQA::kFATAL)) {
2676                                 AliInfo(Form("QA status for %s in Hits and/or SDIGITS  and/or Digits was Fatal; No reconstruction performed", detName.Data())) ;
2677                 } else {
2678                         if ( fRunLocalReconstruction.Contains(AliQA::GetDetName(iDet)) || 
2679                                         fRunLocalReconstruction.Contains("ALL") )  {
2680                                 newRunLocalReconstruction += detName ; 
2681                                 newRunLocalReconstruction += " " ;                      
2682                         }
2683                         if ( fRunTracking.Contains(AliQA::GetDetName(iDet)) || 
2684                                         fRunTracking.Contains("ALL") )  {
2685                                 newRunTracking += detName ; 
2686                                 newRunTracking += " " ;                         
2687                         }
2688                         if ( fFillESD.Contains(AliQA::GetDetName(iDet)) || 
2689                                         fFillESD.Contains("ALL") )  {
2690                                 newFillESD += detName ; 
2691                                 newFillESD += " " ;                     
2692                         }
2693                 }
2694         }
2695         fRunLocalReconstruction = newRunLocalReconstruction ; 
2696         fRunTracking            = newRunTracking ; 
2697         fFillESD                = newFillESD ; 
2698 }
2699
2700 //_____________________________________________________________________________
2701 Int_t AliReconstruction::GetDetIndex(const char* detector)
2702 {
2703   // return the detector index corresponding to detector
2704   Int_t index = -1 ; 
2705   for (index = 0; index < fgkNDetectors ; index++) {
2706     if ( strcmp(detector, fgkDetectorName[index]) == 0 )
2707         break ; 
2708   }     
2709   return index ; 
2710 }
2711 //_____________________________________________________________________________
2712 Bool_t AliReconstruction::FinishPlaneEff() {
2713  //
2714  // Here execute all the necessary operationis, at the end of the tracking phase,
2715  // in case that evaluation of PlaneEfficiencies was required for some detector. 
2716  // E.g., write into a DataBase file the PlaneEfficiency which have been evaluated. 
2717  //
2718  // This Preliminary version works only FOR ITS !!!!!
2719  // other detectors (TOF,TRD, etc. have to develop their specific codes)
2720  //
2721  //  Input: none
2722  //  Return: kTRUE if all operations have been done properly, kFALSE otherwise 
2723  //
2724  Bool_t ret=kFALSE;
2725  //for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
2726  for (Int_t iDet = 0; iDet < 1; iDet++) { // for the time being only ITS  
2727    //if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
2728    if(fTracker[iDet]) {
2729       AliPlaneEff *planeeff=fTracker[iDet]->GetPlaneEff(); 
2730       ret=planeeff->WriteIntoCDB();
2731       if(planeeff->GetCreateHistos()) {
2732         TString name="PlaneEffHisto";
2733         name+=fgkDetectorName[iDet];
2734         name+=".root";
2735         ret*=planeeff->WriteHistosToFile(name,"RECREATE");
2736       }
2737    }
2738  }
2739  return ret;
2740 }
2741 //_____________________________________________________________________________
2742 Bool_t AliReconstruction::InitPlaneEff() {
2743 //
2744  // Here execute all the necessary operations, before of the tracking phase,
2745  // for the evaluation of PlaneEfficiencies, in case required for some detectors.
2746  // E.g., read from a DataBase file a first evaluation of the PlaneEfficiency 
2747  // which should be updated/recalculated.
2748  //
2749  // This Preliminary version will work only FOR ITS !!!!!
2750  // other detectors (TOF,TRD, etc. have to develop their specific codes)
2751  //
2752  //  Input: none
2753  //  Return: kTRUE if all operations have been done properly, kFALSE otherwise
2754  //
2755  AliWarning(Form("Implementation of this method not yet done !! Method return kTRUE"));
2756  return kTRUE;
2757 }
2758
2759 //_____________________________________________________________________________
2760 Bool_t AliReconstruction::InitAliEVE()
2761 {
2762   // This method should be called only in case 
2763   // AliReconstruction is run
2764   // within the alieve environment.
2765   // It will initialize AliEVE in a way
2766   // so that it can visualize event processed
2767   // by AliReconstruction.
2768   // The return flag shows whenever the
2769   // AliEVE initialization was successful or not.
2770
2771   TString macroStr;
2772   macroStr.Form("%s/EVE/macros/alieve_online.C",gSystem->ExpandPathName("$ALICE_ROOT"));
2773   AliInfo(Form("Loading AliEVE macro: %s",macroStr.Data()));
2774   if (gROOT->LoadMacro(macroStr.Data()) != 0) return kFALSE;
2775
2776   gROOT->ProcessLine("if (!gAliEveEvent) {gAliEveEvent = new AliEveEventManager();gAliEveEvent->SetAutoLoad(kTRUE);gAliEveEvent->AddNewEventCommand(\"alieve_online_on_new_event()\");gEve->AddEvent(gAliEveEvent);};");
2777   gROOT->ProcessLine("alieve_online_init()");
2778
2779   return kTRUE;
2780 }
2781   
2782 //_____________________________________________________________________________
2783 void AliReconstruction::RunAliEVE()
2784 {
2785   // Runs AliEVE visualisation of
2786   // the current event.
2787   // Should be executed only after
2788   // successful initialization of AliEVE.
2789
2790   AliInfo("Running AliEVE...");
2791   gROOT->ProcessLine(Form("gAliEveEvent->SetEvent((AliRunLoader*)%p,(AliRawReader*)%p,(AliESDEvent*)%p);",fRunLoader,fRawReader,fesd));
2792   gROOT->ProcessLine("gAliEveEvent->StartStopAutoLoadTimer();");
2793   gSystem->Run();
2794 }
2795
2796 //_____________________________________________________________________________
2797 Bool_t AliReconstruction::SetRunQA(TString detAndAction) 
2798 {
2799         // Allows to run QA for a selected set of detectors
2800         // and a selected set of tasks among RAWS, RECPOINTS and ESDS
2801         // all selected detectors run the same selected tasks
2802         
2803         if (!detAndAction.Contains(":")) {
2804                 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
2805                 fRunQA = kFALSE ;
2806                 return kFALSE ;                 
2807         }
2808         Int_t colon = detAndAction.Index(":") ; 
2809         fQADetectors = detAndAction(0, colon) ; 
2810         if (fQADetectors.Contains("ALL") )
2811                 fQADetectors = fFillESD ; 
2812         fQATasks   = detAndAction(colon+1, detAndAction.Sizeof() ) ; 
2813         if (fQATasks.Contains("ALL") ) {
2814                 fQATasks = Form("%d %d %d", AliQA::kRAWS, AliQA::kRECPOINTS, AliQA::kESDS) ; 
2815         } else {
2816                 fQATasks.ToUpper() ; 
2817                 TString tempo("") ; 
2818                 if ( fQATasks.Contains("RAW") ) 
2819                         tempo = Form("%d ", AliQA::kRAWS) ; 
2820                 if ( fQATasks.Contains("RECPOINT") ) 
2821                         tempo += Form("%d ", AliQA::kRECPOINTS) ; 
2822                 if ( fQATasks.Contains("ESD") ) 
2823                         tempo += Form("%d ", AliQA::kESDS) ; 
2824                 fQATasks = tempo ; 
2825                 if (fQATasks.IsNull()) {
2826                         AliInfo("No QA requested\n")  ;
2827                         fRunQA = kFALSE ;
2828                         return kTRUE ; 
2829                 }
2830         }       
2831         TString tempo(fQATasks) ; 
2832     tempo.ReplaceAll(Form("%d", AliQA::kRAWS), AliQA::GetTaskName(AliQA::kRAWS))        ;
2833     tempo.ReplaceAll(Form("%d", AliQA::kRECPOINTS), AliQA::GetTaskName(AliQA::kRECPOINTS)) ;    
2834     tempo.ReplaceAll(Form("%d", AliQA::kESDS), AliQA::GetTaskName(AliQA::kESDS)) ;      
2835         AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;  
2836         fRunQA = kTRUE ;
2837         return kTRUE; 
2838
2839
2840