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