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