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