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