]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDrawData.cxx
Cross library dependency removed
[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
34 #include "AliTRDrawData.h"
35 #include "AliTRDdigitsManager.h"
36 #include "AliTRDgeometry.h"
37 #include "AliTRDarrayDictionary.h"
38 #include "AliTRDarrayADC.h"
39 #include "AliTRDrawStreamBase.h"
40 #include "AliTRDcalibDB.h"
41 #include "AliTRDSignalIndex.h"
42 #include "AliTRDfeeParam.h"
43 #include "AliTRDmcmSim.h"
44 #include "AliTRDdigitsParam.h"
45
46 ClassImp(AliTRDrawData)
47
48 Int_t AliTRDrawData::fgRawFormatVersion = AliTRDrawData::kRawNewFormat;
49 Int_t AliTRDrawData::fgDataSuppressionLevel = 0;
50
51 //_____________________________________________________________________________
52 AliTRDrawData::AliTRDrawData()
53   :TObject()
54   ,fRunLoader(NULL)
55   ,fGeo(NULL)
56   ,fFee(NULL)
57   ,fNumberOfDDLs(0)
58   ,fTrackletTree(NULL)
59   ,fTrackletContainer(NULL)
60   ,fSMindexPos(0)
61   ,fStackindexPos(0)
62   ,fEventCounter(0)
63   ,fDigitsParam(NULL)
64 {
65   //
66   // Default constructor
67   //
68
69   fFee = AliTRDfeeParam::Instance();
70   fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
71
72 }
73
74 //_____________________________________________________________________________
75 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
76   :TObject(r)
77   ,fRunLoader(NULL)
78   ,fGeo(NULL)
79   ,fFee(NULL)
80   ,fNumberOfDDLs(0)
81   ,fTrackletTree(NULL)
82   ,fTrackletContainer(NULL)
83   ,fSMindexPos(0)
84   ,fStackindexPos(0)
85   ,fEventCounter(0)
86   ,fDigitsParam(NULL)
87 {
88   //
89   // Copy constructor
90   //
91
92   fFee = AliTRDfeeParam::Instance();
93   fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
94
95 }
96
97 //_____________________________________________________________________________
98 AliTRDrawData::~AliTRDrawData()
99 {
100   //
101   // Destructor
102   //
103
104   if (fTrackletContainer){
105     delete fTrackletContainer;
106     fTrackletContainer = NULL;
107   }
108
109 }
110
111 //_____________________________________________________________________________
112 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, const TTree *tracks )
113 {
114   //
115   // Initialize necessary parameters and call one
116   // of the raw data simulator selected by SetRawVersion.
117   //
118   // Currently tracklet output is not spported yet and it
119   // will be supported in higher version simulator.
120   //
121
122   AliTRDdigitsManager* const digitsManager = new AliTRDdigitsManager();
123
124   if (!digitsManager->ReadDigits(digitsTree)) {
125     delete digitsManager;
126     return kFALSE;
127   }
128
129   if (tracks != NULL) {
130     delete digitsManager;
131     AliError("Tracklet input is not supported yet.");
132     return kFALSE;
133   }
134
135   fGeo = new AliTRDgeometry();
136
137   if (!AliTRDcalibDB::Instance()) {
138     AliError("Could not get calibration object");
139     delete fGeo;
140     delete digitsManager;
141     return kFALSE;
142   }
143
144   Int_t retval = kTRUE;
145   Int_t rv     = fFee->GetRAWversion();
146
147   // Call appropriate Raw Simulator
148   if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager); 
149   else {
150     retval = kFALSE;
151     AliWarning(Form("Unsupported raw version (%d).", rv));
152   }
153
154   // Cleanup
155   delete fGeo;
156   delete digitsManager;
157
158   return retval;
159
160 }
161
162 //_____________________________________________________________________________
163 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
164 {
165   //
166   // Raw data simulator for all versions > 0. This is prepared for real data.
167   // This version simulate only raw data with ADC data and not with tracklet.
168   //
169
170   const Int_t kMaxHcWords = (fGeo->TBmax()/3)
171                           * fGeo->ADCmax()
172                           * fGeo->MCMmax()
173                           * fGeo->ROBmaxC1()/2 
174                           + 100 + 20;
175
176   // Buffer to temporary store half chamber data
177   UInt_t     *hcBuffer    = new UInt_t[kMaxHcWords];
178
179   Bool_t newEvent = kFALSE;  // only for correct readout tree
180   Bool_t newSM    = kFALSE;  // new SM flag, for writing SM index words
181   Bool_t newStack = kFALSE;  // new stack flag, for writing stack index words
182
183   // Get digits parameter
184   fDigitsParam = digitsManager->GetDigitsParam();
185
186   // sect is same as iDDL, so I use only sect here.
187   for (Int_t sect = 0; sect < fGeo->Nsector(); sect++) { 
188
189     char name[1024];
190     sprintf(name,"TRD_%d.ddl",sect + AliTRDrawStreamBase::kDDLOffset);
191
192     AliFstream* of = new AliFstream(name);
193
194             // Write a dummy data header
195     AliRawDataHeaderSim  header;  // the event header
196     UInt_t hpos = of->Tellp();
197     of->WriteBuffer((char *) (& header), sizeof(header));
198
199     // Reset payload byte size (payload does not include header).
200     Int_t npayloadbyte = 0;
201
202
203    if ( fgRawFormatVersion == 0 ){
204     // GTU common data header (5x4 bytes per super module, shows link mask)
205     for( Int_t stack = 0; stack < fGeo->Nstack(); stack++ ) {
206         UInt_t gtuCdh = (UInt_t)(0xe << 28);
207         for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
208             Int_t iDet = fGeo->GetDetector(layer, stack, sect);
209
210             // If chamber status is ok, we assume that the optical link is also OK.
211             // This is shown in the GTU link mask.
212             if ( AliTRDcalibDB::Instance()->GetChamberStatus(iDet) )
213                 gtuCdh = gtuCdh | (3 << (2*layer));
214         }
215         of->WriteBuffer((char *) (& gtuCdh), sizeof(gtuCdh));
216         npayloadbyte += 4;
217     }
218    }
219
220
221     // check the existance of the data
222     // SM index word and Stack index word
223    if ( fgRawFormatVersion == 1 ){
224     UInt_t *iwbuffer = new UInt_t[109]; // index word buffer; max 109 = 2 SM headers + 67 dummy headers + 5*8 stack headers
225     Int_t nheader = 0;
226     UInt_t bStackMask = 0x0;
227     Bool_t bStackHasData = kFALSE;
228     Bool_t bSMHasData = kFALSE;
229
230     //iwbuffer[nheader++] = 0x0001a020;   // SM index words 
231     iwbuffer[nheader++] = 0x0044a020;   // SM index words | additional SM header:48 = 1 SM header + 47 dummy words(for future use)
232     iwbuffer[nheader++] = 0x10404071;   // SM header
233     for ( Int_t i=0; i<66; i++ ) iwbuffer[nheader++] = 0x00000000;  // dummy words 
234     iwbuffer[nheader++] = 0x10000000;   // end of dummy words
235
236     for ( Int_t stack= 0; stack < fGeo->Nstack(); stack++) {
237         UInt_t linkMask = 0x0;
238         for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
239             Int_t iDet = fGeo->GetDetector(layer,stack,sect);
240                         AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
241             if ( digits->HasData() ) {
242                 bStackMask = bStackMask | ( 1 << stack ); // active stack mask for new stack
243                 linkMask = linkMask | ( 3 << (2*layer) );    // 3 = 0011
244                 bStackHasData = kTRUE;
245                 bSMHasData = kTRUE;
246             } // has data
247         } // loop over layer
248
249         if ( fgDataSuppressionLevel==0 || bStackHasData ){
250             iwbuffer[nheader++] = 0x0007a000 | linkMask;    // stack index word + link masks
251             if (fgDataSuppressionLevel==0) iwbuffer[nheader-1] = 0x0007afff;  // no suppression
252             iwbuffer[nheader++] = 0x04045b01;               // stack header
253             for (Int_t i=0;i<6;i++) iwbuffer[nheader++] = 0x00000000; // 6 dummy words
254             bStackHasData = kFALSE;
255         }
256     } // loop over stack
257
258     if ( fgDataSuppressionLevel==0 || bSMHasData ){
259         iwbuffer[0] = iwbuffer[0] | bStackMask;  // add stack masks to SM index word
260         if (fgDataSuppressionLevel==0) iwbuffer[0] = 0x0044a03f;    // no suppression : all stacks are active
261         of->WriteBuffer((char *) iwbuffer, nheader*4);
262         AliDebug(11, Form("SM %d index word: %08x", iwbuffer[0]));
263         AliDebug(11, Form("SM %d header: %08x", iwbuffer[1]));
264     }
265    }
266     // end of SM & stack header ------------------------------------------------------------------------
267     // -------------------------------------------------------------------------------------------------
268
269     // Prepare chamber data
270     for( Int_t stack = 0; stack < fGeo->Nstack(); stack++) {
271       for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
272
273         Int_t iDet = fGeo->GetDetector(layer,stack,sect);
274         if (iDet == 0){
275             newEvent = kTRUE; // it is expected that each event has at least one tracklet; 
276                                          // this is only needed for correct readout tree
277             fEventCounter++;
278             AliDebug(11, Form("New event!! Event counter: %d",fEventCounter));
279         }
280
281         if ( stack==0 && layer==0 ) newSM = kTRUE;  // new SM flag
282         if ( layer==0 ) newStack = kTRUE;           // new stack flag
283         AliDebug(15, Form("stack : %d, layer : %d, iDec : %d\n",stack,layer,iDet));
284         // Get the digits array
285                 AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
286         if (fgDataSuppressionLevel==0 || digits->HasData() ) {  // second part is new!! and is for indicating a new event
287
288                 if (digits->HasData()) digits->Expand();
289
290                         Int_t hcwords = 0;
291                         Int_t rv = fFee->GetRAWversion();
292
293
294         if ( fgRawFormatVersion == 0 ){
295       // Process A side of the chamber
296           if ( rv >= 1 && rv <= 2 ) {
297             hcwords = ProduceHcDataV1andV2(digits,0,iDet,hcBuffer,kMaxHcWords);
298           }
299           if ( rv == 3 ) { 
300    
301               hcwords = ProduceHcDataV3     (digits,0,iDet,hcBuffer,kMaxHcWords,newEvent);
302               //hcwords = ProduceHcDataV3     (digits,0,iDet,hcBuffer,kMaxHcWords);
303             if(newEvent == kTRUE) newEvent = kFALSE;
304           }
305
306           of->WriteBuffer((char *) hcBuffer, hcwords*4);
307           npayloadbyte += hcwords*4;
308
309       // Process B side of the chamber
310           if ( rv >= 1 && rv <= 2 ) {
311             hcwords = ProduceHcDataV1andV2(digits,1,iDet,hcBuffer,kMaxHcWords);
312           }
313           if ( rv >= 3 ) {
314            
315               hcwords = ProduceHcDataV3     (digits,1,iDet,hcBuffer,kMaxHcWords,newEvent);
316               //hcwords = ProduceHcDataV3     (digits,1,iDet,hcBuffer,kMaxHcWords);
317           }
318
319           of->WriteBuffer((char *) hcBuffer, hcwords*4);
320           npayloadbyte += hcwords*4;
321
322     } else { // real data format
323
324        if (digits->HasData()){
325         // Process A side of the chamber
326         hcwords = ProduceHcData(digits,0,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
327         //if ( newStack ){
328         //  AssignStackMask(hcBuffer, stack);   // active stack mask for this stack
329         //  hcwords += AddStackIndexWords(hcBuffer, stack, hcwords);
330         //  newStack = kFALSE;
331         //}
332         //if ( newSM ) newSM = kFALSE;
333         if ( newEvent ) newEvent = kFALSE;
334         //AssignLinkMask(hcBuffer, layer);  // active link mask for this layer(2*HC)
335         of->WriteBuffer((char *) hcBuffer, hcwords*4);
336         npayloadbyte += hcwords*4;
337         //for ( Int_t i=0; i<hcwords; i++ ) AliInfo(Form("Buf : %X",hcBuffer[i]));
338
339         // Process B side of the chamber
340         hcwords = ProduceHcData(digits,1,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
341         of->WriteBuffer((char *) hcBuffer, hcwords*4);
342         npayloadbyte += hcwords*4;
343        } else {   // in case of no-suppression or NZS
344 /*        
345         hcBuffer[hcwords++] = fgkEndOfTrackletMarker;
346         hcBuffer[hcwords++] = fgkEndOfTrackletMarker;
347         hcBuffer[hcwords++] = (1<<31) | (0<<24) | (0<<17) | (1<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (0<<2) | 1;
348         hcBuffer[hcwords++] = (24<<26) | (99<<10) | (15<<6) | (11<<2) | 1;
349         hcBuffer[hcwords++] = kEndofrawdatamarker;
350         hcBuffer[hcwords++] = kEndofrawdatamarker;
351         hcBuffer[hcwords++] = kEndofrawdatamarker;
352         hcBuffer[hcwords++] = kEndofrawdatamarker;
353         npayloadbyte += hcwords*4;
354
355         hcBuffer[hcwords++] = fgkEndOfTrackletMarker;
356         hcBuffer[hcwords++] = fgkEndOfTrackletMarker;
357         hcBuffer[hcwords++] = (1<<31) | (0<<24) | (0<<17) | (1<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (1<<2) | 1;
358         hcBuffer[hcwords++] = (24<<26) | (99<<10) | (15<<6) | (11<<2) | 1;
359         hcBuffer[hcwords++] = kEndofrawdatamarker;
360         hcBuffer[hcwords++] = kEndofrawdatamarker;
361         hcBuffer[hcwords++] = kEndofrawdatamarker;
362         hcBuffer[hcwords++] = kEndofrawdatamarker;
363 */        
364         hcwords = ProduceHcDataNoSuppression(0,iDet,hcBuffer,kMaxHcWords); // side 0
365         of->WriteBuffer((char *) hcBuffer, hcwords*4);
366         npayloadbyte += hcwords*4;
367
368         hcwords = ProduceHcDataNoSuppression(1,iDet,hcBuffer,kMaxHcWords); // side 1
369         of->WriteBuffer((char *) hcBuffer, hcwords*4);
370         npayloadbyte += hcwords*4;
371        }
372     }
373
374     } // has data
375
376       } // loop over layer
377     } // loop over stack
378
379     // Complete header
380     header.fSize = UInt_t(of->Tellp()) - hpos;
381     header.SetAttribute(0);  // Valid data
382     of->Seekp(hpos);         // Rewind to header position
383     of->WriteBuffer((char *) (& header), sizeof(header));
384     delete of;
385   } // loop over sector(SM)
386
387   delete [] hcBuffer;
388
389   return kTRUE;
390
391 }
392
393 //_____________________________________________________________________________
394 void AliTRDrawData::ProduceSMIndexData(UInt_t *buf, Int_t& nw){
395         // 
396         // This function generates 
397         //       1) SM index words : ssssssss ssssssss vvvv rrrr r d t mmmmm
398         //          - s : size of SM header (number of header, default = 0x0001)
399         //          - v : SM header version (default = 0xa)
400         //          - r : reserved for future use (default = 00000)
401         //          - d : track data enabled bit (default = 0)
402         //          - t : tracklet data enabled bit (default = 1)
403         //          - m : stack mask (each bit corresponds a stack, default = 11111)
404         //
405         //       2) SM header : rrr c vvvv vvvvvvvv vvvv rrrr bbbbbbbb
406         //          - r : reserved for future use (default = 000)
407         //          - c : clean check out flag (default = 1)
408         //          - v : hardware design revision (default = 0x0404)
409         //          - r : reserved for future use (default = 0x0)
410         //          - b : physical board ID (default = 0x71)
411         //
412         //       3) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm
413         //          - s : size of stack header (number of header, (default = 0x0007)
414         //          - v : header version (default = 0xa)
415         //          - m : link mask (default = 0xfff)
416         //
417         //       4) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
418         //          - v : hardware design revision (default = 0x0404)
419         //          - b : physical board ID (default = 0x5b)
420         //          - r : reserved for future use (default = 0000 000)
421         //          - c : clean checkout flag (default = 1)
422         //      
423         //       and 6 dummy words(0x00000000)
424         //
425         
426     //buf[nw++] = 0x0001a03f;   // SM index words
427     fSMindexPos = nw;       // memorize position of the SM index word for re-allocating stack mask
428     buf[nw++] = 0x0001a020; // SM index words
429     buf[nw++] = 0x10404071; // SM header
430
431     fStackindexPos = nw;    // memorize position of the stack index word for future adding
432         /*  
433     for (Int_t istack=0; istack<5; istack++){
434         buf[nw++] = 0x0007afff; // stack index words
435         buf[nw++] = 0x04045b01; // stack header
436         for (Int_t i=0;i<6;i++) buf[nw++] = 0x00000000; // 6 dummy words
437     } // loop over 5 stacks
438         */
439 }
440
441 //_____________________________________________________________________________
442 void AliTRDrawData::AssignStackMask(UInt_t *buf, Int_t nStack){
443     //
444     // This function re-assign stack mask active(from 0 to 1) in the SM index word
445     //   
446     buf[fSMindexPos] = buf[fSMindexPos] | ( 1 << nStack );
447 }
448
449 //_____________________________________________________________________________  
450 Int_t AliTRDrawData::AddStackIndexWords(UInt_t *buf, Int_t /*nStack*/, Int_t nMax){
451     // 
452     // This function add stack index words and stack header when there is data for the stack
453     //
454     //   1) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm 
455     //      - s : size of stack header (number of header, (default = 0x0007)       
456     //      - v : header version (default = 0xa)
457     //      - m : link mask (default = 0xfff)
458     //      - m : link mask (starting value = 0x000)
459     //
460     //   2) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
461     //      - v : hardware design revision (default = 0x0404)
462     //      - b : physical board ID (default = 0x5b)
463     //      - r : reserved for future use (default = 0000 000)
464     //      - c : clean checkout flag (default = 1)
465     //  
466     //   and 6 dummy words(0x00000000)
467     //
468
469     Int_t nAddedWords = 0;  // Number of added words
470     if ( ShiftWords(buf, fStackindexPos, 8, nMax)== kFALSE ){
471         AliError("Adding stack header failed.");
472         return 0;
473     }
474
475     buf[fStackindexPos++] = 0x0007a000; // stack index words
476     buf[fStackindexPos++] = 0x04045b01; // stack header
477     for (Int_t i=0;i<6;i++) buf[fStackindexPos++] = 0x00000000; // 6 dummy words 
478     nAddedWords += 8;
479
480     return nAddedWords;
481 }
482
483 //_____________________________________________________________________________
484 void AliTRDrawData::AssignLinkMask(UInt_t *buf, Int_t nLayer){
485     //
486     // This function re-assign link mask active(from 0 to 1) in the stack index word
487     //   
488     buf[fStackindexPos-8] = buf[fStackindexPos-8] | ( 3 << (2*nLayer) );    // 3 = 0011 
489 }
490
491 //_____________________________________________________________________________ 
492 Bool_t AliTRDrawData::ShiftWords(UInt_t *buf, Int_t nStart, Int_t nWords, Int_t nMax){
493     //  
494     // This function shifts n words
495     //
496     //if ( nStart+nWords > sizeof(buf)/sizeof(UInt_t) ){
497     //  AliError("Words shift failed. No more buffer space.");
498     //  return kFALSE;
499     //}
500
501     for ( Int_t iw=nMax; iw>nStart-1; iw--){
502         buf[iw+nWords] = buf[iw];
503     }
504     return kTRUE;
505 }
506
507 //_____________________________________________________________________________
508 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*/){
509         //
510         // This function can be used for both ZS and NZS data
511         //
512
513         Int_t           nw = 0;                       // Number of written    words
514         Int_t           of = 0;                       // Number of overflowed words
515         Int_t      *tempnw = 0x0;                     // Number of written    words for temp. buffer
516         Int_t      *tempof = 0x0;                     // Number of overflowed words for temp. buffer
517         Int_t        layer = fGeo->GetLayer( det );   // Layer
518         Int_t        stack = fGeo->GetStack( det );   // Stack
519         Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
520         const Int_t kCtype = fGeo->GetStack(det) == 2 ? 0 : 1;                       // Chamber type (0:C0, 1:C1)
521
522         Bool_t trackletOn = fFee->GetTracklet();     // tracklet simulation active?
523
524         AliDebug(1,Form("Producing raw data for sect=%d layer=%d stack=%d side=%d",sect,layer,stack,side));
525         
526         AliTRDmcmSim* mcm = new AliTRDmcmSim();
527
528         UInt_t *tempBuffer = buf; // tempBuffer used to write ADC data
529                                   // different in case of tracklet writing
530         
531         if (trackletOn) {
532           tempBuffer = new UInt_t[maxSize];
533           tempnw = new Int_t(0);
534           tempof = new Int_t(0);
535         }
536         else {
537           tempnw = &nw;
538           tempof = &of;
539         }
540
541         WriteIntermediateWordsV2(tempBuffer,*tempnw,*tempof,maxSize,det,side);  //??? no tracklet or NZS
542
543         // scanning direction such, that tracklet-words are sorted in ascending z and then in ascending y order
544         // ROB numbering on chamber and MCM numbering on ROB increase with decreasing z and increasing y
545         for (Int_t iRobRow = 0; iRobRow <= (kCtype + 3)-1; iRobRow++ ) {        // ROB number should be increasing
546           Int_t iRob = iRobRow * 2 + side;
547           // MCM on one ROB
548           for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {
549             Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);
550             
551             mcm->Init(det, iRob, iMcm);
552             mcm->SetData(digits);     // no filtering done here (already done in digitizer)
553             if (trackletOn) {
554               mcm->Tracklet();
555               Int_t tempNw = mcm->ProduceTrackletStream(&buf[nw], maxSize - nw);
556               if(  tempNw < 0 ) {
557                 of += tempNw;
558                 nw += maxSize - nw;
559                 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
560               } else {
561                 nw += tempNw;
562               }
563             }
564             mcm->ZSMapping();  // Calculate zero suppression mapping
565                                // at the moment it has to be rerun here
566             // Write MCM data to temp. buffer
567             Int_t tempNw = mcm->ProduceRawStream( &tempBuffer[*tempnw], maxSize - *tempnw, fEventCounter );
568             if ( tempNw < 0 ) {
569               *tempof += tempNw;
570               *tempnw += maxSize - nw;
571               AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
572             } else {
573               *tempnw += tempNw;
574             }
575           }
576         }
577
578         delete mcm;
579
580
581         // in case of tracklet writing copy temp data to final buffer
582         if (trackletOn) {
583           if (nw + *tempnw < maxSize) {
584             memcpy(&buf[nw], tempBuffer, *tempnw * sizeof(UInt_t));
585             nw += *tempnw;
586           }
587           else {
588             AliError("Buffer overflow detected");
589           }
590         }
591
592         // Write end of raw data marker
593         if (nw+3 < maxSize) {
594           buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
595           buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
596           //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
597           //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
598         } else {
599           of++;
600         }
601         
602         if (trackletOn) {
603           delete [] tempBuffer;
604           delete tempof;
605           delete tempnw;
606         }
607
608         if (of != 0) {
609           AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
610         }
611
612         return nw;
613 }
614
615 //_____________________________________________________________________________
616 Int_t AliTRDrawData::ProduceHcDataNoSuppression(Int_t side, Int_t det, UInt_t *buf, Int_t maxSize){
617
618     // This function generates below words for no-suppression option(same as real data format) 
619     //   1. end of tracklet marker
620     //   2. HC index word, HC header
621     //   3. MCM header, ADC mask
622     //   4. end of data marker
623     //
624
625     Int_t   nw = 0;         // number of written words
626     Int_t   of = 0;         // number of overflowed words
627     UInt_t  x  = 0;         // word buffer
628     //UInt_t *tempBuffer  = buf;   // temp buffer
629     Int_t  *tempnw      = &nw;   // temp number of written words
630     Int_t  *tempof      = &of;   // temp number of overflowed words
631
632     const Int_t kCtype = fGeo->GetStack(det) == 2 ? 0 : 1;   // Chamber type (0:C0, 1:C1)
633
634     WriteIntermediateWordsV2(buf, *tempnw, *tempof, maxSize, det, side);  // end of tracklet marker and HC headers
635
636     for (Int_t iRobRow = 0; iRobRow <= (kCtype + 3)-1; iRobRow++ ) {    // ROB number should be increasing
637         Int_t iRob = iRobRow * 2 + side;  // ROB position
638
639         for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {    // MCM on ROB
640             Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);    // MCM position
641
642             if ( nw+2 < maxSize ){
643                 x = 0;
644                 x = (1<<31) | (iRob << 28) | (iMcm << 24) | ((fEventCounter % 0x100000) << 4) | 0xC;    // MCM header
645                 buf[nw++] = x;
646
647                 x = 0;
648                 // Produce ADC mask : nncc cccm mmmm mmmm mmmm mmmm mmmm 1100
649                 //                n : unused , c : ADC count, m : selected ADCs , where ccccc are inverted
650                 x = x | (1 << 30) | (31 << 25) | 0xC;   // 11111 = 31
651                 buf[nw++] = x;
652             } else {
653                 of++;
654             }
655
656         }   // loop over MCM
657     }   // loop over ROB
658
659         // Write end of raw data marker
660         if (nw+1 < maxSize) {
661           buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
662           buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
663           //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
664           //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
665         } else {
666           of++;
667         }
668
669     if ( of != 0 ){     // if there is overflow
670         AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
671     }
672
673     AliDebug(1, Form("Number of written words in this HC is %d",nw));
674
675     return nw;
676 }
677
678 //_____________________________________________________________________________
679 Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDarrayADC *digits, Int_t side
680                                         , Int_t det, UInt_t *buf, Int_t maxSize)
681 {
682   //
683   // This function simulates: 1) SM-I commissiong data Oct. 06 (Raw Version == 1).
684   //                          2) Full Raw Production Version   (Raw Version == 2)
685   //
686   // Produce half chamber data (= an ORI data) for the given chamber (det) and side (side)
687   // where
688   //
689   //   side=0 means A side with ROB positions 0, 2, 4, 6.
690   //   side=1 means B side with ROB positions 1, 3, 5, 7.
691   //
692   // Chamber type (C0 orC1) is determined by "det" automatically.
693   // Appropriate size of buffer (*buf) must be prepared prior to calling this function.
694   // Pointer to the buffer and its size must be given to "buf" and "maxSize".
695   // Return value is the number of valid data filled in the buffer in unit of 32 bits
696   // UInt_t words.
697   // If buffer size if too small, the data is truncated with the buffer size however
698   // the function will finish without crash (this behaviour is similar to the MCM).
699   //
700
701   Int_t           nw = 0;                       // Number of written    words
702   Int_t           of = 0;                       // Number of overflowed words
703   Int_t        layer = fGeo->GetLayer( det );   // Layer
704   Int_t        stack = fGeo->GetStack( det );   // Stack
705   Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
706   Int_t         nRow = fGeo->GetRowMax( layer, stack, sect );
707   Int_t         nCol = fGeo->GetColMax( layer );
708   const Int_t kNTBin = fDigitsParam->GetNTimeBins(det); 
709
710   Int_t       kCtype = 0;                       // Chamber type (0:C0, 1:C1)
711   Int_t          iEv = 0xA;                     // Event ID. Now fixed to 10, how do I get event id?
712   UInt_t           x = 0;                       // General used number
713   Int_t           rv = fFee->GetRAWversion();
714
715   // Check the nCol and nRow.
716   if ((nCol == 144) && 
717       (nRow == 16 || nRow == 12)) {
718     kCtype = (nRow-12) / 4;
719   } 
720   else {
721     AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
722                  ,nRow,nCol));
723     return 0;
724   }
725
726   AliDebug(1,Form("Producing raw data for sect=%d layer=%d stack=%d side=%d"
727                  ,sect,layer,stack,side));
728
729   // Tracklet should be processed here but not implemented yet
730
731   // Write end of tracklet marker
732   if (nw < maxSize) {
733     buf[nw++] = kEndoftrackletmarker;
734   } 
735   else {
736     of++;
737   }
738
739   // Half Chamber header
740   if      ( rv == 1 ) {
741     // Now it is the same version as used in SM-I commissioning.
742     Int_t  dcs = det+100;      // DCS Serial (in simulation, it is meaningless
743     x = (dcs<<20) | (sect<<15) | (layer<<12) | (stack<<9) | (side<<8) | 1;
744     if (nw < maxSize) {
745       buf[nw++] = x; 
746     }
747     else {
748       of++;
749     }
750   } 
751   else if ( rv == 2 ) {
752     // h[0] (there are 3 HC header)
753     Int_t minorv = 0;      // The minor version number
754     Int_t add    = 2;      // The number of additional header words to follow
755     x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (side<<2) | 1;
756     if (nw < maxSize) {
757       buf[nw++] = x; 
758     }
759     else {
760       of++;
761     }
762     // h[1]
763     Int_t bcCtr   = 99; // bunch crossing counter. Here it is set to 99 always for no reason
764     Int_t ptCtr   = 15; // pretrigger counter. Here it is set to 15 always for no reason
765     Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
766     x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1;
767     if (nw < maxSize) {
768       buf[nw++] = x; 
769     }
770     else {
771       of++;
772     }
773     // h[2]
774     Int_t pedSetup       = 1;    // Pedestal filter setup (0:1). Here it is always 1 for no reason
775     Int_t gainSetup      = 1;    // Gain filter setup (0:1). Here it is always 1 for no reason
776     Int_t tailSetup      = 1;    // Tail filter setup (0:1). Here it is always 1 for no reason
777     Int_t xtSetup        = 0;    // Cross talk filter setup (0:1). Here it is always 0 for no reason
778     Int_t nonlinSetup    = 0;    // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
779     Int_t bypassSetup    = 0;    // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
780     Int_t commonAdditive = 10;   // Digital filter common additive (0:63). Here it is always 10 for no reason
781     x = (pedSetup<<31) | (gainSetup<<30) | (tailSetup<<29) | (xtSetup<<28) | (nonlinSetup<<27)
782       | (bypassSetup<<26) | (commonAdditive<<20) | 1;
783     if (nw < maxSize) {
784       buf[nw++] = x; 
785     }
786     else {
787       of++;
788     }
789   }
790
791   // Scan for ROB and MCM
792   for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
793     Int_t iRob = iRobRow * 2 + side;
794     for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
795       Int_t padrow = iRobRow * 4 + iMcm / 4;
796
797       // MCM header
798       x = ((iRob * fGeo->MCMmax() + iMcm) << 24) | ((iEv % 0x100000) << 4) | 0xC;
799       if (nw < maxSize) {
800         buf[nw++] = x; 
801       }
802       else {
803         of++;
804       }
805
806       // ADC data
807       for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
808         Int_t padcol = fFee->GetPadColFromADC(iRob, iMcm, iAdc);
809         UInt_t aa = !(iAdc & 1) + 2;    // 3 for the even ADC channel , 2 for the odd ADC channel
810         UInt_t *a = new UInt_t[kNTBin+2];
811         // 3 timebins are packed into one 32 bits word
812         for (Int_t iT = 0; iT < kNTBin; iT+=3) { 
813           if ((padcol >=    0) && (padcol <  nCol)) {
814             a[iT  ] = ((iT    ) < kNTBin ) ? digits->GetData(padrow,padcol,iT    ) : 0;
815             a[iT+1] = ((iT + 1) < kNTBin ) ? digits->GetData(padrow,padcol,iT + 1) : 0;
816             a[iT+2] = ((iT + 2) < kNTBin ) ? digits->GetData(padrow,padcol,iT + 2) : 0; 
817           } 
818           else {
819             a[iT] = a[iT+1] = a[iT+2] = 0; // This happenes at the edge of chamber (should be pedestal! How?)
820           }
821           x = (a[iT+2] << 22) | (a[iT+1] << 12) | (a[iT] << 2) | aa;
822           if (nw < maxSize) {
823             buf[nw++] = x; 
824           }
825           else {
826             of++;
827           }
828         }
829         // Diagnostics
830         Float_t avg = 0;
831         Float_t rms = 0;
832         for (Int_t iT = 0; iT < kNTBin; iT++) {
833           avg += (Float_t) (a[iT]);
834         }
835         avg /= (Float_t) kNTBin;
836         for (Int_t iT = 0; iT < kNTBin; iT++) {
837           rms += ((Float_t) (a[iT]) - avg) * ((Float_t) (a[iT]) - avg);
838         }
839         rms = TMath::Sqrt(rms / (Float_t) kNTBin);
840         if (rms > 1.7) {
841           AliDebug(2,Form("Large RMS (>1.7)  (ROB,MCM,ADC)=(%02d,%02d,%02d), avg=%03.1f, rms=%03.1f"
842                           ,iRob,iMcm,iAdc,avg,rms));
843         }
844         delete [] a;
845       }
846     }
847   }
848
849   // Write end of raw data marker
850   if (nw < maxSize) {
851     buf[nw++] = kEndofrawdatamarker; 
852   }
853   else {
854     of++;
855   }
856   if (of != 0) {
857     AliWarning("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
858   }
859
860   return nw;
861
862 }
863
864 //_____________________________________________________________________________
865
866 //Int_t AliTRDrawData::ProduceHcDataV3(AliTRDarrayADC *digits, Int_t side , Int_t det, UInt_t *buf, Int_t maxSize)
867 Int_t AliTRDrawData::ProduceHcDataV3(AliTRDarrayADC *digits, Int_t side , Int_t det, UInt_t *buf, Int_t maxSize, Bool_t newEvent = kFALSE)
868 {
869   //
870   // This function simulates: Raw Version == 3 (Zero Suppression Prototype)
871   //
872
873   Int_t           nw = 0;                       // Number of written    words
874   Int_t           of = 0;                       // Number of overflowed words
875   Int_t        layer = fGeo->GetLayer( det );   // Layer
876   Int_t        stack = fGeo->GetStack( det );   // Stack
877   Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
878   Int_t         nRow = fGeo->GetRowMax( layer, stack, sect );
879   Int_t         nCol = fGeo->GetColMax( layer );
880   const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
881   Int_t       kCtype = 0;                       // Chamber type (0:C0, 1:C1)
882   //Int_t          iEv = 0xA;                     // Event ID. Now fixed to 10, how do I get event id?
883
884  
885
886   Bool_t trackletOn = fFee->GetTracklet();     // **new**
887
888   // Check the nCol and nRow.
889   if ((nCol == 144) && 
890       (nRow == 16 || nRow == 12)) {
891     kCtype = (nRow-12) / 4;
892   } 
893   else {
894     AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
895                  ,nRow,nCol));
896     return 0;
897   }
898
899   AliDebug(1,Form("Producing raw data for sect=%d layer=%d stack=%d side=%d"
900                  ,sect,layer,stack,side));
901
902   AliTRDmcmSim** mcm = new AliTRDmcmSim*[(kCtype + 3)*(fGeo->MCMmax())];
903
904   // in case no tracklet-words are processed: write the tracklet-endmarker as well as all additional words immediately and write 
905   // raw-data in one go; if tracklet-processing is enabled, first all tracklet-words of a half-chamber have to be processed before the
906   // additional words (tracklet-endmarker,headers,...)are written. Raw-data is written in a second loop;
907   
908   if (!trackletOn) {
909       WriteIntermediateWords(buf,nw,of,maxSize,det,side); 
910   }
911   
912   // Scan for ROB and MCM
913   // scanning direction such, that tracklet-words are sorted in ascending z and then in ascending y order
914   // ROB numbering on chamber and MCM numbering on ROB increase with decreasing z and increasing y
915   for (Int_t iRobRow =  (kCtype + 3)-1; iRobRow >= 0; iRobRow-- ) {
916     Int_t iRob = iRobRow * 2 + side;
917     // MCM on one ROB
918     for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {
919         Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);
920         Int_t entry = iRobRow*(fGeo->MCMmax()) + iMcm;
921         
922         mcm[entry] = new AliTRDmcmSim();
923         mcm[entry]->Init( det, iRob, iMcm , newEvent);
924         //mcm[entry]->Init( det, iRob, iMcm);
925         if (newEvent == kTRUE) newEvent = kFALSE; // only one mcm is concerned with new event
926         Int_t padrow = mcm[entry]->GetRow();
927
928         // Copy ADC data to MCM simulator
929         for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
930             Int_t padcol = mcm[entry]->GetCol( iAdc );
931             if ((padcol >=    0) && (padcol <  nCol)) {
932                 for (Int_t iT = 0; iT < kNTBin; iT++) { 
933                   mcm[entry]->SetData( iAdc, iT, digits->GetData( padrow, padcol, iT) );
934                 } 
935             } 
936             else {  // this means it is out of chamber, and masked ADC
937                 mcm[entry]->SetDataPedestal( iAdc );
938             }
939         }
940
941         // Simulate process in MCM
942         mcm[entry]->Filter();     // Apply filter
943         mcm[entry]->ZSMapping();  // Calculate zero suppression mapping
944 //jkl   mcm[entry]->CopyArrays();
945 //jkl   mcm[entry]->GeneratefZSM1Dim();
946 //jkl   mcm[entry]->RestoreZeros();
947
948         if (trackletOn) {
949             mcm[entry]->Tracklet(); 
950             Int_t tempNw =  mcm[entry]->ProduceTrackletStream( &buf[nw], maxSize - nw );
951             //Int_t tempNw = 0;
952             if( tempNw < 0 ) {
953                 of += tempNw;
954                 nw += maxSize - nw;
955                 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
956             } else {
957                 nw += tempNw;
958             }
959         }
960         // no tracklets: write raw-data already in this loop
961         else {
962              // Write MCM data to buffer
963             Int_t tempNw =  mcm[entry]->ProduceRawStream( &buf[nw], maxSize - nw );
964             if( tempNw < 0 ) {
965                 of += tempNw;
966                 nw += maxSize - nw;
967                 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
968             } else {
969                 nw += tempNw;
970             }
971             
972             delete mcm[entry];
973         }
974             
975         
976
977
978         //mcm->DumpData( "trdmcmdata.txt", "RFZS" ); // debugging purpose
979     }
980   }
981
982   // if tracklets are switched on, raw-data can be written only after all tracklets
983   if (trackletOn) {
984       WriteIntermediateWords(buf,nw,of,maxSize,det,side); 
985   
986   
987       // Scan for ROB and MCM
988       for (Int_t iRobRow =  (kCtype + 3)-1; iRobRow >= 0; iRobRow-- ) {
989           //Int_t iRob = iRobRow * 2 + side;
990           // MCM on one ROB
991           for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {
992               Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);
993               
994               Int_t entry = iRobRow*(fGeo->MCMmax()) + iMcm; 
995               
996               // Write MCM data to buffer
997               Int_t tempNw =  mcm[entry]->ProduceRawStream( &buf[nw], maxSize - nw );
998               if( tempNw < 0 ) {
999                   of += tempNw;
1000                   nw += maxSize - nw;
1001                   AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
1002               } else {
1003                   nw += tempNw;
1004               }
1005               
1006               delete mcm[entry];
1007           
1008           }
1009       }
1010   }
1011
1012   delete [] mcm;
1013   
1014   // Write end of raw data marker
1015   if (nw < maxSize) {
1016     buf[nw++] = kEndofrawdatamarker; 
1017   }
1018   else {
1019     of++;
1020   }
1021   if (of != 0) {
1022     AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
1023   }
1024
1025
1026   return nw;
1027
1028 }
1029
1030 //_____________________________________________________________________________
1031 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
1032 {
1033   //
1034   // Vx of the raw data reading
1035   //
1036
1037   rawReader->Select("TRD"); //[mj]
1038
1039   AliTRDarrayADC *digits = 0;
1040   AliTRDarrayDictionary *track0 = 0;
1041   AliTRDarrayDictionary *track1 = 0;
1042   AliTRDarrayDictionary *track2 = 0;  
1043
1044   //AliTRDSignalIndex *indexes = 0;
1045   // Create the digits manager
1046   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
1047   digitsManager->CreateArrays();
1048
1049   if (!fTrackletContainer) {
1050   //if (!fTrackletContainer && ( fReconstructor->IsWritingTracklets() || fReconstructor->IsProcessingTracklets() )) {
1051     // maximum tracklets for one HC
1052     const Int_t kTrackletChmb=256;
1053     fTrackletContainer = new UInt_t *[2];
1054     fTrackletContainer[0] = new UInt_t[kTrackletChmb];
1055     fTrackletContainer[1] = new UInt_t[kTrackletChmb];
1056   }
1057
1058   AliTRDrawStreamBase *pinput = AliTRDrawStreamBase::GetRawStream(rawReader);
1059   AliTRDrawStreamBase &input = *pinput;
1060   input.SetRawVersion( fFee->GetRAWversion() ); //<= ADDED by MinJung
1061
1062   AliInfo(Form("Stream version: %s", input.IsA()->GetName()));
1063
1064   // Loop through the digits
1065   Int_t det    = 0;
1066
1067   while (det >= 0)
1068     {
1069       //det = input.NextChamber(digitsManager);
1070       det = input.NextChamber(digitsManager,fTrackletContainer);
1071
1072     //if (!fReconstructor->IsWritingTracklets()) continue;
1073     if (*(fTrackletContainer[0]) > 0 || *(fTrackletContainer[1]) > 0) WriteTracklets(det);
1074
1075       if (det >= 0)
1076         {
1077           // get...
1078           digits = (AliTRDarrayADC *) digitsManager->GetDigits(det);
1079           track0 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,0);
1080           track1 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,1);
1081           track2 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,2);
1082           // and compress
1083           if (digits) digits->Compress();  
1084           if (track0) track0->Compress();   
1085           if (track1) track1->Compress();     
1086           if (track2) track2->Compress();
1087         }
1088     }
1089
1090   if (fTrackletContainer){
1091     delete [] fTrackletContainer[0];
1092     delete [] fTrackletContainer[1];
1093     delete [] fTrackletContainer;
1094     fTrackletContainer = NULL;
1095   }
1096
1097   delete pinput;
1098   pinput = NULL;
1099
1100   return digitsManager;
1101 }
1102
1103 //_____________________________________________________________________________
1104 void AliTRDrawData::WriteIntermediateWords(UInt_t* buf, Int_t& nw, Int_t& of, const Int_t& maxSize, const Int_t& det, const Int_t& side) {
1105     //
1106     // write half-chamber headers 
1107     //
1108     
1109     Int_t        layer = fGeo->GetLayer( det );   // Layer
1110     Int_t        stack = fGeo->GetStack( det );   // Stack
1111     Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
1112     Int_t           rv = fFee->GetRAWversion();
1113     const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
1114     UInt_t           x = 0;
1115
1116     // Write end of tracklet marker
1117     if (nw < maxSize) {
1118         buf[nw++] = kEndoftrackletmarker;
1119     } 
1120     else {
1121         of++;
1122     }
1123     
1124   // Half Chamber header
1125   // h[0] (there are 3 HC header)
1126     Int_t minorv = 0;    // The minor version number
1127     Int_t add    = 2;    // The number of additional header words to follow
1128     x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (side<<2) | 1;
1129     if (nw < maxSize) {
1130         buf[nw++] = x; 
1131     }
1132     else {
1133         of++;
1134     }
1135     // h[1]
1136     Int_t bcCtr   = 99; // bunch crossing counter. Here it is set to 99 always for no reason
1137     Int_t ptCtr   = 15; // pretrigger counter. Here it is set to 15 always for no reason
1138     Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
1139     x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1;
1140     if (nw < maxSize) {
1141         buf[nw++] = x; 
1142     }
1143     else {
1144         of++;
1145     }
1146     // h[2]
1147     Int_t pedSetup       = 1;  // Pedestal filter setup (0:1). Here it is always 1 for no reason
1148     Int_t gainSetup      = 1;  // Gain filter setup (0:1). Here it is always 1 for no reason
1149     Int_t tailSetup      = 1;  // Tail filter setup (0:1). Here it is always 1 for no reason
1150     Int_t xtSetup        = 0;  // Cross talk filter setup (0:1). Here it is always 0 for no reason
1151     Int_t nonlinSetup    = 0;  // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
1152     Int_t bypassSetup    = 0;  // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
1153     Int_t commonAdditive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
1154     x = (pedSetup<<31) | (gainSetup<<30) | (tailSetup<<29) | (xtSetup<<28) | (nonlinSetup<<27)
1155         | (bypassSetup<<26) | (commonAdditive<<20) | 1;
1156     if (nw < maxSize) {
1157         buf[nw++] = x; 
1158     }
1159     else {
1160         of++;
1161     } 
1162 }
1163
1164 //_____________________________________________________________________________
1165 void AliTRDrawData::WriteIntermediateWordsV2(UInt_t* buf, Int_t& nw, Int_t& of, const Int_t& maxSize, const Int_t& det, const Int_t& side) {
1166     // 
1167     // write tracklet end marker(0x10001000) 
1168     // and half chamber headers(H[0] and H[1])
1169     //
1170     
1171     Int_t        layer = fGeo->GetLayer( det );   // Layer
1172     Int_t        stack = fGeo->GetStack( det );   // Stack
1173     Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
1174     Int_t           rv = fFee->GetRAWversion();
1175     const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
1176     Bool_t  trackletOn = fFee->GetTracklet();
1177     UInt_t           x = 0;
1178
1179     // Write end of tracklet marker
1180         if (nw < maxSize){
1181         buf[nw++] = fgkEndOfTrackletMarker;
1182         buf[nw++] = fgkEndOfTrackletMarker;     // the number of tracklet end marker should be more than 2
1183     }
1184     else {
1185         of++;
1186     }
1187
1188    
1189         // Half Chamber header
1190         // h[0] (there are 2 HC headers) xmmm mmmm nnnn nnnq qqss sssp ppcc ci01
1191         //      , where  x : Raw version speacial number (=1)
1192         //                   m : Raw version major number (test pattern, ZS, disable tracklet, 0, options)
1193         //                   n : Raw version minor number
1194         //                   q : number of addtional header words (default = 1)
1195         //                   s : SM sector number (ALICE numbering)
1196         //                   p : plane(layer) number
1197         //                       c : chamber(stack) number
1198         //                       i : side number (0:A, 1:B)
1199         Int_t majorv = 0;       // The major version number 
1200     Int_t minorv = 0;   // The minor version number
1201     Int_t add    = 1;   // The number of additional header words to follow : now 1, previous 2
1202         Int_t tp         = 0;   // test pattern (default=0)
1203         Int_t zs         = (rv==3) ? 1 : 0;                     // zero suppression
1204         Int_t dt         = (trackletOn) ? 0 : 1;        // disable tracklet 
1205
1206         majorv = (tp<<6) | (zs<<5) | (dt<<4) | 1;       // major version
1207
1208     x = (1<<31) | (majorv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (side<<2) | 1;
1209     if (nw < maxSize) buf[nw++] = x; else of++;
1210    
1211     // h[1]             tttt ttbb bbbb bbbb bbbb bbpp pphh hh01
1212         // , where  t : number of time bins
1213         //          b : bunch crossing number
1214         //          p : pretrigger counter
1215         //          h : pretrigger phase
1216     Int_t bcCtr   = 99; // bunch crossing counter. Here it is set to 99 always for no reason
1217     Int_t ptCtr   = 15; // pretrigger counter. Here it is set to 15 always for no reason
1218     Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
1219     //x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1;       // old format
1220     x = ((kNTBin)<<26) | (bcCtr<<10) | (ptCtr<<6) | (ptPhase<<2) | 1;
1221     if (nw < maxSize) buf[nw++] = x; else of++;
1222   
1223 }
1224
1225 //_____________________________________________________________________________
1226 Bool_t AliTRDrawData::WriteTracklets(Int_t det)
1227 {
1228   //
1229   // Write the raw data tracklets into seperate file
1230   //
1231
1232   UInt_t **leaves = new UInt_t *[2];
1233   for (Int_t i=0; i<2 ;i++){
1234     leaves[i] = new UInt_t[258];
1235     leaves[i][0] = det; // det
1236     leaves[i][1] = i;   // side
1237     memcpy(leaves[i]+2, fTrackletContainer[i], sizeof(UInt_t) * 256);
1238   }
1239
1240   if (!fTrackletTree){
1241     AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets");
1242     dl->MakeTree();
1243     fTrackletTree = dl->Tree();
1244   }
1245
1246   TBranch *trkbranch = fTrackletTree->GetBranch("trkbranch");
1247   if (!trkbranch) {
1248     trkbranch = fTrackletTree->Branch("trkbranch",leaves[0],"det/i:side/i:tracklets[256]/i");
1249   }
1250
1251   for (Int_t i=0; i<2; i++){
1252     if (leaves[i][2]>0) {
1253       trkbranch->SetAddress(leaves[i]);
1254       fTrackletTree->Fill();
1255     }
1256   }
1257
1258   AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets");
1259   dl->WriteData("OVERWRITE");
1260   //dl->Unload();
1261   delete [] leaves;
1262
1263   return kTRUE;
1264
1265 }
1266
1267 //_____________________________________________________________________________
1268 Bool_t AliTRDrawData::OpenOutput()
1269 {
1270   //
1271   // Connect the output tree
1272   //
1273
1274   // tracklet writing
1275   if (1){
1276   //if (fReconstructor->IsWritingTracklets()){
1277     TString evfoldname = AliConfig::GetDefaultEventFolderName();
1278     fRunLoader         = AliRunLoader::GetRunLoader(evfoldname);
1279
1280     if (!fRunLoader) {
1281       fRunLoader = AliRunLoader::Open("galice.root");
1282     }
1283     if (!fRunLoader) {
1284       AliError(Form("Can not open session for file galice.root."));
1285       return kFALSE;
1286     }
1287
1288     UInt_t **leaves = new UInt_t *[2];
1289     AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets");
1290     if (!dl) {
1291       AliError("Could not get the tracklets data loader!");
1292       dl = new AliDataLoader("TRD.Tracklets.root","tracklets", "tracklets");
1293       fRunLoader->GetLoader("TRDLoader")->AddDataLoader(dl);
1294     }
1295     fTrackletTree = dl->Tree();
1296     if (!fTrackletTree)
1297       {
1298        dl->MakeTree();
1299        fTrackletTree = dl->Tree();
1300       }
1301     TBranch *trkbranch = fTrackletTree->GetBranch("trkbranch");
1302     if (!trkbranch)
1303       fTrackletTree->Branch("trkbranch",leaves[0],"det/i:side/i:tracklets[256]/i");
1304   }
1305   return kTRUE;
1306
1307 }
1308
1309
1310
1311