Merge with TRDdev.2.0
[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 }
7d619a80 494
7d619a80 495 // in case of tracklet writing copy temp data to final buffer
496 if (trackletOn) {
497 if (nw + *tempnw < maxSize) {
498 memcpy(&buf[nw], tempBuffer, *tempnw * sizeof(UInt_t));
499 nw += *tempnw;
500 }
501 else {
502 AliError("Buffer overflow detected");
503 }
f793c83d 504 }
987ba9a3 505 }
506
8bd87c5e 507 if (trackletOn) {
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}