]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/AliZDC.cxx
Coding convention correction
[u/mrichter/AliRoot.git] / ZDC / AliZDC.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 //                      Zero Degree Calorimeter                              //
21 //           This class contains the basic functions for the ZDCs;           //
22 //            functions specific to one particular geometry are              //
23 //                      contained in the derived classes                     //
24 //                                                                           //
25 ///////////////////////////////////////////////////////////////////////////////
26
27 // --- ROOT system
28 #include <TBRIK.h>
29 #include <TClonesArray.h>
30 #include <TGeometry.h>
31 #include <TNode.h>
32 #include <TTree.h>
33 #include <TFile.h>
34 #include <TSystem.h>
35 #include <TRandom.h>
36
37 // --- AliRoot header files
38 #include "AliDetector.h"
39 #include "AliRawDataHeader.h"
40 #include "AliRawReader.h"
41 #include "AliLoader.h"
42 #include "AliRun.h"
43 #include "AliMC.h"
44 #include "AliLog.h"
45 #include "AliDAQ.h"
46 #include "AliZDC.h"
47 #include "AliZDCHit.h"
48 #include "AliZDCSDigit.h"
49 #include "AliZDCDigit.h"
50 #include "AliZDCDigitizer.h"
51 #include "AliZDCRawStream.h"
52 #include "AliZDCCalibData.h"
53 #include "AliFstream.h"
54
55  
56 ClassImp(AliZDC)
57
58 //_____________________________________________________________________________
59 AliZDC::AliZDC() :
60   AliDetector(),
61   fNoShower  (0),
62   fCalibData (0)
63
64 {
65   //
66   // Default constructor for the Zero Degree Calorimeter base class
67   //
68   
69   fIshunt = 1;
70   fNhits  = 0;
71   fHits = 0;
72   fDigits = 0;
73   fNdigits = 0;
74   
75 }
76  
77 //_____________________________________________________________________________
78 AliZDC::AliZDC(const char *name, const char *title) : 
79   AliDetector(name,title),
80   fNoShower  (0),
81   fCalibData (0)
82                  
83 {
84   //
85   // Standard constructor for the Zero Degree Calorimeter base class
86   //
87     
88   fIshunt = 1;
89   fNhits  = 0;
90   fDigits = 0;
91   fNdigits = 0;
92  
93   fHits = new TClonesArray("AliZDCHit",1000);
94   gAlice->GetMCApp()->AddHitList(fHits);
95   
96   char sensname[5],senstitle[25];
97   sprintf(sensname,"ZDC");
98   sprintf(senstitle,"ZDC dummy");
99   SetName(sensname); SetTitle(senstitle);
100
101 }
102
103 //____________________________________________________________________________ 
104 AliZDC::~AliZDC()
105 {
106   //
107   // ZDC destructor
108   //
109
110   fIshunt = 0;
111   delete fCalibData;
112
113 }
114
115 //_____________________________________________________________________________
116 AliZDC::AliZDC(const AliZDC& ZDC) :
117   AliDetector("ZDC","ZDC")
118 {
119   // copy constructor
120     fNoShower = ZDC.fNoShower;
121     fCalibData = ZDC.fCalibData;
122     fZDCCalibFName = ZDC.fZDCCalibFName;
123 }
124
125 //_____________________________________________________________________________
126 AliZDC& AliZDC::operator=(const AliZDC& ZDC)
127 {
128   // assignement operator
129   if(this!=&ZDC){
130     fNoShower = ZDC.fNoShower;
131     fCalibData = ZDC.fCalibData;
132     fZDCCalibFName = ZDC.fZDCCalibFName;
133   } return *this;
134 }
135
136 //_____________________________________________________________________________
137 void AliZDC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
138 {
139   //
140   //            Add a ZDC hit to the hit list.
141   // -> We make use of 2 array of hits:
142   // [1]  fHits (the usual one) that contains hits for each PRIMARY
143   // [2]  fStHits that contains hits for each EVENT and is used to
144   //      obtain digits at the end of each event
145   //
146   
147   static Float_t primKinEn, xImpact, yImpact, sFlag;
148
149   AliZDCHit *newquad, *curprimquad;
150   newquad = new AliZDCHit(fIshunt, track, vol, hits);
151   TClonesArray &lhits = *fHits;
152   
153   if(fNhits==0){
154       // First hit -> setting flag for primary or secondary particle
155       Int_t primary = gAlice->GetMCApp()->GetPrimary(track);     
156       if(track != primary){
157         newquad->SetSFlag(1);  // SECONDARY particle entering the ZDC
158       }
159       else if(track == primary){
160         newquad->SetSFlag(0);  // PRIMARY particle entering the ZDC
161       }  
162       sFlag     = newquad->GetSFlag();
163       primKinEn = newquad->GetPrimKinEn();
164       xImpact   = newquad->GetXImpact();
165       yImpact   = newquad->GetYImpact();
166    }
167    else{       
168       newquad->SetPrimKinEn(primKinEn);
169       newquad->SetXImpact(xImpact);
170       newquad->SetYImpact(yImpact);
171       newquad->SetSFlag(sFlag);
172    }
173  
174   Int_t j;
175   for(j=0; j<fNhits; j++){
176     // If hits are equal (same track, same volume), sum them.
177      curprimquad = (AliZDCHit*) lhits[j];
178      if(*curprimquad == *newquad){
179         *curprimquad = *curprimquad+*newquad;
180         // CH. debug
181         /*if(newquad->GetEnergy() != 0. || newquad->GetLightPMC() != 0. || 
182            newquad->GetLightPMQ() != 0.){
183           printf("\n\t --- Equal hits found\n");
184           curprimquad->Print("");
185           newquad->Print("");
186           printf("\t --- Det. %d, Quad. %d: X = %f, E = %f, LightPMC = %f, LightPMQ = %f\n",
187           curprimquad->GetVolume(0),curprimquad->GetVolume(1),curprimquad->GetXImpact(),
188           curprimquad->GetEnergy(), curprimquad->GetLightPMC(), curprimquad->GetLightPMQ());
189         }*/
190         //
191         delete newquad;
192         return;
193      } 
194   }
195
196     //Otherwise create a new hit
197     new(lhits[fNhits]) AliZDCHit(*newquad);
198     fNhits++;
199     // CH. debug
200     /*printf("\n\t New ZDC hit added! fNhits = %d\n", fNhits);
201     printf("\t Det. %d, Quad.t %d: X = %f, E = %f, LightPMC = %f, LightPMQ = %f\n",
202     newquad->GetVolume(0),newquad->GetVolume(1),newquad->GetXImpact(),
203     newquad->GetEnergy(), newquad->GetLightPMC(), newquad->GetLightPMQ());
204     */
205     delete newquad;
206 }
207
208 //_____________________________________________________________________________
209 void AliZDC::BuildGeometry()
210 {
211   //
212   // Build the ROOT TNode geometry for event display 
213   // in the Zero Degree Calorimeter
214   // This routine is dummy for the moment
215   //
216
217   TNode *node, *top;
218   TBRIK *brik;
219   const int kColorZDC  = kBlue;
220   
221   //
222   top=gAlice->GetGeometry()->GetNode("alice");
223   
224   // ZDC
225     brik = new TBRIK("S_ZDC","ZDC box","void",300,300,5);
226     top->cd();
227     node = new TNode("ZDC","ZDC","S_ZDC",0,0,600,"");
228     node->SetLineColor(kColorZDC);
229     fNodes->Add(node);
230 }
231
232 //____________________________________________________________________________
233 Float_t AliZDC::ZMin(void) const
234 {
235   // Minimum dimension of the ZDC module in z
236   return -11600.;
237 }
238
239 //____________________________________________________________________________
240 Float_t AliZDC::ZMax(void) const
241 {
242   // Maximum dimension of the ZDC module in z
243   return -11750.;
244 }
245   
246
247 //_____________________________________________________________________________
248 void AliZDC::MakeBranch(Option_t *opt)
249 {
250   //
251   // Create Tree branches for the ZDC
252   //
253
254   char branchname[10];
255   sprintf(branchname,"%s",GetName());
256
257   const char *cH = strstr(opt,"H");
258   
259   if(cH && fLoader->TreeH())
260    fHits   = new TClonesArray("AliZDCHit",1000); 
261   
262   AliDetector::MakeBranch(opt);
263 }
264
265 //_____________________________________________________________________________
266 void AliZDC::Hits2SDigits()
267 {
268   // Create summable digits from hits
269   
270   AliDebug(1,"\n        Entering AliZDC::Hits2Digits() ");
271   
272   fLoader->LoadHits("read");
273   fLoader->LoadSDigits("recreate");
274   AliRunLoader* runLoader = fLoader->GetRunLoader();
275   AliZDCSDigit sdigit;
276   AliZDCSDigit* psdigit = &sdigit;
277
278   // Event loop
279   for(Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
280     Float_t pmCZN = 0, pmCZP = 0, pmQZN[4], pmQZP[4], pmZEM1 = 0, pmZEM2 = 0;
281     for(Int_t i = 0; i < 4; i++) pmQZN[i] = pmQZP[i] = 0;
282
283     runLoader->GetEvent(iEvent);
284     TTree* treeH = fLoader->TreeH();
285     Int_t ntracks = (Int_t) treeH->GetEntries();
286     ResetHits();
287
288     // Tracks loop
289     Int_t sector[2];
290     for(Int_t itrack = 0; itrack < ntracks; itrack++) {
291       treeH->GetEntry(itrack);
292       for(AliZDCHit* zdcHit = (AliZDCHit*)FirstHit(-1); zdcHit;
293                       zdcHit = (AliZDCHit*)NextHit()) { 
294                       
295         sector[0] = zdcHit->GetVolume(0);
296         sector[1] = zdcHit->GetVolume(1);
297         if((sector[1] < 1) || (sector[1] > 4)) {
298           Error("Hits2SDigits", "sector[0] = %d, sector[1] = %d", 
299                 sector[0], sector[1]);
300           continue;
301         }
302         Float_t lightQ = zdcHit->GetLightPMQ();
303         Float_t lightC = zdcHit->GetLightPMC();
304      
305         if(sector[0] == 1) {          //ZN 
306           pmCZN += lightC;
307           pmQZN[sector[1]-1] += lightQ;
308         } else if(sector[0] == 2) {   //ZP 
309           pmCZP += lightC;
310           pmQZP[sector[1]-1] += lightQ;
311         } else if(sector[0] == 3) {   //ZEM 
312           if(sector[1] == 1) pmZEM1 += lightC;
313           else                pmZEM2 += lightQ;
314         }
315       }//Hits loop
316     }
317
318     // create the output tree
319     fLoader->MakeTree("S");
320     TTree* treeS = fLoader->TreeS();
321     const Int_t kBufferSize = 4000;
322     treeS->Branch(GetName(), "AliZDCSDigit", &psdigit, kBufferSize);
323
324     // Create sdigits for ZN1
325     sector[0] = 1; // Detector = ZN1
326     sector[1] = 0; // Common PM ADC
327     new(psdigit) AliZDCSDigit(sector, pmCZN);
328     if(pmCZN > 0) treeS->Fill();
329     for(Int_t j = 0; j < 4; j++) {
330       sector[1] = j+1; // Towers PM ADCs
331       new(psdigit) AliZDCSDigit(sector, pmQZN[j]);
332       if(pmQZN[j] > 0) treeS->Fill();
333     }
334   
335     // Create sdigits for ZP1
336     sector[0] = 2; // Detector = ZP1
337     sector[1] = 0; // Common PM ADC
338     new(psdigit) AliZDCSDigit(sector, pmCZP);
339     if(pmCZP > 0) treeS->Fill();
340     for(Int_t j = 0; j < 4; j++) {
341       sector[1] = j+1; // Towers PM ADCs
342       new(psdigit) AliZDCSDigit(sector, pmQZP[j]);
343       if(pmQZP[j] > 0) treeS->Fill();
344     }
345
346     // Create sdigits for ZEM
347     sector[0] = 3; 
348     sector[1] = 1; // Detector = ZEM1
349     new(psdigit) AliZDCSDigit(sector, pmZEM1);
350     if(pmZEM1 > 0) treeS->Fill();
351     sector[1] = 2; // Detector = ZEM2
352     new(psdigit) AliZDCSDigit(sector, pmZEM2);
353     if(pmZEM2 > 0) treeS->Fill();
354
355     // write the output tree
356     fLoader->WriteSDigits("OVERWRITE");
357   }
358
359   fLoader->UnloadHits();
360   fLoader->UnloadSDigits();
361 }
362
363 //_____________________________________________________________________________
364 AliDigitizer* AliZDC::CreateDigitizer(AliRunDigitizer* manager) const
365 {
366   // Create the digitizer for ZDC
367
368   return new AliZDCDigitizer(manager);
369 }
370
371 //_____________________________________________________________________________
372 void AliZDC::Digits2Raw()
373 {
374   // Convert ZDC digits to raw data
375
376   // Format: 24 int values -> ZN1(C+Q1-4), ZP1(C+Q1-4), ZEM1, ZEM2, ZN(C+Q1-4), ZP2(C+Q1-4), 2 Ref PMs
377   //         + 24 int values for the corresponding out of time channels
378   // For the CAEN module V965 we have an Header, the Data Words and an End Of Block
379   //    12 channels x 2 gain chains read from 1st ADC module
380   //    12 channels x 2 gain chains read from 2nd ADC module
381   //    12 channels x 2 gain chains read from 3rd ADC module (o.o.t.)
382   //    12 channels x 2 gain chains read from 4rth ADC module (o.o.t.)
383   //
384   const int knADCData1=24, knADCData2=24; // In principle the 2 numbers can be different!
385   UInt_t lADCHeader1; 
386   UInt_t lADCHeader2; 
387   UInt_t lADCData1[knADCData1];
388   UInt_t lADCData2[knADCData2];
389   UInt_t lADCData3[knADCData1];
390   UInt_t lADCData4[knADCData2];
391   //
392   UInt_t lADCEndBlock;
393
394   // load the digits
395   fLoader->LoadDigits("read");
396   AliZDCDigit digit;
397   AliZDCDigit* pdigit = &digit;
398   TTree* treeD = fLoader->TreeD();
399   if(!treeD) return;
400   treeD->SetBranchAddress("ZDC", &pdigit);
401   //printf("\t AliZDC::Digits2Raw -> TreeD has %d entries\n",(Int_t) treeD->GetEntries());
402
403   // Fill data array
404   // ADC header
405   UInt_t lADCHeaderGEO = 0;
406   UInt_t lADCHeaderCRATE = 0;
407   UInt_t lADCHeaderCNT1 = knADCData1;
408   UInt_t lADCHeaderCNT2 = knADCData2;
409     
410   lADCHeader1 = lADCHeaderGEO << 27 | 0x1 << 25 | lADCHeaderCRATE << 16 |
411                lADCHeaderCNT1 << 8 ;
412   lADCHeader2 = lADCHeaderGEO << 27 | 0x1 << 25 | lADCHeaderCRATE << 16 |
413                lADCHeaderCNT2 << 8 ;
414       
415   // ADC data word
416   UInt_t lADCDataGEO = lADCHeaderGEO;
417   //
418   UInt_t lADCDataValue1[knADCData1];
419   UInt_t lADCDataValue2[knADCData2];
420   UInt_t lADCDataValue3[knADCData1];
421   UInt_t lADCDataValue4[knADCData2];
422   //
423   UInt_t lADCDataOvFlw1[knADCData1];
424   UInt_t lADCDataOvFlw2[knADCData2];
425   UInt_t lADCDataOvFlw3[knADCData1];
426   UInt_t lADCDataOvFlw4[knADCData2];
427   //
428   for(Int_t i=0; i<knADCData1 ; i++){
429     lADCDataValue1[i] = 0;
430     lADCDataOvFlw1[i] = 0;
431     lADCDataValue3[i] = 0;
432     lADCDataOvFlw3[i] = 0;
433   }
434   for(Int_t i=0; i<knADCData2 ; i++){
435     lADCDataValue2[i] = 0;
436     lADCDataOvFlw2[i] = 0;
437     lADCDataValue4[i] = 0;
438     lADCDataOvFlw4[i] = 0;
439   }
440   //
441   UInt_t lADCDataChannel = 0;
442   
443   // loop over digits
444   for(Int_t iDigit=0; iDigit<treeD->GetEntries(); iDigit++){
445     treeD->GetEntry(iDigit);
446     if(!pdigit) continue;
447     //digit.Print("");
448     
449     // *** ADC data
450     Int_t index=0;
451     if(digit.GetSector(1)!=5){ // ZDC signal channels
452       // *** ADC1 (ZN1, ZP1, ZEM1,2) or ADC3 (ZN1, ZP1, ZEM1,2 o.o.t.)
453       if(digit.GetSector(0)==1 || digit.GetSector(0)==2 || digit.GetSector(0)==3){
454         if(digit.GetSector(0)==1 || digit.GetSector(0)==2){
455           index = (digit.GetSector(0)-1) + 4*digit.GetSector(1); // ZN1 or ZP1
456           lADCDataChannel = 8*(digit.GetSector(0)-1) + digit.GetSector(1);
457         }
458         else if(digit.GetSector(0)==3){ // ZEM 1,2
459           index = 20 + (digit.GetSector(1)-1);
460           lADCDataChannel = 5 + 8*(digit.GetSector(1)-1);
461         }
462         //
463         /*printf("\t AliZDC::Digits2Raw -> idig%d det %d quad %d index %d, ADCch %d ADCVal[%d, %d]\n",
464                 iDigit,digit.GetSector(0),digit.GetSector(1),index,lADCDataChannel,
465                 digit.GetADCValue(0),digit.GetADCValue(1));// Ch. debug
466         */
467         //
468         if(iDigit<knADCData1){ // *** In-time signals
469           lADCDataValue1[index] = digit.GetADCValue(0);   // High gain ADC ch.    
470           if(lADCDataValue1[index] > 2047) lADCDataOvFlw1[index] = 1; 
471           lADCDataValue1[index+2] = digit.GetADCValue(1); // Low gain ADC ch.
472           if(lADCDataValue1[index+2] > 2047) lADCDataOvFlw1[index+2] = 1; 
473         
474           lADCData1[index] = lADCDataGEO << 27 | 0x1 << 24 | lADCDataChannel << 17 | 
475                           lADCDataOvFlw1[index] << 12 | (lADCDataValue1[index] & 0xfff); 
476           lADCData1[index+2] = lADCDataGEO << 27 | 0x1 << 24  | lADCDataChannel << 17 | 0x1 << 16 |
477                           lADCDataOvFlw1[index+2] << 12 | (lADCDataValue1[index+2] & 0xfff);  
478         }
479         else{ // *** Out-of-time signals
480           lADCDataValue3[index] = digit.GetADCValue(0);   // High gain ADC ch.    
481           if(lADCDataValue3[index] > 2047) lADCDataOvFlw3[index] = 1; 
482           lADCDataValue3[index+2] = digit.GetADCValue(1); // Low gain ADC ch.
483           if(lADCDataValue3[index+2] > 2047) lADCDataOvFlw3[index+2] = 1; 
484       
485           lADCData3[index] = lADCDataGEO << 27 | lADCDataChannel << 17 | 
486                           lADCDataOvFlw3[index] << 12 | (lADCDataValue3[index] & 0xfff); 
487           lADCData3[index+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
488                           lADCDataOvFlw3[index+2] << 12 | (lADCDataValue3[index+2] & 0xfff);  
489         }                  
490       }
491       // *** ADC2 (ZN2, ZP2) or ADC4 (ZN2, ZP2 o.o.t.)
492       else if(digit.GetSector(0)==4 || digit.GetSector(0)==5){
493         index = (digit.GetSector(0)-4) + 4*digit.GetSector(1); // ZN2 or ZP2
494         lADCDataChannel = 8*(digit.GetSector(0)-4) + digit.GetSector(1);
495         //
496         /*printf("\t AliZDC::Digits2Raw -> idig%d det %d quad %d index %d, ADCch %d ADCVal[%d, %d]\n",
497                 iDigit,digit.GetSector(0),digit.GetSector(1),index,lADCDataChannel,
498                 digit.GetADCValue(0),digit.GetADCValue(1));// Ch. debug
499         */
500         //
501         if(iDigit<knADCData2){ // *** In-time signals
502           lADCDataValue2[index] = digit.GetADCValue(0);
503           if(lADCDataValue2[index] > 2047) lADCDataOvFlw2[index] = 1; 
504           lADCDataValue2[index+2] = digit.GetADCValue(1);
505           if(lADCDataValue2[index+2] > 2047) lADCDataOvFlw2[index+2] = 1; 
506           //
507           lADCData2[index] =   lADCDataGEO << 27 | lADCDataChannel << 17 | 
508                           lADCDataOvFlw2[index] << 12 | (lADCDataValue2[index] & 0xfff); 
509           lADCData2[index+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
510                           lADCDataOvFlw2[index+2] << 12 | (lADCDataValue2[index+2] & 0xfff);   
511         }                 
512         else{ // *** Out-of-time signals
513           lADCDataValue4[index] = digit.GetADCValue(0);
514           if(lADCDataValue4[index] > 2047) lADCDataOvFlw4[index] = 1; 
515           lADCDataValue4[index+2] = digit.GetADCValue(1);
516           if(lADCDataValue4[index+2] > 2047) lADCDataOvFlw4[index+2] = 1; 
517           //
518           lADCData4[index] =   lADCDataGEO << 27 | lADCDataChannel << 17 | 
519                         lADCDataOvFlw4[index] << 12 | (lADCDataValue4[index] & 0xfff); 
520           lADCData4[index+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
521                         lADCDataOvFlw4[index+2] << 12 | (lADCDataValue4[index+2] & 0xfff);   
522         }                 
523       }
524     }
525     // *** ADC2 (Reference PTMs) or ADC4 (Reference PTMs o.o.t.)
526     else if(digit.GetSector(1)==5){
527       index = 20 + (digit.GetSector(0)-1)*1/3; 
528       lADCDataChannel = 5 + (digit.GetSector(0)-1)*8/3;
529       //
530       /*printf("\t AliZDC::Digits2Raw -> idig%d det %d quad %d index %d, ADCch %d ADCVal[%d, %d]\n",
531                 iDigit,digit.GetSector(0),digit.GetSector(1),index,lADCDataChannel,
532                 digit.GetADCValue(0),digit.GetADCValue(1));// Ch. debug
533       */
534       //
535       if(iDigit<knADCData2){ // *** In-time signals
536         lADCDataValue2[index] = digit.GetADCValue(0);
537         if(lADCDataValue2[index] > 2047) lADCDataOvFlw2[index] = 1; 
538         lADCDataValue2[index+2] = digit.GetADCValue(1);
539         if(lADCDataValue2[index+2] > 2047) lADCDataOvFlw2[index+2] = 1; 
540         //
541         lADCData2[index] =   lADCDataGEO << 27 | lADCDataChannel << 17 | 
542                         lADCDataOvFlw2[index] << 12 | (lADCDataValue2[index] & 0xfff); 
543         lADCData2[index+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
544                         lADCDataOvFlw2[index+2] << 12 | (lADCDataValue2[index+2] & 0xfff);   
545       }                 
546       else{ // *** Out-of-time signals
547         lADCDataValue4[index] = digit.GetADCValue(0);
548         if(lADCDataValue4[index] > 2047) lADCDataOvFlw4[index] = 1; 
549         lADCDataValue4[index+2] = digit.GetADCValue(1);
550         if(lADCDataValue4[index+2] > 2047) lADCDataOvFlw4[index+2] = 1; 
551         //
552         lADCData4[index] =   lADCDataGEO << 27 | lADCDataChannel << 17 | 
553                       lADCDataOvFlw4[index] << 12 | (lADCDataValue4[index] & 0xfff); 
554         lADCData4[index+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
555                       lADCDataOvFlw4[index+2] << 12 | (lADCDataValue4[index+2] & 0xfff);   
556       }                 
557            
558     }
559     if((index<0) || (index>23)) {
560       Error("Digits2Raw", "sector[0] = %d, sector[1] = %d", 
561             digit.GetSector(0), digit.GetSector(1));
562       continue;
563     }
564     
565     
566   }
567   //
568   /*
569   for(Int_t i=0;i<24;i++) printf("\t ADCData1[%d] = %x\n",i,lADCData1[i]);
570   for(Int_t i=0;i<20;i++) printf("\t ADCData2[%d] = %x\n",i,lADCData2[i]);
571   for(Int_t i=0;i<24;i++) printf("\t ADCData3[%d] = %x\n",i,lADCData3[i]);
572   for(Int_t i=0;i<20;i++) printf("\t ADCData4[%d] = %x\n",i,lADCData4[i]);
573   */
574  
575   // End of Block
576   UInt_t lADCEndBlockGEO = lADCHeaderGEO;
577   UInt_t lADCEndBlockEvCount = gAlice->GetEventNrInRun();
578   //  
579   lADCEndBlock = lADCEndBlockGEO << 27 | 0x1 << 26 | lADCEndBlockEvCount;
580   //printf("\t AliZDC::Digits2Raw -> ADCEndBlock = %d\n",lADCEndBlock);
581
582
583   // open the output file
584   char fileName[30];
585   strcpy(fileName,AliDAQ::DdlFileName("ZDC",0));
586
587   AliFstream* file = new AliFstream(fileName);
588
589   // write the DDL data header
590   AliRawDataHeader header;
591   header.fSize = sizeof(header) + 
592                  sizeof(lADCHeader1) + sizeof(lADCData1) + sizeof(lADCEndBlock) +
593                  sizeof(lADCHeader2) + sizeof(lADCData2) + sizeof(lADCEndBlock) +
594                  sizeof(lADCHeader1) + sizeof(lADCData3) + sizeof(lADCEndBlock) +
595                  sizeof(lADCHeader2) + sizeof(lADCData4) + sizeof(lADCEndBlock);
596   //
597   /*printf("sizeof header = %d, ADCHeader1 = %d, ADCData1 = %d, ADCEndBlock = %d\n",
598           sizeof(header),sizeof(lADCHeader1),sizeof(lADCData1),sizeof(lADCEndBlock));
599   printf("sizeof header = %d, ADCHeader2 = %d, ADCData2 = %d, ADCEndBlock = %d\n",
600           sizeof(header),sizeof(lADCHeader2),sizeof(lADCData2),sizeof(lADCEndBlock));
601   */
602   //     
603   header.SetAttribute(0);  // valid data
604   file->WriteBuffer((char*)(&header), sizeof(header));
605
606   // write the raw data and close the file
607   file->WriteBuffer((char*) &lADCHeader1, sizeof (lADCHeader1));
608   file->WriteBuffer((char*)(lADCData1), sizeof(lADCData1));
609   file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
610   file->WriteBuffer((char*) &lADCHeader2, sizeof (lADCHeader2));
611   file->WriteBuffer((char*)(lADCData2), sizeof(lADCData2));
612   file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
613   file->WriteBuffer((char*) &lADCHeader1, sizeof (lADCHeader1));
614   file->WriteBuffer((char*)(lADCData3), sizeof(lADCData3));
615   file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
616   file->WriteBuffer((char*) &lADCHeader2, sizeof (lADCHeader2));
617   file->WriteBuffer((char*)(lADCData4), sizeof(lADCData4));
618   file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
619   delete file;
620
621   // unload the digits
622   fLoader->UnloadDigits();
623 }
624
625 //_____________________________________________________________________________
626 Bool_t AliZDC::Raw2SDigits(AliRawReader* rawReader)
627 {
628   // Convert ZDC raw data to Sdigits
629   
630   AliLoader* loader = (gAlice->GetRunLoader())->GetLoader("ZDCLoader");
631   if(!loader) {
632     AliError("no ZDC loader found");
633     return kFALSE;
634   }
635
636 //  // Event loop
637   Int_t iEvent = 0;
638   while(rawReader->NextEvent()){
639     (gAlice->GetRunLoader())->GetEvent(iEvent++);
640     // Create the output digit tree
641     TTree* treeS = loader->TreeS();
642     if(!treeS){
643       loader->MakeTree("S");
644       treeS = loader->TreeS();
645     }
646     //
647     AliZDCSDigit sdigit;
648     AliZDCSDigit* psdigit = &sdigit;
649     const Int_t kBufferSize = 4000;
650     treeS->Branch("ZDC", "AliZDCSDigit",  &psdigit, kBufferSize);
651     //
652     AliZDCRawStream rawStream(rawReader);
653     Int_t sector[2], resADC, rawADC, corrADC, nPheVal;
654     Int_t jcount = 0;
655     while(rawStream.Next()){
656       if(rawStream.IsADCDataWord()){
657         //For the moment only in-time SDigits are foreseen (1st 48 raw values)
658         if(jcount < 48){ 
659           for(Int_t j=0; j<2; j++) sector[j] = rawStream.GetSector(j);
660           rawADC = rawStream.GetADCValue();
661           resADC = rawStream.GetADCGain();
662           //printf("\t RAw2SDigits raw%d ->  RawADC[%d, %d, %d] read\n",
663           //    jcount, sector[0], sector[1], rawADC);
664           //
665           corrADC = rawADC - Pedestal(sector[0], sector[1], resADC);
666           if(corrADC<0) corrADC=0;
667           nPheVal = ADCch2Phe(sector[0], sector[1], corrADC, resADC);
668           //
669           //printf("\t \t ->  SDigit[%d, %d, %d] created\n",
670           //    sector[0], sector[1], nPheVal);
671           //
672           new(psdigit) AliZDCSDigit(sector, (Float_t) nPheVal);
673           treeS->Fill();
674           jcount++;
675         }
676       }//IsADCDataWord
677     }//rawStream.Next
678     // write the output tree
679     fLoader->WriteSDigits("OVERWRITE");
680     fLoader->UnloadSDigits();
681   }//Event loop 
682    
683   return kTRUE;
684 }
685
686 //_____________________________________________________________________________
687 Int_t AliZDC::Pedestal(Int_t Det, Int_t Quad, Int_t Res) const
688 {
689   // Returns a pedestal for detector det, PM quad, channel with res.
690   //
691   // Getting calibration object for ZDC set
692   AliCDBManager *man = AliCDBManager::Instance();
693   AliCDBEntry  *entry = man->Get("ZDC/Calib/Data");
694   AliZDCCalibData *calibData = (AliZDCCalibData*) entry->GetObject();
695   //
696   if(!calibData){
697     printf("\t No calibration object found for ZDC!");
698     return -1;
699   }
700   //
701   Float_t pedValue;
702   Float_t meanPed, pedWidth;
703   Int_t index=0;
704   if(Quad!=5){
705     if(Det==1 || Det==2)      index = 10*(Det-1)+Quad+5*Res;   // ZN1, ZP1
706     else if(Det==3)           index = 10*(Det-1)+(Quad-1)+Res; // ZEM
707     else if(Det==4 || Det==5) index = 10*(Det-2)+Quad+5*Res+4; // ZN2, ZP2
708   }
709   else index = 10*(Quad-1)+(Det-1)*1/3+2*Res+4; // Reference PMs
710   //
711   //
712   meanPed = calibData->GetMeanPed(index);
713   pedWidth = calibData->GetMeanPedWidth(index);
714   pedValue = gRandom->Gaus(meanPed,pedWidth);
715   //
716   //printf("\t AliZDC::Pedestal - det(%d, %d) - Ped[%d] = %d\n",Det, Quad, index,(Int_t) pedValue); // Chiara debugging!
717   
718   
719
720   return (Int_t) pedValue;
721 }
722
723
724 //_____________________________________________________________________________
725 Int_t AliZDC::ADCch2Phe(Int_t Det, Int_t Quad, Int_t ADCVal, Int_t Res) const
726 {
727   // Evaluation of the no. of phe produced
728   Float_t pmGain[6][5];
729   Float_t resADC[2];
730   for(Int_t j = 0; j < 5; j++){
731     pmGain[0][j] = 50000.;
732     pmGain[1][j] = 100000.;
733     pmGain[2][j] = 100000.;
734     pmGain[3][j] = 50000.;
735     pmGain[4][j] = 100000.;
736     pmGain[5][j] = 100000.;
737   }
738   // ADC Caen V965
739   resADC[0] = 0.0000008; // ADC Resolution high gain: 200 fC/adcCh
740   resADC[1] = 0.0000064; // ADC Resolution low gain:  25  fC/adcCh
741   //
742   Int_t nPhe = (Int_t) (ADCVal * pmGain[Det-1][Quad] * resADC[Res]);
743   //
744   //printf("\t AliZDC::ADCch2Phe -> det(%d, %d) - ADC %d  phe %d\n",Det,Quad,ADCVal,nPhe);
745
746   return nPhe;
747 }
748
749 //______________________________________________________________________
750 void AliZDC::SetTreeAddress(){
751
752   // Set branch address for the Trees.
753   if(fLoader->TreeH() && (fHits == 0x0))
754     fHits   = new TClonesArray("AliZDCHit",1000);
755       
756   AliDetector::SetTreeAddress();
757 }
758  
759 //________________________________________________________________
760 void AliZDC::CreateCalibData()
761 {
762   // 
763   //if(fCalibData) delete fCalibData; // delete previous version
764   fCalibData = new AliZDCCalibData(GetName());
765 }
766 //________________________________________________________________
767 void AliZDC::WriteCalibData(Int_t option)
768 {
769   //
770   const int kCompressLevel = 9;
771   char* fnam = GetZDCCalibFName();
772   if(!fnam || fnam[0]=='\0') {
773     fnam = gSystem->ExpandPathName("$(ALICE_ROOT)/data/AliZDCCalib.root");
774     Warning("WriteCalibData","No File Name is provided, using default %s",fnam);
775   }
776   TFile* cdfile = TFile::Open(fnam,"UPDATE","",kCompressLevel);
777
778   // Writes Calibration Data to current directory. 
779   // User MUST take care of corresponding file opening and ->cd()... !!!
780   // By default, the object is overwritten. Use 0 option for opposite.
781   if(option) option = TObject::kOverwrite;
782   if(fCalibData) fCalibData->Write(0,option);
783   else if(fCalibData) fCalibData->Write(0,option);
784
785   cdfile->Close();
786   delete cdfile;
787 }
788
789 //________________________________________________________________
790 void AliZDC::LoadCalibData()
791 {
792   //
793   char* fnam = GetZDCCalibFName();
794   if(!fnam || fnam[0]=='\0') return; 
795   if(!gAlice->IsFileAccessible(fnam)) {
796     Error("LoadCalibData","ZDC Calibration Data file is not accessible, %s",fnam);
797     exit(1);
798   }
799   TFile* cdfile = TFile::Open(fnam);
800
801   // Loads Calibration Data from current directory. 
802   // User MUST take care of corresponding file opening and ->cd()...!!!
803   //
804   if(fCalibData) delete fCalibData; // delete previous version
805   TString dtname = "Calib_";
806   dtname += GetName();
807   fCalibData = (AliZDCCalibData*) gDirectory->Get(dtname.Data());
808   if(!fCalibData) { 
809     Error("LoadCalibData","No Calibration data found for %s",GetName());
810     exit(1);
811   }
812
813   cdfile->Close();
814   delete cdfile;
815 }
816