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