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