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