Fixing coverity bugs
[u/mrichter/AliRoot.git] / TRD / AliTRDrawData.cxx
CommitLineData
5990c064 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
88cb7938 16/* $Id$ */
5990c064 17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// TRD raw data conversion class //
21// //
22///////////////////////////////////////////////////////////////////////////////
23
0aff7a99 24
090026bf 25#include <TMath.h>
ecf39416 26#include "TClass.h"
5990c064 27
2745a409 28#include "AliDAQ.h"
f7ee745b 29#include "AliRawDataHeaderSim.h"
2745a409 30#include "AliRawReader.h"
31#include "AliLog.h"
0c349049 32#include "AliFstream.h"
c93255fe 33#include "AliLoader.h"
cc26f39c 34#include "AliTreeLoader.h"
2745a409 35
5990c064 36#include "AliTRDrawData.h"
37#include "AliTRDdigitsManager.h"
bd0f8685 38#include "AliTRDgeometry.h"
b65e5048 39#include "AliTRDarrayDictionary.h"
40#include "AliTRDarrayADC.h"
8df1f8f5 41#include "AliTRDrawStream.h"
3551db50 42#include "AliTRDcalibDB.h"
ca21baaa 43#include "AliTRDSignalIndex.h"
dfd03fc3 44#include "AliTRDfeeParam.h"
45#include "AliTRDmcmSim.h"
f2ab58d4 46#include "AliTRDtrackletWord.h"
5896bc23 47#include "AliTRDdigitsParam.h"
dfd03fc3 48
5990c064 49ClassImp(AliTRDrawData)
50
037c5823 51Int_t AliTRDrawData::fgDataSuppressionLevel = 0;
987ba9a3 52
5990c064 53//_____________________________________________________________________________
2cb20be6 54AliTRDrawData::AliTRDrawData()
55 :TObject()
92c7f341 56 ,fRunLoader(NULL)
dfd03fc3 57 ,fGeo(NULL)
58 ,fFee(NULL)
8c703901 59 ,fNumberOfDDLs(0)
92c7f341 60 ,fTrackletTree(NULL)
eba523a2 61 ,fTracklets(NULL)
62 ,fTracks(NULL)
6a04e92b 63 ,fSMindexPos(0)
64 ,fStackindexPos(0)
65 ,fEventCounter(0)
50002f37 66 ,fMcmSim(new AliTRDmcmSim)
a0446ff1 67 ,fDigitsParam(NULL)
5990c064 68{
b864d801 69 //
70 // Default constructor
71 //
0c349049 72
dfd03fc3 73 fFee = AliTRDfeeParam::Instance();
74 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
0c349049 75
5990c064 76}
77
78//_____________________________________________________________________________
8c703901 79AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
80 :TObject(r)
92c7f341 81 ,fRunLoader(NULL)
dfd03fc3 82 ,fGeo(NULL)
83 ,fFee(NULL)
8c703901 84 ,fNumberOfDDLs(0)
92c7f341 85 ,fTrackletTree(NULL)
eba523a2 86 ,fTracklets(NULL)
87 ,fTracks(NULL)
6a04e92b 88 ,fSMindexPos(0)
89 ,fStackindexPos(0)
90 ,fEventCounter(0)
50002f37 91 ,fMcmSim(new AliTRDmcmSim)
a0446ff1 92 ,fDigitsParam(NULL)
8c703901 93{
94 //
95 // Copy constructor
96 //
0c349049 97
dfd03fc3 98 fFee = AliTRDfeeParam::Instance();
99 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
0c349049 100
8c703901 101}
102
103//_____________________________________________________________________________
5990c064 104AliTRDrawData::~AliTRDrawData()
105{
106 //
107 // Destructor
108 //
0c349049 109
eba523a2 110 if (fTracklets){
111 fTracklets->Delete();
112 delete fTracklets;
113 }
114
115 if (fTracks){
116 fTracks->Delete();
117 delete fTracks;
92c7f341 118 }
119
50002f37 120 delete fMcmSim;
121
8c703901 122}
123
124//_____________________________________________________________________________
037c5823 125Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, const TTree *tracks )
8c703901 126{
127 //
128 // Initialize necessary parameters and call one
129 // of the raw data simulator selected by SetRawVersion.
130 //
131 // Currently tracklet output is not spported yet and it
132 // will be supported in higher version simulator.
133 //
134
037c5823 135 AliTRDdigitsManager* const digitsManager = new AliTRDdigitsManager();
8c703901 136
137 if (!digitsManager->ReadDigits(digitsTree)) {
138 delete digitsManager;
139 return kFALSE;
140 }
141
142 if (tracks != NULL) {
143 delete digitsManager;
7925de54 144 AliError("Tracklet input is not supported yet.");
8c703901 145 return kFALSE;
146 }
147
148 fGeo = new AliTRDgeometry();
149
f162af62 150 if (!AliTRDcalibDB::Instance()) {
7925de54 151 AliError("Could not get calibration object");
8c703901 152 delete fGeo;
153 delete digitsManager;
154 return kFALSE;
155 }
156
157 Int_t retval = kTRUE;
dfd03fc3 158 Int_t rv = fFee->GetRAWversion();
8c703901 159
160 // Call appropriate Raw Simulator
dfd03fc3 161 if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager);
7925de54 162 else {
163 retval = kFALSE;
dfd03fc3 164 AliWarning(Form("Unsupported raw version (%d).", rv));
8c703901 165 }
166
167 // Cleanup
168 delete fGeo;
169 delete digitsManager;
170
171 return retval;
172
173}
174
175//_____________________________________________________________________________
50378239 176Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
8c703901 177{
178 //
7925de54 179 // Raw data simulator for all versions > 0. This is prepared for real data.
8c703901 180 // This version simulate only raw data with ADC data and not with tracklet.
8c703901 181 //
182
0c349049 183 const Int_t kMaxHcWords = (fGeo->TBmax()/3)
184 * fGeo->ADCmax()
185 * fGeo->MCMmax()
186 * fGeo->ROBmaxC1()/2
187 + 100 + 20;
8c703901 188
189 // Buffer to temporary store half chamber data
0c349049 190 UInt_t *hcBuffer = new UInt_t[kMaxHcWords];
5896bc23 191
1d93b218 192 Bool_t newEvent = kFALSE; // only for correct readout tree
6a04e92b 193 Bool_t newSM = kFALSE; // new SM flag, for writing SM index words
194 Bool_t newStack = kFALSE; // new stack flag, for writing stack index words
8c703901 195
a0446ff1 196 // Get digits parameter
197 fDigitsParam = digitsManager->GetDigitsParam();
198
8c703901 199 // sect is same as iDDL, so I use only sect here.
053767a4 200 for (Int_t sect = 0; sect < fGeo->Nsector(); sect++) {
8c703901 201
202 char name[1024];
8df1f8f5 203 snprintf(name,1024,"TRD_%d.ddl",sect + AliTRDrawStream::kDDLOffset);
8c703901 204
08f92f14 205 AliFstream* of = new AliFstream(name);
8c703901 206
7d619a80 207 // Write a dummy data header
f7ee745b 208 AliRawDataHeaderSim header; // the event header
08f92f14 209 UInt_t hpos = of->Tellp();
210 of->WriteBuffer((char *) (& header), sizeof(header));
7d619a80 211
8c703901 212 // Reset payload byte size (payload does not include header).
213 Int_t npayloadbyte = 0;
214
6a04e92b 215 // check the existance of the data
216 // SM index word and Stack index word
037c5823 217 UInt_t *iwbuffer = new UInt_t[109]; // index word buffer; max 109 = 2 SM headers + 67 dummy headers + 5*8 stack headers
6a04e92b 218 Int_t nheader = 0;
037c5823 219 UInt_t bStackMask = 0x0;
220 Bool_t bStackHasData = kFALSE;
221 Bool_t bSMHasData = kFALSE;
7d619a80 222
037c5823 223 //iwbuffer[nheader++] = 0x0001a020; // SM index words
f88d630c 224 iwbuffer[nheader++] = 0x0044b020; // SM index words | additional SM header:48 = 1 SM header + 47 dummy words(for future use)
6a04e92b 225 iwbuffer[nheader++] = 0x10404071; // SM header
037c5823 226 for ( Int_t i=0; i<66; i++ ) iwbuffer[nheader++] = 0x00000000; // dummy words
227 iwbuffer[nheader++] = 0x10000000; // end of dummy words
7d619a80 228
6a04e92b 229 for ( Int_t stack= 0; stack < fGeo->Nstack(); stack++) {
7d619a80 230 UInt_t linkMask = 0x0;
231 for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
232 Int_t iDet = fGeo->GetDetector(layer,stack,sect);
233 AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
234 if ( fgDataSuppressionLevel==0 || digits->HasData() ) {
235 bStackMask = bStackMask | ( 1 << stack ); // active stack mask for new stack
236 linkMask = linkMask | ( 3 << (2*layer) ); // 3 = 0011
237 bStackHasData = kTRUE;
238 bSMHasData = kTRUE;
239 } // has data
240 } // loop over layer
241
242 if ( fgDataSuppressionLevel==0 || bStackHasData ){
243 iwbuffer[nheader++] = 0x0007a000 | linkMask; // stack index word + link masks
244 if (fgDataSuppressionLevel==0) iwbuffer[nheader-1] = 0x0007afff; // no suppression
245 iwbuffer[nheader++] = 0x04045b01; // stack header
246 for (Int_t i=0;i<6;i++) iwbuffer[nheader++] = 0x00000000; // 6 dummy words
247 bStackHasData = kFALSE;
248 }
6a04e92b 249 } // loop over stack
7d619a80 250
037c5823 251 if ( fgDataSuppressionLevel==0 || bSMHasData ){
7d619a80 252 iwbuffer[0] = iwbuffer[0] | bStackMask; // add stack masks to SM index word
f88d630c 253 if (fgDataSuppressionLevel==0) iwbuffer[0] = 0x0044b03f; // no suppression : all stacks are active
7d619a80 254 of->WriteBuffer((char *) iwbuffer, nheader*4);
ff792017 255 AliDebug(11, Form("SM %d index word: %08x", sect, iwbuffer[0]));
256 AliDebug(11, Form("SM %d header: %08x", sect, iwbuffer[1]));
6a04e92b 257 }
6a04e92b 258 // end of SM & stack header ------------------------------------------------------------------------
259 // -------------------------------------------------------------------------------------------------
7d619a80 260
8c703901 261 // Prepare chamber data
053767a4 262 for( Int_t stack = 0; stack < fGeo->Nstack(); stack++) {
263 for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
7d619a80 264
265 Int_t iDet = fGeo->GetDetector(layer,stack,sect);
266 if (iDet == 0){
267 newEvent = kTRUE; // it is expected that each event has at least one tracklet;
268 // this is only needed for correct readout tree
269 fEventCounter++;
270 AliDebug(11, Form("New event!! Event counter: %d",fEventCounter));
271 }
272
273 if ( stack==0 && layer==0 ) newSM = kTRUE; // new SM flag
274 if ( layer==0 ) newStack = kTRUE; // new stack flag
275 AliDebug(15, Form("stack : %d, layer : %d, iDec : %d\n",stack,layer,iDet));
276 // Get the digits array
277 AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
278 if (fgDataSuppressionLevel==0 || digits->HasData() ) { // second part is new!! and is for indicating a new event
279
280 if (digits->HasData()) digits->Expand();
281
282 Int_t hcwords = 0;
283
284 // Process A side of the chamber
285 hcwords = ProduceHcData(digits,0,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
286 if ( newEvent ) newEvent = kFALSE;
287 //AssignLinkMask(hcBuffer, layer); // active link mask for this layer(2*HC)
288 of->WriteBuffer((char *) hcBuffer, hcwords*4);
289 npayloadbyte += hcwords*4;
290
291 // Process B side of the chamber
292 hcwords = ProduceHcData(digits,1,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
293 of->WriteBuffer((char *) hcBuffer, hcwords*4);
294 npayloadbyte += hcwords*4;
295 } // has data
296
6a04e92b 297 } // loop over layer
298 } // loop over stack
7d619a80 299
8c703901 300 // Complete header
08f92f14 301 header.fSize = UInt_t(of->Tellp()) - hpos;
8c703901 302 header.SetAttribute(0); // Valid data
08f92f14 303 of->Seekp(hpos); // Rewind to header position
304 of->WriteBuffer((char *) (& header), sizeof(header));
8c703901 305 delete of;
8bd87c5e 306
307 delete [] iwbuffer;
308
037c5823 309 } // loop over sector(SM)
7d619a80 310
0c349049 311 delete [] hcBuffer;
7d619a80 312
8c703901 313 return kTRUE;
8c703901 314}
315
316//_____________________________________________________________________________
987ba9a3 317void AliTRDrawData::ProduceSMIndexData(UInt_t *buf, Int_t& nw){
318 //
319 // This function generates
320 // 1) SM index words : ssssssss ssssssss vvvv rrrr r d t mmmmm
321 // - s : size of SM header (number of header, default = 0x0001)
322 // - v : SM header version (default = 0xa)
323 // - r : reserved for future use (default = 00000)
324 // - d : track data enabled bit (default = 0)
325 // - t : tracklet data enabled bit (default = 1)
326 // - m : stack mask (each bit corresponds a stack, default = 11111)
327 //
328 // 2) SM header : rrr c vvvv vvvvvvvv vvvv rrrr bbbbbbbb
329 // - r : reserved for future use (default = 000)
330 // - c : clean check out flag (default = 1)
331 // - v : hardware design revision (default = 0x0404)
332 // - r : reserved for future use (default = 0x0)
333 // - b : physical board ID (default = 0x71)
334 //
335 // 3) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm
336 // - s : size of stack header (number of header, (default = 0x0007)
337 // - v : header version (default = 0xa)
338 // - m : link mask (default = 0xfff)
339 //
340 // 4) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
341 // - v : hardware design revision (default = 0x0404)
342 // - b : physical board ID (default = 0x5b)
343 // - r : reserved for future use (default = 0000 000)
344 // - c : clean checkout flag (default = 1)
345 //
346 // and 6 dummy words(0x00000000)
347 //
348
6a04e92b 349 //buf[nw++] = 0x0001a03f; // SM index words
350 fSMindexPos = nw; // memorize position of the SM index word for re-allocating stack mask
f88d630c 351 buf[nw++] = 0x0001b020; // SM index words
6a04e92b 352 buf[nw++] = 0x10404071; // SM header
353
354 fStackindexPos = nw; // memorize position of the stack index word for future adding
355 /*
356 for (Int_t istack=0; istack<5; istack++){
357 buf[nw++] = 0x0007afff; // stack index words
358 buf[nw++] = 0x04045b01; // stack header
359 for (Int_t i=0;i<6;i++) buf[nw++] = 0x00000000; // 6 dummy words
360 } // loop over 5 stacks
361 */
362}
987ba9a3 363
6a04e92b 364//_____________________________________________________________________________
365void AliTRDrawData::AssignStackMask(UInt_t *buf, Int_t nStack){
366 //
367 // This function re-assign stack mask active(from 0 to 1) in the SM index word
368 //
369 buf[fSMindexPos] = buf[fSMindexPos] | ( 1 << nStack );
370}
371
372//_____________________________________________________________________________
543691ea 373Int_t AliTRDrawData::AddStackIndexWords(UInt_t *buf, Int_t /*nStack*/, Int_t nMax){
6a04e92b 374 //
375 // This function add stack index words and stack header when there is data for the stack
376 //
377 // 1) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm
378 // - s : size of stack header (number of header, (default = 0x0007)
379 // - v : header version (default = 0xa)
380 // - m : link mask (default = 0xfff)
381 // - m : link mask (starting value = 0x000)
382 //
383 // 2) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
384 // - v : hardware design revision (default = 0x0404)
385 // - b : physical board ID (default = 0x5b)
386 // - r : reserved for future use (default = 0000 000)
387 // - c : clean checkout flag (default = 1)
388 //
389 // and 6 dummy words(0x00000000)
390 //
391
392 Int_t nAddedWords = 0; // Number of added words
393 if ( ShiftWords(buf, fStackindexPos, 8, nMax)== kFALSE ){
394 AliError("Adding stack header failed.");
395 return 0;
396 }
397
398 buf[fStackindexPos++] = 0x0007a000; // stack index words
399 buf[fStackindexPos++] = 0x04045b01; // stack header
400 for (Int_t i=0;i<6;i++) buf[fStackindexPos++] = 0x00000000; // 6 dummy words
401 nAddedWords += 8;
987ba9a3 402
6a04e92b 403 return nAddedWords;
987ba9a3 404}
405
406//_____________________________________________________________________________
6a04e92b 407void AliTRDrawData::AssignLinkMask(UInt_t *buf, Int_t nLayer){
408 //
409 // This function re-assign link mask active(from 0 to 1) in the stack index word
410 //
411 buf[fStackindexPos-8] = buf[fStackindexPos-8] | ( 3 << (2*nLayer) ); // 3 = 0011
412}
413
414//_____________________________________________________________________________
415Bool_t AliTRDrawData::ShiftWords(UInt_t *buf, Int_t nStart, Int_t nWords, Int_t nMax){
416 //
417 // This function shifts n words
418 //
419 //if ( nStart+nWords > sizeof(buf)/sizeof(UInt_t) ){
420 // AliError("Words shift failed. No more buffer space.");
421 // return kFALSE;
422 //}
423
424 for ( Int_t iw=nMax; iw>nStart-1; iw--){
425 buf[iw+nWords] = buf[iw];
426 }
427 return kTRUE;
428}
429
430//_____________________________________________________________________________
0aff7a99 431Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det, UInt_t *buf, Int_t maxSize, Bool_t /*newEvent = kFALSE*/, Bool_t /*newSM = kFALSE*/){
987ba9a3 432 //
7d619a80 433 // This function produces the raw data for one HC, i.e. tracklets, tracklet endmarkers,
434 // raw data, raw data endmarkers.
987ba9a3 435 // This function can be used for both ZS and NZS data
436 //
437
438 Int_t nw = 0; // Number of written words
439 Int_t of = 0; // Number of overflowed words
7d619a80 440 Int_t *tempnw = &nw; // Number of written words for temp. buffer
441 Int_t *tempof = &of; // Number of overflowed words for temp. buffer
987ba9a3 442 Int_t layer = fGeo->GetLayer( det ); // Layer
443 Int_t stack = fGeo->GetStack( det ); // Stack
444 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
f793c83d 445 const Int_t kCtype = fGeo->GetStack(det) == 2 ? 0 : 1; // Chamber type (0:C0, 1:C1)
987ba9a3 446
037c5823 447 Bool_t trackletOn = fFee->GetTracklet(); // tracklet simulation active?
987ba9a3 448
449 AliDebug(1,Form("Producing raw data for sect=%d layer=%d stack=%d side=%d",sect,layer,stack,side));
f793c83d 450
f793c83d 451 UInt_t *tempBuffer = buf; // tempBuffer used to write ADC data
452 // different in case of tracklet writing
453
037c5823 454 if (trackletOn) {
f793c83d 455 tempBuffer = new UInt_t[maxSize];
8bd87c5e 456 tempnw = new Int_t(0);
457 tempof = new Int_t(0);
7d619a80 458 }
459
460 WriteIntermediateWords(tempBuffer,*tempnw,*tempof,maxSize,det,side);
461
462 if (digits->HasData()) {
463 // scanning direction such, that tracklet-words are sorted in ascending z and then in ascending y order
464 // ROB numbering on chamber and MCM numbering on ROB increase with decreasing z and increasing y
465 for (Int_t iRobRow = 0; iRobRow <= (kCtype + 3)-1; iRobRow++ ) {
466 // ROB number should be increasing
467 Int_t iRob = iRobRow * 2 + side;
468 // MCM on one ROB
469 for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {
470 Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);
471
50002f37 472 fMcmSim->Init(det, iRob, iMcm);
473 fMcmSim->SetData(digits); // no filtering done here (already done in digitizer)
7d619a80 474 if (trackletOn) {
50002f37 475 fMcmSim->Tracklet();
476 Int_t tempNw = fMcmSim->ProduceTrackletStream(&buf[nw], maxSize - nw);
7d619a80 477 if( tempNw < 0 ) {
478 of += tempNw;
479 nw += maxSize - nw;
480 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
481 } else {
482 nw += tempNw;
483 }
484 }
50002f37 485 fMcmSim->ZSMapping(); // Calculate zero suppression mapping
7d619a80 486 // at the moment it has to be rerun here
487 // Write MCM data to temp. buffer
50002f37 488 Int_t tempNw = fMcmSim->ProduceRawStream( &tempBuffer[*tempnw], maxSize - *tempnw, fEventCounter );
7d619a80 489 if ( tempNw < 0 ) {
490 *tempof += tempNw;
491 *tempnw += maxSize - nw;
f793c83d 492 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
493 } else {
7d619a80 494 *tempnw += tempNw;
f793c83d 495 }
496 }
f793c83d 497 }
7d619a80 498
7d619a80 499 // in case of tracklet writing copy temp data to final buffer
500 if (trackletOn) {
501 if (nw + *tempnw < maxSize) {
502 memcpy(&buf[nw], tempBuffer, *tempnw * sizeof(UInt_t));
503 nw += *tempnw;
504 }
505 else {
506 AliError("Buffer overflow detected");
507 }
f793c83d 508 }
987ba9a3 509 }
510
8bd87c5e 511 if (trackletOn) {
512 delete [] tempBuffer;
513 delete tempof;
514 delete tempnw;
515 }
516
987ba9a3 517 // Write end of raw data marker
6a04e92b 518 if (nw+3 < maxSize) {
7d619a80 519 buf[nw++] = fgkEndOfDataMarker;
520 buf[nw++] = fgkEndOfDataMarker;
521 buf[nw++] = fgkEndOfDataMarker;
522 buf[nw++] = fgkEndOfDataMarker;
987ba9a3 523 } else {
7d619a80 524 of += 4;
987ba9a3 525 }
526
527 if (of != 0) {
f793c83d 528 AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
987ba9a3 529 }
530
531 return nw;
532}
533
987ba9a3 534//_____________________________________________________________________________
7925de54 535AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
5990c064 536{
b864d801 537 //
50378239 538 // Vx of the raw data reading
b864d801 539 //
5990c064 540
e2e85093 541 rawReader->Select("TRD"); //[mj]
542
b65e5048 543 AliTRDarrayADC *digits = 0;
544 AliTRDarrayDictionary *track0 = 0;
545 AliTRDarrayDictionary *track1 = 0;
546 AliTRDarrayDictionary *track2 = 0;
5990c064 547
dfbb4bb9 548 //AliTRDSignalIndex *indexes = 0;
549 // Create the digits manager
550 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
551 digitsManager->CreateArrays();
552
eba523a2 553 AliTRDrawStream input(rawReader);
dfbb4bb9 554
f2ab58d4 555 // ----- preparing tracklet output -----
556 AliDataLoader *trklLoader = AliRunLoader::Instance()->GetLoader("TRDLoader")->GetDataLoader("tracklets");
557 if (!trklLoader) {
f2ab58d4 558 trklLoader = new AliDataLoader("TRD.Tracklets.root","tracklets", "tracklets");
559 AliRunLoader::Instance()->GetLoader("TRDLoader")->AddDataLoader(trklLoader);
560 }
8bd87c5e 561 if (!trklLoader) {
562 return 0x0;
563 }
cc26f39c 564 AliTreeLoader *trklTreeLoader = dynamic_cast<AliTreeLoader*> (trklLoader->GetBaseLoader("tracklets-raw"));
565 if (!trklTreeLoader) {
566 trklTreeLoader = new AliTreeLoader("tracklets-raw", trklLoader);
567 trklLoader->AddBaseLoader(trklTreeLoader);
568 }
8bd87c5e 569 if (!trklTreeLoader) {
570 return 0x0;
571 }
cc26f39c 572
573 if (!trklTreeLoader->Tree())
574 trklTreeLoader->MakeTree();
f2ab58d4 575
eba523a2 576 input.SetTrackletArray(TrackletsArray());
577 input.SetTrackArray(TracksArray());
578
dfbb4bb9 579 // Loop through the digits
580 Int_t det = 0;
581
582 while (det >= 0)
583 {
eba523a2 584 det = input.NextChamber(digitsManager);
50002f37 585
dfbb4bb9 586 if (det >= 0)
587 {
588 // get...
b65e5048 589 digits = (AliTRDarrayADC *) digitsManager->GetDigits(det);
590 track0 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,0);
591 track1 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,1);
592 track2 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,2);
dfbb4bb9 593 // and compress
b65e5048 594 if (digits) digits->Compress();
595 if (track0) track0->Compress();
596 if (track1) track1->Compress();
597 if (track2) track2->Compress();
dfbb4bb9 598 }
599 }
600
8eab6a01 601 for (Int_t iSector = 0; iSector < fGeo->Nsector(); iSector++) {
602 fTrgFlags[iSector] = input.GetTriggerFlags(iSector);
603 }
604
8bd87c5e 605 trklTreeLoader->WriteData("OVERWRITE");
606 trklLoader->UnloadAll();
f2ab58d4 607
dfbb4bb9 608 return digitsManager;
609}
610
1d93b218 611//_____________________________________________________________________________
612void AliTRDrawData::WriteIntermediateWords(UInt_t* buf, Int_t& nw, Int_t& of, const Int_t& maxSize, const Int_t& det, const Int_t& side) {
037c5823 613 //
614 // write tracklet end marker(0x10001000)
615 // and half chamber headers(H[0] and H[1])
616 //
987ba9a3 617
618 Int_t layer = fGeo->GetLayer( det ); // Layer
619 Int_t stack = fGeo->GetStack( det ); // Stack
620 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
621 Int_t rv = fFee->GetRAWversion();
a0446ff1 622 const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
5896bc23 623 Bool_t trackletOn = fFee->GetTracklet();
987ba9a3 624 UInt_t x = 0;
625
626 // Write end of tracklet marker
7d619a80 627 if (nw < maxSize){
628 buf[nw++] = fgkEndOfTrackletMarker;
629 buf[nw++] = fgkEndOfTrackletMarker; // the number of tracklet end marker should be more than 2
6a04e92b 630 }
631 else {
7d619a80 632 of++;
6a04e92b 633 }
987ba9a3 634
7d619a80 635 // Half Chamber header
636 // h[0] (there are 2 HC headers) xmmm mmmm nnnn nnnq qqss sssp ppcc ci01
637 // , where x : Raw version speacial number (=1)
638 // m : Raw version major number (test pattern, ZS, disable tracklet, 0, options)
639 // n : Raw version minor number
640 // q : number of addtional header words (default = 1)
641 // s : SM sector number (ALICE numbering)
642 // p : plane(layer) number
643 // c : chamber(stack) number
644 // i : side number (0:A, 1:B)
645 Int_t majorv = 0; // The major version number
987ba9a3 646 Int_t minorv = 0; // The minor version number
647 Int_t add = 1; // The number of additional header words to follow : now 1, previous 2
7d619a80 648 Int_t tp = 0; // test pattern (default=0)
649 Int_t zs = (rv==3) ? 1 : 0; // zero suppression
650 Int_t dt = (trackletOn) ? 0 : 1; // disable tracklet
651
652 majorv = (tp<<6) | (zs<<5) | (dt<<4) | 1; // major version
653
987ba9a3 654 x = (1<<31) | (majorv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (side<<2) | 1;
655 if (nw < maxSize) buf[nw++] = x; else of++;
7d619a80 656
987ba9a3 657 // h[1] tttt ttbb bbbb bbbb bbbb bbpp pphh hh01
7d619a80 658 // , where t : number of time bins
659 // b : bunch crossing number
660 // p : pretrigger counter
661 // h : pretrigger phase
987ba9a3 662 Int_t bcCtr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
663 Int_t ptCtr = 15; // pretrigger counter. Here it is set to 15 always for no reason
664 Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
665 //x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1; // old format
6a04e92b 666 x = ((kNTBin)<<26) | (bcCtr<<10) | (ptCtr<<6) | (ptPhase<<2) | 1;
987ba9a3 667 if (nw < maxSize) buf[nw++] = x; else of++;
987ba9a3 668}
1d93b218 669
eba523a2 670TClonesArray *AliTRDrawData::TrackletsArray()
92c7f341 671{
eba523a2 672 // Returns the array of on-line tracklets
92c7f341 673
eba523a2 674 if (!fTracklets) {
9cb9c409 675 fTracklets = new TClonesArray("AliTRDtrackletWord", 200);
92c7f341 676 }
eba523a2 677 return fTracklets;
678}
92c7f341 679
eba523a2 680TClonesArray* AliTRDrawData::TracksArray()
681{
682 // return array of GTU tracks (create TClonesArray if necessary)
92c7f341 683
eba523a2 684 if (!fTracks) {
685 fTracks = new TClonesArray("AliESDTrdTrack",100);
92c7f341 686 }
eba523a2 687 return fTracks;
92c7f341 688}