Fix aborting error
[u/mrichter/AliRoot.git] / TRD / AliTRDgtuSim.cxx
CommitLineData
52c19022 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: AliTRDgtuSim.cxx 28397 2008-09-02 09:33:00Z cblume $ */
17
18////////////////////////////////////////////////////////////////////////////
19// //
20// GTU simulation //
21// //
22// Authors: J. Klein (Jochen.Klein@cern.ch) //
23// //
24////////////////////////////////////////////////////////////////////////////
25
26#include <stdio.h>
27#include <fstream>
28#include <string>
29
30#include "TFile.h"
31#include "TROOT.h"
32#include "TClonesArray.h"
33
34#include "AliTRDgtuSim.h"
52c19022 35#include "AliTRDgtuTMU.h"
36#include "AliTRDtrackGTU.h"
37#include "AliTRDtrackletWord.h"
38#include "AliTRDtrackletMCM.h"
39#include "AliESDEvent.h"
40
41#include "AliRun.h"
42#include "AliRunLoader.h"
43#include "AliLoader.h"
44#include "AliLog.h"
45
46ClassImp(AliTRDgtuSim)
47
48AliTRDgtuSim::AliTRDgtuSim(AliRunLoader *rl)
49 : TObject(),
50 fRunLoader(rl),
51 fTMU(0x0),
52 fTrackletArray(0x0),
53 fTrackTree(0x0),
54 fTrackletTree(0x0)
55{
56 fTrackletTree = new TTree("gtutracklets", "Tree with GTU tracklets");
80f93426 57 fTrackletTree->SetDirectory(0);
52c19022 58}
59
60AliTRDgtuSim::~AliTRDgtuSim()
61{
62 if (fTrackletArray)
63 fTrackletArray->Delete();
64 delete fTrackletArray;
65 delete fTrackletTree;
66}
67
68Bool_t AliTRDgtuSim::RunGTUFromTrackletFile(TString filename, Int_t event, Int_t noev)
69{
70 AliInfo(Form("Running the GTU simulation on file: %s", filename.Data()));
71 ifstream input(filename.Data());
72
73 std::string str;
74 TString string;
75 int lineno = 0;
76
77 Int_t iEventPrev = -1;
78 Int_t iStackPrev = -1;
79 Int_t iSecPrev = -1;
80 Int_t iSec = -1;
81 Int_t iStack = -1;
82 Int_t iLink = -1;
83 Int_t iEvent = -1;
84 Int_t evcnt = -1;
85
86 fTMU = 0x0;
87
c8b1590d 88 AliDebug(5,"--------- Reading from file ----------");
52c19022 89 while (getline(input, str)) {
90 lineno++;
91 string = str;
c8b1590d 92 AliDebug(5,Form("Line %i : %s", lineno, string.Data()));
52c19022 93
94 TObjArray *tokens = string.Tokenize(" ");
95 if (tokens->GetEntriesFast() < 7) {
96 AliWarning(Form("Invalid input in line %i, too few parameters", lineno));
97 continue;
98 }
99
100 if ( ((TObjString*) tokens->At(0))->GetString().Atoi() < event)
101 continue;
102
103 iEvent = ((TObjString*) tokens->At(0))->GetString().Atoi();
104 iSec = ((TObjString*) tokens->At(1))->GetString().Atoi();
105 iStack = ((TObjString*) tokens->At(2))->GetString().Atoi();
106 iLink = 2 * ((TObjString*) tokens->At(3))->GetString().Atoi() + ((TObjString*) tokens->At(4))->GetString().Atoi();
107
108 if (iEvent != iEventPrev || iStack != iStackPrev || iSec != iSecPrev) {
109 if(fTMU) {
110 TList *ListOfTracks = new TList();
111 fTMU->SetStack(iStackPrev);
112 fTMU->SetSector(iSecPrev);
113 fTMU->RunTMU(ListOfTracks);
c8b1590d 114 AliDebug(1,Form("--- There are %i tracks. Writing ...", ListOfTracks->GetEntries()));
52c19022 115 WriteTracksToTree(ListOfTracks);
116 fTMU->WriteTrackletsToTree(fTrackletTree);
117 WriteTracksToDataFile(ListOfTracks, iEventPrev);
118 if (ListOfTracks->GetEntries() > 0)
c8b1590d 119 AliDebug(2,Form(" %d GeV/c", ((AliTRDtrackGTU*) ListOfTracks->At(0))->GetPt() ));
52c19022 120 delete fTMU;
121 fTMU = new AliTRDgtuTMU();
122 delete ListOfTracks;
123 ListOfTracks = 0x0;
124 } else {
125 fTMU = new AliTRDgtuTMU();
126 }
127 iStackPrev = iStack;
128 iSecPrev = iSec;
129 iEventPrev = iEvent;
130 evcnt++;
131 if (evcnt == noev)
132 break;
133 }
134 for (Int_t i = 5; i < tokens->GetEntriesFast(); i++) {
135 UInt_t trackletWord = 0;
136 sscanf(((TObjString*) tokens->At(i))->GetString().Data(), "%x", &trackletWord);
137 if (trackletWord == 0x10001000)
138 break;
c8b1590d 139 AliDebug(2,Form("%i. tracklet: %s -> 0x%08x", i-4, ((TObjString*) tokens->At(i))->GetString().Data(), trackletWord));
52c19022 140 AliTRDtrackletWord *trkl = new AliTRDtrackletWord(trackletWord);
141 fTMU->AddTracklet(trkl, iLink);
142 }
143 }
144
145 if (fTMU && evcnt < noev) {
146 TList *ListOfTracks = new TList();
147 fTMU->SetStack(iStackPrev);
148 fTMU->SetSector(iSecPrev);
149 fTMU->RunTMU(ListOfTracks);
150 WriteTracksToTree(ListOfTracks);
151 fTMU->WriteTrackletsToTree(fTrackletTree);
152 WriteTracksToDataFile(ListOfTracks, iEventPrev);
153 delete fTMU;
154 delete ListOfTracks;
155 fTMU = 0x0;
156 }
157
158 AliInfo(Form("Analyzed %i events", evcnt));
159 return kTRUE;
160}
161
162Bool_t AliTRDgtuSim::RunGTU(AliLoader *loader, AliESDEvent *esd)
163{
164 if (!LoadTracklets(loader)) {
a8518fd2 165 AliError("Could not load the tracklets. Nothing done ...");
52c19022 166 return kFALSE;
167 }
168
169 Int_t iStackPrev = -1;
170 Int_t iSecPrev = -1;
171 Int_t iSec = -1;
172 Int_t iStack = -1;
173 Int_t iLink = -1;
174
175 if (fTMU) {
176 delete fTMU;
177 fTMU = 0x0;
178 }
179
180 TList *ListOfTracks = new TList();
181
182 TIter next(fTrackletArray);
183 AliTRDtrackletBase *trkl;
184
4cc89512 185 while ((trkl = (AliTRDtrackletBase*) next())) {
52c19022 186 iSec = trkl->GetDetector() / 30;
187 iStack = (trkl->GetDetector() % 30) / 6;
188 iLink = 2 * (trkl->GetDetector() % 6) + (trkl->GetYbin() < 0 ? 0 : 1);
189
190 if (iStack != iStackPrev || iSec != iSecPrev) {
191 if(fTMU) {
192 fTMU->SetStack(iStackPrev);
193 fTMU->SetSector(iSecPrev);
194 fTMU->RunTMU(ListOfTracks);
195 WriteTracksToTree(ListOfTracks);
196 fTMU->WriteTrackletsToTree(fTrackletTree);
197 WriteTracksToESD(ListOfTracks, esd);
198 fTMU->Reset();
199 ListOfTracks->Delete();
200 } else {
201 fTMU = new AliTRDgtuTMU();
202 }
203 iStackPrev = iStack;
204 iSecPrev = iSec;
205 }
206 fTMU->AddTracklet(trkl, iLink);
207 }
208
209 if (fTMU) {
210 fTMU->SetStack(iStackPrev);
211 fTMU->SetSector(iSecPrev);
212 fTMU->RunTMU(ListOfTracks);
213 WriteTracksToTree(ListOfTracks);
214 fTMU->WriteTrackletsToTree(fTrackletTree);
215 WriteTracksToESD(ListOfTracks, esd);
216 delete fTMU;
217 fTMU = 0x0;
218 ListOfTracks->Delete();
219 }
220
221 delete ListOfTracks;
222
223 return kTRUE;
224}
225
226Bool_t AliTRDgtuSim::LoadTracklets(AliLoader *loader)
227{
c8b1590d 228 AliDebug(1,"Loading tracklets ...");
52c19022 229
230 if (!loader) {
231 AliError("No loader given!");
232 return kFALSE;
233 }
234
235 AliDataLoader *trackletLoader = loader->GetDataLoader("tracklets");
236 if (!trackletLoader) {
237 AliError("No tracklet loader found!");
238 return kFALSE;
239 }
240
241 trackletLoader->Load();
242
243 TTree *trackletTree = trackletLoader->Tree();
244 if (!trackletTree) {
245 AliError("No tracklet tree found");
246 return kFALSE;
247 }
248
249
250 TBranch *trklbranch = trackletTree->GetBranch("mcmtrklbranch");
251 if (trklbranch) {
252 if (!fTrackletArray)
253 fTrackletArray = new TClonesArray("AliTRDtrackletMCM", 1000);
254 else if ((TClass::GetClass("AliTRDtrackletMCM"))->InheritsFrom(fTrackletArray->Class()))
255 fTrackletArray->Delete();
256 else {
257 fTrackletArray->Delete();
258 delete fTrackletArray;
259 fTrackletArray = new TClonesArray("AliTRDtrackletMCM", 1000);
260 }
261
262 AliTRDtrackletMCM *trkl = new AliTRDtrackletMCM;
263 trklbranch->SetAddress(&trkl);
264 for (Int_t iTracklet = 0; iTracklet < trklbranch->GetEntries(); iTracklet++) {
265 trklbranch->GetEntry(iTracklet);
266 new ((*fTrackletArray)[fTrackletArray->GetEntries()]) AliTRDtrackletMCM(*trkl);
267 }
268 return kTRUE;
269 }
270
271 trklbranch = trackletTree->GetBranch("trkbranch");
272
273 if (!trklbranch) {
274 AliError("Could not get trkbranch");
275 return kFALSE;
276 }
277
278 if (!fTrackletArray)
279 fTrackletArray = new TClonesArray("AliTRDtrackletWord", 1000);
280 else if ((TClass::GetClass("AliTRDtrackletWord"))->InheritsFrom(fTrackletArray->Class()))
281 fTrackletArray->Delete();
282 else {
283 fTrackletArray->Delete();
284 delete fTrackletArray;
285 fTrackletArray = new TClonesArray("AliTRDtrackletWord", 1000);
286 }
287
288 Int_t notrkl = 0;
289 UInt_t *leaves = new UInt_t[258];
c8b1590d 290 AliDebug(1,Form("No. of entries: %i", trklbranch->GetEntries()));
52c19022 291
292 for (Int_t iEntry = 0; iEntry < trklbranch->GetEntries(); iEntry++) {
293 trklbranch->SetAddress(leaves);
294 trklbranch->GetEntry(iEntry);
295 for (Int_t iTracklet = 0; iTracklet < 256; iTracklet++) {
296 if (leaves[2 + iTracklet] == 0)
297 break;
4ff7ed2b 298 new((*fTrackletArray)[notrkl]) AliTRDtrackletWord(leaves[2 + iTracklet], 2*leaves[0] + leaves[1]);
52c19022 299 notrkl++;
300 }
c8b1590d 301 AliDebug(2,Form("Entry: %3i: Det: %3i, side: %i, 1st tracklet: 0x%08x, no: %i", iEntry, leaves[0], leaves[1], leaves[2], notrkl));
52c19022 302 }
303
304 return kTRUE;
305}
306
307Bool_t AliTRDgtuSim::WriteTracksToDataFile(TList *ListOfTracks, Int_t event)
308{
309 Int_t sm = 0;
310 Int_t stack = 0;
311
312 FILE *out;
313 out = fopen("test.data", "a");
314
c8b1590d 315 AliDebug(1,Form("%i tracks found in event %i", ListOfTracks->GetSize(), event));
52c19022 316 fprintf(out, "0 %5i %2i %i 00000000\n", event, sm, stack);
317 for (Int_t i = 0; i < ListOfTracks->GetSize(); i++) {
318 AliTRDtrackGTU *trk = (AliTRDtrackGTU*) ListOfTracks->At(i);
319 sm = trk->GetSector();
320 stack = trk->GetStack();
321 fprintf(out, "1 %5i %2i %2i %3i %3i %3i %3i %3i %3i %3i %4i %f\n", event, sm, stack, trk->GetTrackletMask(),
322 trk->GetTrackletIndex(5),
323 trk->GetTrackletIndex(4),
324 trk->GetTrackletIndex(3),
325 trk->GetTrackletIndex(2),
326 trk->GetTrackletIndex(1),
327 trk->GetTrackletIndex(0),
328 trk->GetPtInt(),
329 trk->GetPt());
330 }
331 fclose(out);
332 return kTRUE;
333}
334
335Bool_t AliTRDgtuSim::WriteTracksToTree(TList *ListOfTracks, Int_t /*event*/)
336{
c8b1590d 337 AliDebug(1,Form("Writing %i tracks to the tree...", ListOfTracks->GetEntries()));
52c19022 338
339 if (!ListOfTracks)
340 return kFALSE;
341
342 if (ListOfTracks->GetEntries() <= 0)
343 return kTRUE;
344
80f93426 345 if (!fTrackTree) {
52c19022 346 fTrackTree = new TTree("gtutracks", "GTU tracks");
80f93426 347 fTrackTree->SetDirectory(0);
348 }
52c19022 349
350 AliTRDtrackGTU *trk = 0x0;
351 TBranch *branch = fTrackTree->GetBranch("TRDgtuTrack");
352 if (!branch) {
353 branch = fTrackTree->Branch("TRDgtuTrack", "AliTRDtrackGTU", &trk, 32000, 99);
354 }
355
356 TIter next(ListOfTracks);
4cc89512 357 while ((trk = (AliTRDtrackGTU*) next())) {
358 trk->CookLabel();
52c19022 359 branch->SetAddress(&trk);
360 fTrackTree->Fill();
361 }
362 fTrackTree->ResetBranchAddress(branch);
363
364 return kTRUE;
365}
366
367Bool_t AliTRDgtuSim::WriteTreesToFile() {
368 TFile *f = TFile::Open("TRD.GtuTracking.root", "RECREATE");
369 f->cd();
370 if (fTrackTree)
371 f->WriteTObject(fTrackTree);
372 if (fTrackletTree)
373 f->WriteTObject(fTrackletTree);
374 f->Close();
375 return kTRUE;
376}
377
378Bool_t AliTRDgtuSim::WriteTracksToESD(TList *ListOfTracks, AliESDEvent *esd)
379{
380 if (esd) {
381 TIter next(ListOfTracks);
382 while (AliTRDtrackGTU *trk = (AliTRDtrackGTU*) next()) {
383 AliESDTrdTrack *trdtrack = trk->CreateTrdTrack();
384 esd->AddTrdTrack(trdtrack);
385 delete trdtrack;
386 }
387 }
388 return kTRUE;
389}
c8b1590d 390
391Bool_t AliTRDgtuSim::WriteTracksToLoader()
392{
393 if (!fTrackTree) {
394 AliError("No track tree found!");
395 return kFALSE;
396 }
397
398 AliRunLoader *rl = AliRunLoader::Instance();
399 AliDataLoader *dl = 0x0;
400 if (rl)
401 dl = rl->GetLoader("TRDLoader")->GetDataLoader("gtutracks");
402 if (!dl) {
403 AliError("Could not get the GTU-track data loader!");
404 return kFALSE;
405 }
406
407 TTree *trackTree = dl->Tree();
408 if (!trackTree) {
409 dl->MakeTree();
410 trackTree = dl->Tree();
411 }
412
413 AliTRDtrackGTU *trk = 0x0;
414 TBranch *trkbranch = trackTree->GetBranch("TRDtrackGTU");
415 if (!trkbranch)
416 trkbranch = trackTree->Branch("TRDtrackGTU", "AliTRDtrackGTU", &trk, 32000);
417
418 TBranch *branch = fTrackTree->GetBranch("TRDgtuTrack");
419 if (!branch)
420 return kFALSE;
421
422 AliDebug(1,Form("Found %i tracks", branch->GetEntries()));
423
424 for (Int_t iTrack = 0; iTrack < branch->GetEntries(); iTrack++) {
425 branch->SetAddress(&trk);
426 branch->GetEntry(iTrack);
427 trkbranch->SetAddress(&trk);
428 trackTree->Fill();
429 }
430
431 dl->WriteData("OVERWRITE");
432
433 return kTRUE;
434}