Added methods for finding a given module in the DDL map (F. Prino)
[u/mrichter/AliRoot.git] / STEER / AliTrackMapper.cxx
CommitLineData
f0a0d075 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
acd84897 16/* $Id$ */
f0a0d075 17
18////////////////////////////////////////////////////////////////////////
19//
20// AliTrackMapper.cxx
21// description:
22// create an AliTrackMap - a relation between track label and
23// it's index in TreeH. Check all branches with hits.
24// Output is in the root file.
25//
26////////////////////////////////////////////////////////////////////////
27
b16a1b1e 28#include <Riostream.h>
f0a0d075 29
0742d588 30#include "TError.h"
f0a0d075 31#include "TFile.h"
f0a0d075 32#include "TStopwatch.h"
0742d588 33#include "TTree.h"
f0a0d075 34
21bf7095 35#include "AliLog.h"
f0a0d075 36#include "AliDetector.h"
0742d588 37#include "AliHit.h"
38#include "AliLoader.h"
f0a0d075 39#include "AliRun.h"
88cb7938 40#include "AliRunLoader.h"
0742d588 41#include "AliTrackMap.h"
42#include "AliTrackMapper.h"
f0a0d075 43
44ClassImp(AliTrackMapper)
45
e2afb3b6 46//_______________________________________________________________________
47AliTrackMapper::AliTrackMapper():
48 fDEBUG(0)
49{
50 //
51 // Default ctor
52 //
53}
54
55
56//_______________________________________________________________________
f0a0d075 57void AliTrackMapper::CreateMap(Int_t nEvents, Int_t firstEventNr,
e2afb3b6 58 const char* fnMap, const char* fnHits)
f0a0d075 59{
e2afb3b6 60 //
61 // method to create a track map for a given number of events starting
62 // from event firstEventNr. This method just opens and closes files and
63 // loops over events, the creation of map is delegated to
64 // CreateMap(Int_t eventNr, TFile* fileMap) method
65 //
f0a0d075 66 TStopwatch timer;
67 timer.Start();
68
69 TFile *fileMap=TFile::Open(fnMap,"new");
21bf7095 70 if (!fileMap->IsOpen()) {AliErrorClass(Form("Can't open output file %s!", fnMap)); return;}
88cb7938 71
72 AliRunLoader* rl = AliRunLoader::Open(fnHits);
21bf7095 73 if (rl == 0x0) {AliErrorClass(Form("Can't open input file %s!\n", fnHits)); return;}
88cb7938 74 if (rl->LoadgAlice())
75 {
21bf7095 76 AliErrorClass("Error occured while loading AliRun");
88cb7938 77 return;
78 }
79
80 if (!(gAlice=rl->GetAliRun())) {
21bf7095 81 AliErrorClass("gAlice have not been found on session !");
f0a0d075 82 return;
83 }
e2afb3b6 84
88cb7938 85 rl->LoadKinematics();
86
f0a0d075 87 for (Int_t eventNr = firstEventNr; eventNr < firstEventNr+nEvents;
88 eventNr++) {
88cb7938 89 CreateMap(eventNr,fileMap,rl);
f0a0d075 90 } // end loop over events
e2afb3b6 91
88cb7938 92 delete rl;
f0a0d075 93 fileMap->Close();
94 delete fileMap;
95 timer.Stop();
96 if (fDEBUG > 0) timer.Print();
97}
98
e2afb3b6 99//_______________________________________________________________________
88cb7938 100Int_t AliTrackMapper::CreateMap(Int_t eventNr, TFile* fileMap,AliRunLoader* rl)
e2afb3b6 101{
102 //
103 // create an AliTrackMap for a given event
104 // correct gAlice must be already present in memory
105 //
f0a0d075 106
88cb7938 107 rl->GetEvent(eventNr);
108
109 TTree *treeK = rl->TreeK();
f0a0d075 110 if (!treeK) {
21bf7095 111 AliErrorClass(Form("Event %d, treeK not found.", eventNr));
f0a0d075 112 return -1;
113 }
114 Int_t nAllParticles = static_cast<Int_t>(treeK->GetEntries());
115 Int_t *trackMap = new Int_t[nAllParticles];
116 for (Int_t i = 0; i<nAllParticles; i++) {trackMap[i] = kNoEntry;}
117
f0a0d075 118
f0a0d075 119 TObjArray *modules = gAlice->Detectors();
120 if (!modules) {
21bf7095 121 AliErrorClass("TObjArray with modules not found.");
f0a0d075 122 return -1;
123 }
124 Int_t nModules = static_cast<Int_t>(modules->GetEntries());
5c7406fa 125 AliHit* hit;
88cb7938 126 for (Int_t iModule = 0; iModule < nModules; iModule++)
127 {
128 AliDetector * detector = dynamic_cast<AliDetector*>(modules->At(iModule));
129 if (!detector) continue;
130 AliLoader* loader = detector->GetLoader();
131 if (loader == 0x0)
132 {
21bf7095 133 AliWarningClass(Form("Can not get loader from detector %s.",
134 detector->GetName()));
88cb7938 135 continue;
136 }
137 Int_t retval = loader->LoadHits();
138 if (retval) {
21bf7095 139 AliErrorClass(Form("Event %d: error occured while loading hits for %s",
140 eventNr,detector->GetName()));
88cb7938 141 return -1;
142 }
143
144 TTree *treeH = loader->TreeH();
145 if (!treeH) {
21bf7095 146 AliErrorClass(Form("Event %d: Can not get TreeH for %s",
147 eventNr,detector->GetName()));
88cb7938 148 return -1;
149 }
150 Int_t treeHEntries = static_cast<Int_t>(treeH->GetEntries());
21bf7095 151 AliDebugClass(2, Form("treeHEntries %d", treeHEntries));
88cb7938 152
153 detector->ResetHits();
154
155 for (Int_t treeHIndex = 0; treeHIndex < treeHEntries; treeHIndex++)
156 { // process only detectors with shunt = 0
157 treeH->GetEvent(treeHIndex);
5c7406fa 158 if (detector->GetIshunt()) continue;
f0a0d075 159
e2afb3b6 160 hit=dynamic_cast<AliHit*>(detector->FirstHit(-1));
f0a0d075 161 Int_t lastLabel=-1, label;
e2afb3b6 162 for( ; hit; hit=dynamic_cast<AliHit*>(detector->NextHit()) ) {
88cb7938 163 label=hit->Track();
164 if (lastLabel != label) {
165 if (label < 0 || label >= nAllParticles) {
21bf7095 166 AliErrorClass(Form("label out of range. Event %d treeHIndex %d label = %d",
167 eventNr, treeHIndex, label));
88cb7938 168 return -2;
169 }
170 if (trackMap[label] >=0 && trackMap[label] != treeHIndex) {
21bf7095 171 AliErrorClass(Form("different treeHIndex for label %d indeces: %d != %d event %d detector %s",
172 label, trackMap[label], treeHIndex, eventNr, detector->GetName()));
88cb7938 173 return -3;
174 }
175 trackMap[label] = treeHIndex;
21bf7095 176 AliDebugClass(3, Form("treeHIndex, label = %d %d", treeHIndex, label));
88cb7938 177 lastLabel = label;
178 }
f0a0d075 179 }
88cb7938 180 }//loop over hits in module
181 loader->UnloadHits();
182 }//loop over modules
f0a0d075 183
21bf7095 184 ToAliDebugClass(3,
f0a0d075 185 for (Int_t i = 0; i < nAllParticles; i++) {
186 cout<<eventNr<<"\t"<<i<<"\t"<<trackMap[i]<<endl;
187 }
21bf7095 188 )
f0a0d075 189 fileMap->cd();
190 AliTrackMap* trackMapObject = new AliTrackMap(nAllParticles, trackMap);
191 trackMapObject->SetEventNr(eventNr);
192 trackMapObject->Write();
193 delete trackMapObject;
194
195 delete [] trackMap;
196 return 0;
197}
198
e2afb3b6 199//_______________________________________________________________________
f0a0d075 200AliTrackMap* AliTrackMapper::LoadTrackMap(Int_t eventNr, const char* fnMap, TFile* &fileMap) {
e2afb3b6 201 //
202 // read an AliTrackMap object for the given event eventNr from
203 // the file fileMap
204 //
f0a0d075 205 fileMap=TFile::Open(fnMap);
21bf7095 206 if (!fileMap->IsOpen()) {AliErrorClass(Form("Can't open file %s with map!", fnMap)); return 0;}
f0a0d075 207 char mapName[20];
208 sprintf(mapName,"AliTrackMap_%5.5d",eventNr);
e2afb3b6 209 AliTrackMap* trackMapObject = dynamic_cast<AliTrackMap*>(fileMap->Get(mapName));
f0a0d075 210 if (!trackMapObject) {
21bf7095 211 AliErrorClass(Form("map named %s not found.", mapName));
f0a0d075 212 return 0;
213 }
214 return trackMapObject;
215}
e2afb3b6 216
217//_______________________________________________________________________
f0a0d075 218void AliTrackMapper::CheckTrackMap(Int_t eventNr, const char* fnMap) {
e2afb3b6 219 //
220 //
221 //
f0a0d075 222 TFile *fileMap;
223 AliTrackMap* trackMapObject = LoadTrackMap(eventNr, fnMap, fileMap);
224 if (!trackMapObject) return;
e2afb3b6 225
f0a0d075 226 trackMapObject->PrintValues();
e2afb3b6 227
f0a0d075 228 delete trackMapObject;
229 fileMap->Close();
230 delete fileMap;
231}
f0a0d075 232