]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/AliZDCRawStream.cxx
c7ac3a38a3ef1a71a31754ba38f1d5126614e85c
[u/mrichter/AliRoot.git] / ZDC / AliZDCRawStream.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 // This class provides access to ZDC digits in raw data.                     //
21 //                                                                           //
22 // It loops over all ZDC digits in the raw data given by the AliRawReader.   //
23 // The Next method goes to the next digit. If there are no digits left       //
24 // it returns kFALSE.                                                        //
25 // Getters provide information about the current digit.                      //
26 //                                                                           //
27 ///////////////////////////////////////////////////////////////////////////////
28
29 #include <TSystem.h>
30 #include "AliZDCRawStream.h"
31 #include "AliRawReader.h"
32 #include "AliRawDataHeader.h"
33 #include "AliRawEventHeaderBase.h"
34 #include "AliLog.h"
35
36 ClassImp(AliZDCRawStream)
37
38
39 //_____________________________________________________________________________
40 AliZDCRawStream::AliZDCRawStream(AliRawReader* rawReader) :
41   fRawReader(rawReader),
42   fBuffer(0),
43   fReadOutCard(-1),
44   fEvType(0),
45   fPosition(0),
46   fIsCalib(kFALSE),
47   fIsDARCHeader(kFALSE),
48   fIsHeaderMapping(kFALSE),
49   fIsChMapping(kFALSE),
50   fIsADCDataWord(kFALSE),
51   fIsADCHeader(kFALSE),
52   fIsADCEOB(kFALSE),
53   fSODReading(kFALSE),
54   fIsMapRead(kFALSE),
55   fDeadfaceOffset(-1),
56   fDeadbeefOffset(-1),
57   fDataOffset(0),
58   fModType(-1),
59   fADCModule(-1),
60   fADCNChannels(-1),     
61   fADCChannel(-1),       
62   fADCValue(-1),         
63   fADCGain(-1),
64   fIsUnderflow(kFALSE),
65   fIsOverflow(kFALSE),
66   fScGeo(0),      
67   fScNWords(0),   
68   fScTriggerSource(0),    
69   fScTriggerNumber(0),
70   fIsScEventGood(kTRUE),
71   fIsScHeaderRead(kFALSE),
72   fScStartCounter(0),
73   fScEvCounter(0),
74   fIsScalerWord(kFALSE),
75   fDetPattern(0),
76   fTrigCountNWords(0),
77   fIsTriggerScaler(kFALSE),
78   fTrigCountStart(0),
79   fMBTrigInput(0),         
80   fCentralTrigInput(0), 
81   fSCentralTrigInput(0),
82   fEMDTrigInput(0),     
83   fL0Received(0),          
84   fMBtrig2CTP(0),          
85   fCentralTrig2CTP(0),  
86   fSCentralTrig2CTP(0), 
87   fEMDTrig2CTP(0),            
88   fTrigHistNWords(0),
89   fIsTriggerHistory(kFALSE),
90   fTrigHistStart(0),
91   fPileUpBit1stWord(0),
92   fL0Bit1stWord(0),
93   fCentralTrigHist(0),
94   fMBTrigHist(0),
95   fPileUpBit2ndWord(0),
96   fL0Bit2ndWord(0), 
97   fSCentralTrigHist(0),
98   fEMDTrigHist(0),
99   fNChannelsOn(0),
100   fCurrentCh(-1),
101   fCabledSignal(-1),
102   fCurrScCh(-1),
103   fIsADCEventGood(kTRUE),
104   fIsL0BitSet(kTRUE),
105   fIsPileUpEvent(kFALSE),
106   fIsZDCTDCHeader(kFALSE),
107   fIsTDCHeaderRead(kFALSE),
108   fTDCStartCounter(0),
109   fIsADDChannel(kFALSE),
110   fIsADDTDCHeader(kFALSE),
111   fIsADDTDCdatum(kFALSE)
112 {
113   // Create an object to read ZDC raw digits
114   fRawReader->Reset();
115   fRawReader->Select("ZDC");
116   //
117   const int kNch = 48;
118   for(Int_t i=0; i<kNch; i++){
119     for(Int_t j=0; j<5; j++){
120       fMapADC[i][j]=-1;
121       if(i<32) fScalerMap[i][j]=-1;
122     }
123   }
124   
125   for(Int_t k=0; k<4; k++) fCPTInput[k] = 0;
126
127 }
128
129 //_____________________________________________________________________________
130 AliZDCRawStream::AliZDCRawStream(const AliZDCRawStream& stream) :
131   TObject(stream),
132   fRawReader(stream.fRawReader),
133   fBuffer(stream.GetRawBuffer()),
134   fReadOutCard(stream.GetReadOutCard()),
135   fEvType(stream.fEvType),
136   fPosition(stream.fPosition),
137   fIsCalib(stream.fIsCalib),
138   fIsDARCHeader(stream.fIsDARCHeader), 
139   fIsHeaderMapping(stream.fIsHeaderMapping),
140   fIsChMapping(stream.fIsChMapping),
141   fIsADCDataWord(stream.fIsADCDataWord), 
142   fIsADCHeader(stream.fIsADCHeader), 
143   fIsADCEOB(stream.fIsADCEOB), 
144   fSODReading(stream.fSODReading),
145   fIsMapRead(stream.fIsMapRead),
146   fDeadfaceOffset(stream.GetDeadfaceOffset()),
147   fDeadbeefOffset(stream.GetDeadbeefOffset()),
148   fDataOffset(stream.GetDataOffset()),
149   fModType(stream.GetModType()),
150   fADCModule(stream.GetADCModule()),     
151   fADCNChannels(stream.GetADCNChannels()),       
152   fADCChannel(stream.GetADCChannel()),   
153   fADCValue(stream.GetADCValue()),       
154   fADCGain(stream.GetADCGain()),
155   fIsUnderflow(stream.fIsUnderflow),
156   fIsOverflow(stream.fIsOverflow),
157   fScGeo(stream.GetScGeo()),      
158   fScNWords(stream.GetScNWords()),        
159   fScTriggerSource(stream.GetScTriggerSource()),          
160   fScTriggerNumber(stream.fScTriggerNumber),
161   fIsScEventGood(stream.fIsScEventGood),
162   fIsScHeaderRead(stream.fIsScHeaderRead),
163   fScStartCounter(stream.fScStartCounter),
164   fScEvCounter(stream.fScEvCounter),
165   fIsScalerWord(stream.fIsScalerWord),
166   fDetPattern(stream.fDetPattern),
167   fTrigCountNWords(stream.fTrigCountNWords),
168   fIsTriggerScaler(stream.fIsTriggerScaler),
169   fTrigCountStart(stream.fTrigCountStart),
170   fMBTrigInput(stream.fMBTrigInput),       
171   fCentralTrigInput(stream.fCentralTrigInput), 
172   fSCentralTrigInput(stream.fSCentralTrigInput),
173   fEMDTrigInput(stream.fEMDTrigInput),     
174   fL0Received(stream.fL0Received),         
175   fMBtrig2CTP(stream.fMBtrig2CTP),         
176   fCentralTrig2CTP(stream.fCentralTrig2CTP),  
177   fSCentralTrig2CTP(stream.fSCentralTrig2CTP), 
178   fEMDTrig2CTP(stream.fEMDTrig2CTP),          
179   fTrigHistNWords(stream.fTrigHistNWords),
180   fIsTriggerHistory(stream.fIsTriggerHistory),
181   fTrigHistStart(stream.fTrigHistStart),
182   fPileUpBit1stWord(stream.fPileUpBit1stWord),
183   fL0Bit1stWord(stream.fL0Bit1stWord), 
184   fCentralTrigHist(stream.fCentralTrigHist),
185   fMBTrigHist(stream.fMBTrigHist),
186   fPileUpBit2ndWord(stream.fPileUpBit2ndWord),
187   fL0Bit2ndWord(stream.fL0Bit2ndWord), 
188   fSCentralTrigHist(stream.fSCentralTrigHist),
189   fEMDTrigHist(stream.fEMDTrigHist),
190   fNChannelsOn(stream.fNChannelsOn),
191   fCurrentCh(stream.fCurrentCh),
192   fCabledSignal(stream.GetCabledSignal()),
193   fCurrScCh(stream.fCurrScCh),
194   fIsADCEventGood(stream.fIsADCEventGood),
195   fIsL0BitSet(stream.fIsL0BitSet),
196   fIsPileUpEvent(stream.fIsPileUpEvent),
197   fIsZDCTDCHeader(stream.fIsZDCTDCHeader),
198   fIsTDCHeaderRead(stream.fIsTDCHeaderRead),
199   fTDCStartCounter(stream.fTDCStartCounter),
200   fIsADDChannel(stream.fIsADDChannel),
201   fIsADDTDCHeader(stream.fIsADDTDCHeader),
202   fIsADDTDCdatum(stream.fIsADDTDCdatum)
203
204 {
205   // Copy constructor
206   const int kNch = 48;
207   for(Int_t j=0; j<2; j++) fSector[j] = stream.GetSector(j);     
208   for(Int_t i=0; i<kNch; i++){
209     for(Int_t j=0; j<5; j++){
210       fMapADC[i][j] = stream.fMapADC[i][j];
211       if(i<32) fScalerMap[i][j] = stream.fMapADC[i][j];
212     }
213   }
214   
215   for(Int_t k=0; k<4; k++) fCPTInput[k] = stream.fCPTInput[k];
216 }
217
218 //_____________________________________________________________________________
219 AliZDCRawStream& AliZDCRawStream::operator = (const AliZDCRawStream& 
220                                               /* stream */)
221 {
222   // Assignment operator
223   Fatal("operator =", "assignment operator not implemented");
224   return *this;
225 }
226
227 //_____________________________________________________________________________
228 AliZDCRawStream::~AliZDCRawStream()
229 {
230 // Destructor
231
232 }
233
234 //_____________________________________________________________________________
235 void AliZDCRawStream::ReadChMap()
236 {
237   // Reading channel map
238   const int kNch = 48;
239   AliDebug(2,"\t Reading ZDC ADC mapping from OCDB\n");
240   AliZDCChMap * chMap = GetChMap();
241   //chMap->Print("");
242   for(Int_t i=0; i<kNch; i++){
243     fMapADC[i][0] = chMap->GetADCModule(i);
244     fMapADC[i][1] = chMap->GetADCChannel(i);
245     fMapADC[i][2] = -1;
246     fMapADC[i][3] = chMap->GetDetector(i);
247     fMapADC[i][4] = chMap->GetSector(i);
248   }
249   fIsMapRead = kTRUE;
250 }
251
252 //_____________________________________________________________________________
253 void AliZDCRawStream::ReadCDHHeader()
254 {
255   // Reading CDH 
256   const AliRawDataHeader* header = fRawReader->GetDataHeader();
257   if(!header) {
258       AliError(" No CDH in raw data streaming");
259       fRawReader->AddMajorErrorLog(kCDHError);
260       return;
261   }
262   else{
263     //printf("\t AliZDCRawStream::ReadCDHHeader -> Data Size = %x\n",fRawReader->GetDataSize());
264
265     UChar_t message = header->GetAttributes();
266     //printf("\t AliZDCRawStream::ReadCDHHeader -> Attributes %x\n",message);
267     
268     if((message & 0xf0) == 0x0){ // PHYSICS RUN
269        //printf("\t PHYSICS RUN raw data found\n");
270     }
271     else if((message & 0xf0) == 0x10){ // COSMIC RUN
272        //printf("\t STANDALONE_COSMIC RUN raw data found\n");
273     }
274     else if((message & 0xf0) == 0x20){ // PEDESTAL RUN
275        //printf("\t STANDALONE_PEDESTAL RUN raw data found\n");
276     }
277     else if((message & 0xf0) == 0x30){ // LASER RUN
278        //printf("\t STANDALONE_LASER RUN raw data found\n");
279     }
280     else if((message & 0xf0) == 0x40){ // CALIBRATION_CENTRAL RUN
281        //printf("\t CALIBRATION_CENTRAL RUN raw data found\n");
282     }
283     else if((message & 0xf0) == 0x50){ // CALIBRATION_SEMICENTRAL
284        //printf("\t CALIBRATION_SEMICENTRAL RUN raw data found\n");
285     }
286     else if((message & 0xf0) == 0x60){ // CALIBRATION_MB
287        //printf("\t CALIBRATION_MB RUN raw data found\n");
288     }
289     else if((message & 0xf0) == 0x70){ // CALIBRATION_EMD
290        //printf("\t CALIBRATION_EMD RUN raw data found\n");
291     }
292     // *** Checking the bit indicating the used readout card
293     // (the payload is different in the 2 cases!)
294     if((message & 0x08) == 0){  // ** DARC card
295        fReadOutCard = 0;
296        fIsDARCHeader = kTRUE;
297        //AliInfo("\t ZDC readout card used: DARC");
298     }
299     else if((message & 0x08) == 0x08){  // ** ZRC card
300        fReadOutCard = 1;
301        //AliInfo("\t ZDC readout card used: ZRC");
302     }
303
304     if(header->GetL1TriggerMessage() & 0x1){ // Calibration bit set in CDH
305       fIsCalib = kTRUE;
306     }
307     //printf("\t AliZDCRawStream::ReadCDHHeader -> L1TriggerMessage %x\n",header->GetL1TriggerMessage());
308     //printf("\t AliZDCRawStream::ReadCDHHeader -> Calibration bit = %d\n",fIsCalib);    
309     
310 /*    UInt_t status = header->GetStatus();
311     //printf("\t AliZDCRawStream::ReadCDHHeader -> status = %d\n",status);
312     if((status & 0x000f) == 0x0001){
313       AliDebug(2,"CDH -> DARC trg0 overlap error");
314       fRawReader->AddMajorErrorLog(kDARCError);
315     }
316     if((status & 0x000f) == 0x0002){
317       AliDebug(2,"CDH -> DARC trg0 missing error");
318       fRawReader->AddMajorErrorLog(kDARCError);
319     }
320     if((status & 0x000f) == 0x0004){
321       AliDebug(2,"CDH -> DARC data parity error");
322       fRawReader->AddMajorErrorLog(kDARCError);
323     }
324     if((status & 0x000f) == 0x0008){
325       AliDebug(2,"CDH -> DARC ctrl parity error");
326       fRawReader->AddMajorErrorLog(kDARCError);
327     }
328     //
329     if((status & 0x00f0) == 0x0010){
330       AliDebug(2,"CDH -> DARC trg unavailable");
331       fRawReader->AddMajorErrorLog(kDARCError);
332     }
333     if((status & 0x00f0) == 0x0020){
334       AliDebug(2,"CDH -> DARC FEE error");
335       fRawReader->AddMajorErrorLog(kDARCError);
336     }
337     //
338     if((status & 0x0f00) == 0x0200){
339       AliDebug(2,"CDH -> DARC L1 time violation");
340       fRawReader->AddMajorErrorLog(kDARCError);
341     }
342     if((status & 0x0f00) == 0x0400){
343       AliDebug(2,"CDH -> DARC L2 time-out");
344       fRawReader->AddMajorErrorLog(kDARCError);
345     }
346     if((status & 0x0f00) == 0x0800){
347       AliDebug(2,"CDH -> DARC prepulse time violation");
348       fRawReader->AddMajorErrorLog(kDARCError);
349     }
350     //
351     if((status & 0xf000) == 0x1000){
352       AliDebug(2,"CDH -> DARC other error");
353       fRawReader->AddMajorErrorLog(kDARCError);
354     }
355     */
356   }
357   
358 }
359
360 //_____________________________________________________________________________
361 Bool_t AliZDCRawStream::Next()
362 {
363   // Read the next raw digit
364   // Returns kFALSE if there is no digit left
365
366   if(!fRawReader->ReadNextInt((UInt_t&) fBuffer)) return kFALSE;
367   const int kNch = 48;
368   Int_t kFirstADCGeo=0, kLastADCGeo=3;
369   Int_t kADDADCGeo=5, kADDTDCGeo=6;
370   Int_t kZDCTDCGeo=8, kScalerGeo=16;
371   Int_t kPUGeo=29, kTrigScales=30, kTrigHistory=31;
372   //
373   fIsHeaderMapping = kFALSE; fIsChMapping = kFALSE; 
374   fIsADCHeader = kFALSE; fIsADCDataWord = kFALSE; fIsADCEOB = kFALSE;
375   fIsADDChannel = kFALSE; fIsADDTDCHeader= kFALSE; fIsADDTDCdatum=kFALSE;
376   fIsUnderflow = kFALSE; fIsOverflow = kFALSE; fIsScalerWord = kFALSE;
377   fSector[0] = fSector[1] = -1;
378   for(Int_t kl=0; kl<4; kl++) fCPTInput[kl] = 0;
379
380   fEvType = fRawReader->GetType();
381   if(fPosition==0){
382     //if(fEvType==7 || fEvType ==8){ //Physics or calibration event
383       ReadCDHHeader();
384     //}
385     fCurrentCh=0; fCurrScCh=0; fNChannelsOn=0;
386     // Ch. debug
387     //printf("\n  AliZDCRawStream::Next() - ev. type %d",fEvType);
388   }
389   
390   // Ch. debug
391   //printf("\n  AliZDCRawStream::Next() - fBuffer[%d] = %x\n",fPosition, fBuffer);
392   
393   // *** End of ZDC event
394   if(fBuffer == 0xcafefade){
395     //printf("\n  AliZDCRawStream::Next() ***** End of ZDC event *****\n\n");
396     return kFALSE;
397   }
398   
399   // -------------------------------------------
400   // --- DARC header
401   // -------------------------------------------
402   // If the CDH has been read then 
403   // the DARC header must follow
404   if(fReadOutCard==0 && fIsDARCHeader){
405     //printf("\t ---- DARC header ----\n");
406     if(fIsCalib){
407       fDeadfaceOffset = 9;
408       fDeadbeefOffset = 25;
409     }
410     else{
411       fDeadfaceOffset = 1;
412       fDeadbeefOffset = 7;
413     }
414     fDataOffset = 1+fDeadbeefOffset;
415     fIsDARCHeader = kFALSE;
416   }
417     
418   // ---------------------------------------------
419   // --- Start of data event (SOD)             ---
420   // --- decoding mapping of connected ADC ch. ---
421   // ---------------------------------------------
422   // In the SOD event ADC ch. mapping is written
423   if(fEvType==10){
424    if(fSODReading){
425     
426     if(fPosition>=fDataOffset){
427       if((fBuffer&0xff000001) == 0xff000001){ // ************** Mapping
428         // DARC 1st datum @ fDataOffset+1 \ ZRC 1st valid datum @ fDataOffset=0
429         if((fPosition==fDataOffset+1) || (fPosition==fDataOffset)){ 
430            printf("\n\n ------ AliZDCRawStream -> Reading mapping from StartOfData event ------\n");
431            fCurrentCh=0; fCurrScCh=0;   
432         }
433         else{
434           printf(" ------ AliZDCRawStream -> End of ZDC StartOfData event ------\n\n");
435           fSODReading = kFALSE;
436           return kFALSE;
437         }
438       }
439       else if((fBuffer&0xff000002) == 0xff000002){ // ************** Threshold settings
440         fPosition++;
441         return kFALSE; // !!!!!!!!!!!!!!!!!!!!!  For the moment thresholds are not read
442       }
443       else if((fBuffer&0x80000000)>>31 == 1){ // --- Mapping header
444         fIsHeaderMapping = kTRUE;
445         fADCModule = ((fBuffer & 0x7f000000)>>24); // GEO address!!!
446         fModType = ((fBuffer & 0x7ff00)>>8); 
447         fADCNChannels = (fBuffer & 0xff);          // # of channels following the header
448         //
449         printf("  ******** GEO %d, mod. type %d, #ch. %d\n",fADCModule,fModType,fADCNChannels);
450       }
451       else if((fBuffer&0x80000000)>>31 == 0){ // --- Mapping channel
452         fADCChannel = ((fBuffer & 0x3fff0000)>>16);
453         fCabledSignal = (fBuffer&0xffff);
454         //
455         if(fModType == kV965){ // ******** ADCs ********************************
456           // Channel signal
457           if((fBuffer&0x40000000)>>30==0 && fADCModule<=kLastADCGeo){ // *ZDC* high range chain ADC
458             fIsChMapping = kTRUE;
459             fMapADC[fCurrentCh][0] = fADCModule;
460             fMapADC[fCurrentCh][1] = fADCChannel;
461             fMapADC[fCurrentCh][2] = fCabledSignal;
462             //  - No. of channels on
463             fNChannelsOn++;
464             //
465             // Determining detector and sector
466             // -----------------------------------------
467             //  For the decoding of the following lines 
468             //  look the enum in AliZDCRawStream.h file
469             // -----------------------------------------
470             if((fCabledSignal>=2 && fCabledSignal<=6) || (fCabledSignal>=26 && fCabledSignal<=30)
471               || fCabledSignal==24 || fCabledSignal==48){
472               fMapADC[fCurrentCh][3] = 4; //ZNA
473               //
474               if(fCabledSignal==kZNAC || fCabledSignal==kZNACoot)   fMapADC[fCurrentCh][4]=0;
475               else if(fCabledSignal==3 || fCabledSignal==27)  fMapADC[fCurrentCh][4]=1;
476               else if(fCabledSignal==4 || fCabledSignal==28)  fMapADC[fCurrentCh][4]=2;
477               else if(fCabledSignal==5 || fCabledSignal==29)  fMapADC[fCurrentCh][4]=3;
478               else if(fCabledSignal==6 || fCabledSignal==30)  fMapADC[fCurrentCh][4]=4;
479               else if(fCabledSignal==24 || fCabledSignal==48) fMapADC[fCurrentCh][4]=5; //Reference PTM
480             }
481             else if((fCabledSignal>=7 && fCabledSignal<=11) || (fCabledSignal>=31 && fCabledSignal<=35)){
482               fMapADC[fCurrentCh][3] = 5; //ZPA
483               //
484               if(fCabledSignal==7 || fCabledSignal==31)       fMapADC[fCurrentCh][4]=0;
485               else if(fCabledSignal==8 || fCabledSignal==32)  fMapADC[fCurrentCh][4]=1;
486               else if(fCabledSignal==9 || fCabledSignal==33)  fMapADC[fCurrentCh][4]=2;
487               else if(fCabledSignal==10 || fCabledSignal==34) fMapADC[fCurrentCh][4]=3;
488               else if(fCabledSignal==11 || fCabledSignal==35) fMapADC[fCurrentCh][4]=4;
489             }
490             else if((fCabledSignal>=12 && fCabledSignal<=16) || (fCabledSignal>=36 && fCabledSignal<=40)
491                || fCabledSignal==25 || fCabledSignal==49){
492               fMapADC[fCurrentCh][3] = 1; //ZNC
493               //
494               if(fCabledSignal==12 || fCabledSignal==36)      fMapADC[fCurrentCh][4]=0;
495               else if(fCabledSignal==13 || fCabledSignal==37) fMapADC[fCurrentCh][4]=1;
496               else if(fCabledSignal==14 || fCabledSignal==38) fMapADC[fCurrentCh][4]=2;
497               else if(fCabledSignal==15 || fCabledSignal==39) fMapADC[fCurrentCh][4]=3;
498               else if(fCabledSignal==16 || fCabledSignal==40) fMapADC[fCurrentCh][4]=4;
499               else if(fCabledSignal==25 || fCabledSignal==49) fMapADC[fCurrentCh][4]=5; //Reference PTM
500             }
501             else if((fCabledSignal>=17 && fCabledSignal<=21) || (fCabledSignal>=41 && fCabledSignal<=45)){
502               fMapADC[fCurrentCh][3] = 2; //ZPC
503               //
504               if(fCabledSignal==17 || fCabledSignal==41)      fMapADC[fCurrentCh][4]=0;
505               else if(fCabledSignal==18 || fCabledSignal==42) fMapADC[fCurrentCh][4]=1;
506               else if(fCabledSignal==19 || fCabledSignal==43) fMapADC[fCurrentCh][4]=2;
507               else if(fCabledSignal==20 || fCabledSignal==44) fMapADC[fCurrentCh][4]=3;
508               else if(fCabledSignal==21 || fCabledSignal==45) fMapADC[fCurrentCh][4]=4;
509             }
510             else if(fCabledSignal==22 || fCabledSignal==23 || fCabledSignal==46 || fCabledSignal==47){
511               fMapADC[fCurrentCh][3] = 3; // ZEM
512               //
513               if(fCabledSignal==22 || fCabledSignal==46)      fMapADC[fCurrentCh][4]=1;
514               else if(fCabledSignal==23 || fCabledSignal==47) fMapADC[fCurrentCh][4]=2;
515             }
516             //Ch. debug
517             //printf("\tADC mod. %d ch. %d signal %d ",fADCModule,fADCChannel,fCabledSignal);
518             //printf("  det. %d sec. %d\n",fMapADC[fCurrentCh][3],fMapADC[fCurrentCh][4]);
519             //
520             fCurrentCh++;
521             //
522           } // high range channels
523         }// ModType=1 (ADC mapping)
524         else if(fModType == kV830){  // ******** VME scaler **************************
525           fIsChMapping = kTRUE;
526           fScalerMap[fCurrScCh][0] = fADCModule;
527           fScalerMap[fCurrScCh][1] = fADCChannel;
528           fScalerMap[fCurrScCh][2] = fCabledSignal;
529           //
530           // Determining detector and sector
531           // -----------------------------------------
532           //  For the decoding of the following lines 
533           //  look the enum in AliZDCRawStream.h file
534           // -----------------------------------------
535           // NOT CONSIDERING OUT OF TIME OR REFERENCE SIGNALS FOR SCALER!!!!!
536           if((fCabledSignal>=2 && fCabledSignal<=6) ||
537              (fCabledSignal>=61 && fCabledSignal<=65)){
538             fScalerMap[fCurrScCh][3] = 4; //ZNA
539             //
540             if(fCabledSignal==2 || fCabledSignal==61)      fScalerMap[fCurrScCh][4]=0;
541             else if(fCabledSignal==3 || fCabledSignal==62)  fScalerMap[fCurrScCh][4]=1;
542             else if(fCabledSignal==4 || fCabledSignal==63)  fScalerMap[fCurrScCh][4]=2;
543             else if(fCabledSignal==5 || fCabledSignal==64)  fScalerMap[fCurrScCh][4]=3;
544             else if(fCabledSignal==6 || fCabledSignal==65)  fScalerMap[fCurrScCh][4]=4;
545           }
546           else if((fCabledSignal>=7 && fCabledSignal<=11) ||
547              (fCabledSignal>=66 && fCabledSignal<=70)){
548             fScalerMap[fCurrScCh][3] = 5; //ZPA
549             //
550             if(fCabledSignal==7 || fCabledSignal==66)      fScalerMap[fCurrScCh][4]=0;
551             else if(fCabledSignal==8 || fCabledSignal==67)  fScalerMap[fCurrScCh][4]=1;
552             else if(fCabledSignal==9 || fCabledSignal==68)  fScalerMap[fCurrScCh][4]=2;
553             else if(fCabledSignal==10 || fCabledSignal==69) fScalerMap[fCurrScCh][4]=3;
554             else if(fCabledSignal==11 || fCabledSignal==70) fScalerMap[fCurrScCh][4]=4;
555           }
556           else if((fCabledSignal>=12 && fCabledSignal<=16) ||
557              (fCabledSignal>=71 && fCabledSignal<=75)){
558             fScalerMap[fCurrScCh][3] = 1; //ZNC
559             //
560             if(fCabledSignal==12 || fCabledSignal==71)      fScalerMap[fCurrScCh][4]=0;
561             else if(fCabledSignal==13 || fCabledSignal==72) fScalerMap[fCurrScCh][4]=1;
562             else if(fCabledSignal==14 || fCabledSignal==73) fScalerMap[fCurrScCh][4]=2;
563             else if(fCabledSignal==15 || fCabledSignal==74) fScalerMap[fCurrScCh][4]=3;
564             else if(fCabledSignal==16 || fCabledSignal==75) fScalerMap[fCurrScCh][4]=4;
565           }
566           else if((fCabledSignal>=17 && fCabledSignal<=21) ||
567              (fCabledSignal>=76 && fCabledSignal<=80)){
568             fScalerMap[fCurrScCh][3] = 2; //ZPC
569             //
570             if(fCabledSignal==17 || fCabledSignal==76)      fScalerMap[fCurrScCh][4]=0;
571             else if(fCabledSignal==18 || fCabledSignal==77) fScalerMap[fCurrScCh][4]=1;
572             else if(fCabledSignal==19 || fCabledSignal==78) fScalerMap[fCurrScCh][4]=2;
573             else if(fCabledSignal==20 || fCabledSignal==79) fScalerMap[fCurrScCh][4]=3;
574             else if(fCabledSignal==21 || fCabledSignal==80) fScalerMap[fCurrScCh][4]=4;
575           }
576           else if(fCabledSignal==22 || fCabledSignal==23 ||
577                   fCabledSignal==81 || fCabledSignal==82){
578             fScalerMap[fCurrScCh][3] = 3; // ZEM
579             //
580             if(fCabledSignal==22 || fCabledSignal==81)     fScalerMap[fCurrScCh][4]=1;
581             else if(fCabledSignal==23 || fCabledSignal==82) fScalerMap[fCurrScCh][4]=2;
582           }
583           // Ch debug.
584           //printf("\t VME scaler mod. %d ch. %d, signal %d",fScalerMap[fCurrScCh][0],fADCChannel,fCabledSignal);
585           //if(fCabledSignal!=0 && fCabledSignal!=1) printf("  det. %d sec. %d\n",fScalerMap[fCurrScCh][3],fScalerMap[fCurrScCh][4]);
586           //else printf("  Signal void/not connected\n");
587           
588           fCurrScCh++;
589         }
590         /*else if(fModType == kTRG){ // **** scalers from trigger card
591           //printf("\t Trigger scaler mod. %d ch. %d, signal %d\n",fADCModule,fADCChannel,fCabledSignal);         
592         }
593         else if(fModType == kTRGI){ // **** trigger history from trigger card
594           //printf("\t Trigger card ch. %d, signal %d\n",fADCChannel,fCabledSignal);
595         }
596         else if(fModType == kPU){ // **** pattern unit
597           //printf("\t P.U. mod. %d ch. %d, signal %d\n",fADCModule,fADCChannel,fCabledSignal);
598         }*/
599       }//reading channel mapping
600     }
601    } // if fSODREading
602    fPosition++;
603    return kTRUE;
604   } // ------------------------------- SOD event
605   
606   // -------------------------------------------
607   // --- DARC data
608   // -------------------------------------------
609   if(fPosition<fDeadfaceOffset && fReadOutCard==0){
610     fPosition++;
611     return kTRUE;
612   }
613   else if(fPosition==fDeadfaceOffset && fReadOutCard==0){
614     if(fBuffer != 0xdeadface){
615       //AliWarning(" NO deadface after DARC data");
616       fRawReader->AddMajorErrorLog(kDARCError); 
617     }
618     else{
619       fPosition++;
620       return kTRUE;
621     }
622   }
623   else if(fPosition>fDeadfaceOffset && fPosition<fDeadbeefOffset && fReadOutCard==0){
624     fPosition++;
625     return kTRUE;
626   }
627   else if(fPosition==fDeadbeefOffset && fReadOutCard==0){
628     if(fBuffer != 0xdeadbeef){
629       //AliWarning(" NO deadbeef after DARC global data");
630       fRawReader->AddMajorErrorLog(kDARCError);  
631       fPosition++;
632       return kFALSE;
633     }
634     else{
635       fPosition++;
636       return kTRUE;
637     }
638   } // ------------------------------- End of DARC data
639   
640   // ---------------------------------------------
641   // --- ZDC data
642   // --- ADCs + VME scaler + trigger card + P.U.
643   // ---------------------------------------------
644   else if(fPosition>=fDataOffset){
645     
646     //printf("AliZDCRawStream: fSODReading = %d\n", fSODReading);
647     if(!fSODReading && !fIsMapRead) ReadChMap();
648     
649     //  !!!!!!!!!!!!!!! DARC readout card only !!!!!!!!!!!
650     // Not valid datum before the event 
651     // there MUST be a NOT valid datum before the event!!!
652     if(fReadOutCard==0){
653       if(fPosition==fDataOffset){ 
654         //printf("\t **** ZDC data begin ****\n");
655         if((fBuffer & 0x07000000) != 0x06000000){
656           fRawReader->AddMajorErrorLog(kZDCDataError);
657         }
658         else if((fBuffer & 0x07000000) == 0x06000001){ // Corrupted event!!!
659           fIsADCEventGood = kFALSE;
660         }
661       }
662     
663       // If the not valid datum isn't followed by the 1st ADC header
664       // the event is corrupted (i.e., 2 gates arrived before trigger)
665       else if(fPosition==fDataOffset+1){ 
666         if((fBuffer & 0x07000000) != 0x02000000){
667           AliWarning("ZDC ADC -> The not valid datum is NOT followed by an ADC header!");
668           fRawReader->AddMajorErrorLog(kZDCDataError);
669           fIsADCEventGood = kFALSE;
670           fPosition++;
671           return kFALSE;
672         }
673       }
674     }
675      
676     // Get geo address of current word
677     fADCModule = (Int_t) ((fBuffer & 0xf8000000)>>27);
678     if(fIsTDCHeaderRead)fADCModule = kZDCTDCGeo;
679     
680     // ************************************ ADC MODULES ************************************
681     if(fADCModule>=kFirstADCGeo && fADCModule<=kLastADCGeo){
682       // *** ADC header
683       if((fBuffer & 0x07000000) == 0x02000000){
684         fIsADCHeader = kTRUE;
685         fADCNChannels = ((fBuffer & 0x00003f00)>>8);
686         //printf("  AliZDCRawStream -> ADC HEADER: mod.%d has %d ch. \n",fADCModule,fADCNChannels);
687       }
688       // *** ADC data word
689       else if((fBuffer & 0x07000000) == 0x00000000){
690         fIsADCDataWord = kTRUE;
691         fADCChannel = ((fBuffer & 0x1e0000) >> 17);
692         fADCGain = ((fBuffer & 0x10000) >> 16);       
693         fADCValue = (fBuffer & 0xfff);  
694         //
695         //printf("  AliZDCRawStream -> ADC DATUM: mod. %d ch. %d gain %d value %d\n",
696         //  fADCModule,fADCChannel,fADCGain,fADCValue);
697         
698         // Checking if the channel map for the ADCs has been provided/read
699         if(fMapADC[0][0]==-1){
700           printf("\t ATTENTION!!! No ADC mapping has been found/provided!!!\n");
701           return kFALSE;
702         }
703         //
704         /*for(Int_t ci=0; ci<kNch; ci++){
705           printf("  %d mod %d ch %d det %d sec %d\n",ci,fMapADC[ci][0],
706           fMapADC[ci][1], fMapADC[ci][3], fMapADC[ci][4]);
707         }*/
708                 
709         // Scan of the map to assign the correct volumes
710         Int_t foundMapEntry = kFALSE;
711         for(Int_t k=0; k<kNch; k++){
712            if(fADCModule==fMapADC[k][0] && fADCChannel==fMapADC[k][1]){
713              fSector[0] = fMapADC[k][3];
714              fSector[1] = fMapADC[k][4];
715              foundMapEntry = kTRUE;
716              break;
717            } 
718         }
719         if(foundMapEntry==kFALSE){
720           AliWarning(Form(" No valid entry in ADC mapping for raw data %d ADCmod. %d ch. %d\n",
721               fPosition,fADCModule,fADCChannel));
722         }
723         //
724         //printf("AliZDCRawStream -> ADCmod. %d ch. %d -> det %d sec %d\n",
725         //  fADCModule,fADCChannel,fSector[0],fSector[1]);
726         
727         // Final checks
728         if(foundMapEntry==kTRUE){
729           if(fSector[0]<1 || fSector[0]>5){
730             AliWarning(Form(" No valid detector assignment: %d",fSector[0]));
731             fRawReader->AddMajorErrorLog(kInvalidSector);
732           }
733           //
734           if(fSector[1]<0 || fSector[1]>5){
735             AliWarning(Form(" No valid sector assignment: %d",fSector[1]));
736             fRawReader->AddMajorErrorLog(kInvalidSector);
737           }
738           //
739           if(fADCModule<0 || fADCModule>3){
740             AliError(Form(" No valid ADC module: %d",fADCModule));
741             fRawReader->AddMajorErrorLog(kInvalidADCModule);
742           }
743         }
744
745         // Checking the underflow and overflow bits
746         if(fBuffer & 0x1000)       fIsUnderflow = kTRUE;
747         else if (fBuffer & 0x2000) fIsOverflow = kTRUE; 
748                 
749       }//ADC data word
750       // *** ADC EOB
751       else if((fBuffer & 0x07000000) == 0x04000000){
752         fIsADCEOB = kTRUE;
753         //printf("  AliZDCRawStream -> EOB --------------------------\n");
754       }
755     }//ADC module
756     // ********************************* ADD ADC *********************************
757     else if(fADCModule == kADDADCGeo){
758       // *** ADC header
759       if((fBuffer & 0x07000000) == 0x02000000){
760         fIsADCHeader = kTRUE;
761         fADCNChannels = ((fBuffer & 0x00003f00)>>8);
762         //printf("  AliZDCRawStream -> ADD ADC HEADER: mod.%d has %d ch. \n",fADCModule,fADCNChannels);
763       }
764       // *** ADC data word
765       else if((fBuffer & 0x07000000) == 0x00000000){
766         fIsADDChannel = kTRUE;
767         fADCChannel = ((fBuffer & 0x1e0000) >> 17);
768         fADCGain = ((fBuffer & 0x10000) >> 16);       
769         fADCValue = (fBuffer & 0xfff);  
770         //
771         //printf("  ADD ADC DATUM -> mod. %d ch. %d gain %d value %d\n",
772         //  fADCModule,fADCChannel,fADCGain,fADCValue);
773       }
774       // *** ADC EOB
775       else if((fBuffer & 0x07000000) == 0x04000000){
776         fIsADCEOB = kTRUE;
777         //printf("  AliZDCRawStream -> EOB --------------------------\n");
778       }
779     }
780     // ********************************* ZDC TDC *********************************
781     else if(fADCModule==kZDCTDCGeo && fIsTDCHeaderRead==kFALSE){
782       fIsZDCTDCHeader = kTRUE;
783       fIsTDCHeaderRead = kTRUE;
784       fTDCStartCounter = fPosition;
785       //Ch. debug
786       //printf("  AliZDCRawStream -> ZDC TDC: mod.%d\n",fADCModule);
787     }
788     // ********************************* ADD TDC *********************************
789     else if(fADCModule == kADDTDCGeo){
790       // *** TDC header
791       if((fBuffer & 0x07000000) == 0x02000000){
792         fIsADDTDCHeader = kTRUE;
793         fADCNChannels = ((fBuffer & 0x00003f00)>>8);
794         //printf("  AliZDCRawStream -> ADD TDC: mod.%d has %d ch. \n",fADCModule,fADCNChannels);
795       }
796       // *** TDC  word
797       else if((fBuffer & 0x07000000) == 0x00000000){
798         fIsADDTDCdatum = kTRUE;
799       }
800       // *** TDC  EOB
801       if((fBuffer & 0x07000000) == 0x04000000){
802         fIsADCEOB = kTRUE;
803         //printf("  AliZDCRawStream -> ADD TDC EOB\n");
804       }
805    }
806     // ********************************* VME SCALER HEADER *********************************
807     else if(fADCModule == kScalerGeo){
808       if(fBuffer & 0x04000000 && fIsScHeaderRead==kFALSE){ // *** Scaler header
809         fScGeo = (fBuffer & 0xf8000000)>>27;       
810         fScNWords = (fBuffer & 0x00fc0000)>>18;    
811         fScTriggerSource = (fBuffer & 0x00030000)>>16;     
812         fScTriggerNumber = (fBuffer & 0x0000ffff);
813         fIsScHeaderRead = kTRUE; 
814         fScStartCounter = fPosition;
815         //Ch. debug
816         //printf("  AliZDCRawStream -> VME SCALER HEADER: geo %d Nwords %d TrigSource %d TrigNo. %d\n",
817         //   fScGeo,fScNWords,fScTriggerSource,fScTriggerNumber);
818       } 
819       else if(!(fBuffer & 0x04000000)){
820         fIsScEventGood = kFALSE;
821       }
822     }
823     // *********************************** PATTERN UNIT ***********************************
824     else if(fADCModule == kPUGeo){
825       // still to be implemented!!! Not yet in data!!!
826       fDetPattern = (fBuffer & 0x0000003f);
827       // Ch. debug
828       //printf("  AliZDCRawStream -> Pattern Unit\n");
829       
830     }
831     // ******************************** TRIGGER CARD COUNTS ********************************
832     else if(fADCModule == kTrigScales){
833       if(fIsTriggerScaler == kFALSE){
834         fTrigCountNWords = (Int_t) ((fBuffer & 0xfc0000)>>17);
835         fTrigCountStart = fPosition;
836         fIsTriggerScaler = kTRUE;
837       }
838       // Ch. debug
839       //printf("  AliZDCRawStream -> Trigger Scaler header\n");      
840     }
841     // ********************************** TRIGGER HISTORY **********************************
842     else if(fADCModule == kTrigHistory){
843       if(fIsTriggerHistory == kFALSE){
844         fTrigHistNWords = (Int_t) ((fBuffer & 0xfc0000)>>17);
845         fTrigHistStart = fPosition;
846         fIsTriggerHistory = kTRUE;
847       }
848       // Ch. debug
849       //printf("  AliZDCRawStream -> Trigger History header\n");
850       
851     } 
852     // ********************************** VME SCALER DATA **********************************
853     //  Reading VME scaler data 
854     if(fIsScHeaderRead && fPosition>=fScStartCounter+1){ // *** Scaler word
855       fADCModule=kScalerGeo; fIsADCDataWord=kFALSE; fIsScalerWord=kTRUE;
856       fScEvCounter = fBuffer;
857       Int_t nWords = (Int_t) (fScNWords);
858       if(fPosition == fScStartCounter+nWords) fIsScHeaderRead = kFALSE;
859       //Ch. debug
860       //printf("  AliZDCRawStream -> scaler datum %d", fScEvCounter);
861     }
862     // ********************************** ZDC TDC DATA **********************************
863     //  ZDC TDC data
864     if(fIsTDCHeaderRead && fPosition>=fTDCStartCounter+1){ 
865       fIsADCDataWord=kFALSE; fIsScalerWord=kFALSE;
866       //printf("  AliZDCRawStream -> ...skipping ZDC TDC datum\n");
867       // For the moment we skip the TDC data
868       if(((fBuffer & 0xf0000000) == 0x80000000) && ((fBuffer & 0x08000000) >> 27) == 0){
869         fIsTDCHeaderRead = kFALSE;
870         // Ch. debug
871         //printf("  AliZDCRawStream -> ZDC TDC trailer\n");
872       }
873     }
874     // ******************************** TRIGGER SCALER DATA ********************************
875     //  Reading trigger scaler data 
876     if(fIsTriggerScaler && fPosition>=fTrigCountStart+1){
877       fADCModule = kTrigScales; fIsADCDataWord = kFALSE;        
878       if(fPosition == fTrigCountStart+1)      fMBTrigInput = fBuffer;               
879       else if(fPosition == fTrigCountStart+2) fCentralTrigInput = fBuffer;                  
880       else if(fPosition == fTrigCountStart+3) fSCentralTrigInput = fBuffer;
881       else if(fPosition == fTrigCountStart+4) fEMDTrigInput = fBuffer; 
882       else if(fPosition == fTrigCountStart+5) fL0Received = fBuffer;
883       else if(fPosition == fTrigCountStart+6) fMBtrig2CTP = fBuffer;     
884       else if(fPosition == fTrigCountStart+7) fCentralTrig2CTP = fBuffer;  
885       else if(fPosition == fTrigCountStart+8) fSCentralTrig2CTP = fBuffer; 
886       else if(fPosition == fTrigCountStart+9){
887         fEMDTrig2CTP = fBuffer;       
888         fIsTriggerScaler = kFALSE;
889       }
890       // Ch. debug
891       //printf("  AliZDCRawStream -> Trigger Scaler datum %d\n", fPosition-fTrigCountStart);
892     }
893     // ******************************* TRIGGER HISTORY WORDS ******************************
894     //  Reading trigger history
895     if(fIsTriggerHistory && fPosition>=fTrigHistStart+1){
896         fADCModule = kTrigHistory; fIsADCDataWord = kFALSE;     
897         if(fPosition == fTrigHistStart+1){
898           fPileUpBit1stWord = (fBuffer & 0x80000000) >> 31;
899           fL0Bit1stWord = (fBuffer & 0x40000000) >> 30;        
900           fCentralTrigHist = (fBuffer & 0x3fff8000) >> 14; 
901           fMBTrigHist =  (fBuffer & 0x00007fff);        
902           //
903           fCPTInput[0] = (fBuffer & 0x00000080) >> 6;  // MB bit
904           fCPTInput[1] = (fBuffer & 0x00400000) >> 21; // CENTRAL bit
905         }
906         
907         else if(fPosition == fTrigHistStart+fTrigHistNWords){
908           fPileUpBit2ndWord = (fBuffer & 0x80000000) >> 31;
909           fL0Bit2ndWord = (fBuffer & 0x40000000) >> 30;      
910           fSCentralTrigHist = (fBuffer & 0x3fff8000) >> 14; 
911           fEMDTrigHist =  (fBuffer & 0x00007fff);        
912           //
913           fCPTInput[2] = (fBuffer & 0x00000080) >> 6;  // SEMICENTRAL bit
914           fCPTInput[3] = (fBuffer & 0x00400000) >> 21; // EMD bit
915           //
916           fIsTriggerHistory = kFALSE;
917           
918           // Checking if the event is good
919           // (1) both history word pile up bits must be = 0
920           if(fPileUpBit1stWord==0 && fPileUpBit2ndWord==0) fIsPileUpEvent = kFALSE;
921           else{
922             fIsPileUpEvent = kTRUE;
923             printf("  AliZDCRawStream -> PILE UP EVENT: bitPileUp0 %d bitPileUp1 %d\n",
924                 fPileUpBit1stWord, fPileUpBit2ndWord);
925           }
926           // (2) both history word L0 bits must be = 1
927           if(fL0Bit1stWord==1 && fL0Bit2ndWord==1) fIsL0BitSet = kTRUE;
928           else{
929             fIsL0BitSet = kFALSE;
930             printf("  AliZDCRawStream -> L0 wrongly set: bitL0word0 %d bitL0word1 %d\n",
931                 fL0Bit1stWord, fL0Bit2ndWord);
932           }
933         }       
934         // Ch. debug
935         //printf("  AliZDCRawStream -> Trigger history word %d\n", fPosition-fTrigHistStart);
936     }
937     
938   }
939
940   fPosition++;
941
942   return kTRUE;
943 }
944
945 //_____________________________________________________________________________
946 AliCDBStorage* AliZDCRawStream::SetStorage(const char *uri) 
947 {
948   // Setting the storage
949   
950   AliCDBStorage *storage = AliCDBManager::Instance()->GetStorage(uri); 
951
952   return storage; 
953 }
954
955
956 //_____________________________________________________________________________
957 AliZDCChMap* AliZDCRawStream::GetChMap() const
958 {
959
960   // Getting calibration object for ZDC
961
962   AliCDBEntry  *entry = AliCDBManager::Instance()->Get("ZDC/Calib/ChMap");
963   if(!entry) AliFatal("No calibration data loaded!");  
964
965   AliZDCChMap *calibdata = dynamic_cast<AliZDCChMap*> (entry->GetObject());
966   if(!calibdata) AliFatal("Wrong calibration object in calibration  file!");
967
968   return calibdata;
969 }