ROOT logo
#if !defined(__CINT__) || defined(__MAKECINT__)

#include <Riostream.h>
#include <TROOT.h>
#include <TString.h>
#include "AliRawReader.h"
#include "AliRawReaderDate.h"
#include "AliRawReaderRoot.h"
#include "AliRun.h"
#include "AliRunLoader.h"
#include "AliITSInitGeometry.h"
#include "AliITSgeom.h"
#include "AliITSLoader.h"
#include "AliCDBManager.h"
#include "AliITSDetTypeRec.h"
#include "AliGeomManager.h"

#endif

/*
$Id$ 
*/
/***************************************************************
 *  This macro performs the ITS local reconstruction           *
 *  It is intended for special purposes and tests.             *
 *  The reccomended way to reconstruct ITs is via the          *
 *  class STEER/AliReconstruction                              *
 *  Present version: M.Masera - Previous version: J. Belikov   *
 ***************************************************************/

void Reconstruct(AliRunLoader* runLoader,Option_t *opt);
void Reconstruct(AliRunLoader* runLoader, AliRawReader *rawreader,Option_t *opt);

Int_t AliITSFindClustersV2(char *inputRawData = NULL,TString filename="galice.root",Option_t *opt="All"){
  // if kine tree is available MC labels are used for rec points
  // set opt equal to "SPD" or to "SDD" or to "SSD" do build
  // rec points for individual subdetectors 
  if (gAlice) {
    delete AliRunLoader::Instance();
    delete gAlice;
    gAlice = NULL;
  }

  // Get geometry
  AliGeomManager::LoadGeometry("geometry.root");

  //Get Run loader and ITS loader - set geometry
  AliRunLoader* rl = AliRunLoader::Open(filename.Data());

  AliITSInitGeometry initgeom;
  AliITSgeom *geom = initgeom.CreateAliITSgeom();
  printf("Geometry name: %s \n",(initgeom.GetGeometryName()).Data());
  AliITSLoader* loader = static_cast<AliITSLoader*>(rl->GetLoader("ITSLoader"));
  if (!loader) {
    Error("Init", "ITS loader not found");
    return -1;
  }
  loader->SetITSgeom(geom);

  // Set OCDB if needed
  AliCDBManager* man = AliCDBManager::Instance();
  if (!man->IsDefaultStorageSet()) {
    printf("Setting a local default storage and run number 0\n");
    man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
    man->SetRun(0);
  }
  else {
    printf("Using deafult storage \n");
  }

  AliRawReader *rawreader = NULL;
  TString fileRaw(inputRawData);
  if(!fileRaw.IsNull()){
    if (fileRaw.EndsWith(".root")) {
      cout<<"Raw data format - ROOT file \n"; 
      rawreader = new AliRawReaderRoot(fileRaw); // root format
    }
    else {
      cout<<"Raw data format - DATE file \n";
      rawreader = new AliRawReaderDate(fileRaw);  // DATE format
    }
    //    if (!fEquipIdMap.IsNull() && fRawReader)fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
    Reconstruct(rl,rawreader,opt);
  }
  else {
    cout<< "Starting from DIGITS \n";
    Reconstruct(rl,opt);
  }

  return 0;
}

//________________________________________________________________________
void Reconstruct(AliRunLoader* runLoader,Option_t *opt){
// reconstruct clusters starting from DIGITS
// MC truth if available is used to label clusters according to the particles
// originating them

  AliITSLoader* loader = 
                static_cast<AliITSLoader*>(runLoader->GetLoader("ITSLoader"));
  if (!loader) {
    Error("Reconstruct", "ITS loader not found");
    return;
  }
  AliITSDetTypeRec* rec = new AliITSDetTypeRec();
  rec->SetITSgeom(loader->GetITSgeom());
  rec->SetDefaults();

  runLoader->LoadKinematics();
  TTree *trK=(TTree*)runLoader->TreeK();
  if(trK){
    cout<<"kine tree found - MC labels will be used in RP \n";
    if(runLoader->LoadgAlice())gAlice = runLoader->GetAliRun();
  }
  else{
    cout<<"kine tree not found - MC labels will not b used\n";
  }

  Int_t nEvents = runLoader->GetNumberOfEvents();
  // loop on the events

  for (Int_t iEvent = 0; iEvent < nEvents; iEvent++) {
    runLoader->GetEvent(iEvent);
    cout<<">>>>>>>   Processing event number "<<iEvent+1<<endl;
    loader->LoadRecPoints("update");
    loader->CleanRecPoints();
    loader->MakeRecPointsContainer();
    loader->LoadDigits("read");
    TTree *tR = loader->TreeR();
    TTree *tD = loader->TreeD();
    if(!tR){
      cout<<"Tree R pointer not found - Abort \n";
      break;
    }
    rec->SetTreeAddressD(tD);
    rec->MakeBranch(tR,"R");
    rec->SetTreeAddressR(tR);
    rec->DigitsToRecPoints(tD,tR,0,opt,kTRUE);    
    loader->WriteRecPoints("OVERWRITE");
    loader->UnloadRecPoints();
    loader->UnloadDigits();
    runLoader->UnloadKinematics();
  }

}

void Reconstruct(AliRunLoader* runLoader, AliRawReader *rawreader,Option_t *opt){
// reconstruct clusters starting from raw data (root or DATE format)
// MC truth if available is used to label clusters according to the particles
// originating them

  AliITSLoader* loader = static_cast<AliITSLoader*>(runLoader->GetLoader("ITSLoader"));
  if (!loader) {
    Error("Reconstruct", "ITS loader not found");
    return;
  }
  AliITSDetTypeRec* rec = new AliITSDetTypeRec();
  rec->SetITSgeom(loader->GetITSgeom());
  rec->SetDefaults();
  // direct clusterfinding starting from raw data is implemented only 
  // in AliITSClusterFinderV2
  rec->SetDefaultClusterFindersV2(kTRUE);

  runLoader->LoadKinematics();
  TTree *trK=(TTree*)runLoader->TreeK();
  if(trK){
    cout<<"kine tree found - MC labels will be used in RP \n";
    if(runLoader->LoadgAlice())gAlice = runLoader->GetAliRun();
  }
  Int_t nEvents = runLoader->GetNumberOfEvents();
  rawreader->RewindEvents();
  for (Int_t iEvent = 0; iEvent < nEvents; iEvent++) {
    rawreader->NextEvent();
    runLoader->GetEvent(iEvent);
    cout<<">>>>>>>   Processing event number: "<<iEvent<<endl;
    loader->LoadRecPoints("update");
    loader->CleanRecPoints();
    loader->MakeRecPointsContainer();
    TTree *tR = loader->TreeR();
    if(!tR){
      cout<<"Tree R pointer not found - Abort \n";
      break;
    }
    rec->DigitsToRecPoints(rawreader,tR,opt);
    rec->ResetRecPoints();
    rec->ResetClusters();    
    loader->WriteRecPoints("OVERWRITE");
    loader->UnloadRecPoints();
    loader->UnloadDigits();
    runLoader->UnloadKinematics();
  }

}
 AliITSFindClustersV2.C:1
 AliITSFindClustersV2.C:2
 AliITSFindClustersV2.C:3
 AliITSFindClustersV2.C:4
 AliITSFindClustersV2.C:5
 AliITSFindClustersV2.C:6
 AliITSFindClustersV2.C:7
 AliITSFindClustersV2.C:8
 AliITSFindClustersV2.C:9
 AliITSFindClustersV2.C:10
 AliITSFindClustersV2.C:11
 AliITSFindClustersV2.C:12
 AliITSFindClustersV2.C:13
 AliITSFindClustersV2.C:14
 AliITSFindClustersV2.C:15
 AliITSFindClustersV2.C:16
 AliITSFindClustersV2.C:17
 AliITSFindClustersV2.C:18
 AliITSFindClustersV2.C:19
 AliITSFindClustersV2.C:20
 AliITSFindClustersV2.C:21
 AliITSFindClustersV2.C:22
 AliITSFindClustersV2.C:23
 AliITSFindClustersV2.C:24
 AliITSFindClustersV2.C:25
 AliITSFindClustersV2.C:26
 AliITSFindClustersV2.C:27
 AliITSFindClustersV2.C:28
 AliITSFindClustersV2.C:29
 AliITSFindClustersV2.C:30
 AliITSFindClustersV2.C:31
 AliITSFindClustersV2.C:32
 AliITSFindClustersV2.C:33
 AliITSFindClustersV2.C:34
 AliITSFindClustersV2.C:35
 AliITSFindClustersV2.C:36
 AliITSFindClustersV2.C:37
 AliITSFindClustersV2.C:38
 AliITSFindClustersV2.C:39
 AliITSFindClustersV2.C:40
 AliITSFindClustersV2.C:41
 AliITSFindClustersV2.C:42
 AliITSFindClustersV2.C:43
 AliITSFindClustersV2.C:44
 AliITSFindClustersV2.C:45
 AliITSFindClustersV2.C:46
 AliITSFindClustersV2.C:47
 AliITSFindClustersV2.C:48
 AliITSFindClustersV2.C:49
 AliITSFindClustersV2.C:50
 AliITSFindClustersV2.C:51
 AliITSFindClustersV2.C:52
 AliITSFindClustersV2.C:53
 AliITSFindClustersV2.C:54
 AliITSFindClustersV2.C:55
 AliITSFindClustersV2.C:56
 AliITSFindClustersV2.C:57
 AliITSFindClustersV2.C:58
 AliITSFindClustersV2.C:59
 AliITSFindClustersV2.C:60
 AliITSFindClustersV2.C:61
 AliITSFindClustersV2.C:62
 AliITSFindClustersV2.C:63
 AliITSFindClustersV2.C:64
 AliITSFindClustersV2.C:65
 AliITSFindClustersV2.C:66
 AliITSFindClustersV2.C:67
 AliITSFindClustersV2.C:68
 AliITSFindClustersV2.C:69
 AliITSFindClustersV2.C:70
 AliITSFindClustersV2.C:71
 AliITSFindClustersV2.C:72
 AliITSFindClustersV2.C:73
 AliITSFindClustersV2.C:74
 AliITSFindClustersV2.C:75
 AliITSFindClustersV2.C:76
 AliITSFindClustersV2.C:77
 AliITSFindClustersV2.C:78
 AliITSFindClustersV2.C:79
 AliITSFindClustersV2.C:80
 AliITSFindClustersV2.C:81
 AliITSFindClustersV2.C:82
 AliITSFindClustersV2.C:83
 AliITSFindClustersV2.C:84
 AliITSFindClustersV2.C:85
 AliITSFindClustersV2.C:86
 AliITSFindClustersV2.C:87
 AliITSFindClustersV2.C:88
 AliITSFindClustersV2.C:89
 AliITSFindClustersV2.C:90
 AliITSFindClustersV2.C:91
 AliITSFindClustersV2.C:92
 AliITSFindClustersV2.C:93
 AliITSFindClustersV2.C:94
 AliITSFindClustersV2.C:95
 AliITSFindClustersV2.C:96
 AliITSFindClustersV2.C:97
 AliITSFindClustersV2.C:98
 AliITSFindClustersV2.C:99
 AliITSFindClustersV2.C:100
 AliITSFindClustersV2.C:101
 AliITSFindClustersV2.C:102
 AliITSFindClustersV2.C:103
 AliITSFindClustersV2.C:104
 AliITSFindClustersV2.C:105
 AliITSFindClustersV2.C:106
 AliITSFindClustersV2.C:107
 AliITSFindClustersV2.C:108
 AliITSFindClustersV2.C:109
 AliITSFindClustersV2.C:110
 AliITSFindClustersV2.C:111
 AliITSFindClustersV2.C:112
 AliITSFindClustersV2.C:113
 AliITSFindClustersV2.C:114
 AliITSFindClustersV2.C:115
 AliITSFindClustersV2.C:116
 AliITSFindClustersV2.C:117
 AliITSFindClustersV2.C:118
 AliITSFindClustersV2.C:119
 AliITSFindClustersV2.C:120
 AliITSFindClustersV2.C:121
 AliITSFindClustersV2.C:122
 AliITSFindClustersV2.C:123
 AliITSFindClustersV2.C:124
 AliITSFindClustersV2.C:125
 AliITSFindClustersV2.C:126
 AliITSFindClustersV2.C:127
 AliITSFindClustersV2.C:128
 AliITSFindClustersV2.C:129
 AliITSFindClustersV2.C:130
 AliITSFindClustersV2.C:131
 AliITSFindClustersV2.C:132
 AliITSFindClustersV2.C:133
 AliITSFindClustersV2.C:134
 AliITSFindClustersV2.C:135
 AliITSFindClustersV2.C:136
 AliITSFindClustersV2.C:137
 AliITSFindClustersV2.C:138
 AliITSFindClustersV2.C:139
 AliITSFindClustersV2.C:140
 AliITSFindClustersV2.C:141
 AliITSFindClustersV2.C:142
 AliITSFindClustersV2.C:143
 AliITSFindClustersV2.C:144
 AliITSFindClustersV2.C:145
 AliITSFindClustersV2.C:146
 AliITSFindClustersV2.C:147
 AliITSFindClustersV2.C:148
 AliITSFindClustersV2.C:149
 AliITSFindClustersV2.C:150
 AliITSFindClustersV2.C:151
 AliITSFindClustersV2.C:152
 AliITSFindClustersV2.C:153
 AliITSFindClustersV2.C:154
 AliITSFindClustersV2.C:155
 AliITSFindClustersV2.C:156
 AliITSFindClustersV2.C:157
 AliITSFindClustersV2.C:158
 AliITSFindClustersV2.C:159
 AliITSFindClustersV2.C:160
 AliITSFindClustersV2.C:161
 AliITSFindClustersV2.C:162
 AliITSFindClustersV2.C:163
 AliITSFindClustersV2.C:164
 AliITSFindClustersV2.C:165
 AliITSFindClustersV2.C:166
 AliITSFindClustersV2.C:167
 AliITSFindClustersV2.C:168
 AliITSFindClustersV2.C:169
 AliITSFindClustersV2.C:170
 AliITSFindClustersV2.C:171
 AliITSFindClustersV2.C:172
 AliITSFindClustersV2.C:173
 AliITSFindClustersV2.C:174
 AliITSFindClustersV2.C:175
 AliITSFindClustersV2.C:176
 AliITSFindClustersV2.C:177
 AliITSFindClustersV2.C:178
 AliITSFindClustersV2.C:179
 AliITSFindClustersV2.C:180
 AliITSFindClustersV2.C:181
 AliITSFindClustersV2.C:182
 AliITSFindClustersV2.C:183
 AliITSFindClustersV2.C:184
 AliITSFindClustersV2.C:185
 AliITSFindClustersV2.C:186
 AliITSFindClustersV2.C:187
 AliITSFindClustersV2.C:188
 AliITSFindClustersV2.C:189
 AliITSFindClustersV2.C:190
 AliITSFindClustersV2.C:191
 AliITSFindClustersV2.C:192
 AliITSFindClustersV2.C:193
 AliITSFindClustersV2.C:194