use AliLog message scheme
[u/mrichter/AliRoot.git] / STEER / AliReconstruction.cxx
CommitLineData
596a855f 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. //
c71de921 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// //
596a855f 41// The name of the galice file can be changed from the default //
e583c30d 42// "galice.root" by passing it as argument to the AliReconstruction //
43// constructor or by //
596a855f 44// //
45// rec.SetGAliceFile("..."); //
46// //
59697224 47// The local reconstruction can be switched on or off for individual //
48// detectors by //
596a855f 49// //
59697224 50// rec.SetRunLocalReconstruction("..."); //
596a855f 51// //
52// The argument is a (case sensitive) string with the names of the //
53// detectors separated by a space. The special string "ALL" selects all //
54// available detectors. This is the default. //
55// //
c71de921 56// The reconstruction of the primary vertex position can be switched off by //
57// //
58// rec.SetRunVertexFinder(kFALSE); //
59// //
596a855f 60// The tracking in ITS, TPC and TRD and the creation of ESD tracks can be //
61// switched off by //
62// //
63// rec.SetRunTracking(kFALSE); //
64// //
65// The filling of additional ESD information can be steered by //
66// //
67// rec.SetFillESD("..."); //
68// //
69// Again, the string specifies the list of detectors. The default is "ALL". //
70// //
c71de921 71// The reconstruction requires digits or raw data as input. For the creation //
72// of digits and raw data have a look at the class AliSimulation. //
596a855f 73// //
24f7a148 74// For debug purposes the method SetCheckPointLevel can be used. If the //
75// argument is greater than 0, files with ESD events will be written after //
76// selected steps of the reconstruction for each event: //
77// level 1: after tracking and after filling of ESD (final) //
78// level 2: in addition after each tracking step //
79// level 3: in addition after the filling of ESD for each detector //
80// If a final check point file exists for an event, this event will be //
81// skipped in the reconstruction. The tracking and the filling of ESD for //
82// a detector will be skipped as well, if the corresponding check point //
83// file exists. The ESD event will then be loaded from the file instead. //
84// //
596a855f 85///////////////////////////////////////////////////////////////////////////////
86
024a7e64 87#include <TArrayF.h>
88#include <TFile.h>
89#include <TSystem.h>
90#include <TROOT.h>
91#include <TPluginManager.h>
fd46e2d2 92#include <TStopwatch.h>
596a855f 93
94#include "AliReconstruction.h"
815c2b38 95#include "AliLog.h"
596a855f 96#include "AliRunLoader.h"
97#include "AliRun.h"
b649205a 98#include "AliRawReaderFile.h"
99#include "AliRawReaderDate.h"
100#include "AliRawReaderRoot.h"
596a855f 101#include "AliTracker.h"
102#include "AliESD.h"
2257f27e 103#include "AliESDVertex.h"
104#include "AliVertexer.h"
596a855f 105#include "AliHeader.h"
106#include "AliGenEventHeader.h"
107#include "AliESDpid.h"
a866ac60 108#include "AliMagF.h"
596a855f 109
110ClassImp(AliReconstruction)
111
112
113//_____________________________________________________________________________
482070f2 114const char* AliReconstruction::fgkDetectorName[AliReconstruction::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "RICH", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "START", "VZERO", "CRT", "HLT"};
c757bafd 115
116//_____________________________________________________________________________
e583c30d 117AliReconstruction::AliReconstruction(const char* gAliceFilename,
118 const char* name, const char* title) :
119 TNamed(name, title),
120
59697224 121 fRunLocalReconstruction("ALL"),
2257f27e 122 fRunVertexFinder(kTRUE),
e583c30d 123 fRunTracking(kTRUE),
124 fFillESD("ALL"),
125 fGAliceFileName(gAliceFilename),
b649205a 126 fInput(""),
e583c30d 127 fStopOnError(kFALSE),
24f7a148 128 fCheckPointLevel(0),
e583c30d 129
130 fRunLoader(NULL),
b649205a 131 fRawReader(NULL),
e583c30d 132 fITSLoader(NULL),
2257f27e 133 fITSVertexer(NULL),
e583c30d 134 fITSTracker(NULL),
135 fTPCLoader(NULL),
136 fTPCTracker(NULL),
137 fTRDLoader(NULL),
138 fTRDTracker(NULL),
139 fTOFLoader(NULL),
efd2085e 140 fTOFTracker(NULL),
141
142 fReconstructors(),
143 fOptions()
596a855f 144{
145// create reconstruction object with default parameters
146
596a855f 147}
148
149//_____________________________________________________________________________
150AliReconstruction::AliReconstruction(const AliReconstruction& rec) :
e583c30d 151 TNamed(rec),
152
59697224 153 fRunLocalReconstruction(rec.fRunLocalReconstruction),
2257f27e 154 fRunVertexFinder(rec.fRunVertexFinder),
e583c30d 155 fRunTracking(rec.fRunTracking),
156 fFillESD(rec.fFillESD),
157 fGAliceFileName(rec.fGAliceFileName),
b649205a 158 fInput(rec.fInput),
e583c30d 159 fStopOnError(rec.fStopOnError),
24f7a148 160 fCheckPointLevel(0),
e583c30d 161
162 fRunLoader(NULL),
b649205a 163 fRawReader(NULL),
e583c30d 164 fITSLoader(NULL),
2257f27e 165 fITSVertexer(NULL),
e583c30d 166 fITSTracker(NULL),
167 fTPCLoader(NULL),
168 fTPCTracker(NULL),
169 fTRDLoader(NULL),
170 fTRDTracker(NULL),
171 fTOFLoader(NULL),
efd2085e 172 fTOFTracker(NULL),
173
174 fReconstructors(),
175 fOptions()
596a855f 176{
177// copy constructor
178
efd2085e 179 for (Int_t i = 0; i < fOptions.GetEntriesFast(); i++) {
180 if (rec.fOptions[i]) fOptions.Add(rec.fOptions[i]->Clone());
181 }
596a855f 182}
183
184//_____________________________________________________________________________
185AliReconstruction& AliReconstruction::operator = (const AliReconstruction& rec)
186{
187// assignment operator
188
189 this->~AliReconstruction();
190 new(this) AliReconstruction(rec);
191 return *this;
192}
193
194//_____________________________________________________________________________
195AliReconstruction::~AliReconstruction()
196{
197// clean up
198
e583c30d 199 CleanUp();
efd2085e 200 fOptions.Delete();
596a855f 201}
202
203
204//_____________________________________________________________________________
205void AliReconstruction::SetGAliceFile(const char* fileName)
206{
207// set the name of the galice file
208
209 fGAliceFileName = fileName;
210}
211
efd2085e 212//_____________________________________________________________________________
213void AliReconstruction::SetOption(const char* detector, const char* option)
214{
215// set options for the reconstruction of a detector
216
217 TObject* obj = fOptions.FindObject(detector);
218 if (obj) fOptions.Remove(obj);
219 fOptions.Add(new TNamed(detector, option));
220}
221
596a855f 222
223//_____________________________________________________________________________
b649205a 224Bool_t AliReconstruction::Run(const char* input)
596a855f 225{
226// run the reconstruction
227
b649205a 228 // set the input
229 if (!input) input = fInput.Data();
230 TString fileName(input);
231 if (fileName.EndsWith("/")) {
232 fRawReader = new AliRawReaderFile(fileName);
233 } else if (fileName.EndsWith(".root")) {
234 fRawReader = new AliRawReaderRoot(fileName);
235 } else if (!fileName.IsNull()) {
236 fRawReader = new AliRawReaderDate(fileName);
237 fRawReader->SelectEvents(7);
238 }
239
596a855f 240 // open the run loader
596a855f 241 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data());
242 if (!fRunLoader) {
815c2b38 243 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
e583c30d 244 CleanUp();
596a855f 245 return kFALSE;
246 }
247 fRunLoader->LoadgAlice();
e583c30d 248 AliRun* aliRun = fRunLoader->GetAliRun();
249 if (!aliRun) {
815c2b38 250 AliError(Form("no gAlice object found in file %s",
251 fGAliceFileName.Data()));
e583c30d 252 CleanUp();
596a855f 253 return kFALSE;
254 }
e583c30d 255 gAlice = aliRun;
e1e1d970 256 AliTracker::SetFieldMap(gAlice->Field());
596a855f 257
59697224 258 // load the reconstructor objects
259 TPluginManager* pluginManager = gROOT->GetPluginManager();
c757bafd 260 for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
261 TString detName = fgkDetectorName[iDet];
262 TString recName = "Ali" + detName + "Reconstructor";
482070f2 263 if (!gAlice->GetDetector(detName) && detName != "HLT") continue;
264
265 if(detName == "HLT") {
266 if (!gROOT->GetClass("AliLevel3")) {
267 gSystem->Load("libAliL3Src.so");
268 gSystem->Load("libAliL3Misc.so");
269 gSystem->Load("libAliL3Hough.so");
270 gSystem->Load("libAliL3Comp.so");
271 }
272 }
c757bafd 273
274 AliReconstructor* reconstructor = NULL;
275 // first check if a plugin is defined for the reconstructor
59697224 276 TPluginHandler* pluginHandler =
c757bafd 277 pluginManager->FindHandler("AliReconstructor", detName);
278 // if not, but the reconstructor class is implemented, add a plugin for it
279 if (!pluginHandler && gROOT->GetClass(recName.Data())) {
815c2b38 280 AliDebug(1, Form("defining plugin for %s", recName.Data()));
c757bafd 281 pluginManager->AddHandler("AliReconstructor", detName,
282 recName, detName, recName + "()");
283 pluginHandler = pluginManager->FindHandler("AliReconstructor", detName);
284 }
285 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
286 reconstructor = (AliReconstructor*) pluginHandler->ExecPlugin(0);
287 }
288 // if there is no reconstructor class for the detector use the dummy one
ea1a6e3f 289 if (!reconstructor && gAlice->GetDetector(detName)) {
815c2b38 290 AliDebug(1, Form("using dummy reconstructor for %s", detName.Data()));
c757bafd 291 reconstructor = new AliDummyReconstructor(gAlice->GetDetector(detName));
292 }
efd2085e 293 if (reconstructor) {
294 TObject* obj = fOptions.FindObject(detName.Data());
295 if (obj) reconstructor->SetOption(obj->GetTitle());
296 fReconstructors.Add(reconstructor);
297 }
59697224 298 }
299
596a855f 300 // local reconstruction
59697224 301 if (!fRunLocalReconstruction.IsNull()) {
302 if (!RunLocalReconstruction(fRunLocalReconstruction)) {
e583c30d 303 if (fStopOnError) {CleanUp(); return kFALSE;}
596a855f 304 }
305 }
2257f27e 306 if (!fRunVertexFinder && !fRunTracking && fFillESD.IsNull()) return kTRUE;
307
308 // get vertexer
309 if (fRunVertexFinder && !CreateVertexer()) {
310 if (fStopOnError) {
311 CleanUp();
312 return kFALSE;
313 }
314 }
596a855f 315
24f7a148 316 // get loaders and trackers
317 if (fRunTracking && !CreateTrackers()) {
318 if (fStopOnError) {
319 CleanUp();
320 return kFALSE;
321 }
596a855f 322 }
24f7a148 323
36711aa4 324 // create the ESD output file and tree
596a855f 325 TFile* file = TFile::Open("AliESDs.root", "RECREATE");
326 if (!file->IsOpen()) {
815c2b38 327 AliError("opening AliESDs.root failed");
e583c30d 328 if (fStopOnError) {CleanUp(file); return kFALSE;}
596a855f 329 }
36711aa4 330 AliESD* esd = new AliESD;
331 TTree* tree = new TTree("esdTree", "Tree with ESD objects");
332 tree->Branch("ESD", "AliESD", &esd);
333 delete esd;
334 gROOT->cd();
596a855f 335
336 // loop over events
b649205a 337 if (fRawReader) fRawReader->RewindEvents();
596a855f 338 for (Int_t iEvent = 0; iEvent < fRunLoader->GetNumberOfEvents(); iEvent++) {
815c2b38 339 AliInfo(Form("processing event %d", iEvent));
596a855f 340 fRunLoader->GetEvent(iEvent);
b649205a 341 if (fRawReader) fRawReader->NextEvent();
24f7a148 342
343 char fileName[256];
344 sprintf(fileName, "ESD_%d.%d_final.root",
345 aliRun->GetRunNumber(), aliRun->GetEvNumber());
346 if (!gSystem->AccessPathName(fileName)) continue;
347
36711aa4 348 esd = new AliESD;
e583c30d 349 esd->SetRunNumber(aliRun->GetRunNumber());
350 esd->SetEventNumber(aliRun->GetEvNumber());
351 esd->SetMagneticField(aliRun->Field()->SolenoidField());
596a855f 352
2257f27e 353 // vertex finder
354 if (fRunVertexFinder) {
355 if (!ReadESD(esd, "vertex")) {
356 if (!RunVertexFinder(esd)) {
357 if (fStopOnError) {CleanUp(file); return kFALSE;}
358 }
359 if (fCheckPointLevel > 0) WriteESD(esd, "vertex");
360 }
361 }
362
596a855f 363 // barrel tracking
364 if (fRunTracking) {
24f7a148 365 if (!ReadESD(esd, "tracking")) {
366 if (!RunTracking(esd)) {
367 if (fStopOnError) {CleanUp(file); return kFALSE;}
368 }
369 if (fCheckPointLevel > 0) WriteESD(esd, "tracking");
596a855f 370 }
371 }
372
373 // fill ESD
374 if (!fFillESD.IsNull()) {
375 if (!FillESD(esd, fFillESD)) {
e583c30d 376 if (fStopOnError) {CleanUp(file); return kFALSE;}
596a855f 377 }
378 }
379
380 // combined PID
381 AliESDpid::MakePID(esd);
24f7a148 382 if (fCheckPointLevel > 1) WriteESD(esd, "PID");
596a855f 383
384 // write ESD
36711aa4 385 tree->Fill();
24f7a148 386
387 if (fCheckPointLevel > 0) WriteESD(esd, "final");
d9b8978b 388 delete esd;
596a855f 389 }
390
36711aa4 391 file->cd();
392 tree->Write();
e583c30d 393 CleanUp(file);
596a855f 394
395 return kTRUE;
396}
397
398
399//_____________________________________________________________________________
59697224 400Bool_t AliReconstruction::RunLocalReconstruction(const TString& detectors)
596a855f 401{
59697224 402// run the local reconstruction
596a855f 403
030b532d 404 TStopwatch stopwatch;
405 stopwatch.Start();
406
596a855f 407 TString detStr = detectors;
c757bafd 408 for (Int_t iDet = 0; iDet < fReconstructors.GetEntriesFast(); iDet++) {
409 AliReconstructor* reconstructor =
410 (AliReconstructor*) fReconstructors[iDet];
411 TString detName = reconstructor->GetDetectorName();
412 if (IsSelected(detName, detStr)) {
815c2b38 413 AliInfo(Form("running reconstruction for %s", detName.Data()));
030b532d 414 TStopwatch stopwatchDet;
415 stopwatchDet.Start();
b649205a 416 if (fRawReader) {
417 fRawReader->RewindEvents();
418 reconstructor->Reconstruct(fRunLoader, fRawReader);
419 } else {
420 reconstructor->Reconstruct(fRunLoader);
421 }
815c2b38 422 AliInfo(Form("execution time for %s:", detName.Data()));
423 ToAliInfo(stopwatchDet.Print());
596a855f 424 }
425 }
426
427 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
815c2b38 428 AliError(Form("the following detectors were not found: %s",
429 detStr.Data()));
596a855f 430 if (fStopOnError) return kFALSE;
431 }
432
815c2b38 433 AliInfo("execution time:");
434 ToAliInfo(stopwatch.Print());
030b532d 435
596a855f 436 return kTRUE;
437}
438
439//_____________________________________________________________________________
2257f27e 440Bool_t AliReconstruction::RunVertexFinder(AliESD*& esd)
596a855f 441{
442// run the barrel tracking
443
030b532d 444 TStopwatch stopwatch;
445 stopwatch.Start();
446
2257f27e 447 AliESDVertex* vertex = NULL;
448 Double_t vtxPos[3] = {0, 0, 0};
449 Double_t vtxErr[3] = {0.07, 0.07, 0.1};
450 TArrayF mcVertex(3);
a6b0b91b 451 if (fRunLoader->GetHeader() && fRunLoader->GetHeader()->GenEventHeader()) {
452 fRunLoader->GetHeader()->GenEventHeader()->PrimaryVertex(mcVertex);
453 for (Int_t i = 0; i < 3; i++) vtxPos[i] = mcVertex[i];
454 }
2257f27e 455
456 if (fITSVertexer) {
815c2b38 457 AliInfo("running the ITS vertex finder");
2257f27e 458 vertex = fITSVertexer->FindVertexForCurrentEvent(fRunLoader->GetEventNumber());
459 if(!vertex){
815c2b38 460 AliWarning("Vertex not found");
c710f220 461 vertex = new AliESDVertex();
2257f27e 462 }
463 else {
464 vertex->SetTruePos(vtxPos); // store also the vertex from MC
465 }
466
467 } else {
815c2b38 468 AliInfo("getting the primary vertex from MC");
2257f27e 469 vertex = new AliESDVertex(vtxPos, vtxErr);
470 }
471
472 if (vertex) {
473 vertex->GetXYZ(vtxPos);
474 vertex->GetSigmaXYZ(vtxErr);
475 } else {
815c2b38 476 AliWarning("no vertex reconstructed");
2257f27e 477 vertex = new AliESDVertex(vtxPos, vtxErr);
478 }
479 esd->SetVertex(vertex);
24f7a148 480 if (fITSTracker) fITSTracker->SetVertex(vtxPos, vtxErr);
481 if (fTPCTracker) fTPCTracker->SetVertex(vtxPos, vtxErr);
482 if (fTRDTracker) fTRDTracker->SetVertex(vtxPos, vtxErr);
2257f27e 483 delete vertex;
484
815c2b38 485 AliInfo("execution time:");
486 ToAliInfo(stopwatch.Print());
2257f27e 487
488 return kTRUE;
489}
490
491//_____________________________________________________________________________
492Bool_t AliReconstruction::RunTracking(AliESD*& esd)
493{
494// run the barrel tracking
495
496 TStopwatch stopwatch;
497 stopwatch.Start();
24f7a148 498
499 if (!fTPCTracker) {
815c2b38 500 AliError("no TPC tracker");
24f7a148 501 return kFALSE;
502 }
815c2b38 503 AliInfo("running tracking");
596a855f 504
85555ca8 505 // TPC tracking
815c2b38 506 AliDebug(1, "TPC tracking");
596a855f 507 fTPCLoader->LoadRecPoints("read");
508 TTree* tpcTree = fTPCLoader->TreeR();
509 if (!tpcTree) {
815c2b38 510 AliError("Can't get the TPC cluster tree");
596a855f 511 return kFALSE;
24f7a148 512 }
596a855f 513 fTPCTracker->LoadClusters(tpcTree);
514 if (fTPCTracker->Clusters2Tracks(esd) != 0) {
815c2b38 515 AliError("TPC Clusters2Tracks failed");
596a855f 516 return kFALSE;
517 }
24f7a148 518 if (fCheckPointLevel > 1) WriteESD(esd, "TPC.tracking");
519
520 if (!fITSTracker) {
815c2b38 521 AliWarning("no ITS tracker");
24f7a148 522 } else {
523
efd2085e 524 GetReconstructor("TPC")->FillESD(fRunLoader, esd); // preliminary
24f7a148 525 AliESDpid::MakePID(esd); // PID for the ITS tracker
526
527 // ITS tracking
815c2b38 528 AliDebug(1, "ITS tracking");
24f7a148 529 fITSLoader->LoadRecPoints("read");
530 TTree* itsTree = fITSLoader->TreeR();
531 if (!itsTree) {
532 Error("RunTracking", "Can't get the ITS cluster tree");
533 return kFALSE;
534 }
535 fITSTracker->LoadClusters(itsTree);
536 if (fITSTracker->Clusters2Tracks(esd) != 0) {
815c2b38 537 AliError("ITS Clusters2Tracks failed");
24f7a148 538 return kFALSE;
539 }
540 if (fCheckPointLevel > 1) WriteESD(esd, "ITS.tracking");
596a855f 541
24f7a148 542 if (!fTRDTracker) {
815c2b38 543 AliWarning("no TRD tracker");
24f7a148 544 } else {
545 // ITS back propagation
815c2b38 546 AliDebug(1, "ITS back propagation");
24f7a148 547 if (fITSTracker->PropagateBack(esd) != 0) {
815c2b38 548 AliError("ITS backward propagation failed");
24f7a148 549 return kFALSE;
550 }
551 if (fCheckPointLevel > 1) WriteESD(esd, "ITS.back");
596a855f 552
24f7a148 553 // TPC back propagation
815c2b38 554 AliDebug(1, "TPC back propagation");
24f7a148 555 if (fTPCTracker->PropagateBack(esd) != 0) {
815c2b38 556 AliError("TPC backward propagation failed");
24f7a148 557 return kFALSE;
558 }
559 if (fCheckPointLevel > 1) WriteESD(esd, "TPC.back");
560
561 // TRD back propagation
815c2b38 562 AliDebug(1, "TRD back propagation");
24f7a148 563 fTRDLoader->LoadRecPoints("read");
564 TTree* trdTree = fTRDLoader->TreeR();
565 if (!trdTree) {
815c2b38 566 AliError("Can't get the TRD cluster tree");
24f7a148 567 return kFALSE;
568 }
569 fTRDTracker->LoadClusters(trdTree);
570 if (fTRDTracker->PropagateBack(esd) != 0) {
815c2b38 571 AliError("TRD backward propagation failed");
24f7a148 572 return kFALSE;
573 }
574 if (fCheckPointLevel > 1) WriteESD(esd, "TRD.back");
575
576 if (!fTOFTracker) {
815c2b38 577 AliWarning("no TOF tracker");
24f7a148 578 } else {
579 // TOF back propagation
815c2b38 580 AliDebug(1, "TOF back propagation");
24f7a148 581 fTOFLoader->LoadDigits("read");
582 TTree* tofTree = fTOFLoader->TreeD();
583 if (!tofTree) {
815c2b38 584 AliError("Can't get the TOF digits tree");
24f7a148 585 return kFALSE;
586 }
587 fTOFTracker->LoadClusters(tofTree);
588 if (fTOFTracker->PropagateBack(esd) != 0) {
815c2b38 589 AliError("TOF backward propagation failed");
24f7a148 590 return kFALSE;
591 }
592 if (fCheckPointLevel > 1) WriteESD(esd, "TOF.back");
593 fTOFTracker->UnloadClusters();
594 fTOFLoader->UnloadDigits();
595 }
596a855f 596
24f7a148 597 // TRD inward refit
815c2b38 598 AliDebug(1, "TRD inward refit");
24f7a148 599 if (fTRDTracker->RefitInward(esd) != 0) {
815c2b38 600 AliError("TRD inward refit failed");
24f7a148 601 return kFALSE;
602 }
603 if (fCheckPointLevel > 1) WriteESD(esd, "TRD.refit");
604 fTRDTracker->UnloadClusters();
605 fTRDLoader->UnloadRecPoints();
606
607 // TPC inward refit
815c2b38 608 AliInfo("TPC inward refit");
24f7a148 609 if (fTPCTracker->RefitInward(esd) != 0) {
815c2b38 610 AliError("TPC inward refit failed");
24f7a148 611 return kFALSE;
612 }
613 if (fCheckPointLevel > 1) WriteESD(esd, "TPC.refit");
614
615 // ITS inward refit
815c2b38 616 AliInfo("ITS inward refit");
24f7a148 617 if (fITSTracker->RefitInward(esd) != 0) {
815c2b38 618 AliError("ITS inward refit failed");
24f7a148 619 return kFALSE;
620 }
621 if (fCheckPointLevel > 1) WriteESD(esd, "ITS.refit");
596a855f 622
24f7a148 623 } // if TRD tracker
624 fITSTracker->UnloadClusters();
625 fITSLoader->UnloadRecPoints();
596a855f 626
24f7a148 627 } // if ITS tracker
596a855f 628 fTPCTracker->UnloadClusters();
629 fTPCLoader->UnloadRecPoints();
596a855f 630
815c2b38 631 AliInfo("execution time:");
632 ToAliInfo(stopwatch.Print());
030b532d 633
596a855f 634 return kTRUE;
635}
636
637//_____________________________________________________________________________
24f7a148 638Bool_t AliReconstruction::FillESD(AliESD*& esd, const TString& detectors)
596a855f 639{
640// fill the event summary data
641
030b532d 642 TStopwatch stopwatch;
643 stopwatch.Start();
815c2b38 644 AliInfo("filling ESD");
030b532d 645
596a855f 646 TString detStr = detectors;
c757bafd 647 for (Int_t iDet = 0; iDet < fReconstructors.GetEntriesFast(); iDet++) {
648 AliReconstructor* reconstructor =
649 (AliReconstructor*) fReconstructors[iDet];
650 TString detName = reconstructor->GetDetectorName();
651 if (IsSelected(detName, detStr)) {
652 if (!ReadESD(esd, detName.Data())) {
815c2b38 653 AliDebug(1, Form("filling ESD for %s", detName.Data()));
b649205a 654 if (fRawReader) {
655 reconstructor->FillESD(fRunLoader, fRawReader, esd);
656 } else {
657 reconstructor->FillESD(fRunLoader, esd);
658 }
c757bafd 659 if (fCheckPointLevel > 2) WriteESD(esd, detName.Data());
24f7a148 660 }
596a855f 661 }
662 }
663
664 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
815c2b38 665 AliError(Form("the following detectors were not found: %s",
666 detStr.Data()));
596a855f 667 if (fStopOnError) return kFALSE;
668 }
669
815c2b38 670 AliInfo("execution time:");
671 ToAliInfo(stopwatch.Print());
030b532d 672
596a855f 673 return kTRUE;
674}
675
676
677//_____________________________________________________________________________
678Bool_t AliReconstruction::IsSelected(TString detName, TString& detectors) const
679{
680// check whether detName is contained in detectors
681// if yes, it is removed from detectors
682
683 // check if all detectors are selected
684 if ((detectors.CompareTo("ALL") == 0) ||
685 detectors.BeginsWith("ALL ") ||
686 detectors.EndsWith(" ALL") ||
687 detectors.Contains(" ALL ")) {
688 detectors = "ALL";
689 return kTRUE;
690 }
691
692 // search for the given detector
693 Bool_t result = kFALSE;
694 if ((detectors.CompareTo(detName) == 0) ||
695 detectors.BeginsWith(detName+" ") ||
696 detectors.EndsWith(" "+detName) ||
697 detectors.Contains(" "+detName+" ")) {
698 detectors.ReplaceAll(detName, "");
699 result = kTRUE;
700 }
701
702 // clean up the detectors string
703 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
704 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
705 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
706
707 return result;
708}
e583c30d 709
710//_____________________________________________________________________________
c757bafd 711AliReconstructor* AliReconstruction::GetReconstructor(const char* detName) const
712{
713// get the reconstructor object for a detector
714
715 for (Int_t iDet = 0; iDet < fReconstructors.GetEntriesFast(); iDet++) {
716 AliReconstructor* reconstructor =
717 (AliReconstructor*) fReconstructors[iDet];
718 if (strcmp(reconstructor->GetDetectorName(), detName) == 0) {
719 return reconstructor;
720 }
721 }
722 return NULL;
723}
724
725//_____________________________________________________________________________
2257f27e 726Bool_t AliReconstruction::CreateVertexer()
727{
728// create the vertexer
729
730 fITSVertexer = NULL;
c757bafd 731 AliReconstructor* itsReconstructor = GetReconstructor("ITS");
59697224 732 if (itsReconstructor) {
733 fITSVertexer = itsReconstructor->CreateVertexer(fRunLoader);
2257f27e 734 }
735 if (!fITSVertexer) {
815c2b38 736 AliWarning("couldn't create a vertexer for ITS");
2257f27e 737 if (fStopOnError) return kFALSE;
738 }
739
740 return kTRUE;
741}
742
743//_____________________________________________________________________________
24f7a148 744Bool_t AliReconstruction::CreateTrackers()
745{
746// get the loaders and create the trackers
747
24f7a148 748 fITSTracker = NULL;
749 fITSLoader = fRunLoader->GetLoader("ITSLoader");
750 if (!fITSLoader) {
815c2b38 751 AliWarning("no ITS loader found");
24f7a148 752 if (fStopOnError) return kFALSE;
753 } else {
c757bafd 754 AliReconstructor* itsReconstructor = GetReconstructor("ITS");
59697224 755 if (itsReconstructor) {
756 fITSTracker = itsReconstructor->CreateTracker(fRunLoader);
24f7a148 757 }
758 if (!fITSTracker) {
815c2b38 759 AliWarning("couldn't create a tracker for ITS");
24f7a148 760 if (fStopOnError) return kFALSE;
761 }
762 }
763
764 fTPCTracker = NULL;
765 fTPCLoader = fRunLoader->GetLoader("TPCLoader");
766 if (!fTPCLoader) {
815c2b38 767 AliError("no TPC loader found");
24f7a148 768 if (fStopOnError) return kFALSE;
769 } else {
c757bafd 770 AliReconstructor* tpcReconstructor = GetReconstructor("TPC");
59697224 771 if (tpcReconstructor) {
772 fTPCTracker = tpcReconstructor->CreateTracker(fRunLoader);
24f7a148 773 }
774 if (!fTPCTracker) {
815c2b38 775 AliError("couldn't create a tracker for TPC");
24f7a148 776 if (fStopOnError) return kFALSE;
777 }
778 }
779
780 fTRDTracker = NULL;
781 fTRDLoader = fRunLoader->GetLoader("TRDLoader");
782 if (!fTRDLoader) {
815c2b38 783 AliWarning("no TRD loader found");
24f7a148 784 if (fStopOnError) return kFALSE;
785 } else {
c757bafd 786 AliReconstructor* trdReconstructor = GetReconstructor("TRD");
59697224 787 if (trdReconstructor) {
788 fTRDTracker = trdReconstructor->CreateTracker(fRunLoader);
24f7a148 789 }
790 if (!fTRDTracker) {
815c2b38 791 AliWarning("couldn't create a tracker for TRD");
24f7a148 792 if (fStopOnError) return kFALSE;
793 }
794 }
795
796 fTOFTracker = NULL;
797 fTOFLoader = fRunLoader->GetLoader("TOFLoader");
798 if (!fTOFLoader) {
815c2b38 799 AliWarning("no TOF loader found");
24f7a148 800 if (fStopOnError) return kFALSE;
801 } else {
c757bafd 802 AliReconstructor* tofReconstructor = GetReconstructor("TOF");
59697224 803 if (tofReconstructor) {
804 fTOFTracker = tofReconstructor->CreateTracker(fRunLoader);
24f7a148 805 }
806 if (!fTOFTracker) {
815c2b38 807 AliWarning("couldn't create a tracker for TOF");
24f7a148 808 if (fStopOnError) return kFALSE;
809 }
810 }
811
812 return kTRUE;
813}
814
815//_____________________________________________________________________________
e583c30d 816void AliReconstruction::CleanUp(TFile* file)
817{
818// delete trackers and the run loader and close and delete the file
819
59697224 820 fReconstructors.Delete();
821
2257f27e 822 delete fITSVertexer;
823 fITSVertexer = NULL;
e583c30d 824 delete fITSTracker;
825 fITSTracker = NULL;
826 delete fTPCTracker;
827 fTPCTracker = NULL;
828 delete fTRDTracker;
829 fTRDTracker = NULL;
830 delete fTOFTracker;
831 fTOFTracker = NULL;
832
833 delete fRunLoader;
834 fRunLoader = NULL;
b649205a 835 delete fRawReader;
836 fRawReader = NULL;
e583c30d 837
838 if (file) {
839 file->Close();
840 delete file;
841 }
842}
24f7a148 843
844
845//_____________________________________________________________________________
846Bool_t AliReconstruction::ReadESD(AliESD*& esd, const char* recStep) const
847{
848// read the ESD event from a file
849
850 if (!esd) return kFALSE;
851 char fileName[256];
852 sprintf(fileName, "ESD_%d.%d_%s.root",
853 esd->GetRunNumber(), esd->GetEventNumber(), recStep);
854 if (gSystem->AccessPathName(fileName)) return kFALSE;
855
815c2b38 856 AliDebug(1, Form("reading ESD from file %s", fileName));
24f7a148 857 TFile* file = TFile::Open(fileName);
858 if (!file || !file->IsOpen()) {
815c2b38 859 AliError(Form("opening %s failed", fileName));
24f7a148 860 delete file;
861 return kFALSE;
862 }
863
864 gROOT->cd();
865 delete esd;
866 esd = (AliESD*) file->Get("ESD");
867 file->Close();
868 delete file;
869 return kTRUE;
870}
871
872//_____________________________________________________________________________
873void AliReconstruction::WriteESD(AliESD* esd, const char* recStep) const
874{
875// write the ESD event to a file
876
877 if (!esd) return;
878 char fileName[256];
879 sprintf(fileName, "ESD_%d.%d_%s.root",
880 esd->GetRunNumber(), esd->GetEventNumber(), recStep);
881
815c2b38 882 AliDebug(1, Form("writing ESD to file %s", fileName));
24f7a148 883 TFile* file = TFile::Open(fileName, "recreate");
884 if (!file || !file->IsOpen()) {
815c2b38 885 AliError(Form("opening %s failed", fileName));
24f7a148 886 } else {
887 esd->Write("ESD");
888 file->Close();
889 }
890 delete file;
891}