]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/AliZDC.cxx
Removing DistancetoPrimitive to avoid compilation warnings (icc)
[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 <TGeometry.h>
30 #include <TNode.h>
31 #include <TTree.h>
32 #include <TFile.h>
33 #include <TSystem.h>
34
35 // --- AliRoot header files
36 #include "AliDetector.h"
37 #include "AliZDC.h"
38 #include "AliZDCHit.h"
39 #include "AliZDCSDigit.h"
40 #include "AliZDCDigit.h"
41 #include "AliZDCDigitizer.h"
42 #include "AliZDCRawStream.h"
43 #include "AliZDCCalibData.h"
44
45 #include "AliRawDataHeader.h"
46 #include "AliLoader.h"
47 #include "AliRun.h"
48 #include "AliMC.h"
49 #include "AliLog.h"
50
51  
52 ClassImp(AliZDC)
53
54 AliZDC *gZDC;
55  
56 //_____________________________________________________________________________
57 AliZDC::AliZDC()
58 {
59   //
60   // Default constructor for the Zero Degree Calorimeter base class
61   //
62   
63   fIshunt     = 1;
64   fNoShower   = 0;
65
66   fHits       = 0;
67   fNhits      = 0;
68
69   fDigits     = 0;
70   fNdigits    = 0;
71   
72   fCalibData  = 0;
73
74 }
75  
76 //_____________________________________________________________________________
77 AliZDC::AliZDC(const char *name, const char *title)
78   : AliDetector(name,title)
79 {
80   //
81   // Standard constructor for the Zero Degree Calorimeter base class
82   //
83
84   fIshunt   = 1;
85   fNoShower = 0;
86
87   // Allocate the hits array  
88   fHits   = new TClonesArray("AliZDCHit",1000);
89   gAlice->GetMCApp()->AddHitList(fHits);
90   
91   char sensname[5],senstitle[25];
92   sprintf(sensname,"ZDC");
93   sprintf(senstitle,"ZDC dummy");
94   SetName(sensname); SetTitle(senstitle);
95
96   fDigits     = 0;
97   fNdigits    = 0;
98   
99   fCalibData  = 0;
100
101   gZDC=this;
102
103 }
104 //____________________________________________________________________________ 
105 AliZDC::~AliZDC()
106 {
107   //
108   // ZDC destructor
109   //
110
111   fIshunt   = 0;
112   gZDC=0;
113
114   delete fCalibData;
115
116 }
117 //_____________________________________________________________________________
118 void AliZDC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
119 {
120   //
121   //            Add a ZDC hit to the hit list.
122   // -> We make use of 2 array of hits:
123   // [1]  fHits (the usual one) that contains hits for each PRIMARY
124   // [2]  fStHits that contains hits for each EVENT and is used to
125   //      obtain digits at the end of each event
126   //
127   
128   static Float_t primKinEn, xImpact, yImpact, sFlag;
129
130   AliZDCHit *newquad, *curprimquad;
131   newquad = new AliZDCHit(fIshunt, track, vol, hits);
132   TClonesArray &lhits = *fHits;
133   
134   if(fNhits==0){
135       // First hit -> setting flag for primary or secondary particle
136       Int_t primary = gAlice->GetMCApp()->GetPrimary(track);     
137       if(track != primary){
138         newquad->SetSFlag(1);  // SECONDARY particle entering the ZDC
139       }
140       else if(track == primary){
141         newquad->SetSFlag(0);  // PRIMARY particle entering the ZDC
142       }  
143       sFlag     = newquad->GetSFlag();
144       primKinEn = newquad->GetPrimKinEn();
145       xImpact   = newquad->GetXImpact();
146       yImpact   = newquad->GetYImpact();
147    }
148    else{       
149       newquad->SetPrimKinEn(primKinEn);
150       newquad->SetXImpact(xImpact);
151       newquad->SetYImpact(yImpact);
152       newquad->SetSFlag(sFlag);
153    }
154  
155   Int_t j;
156   for(j=0; j<fNhits; j++){
157     // If hits are equal (same track, same volume), sum them.
158      curprimquad = (AliZDCHit*) lhits[j];
159      if(*curprimquad == *newquad){
160         *curprimquad = *curprimquad+*newquad;
161         delete newquad;
162         return;
163      } 
164   }
165
166     //Otherwise create a new hit
167     new(lhits[fNhits]) AliZDCHit(newquad);
168     fNhits++;
169     
170     delete newquad;
171 }
172
173 //_____________________________________________________________________________
174 void AliZDC::BuildGeometry()
175 {
176   //
177   // Build the ROOT TNode geometry for event display 
178   // in the Zero Degree Calorimeter
179   // This routine is dummy for the moment
180   //
181
182   TNode *node, *top;
183   TBRIK *brik;
184   const int kColorZDC  = kBlue;
185   
186   //
187   top=gAlice->GetGeometry()->GetNode("alice");
188   
189   // ZDC
190     brik = new TBRIK("S_ZDC","ZDC box","void",300,300,5);
191     top->cd();
192     node = new TNode("ZDC","ZDC","S_ZDC",0,0,600,"");
193     node->SetLineColor(kColorZDC);
194     fNodes->Add(node);
195 }
196
197 //____________________________________________________________________________
198 Float_t AliZDC::ZMin(void) const
199 {
200   // Minimum dimension of the ZDC module in z
201   return -11600.;
202 }
203
204 //____________________________________________________________________________
205 Float_t AliZDC::ZMax(void) const
206 {
207   // Maximum dimension of the ZDC module in z
208   return -11750.;
209 }
210   
211
212 //_____________________________________________________________________________
213 void AliZDC::MakeBranch(Option_t *opt, const char * /*file*/)
214 {
215   //
216   // Create Tree branches for the ZDC
217   //
218
219   char branchname[10];
220   sprintf(branchname,"%s",GetName());
221
222   const char *cH = strstr(opt,"H");
223   
224   if (cH && fLoader->TreeH())
225    fHits   = new TClonesArray("AliZDCHit",1000); 
226   
227   AliDetector::MakeBranch(opt);
228 }
229
230 //_____________________________________________________________________________
231 void AliZDC::Hits2SDigits()
232 {
233   // Create summable digits from hits
234   
235   AliDebug(1,"\n        Entering AliZDC::Hits2Digits() ");
236   
237   fLoader->LoadHits("read");
238   fLoader->LoadSDigits("recreate");
239   AliRunLoader* runLoader = fLoader->GetRunLoader();
240   AliZDCSDigit sdigit;
241   AliZDCSDigit* psdigit = &sdigit;
242
243   // Event loop
244   for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
245     Float_t pmCZN = 0, pmCZP = 0, pmQZN[4], pmQZP[4], pmZEM1 = 0, pmZEM2 = 0;
246     for (Int_t i = 0; i < 4; i++) pmQZN[i] = pmQZP[i] = 0;
247
248     runLoader->GetEvent(iEvent);
249     TTree* treeH = fLoader->TreeH();
250     Int_t ntracks = (Int_t) treeH->GetEntries();
251     ResetHits();
252
253     // Tracks loop
254     Int_t sector[2];
255     for (Int_t itrack = 0; itrack < ntracks; itrack++) {
256       treeH->GetEntry(itrack);
257       for (AliZDCHit* zdcHit = (AliZDCHit*)FirstHit(-1); zdcHit;
258                       zdcHit = (AliZDCHit*)NextHit()) { 
259                       
260         sector[0] = zdcHit->GetVolume(0);
261         sector[1] = zdcHit->GetVolume(1);
262         if ((sector[1] < 1) || (sector[1] > 4)) {
263           Error("Hits2SDigits", "sector[0] = %d, sector[1] = %d", 
264                 sector[0], sector[1]);
265           continue;
266         }
267         Float_t lightQ = zdcHit->GetLightPMQ();
268         Float_t lightC = zdcHit->GetLightPMC();
269      
270         if (sector[0] == 1) {          //ZN 
271           pmCZN += lightC;
272           pmQZN[sector[1]-1] += lightQ;
273         } else if (sector[0] == 2) {   //ZP 
274           pmCZP += lightC;
275           pmQZP[sector[1]-1] += lightQ;
276         } else if (sector[0] == 3) {   //ZEM 
277           if (sector[1] == 1) pmZEM1 += lightC;
278           else                pmZEM2 += lightQ;
279         }
280       }//Hits loop
281     }
282
283     // create the output tree
284     fLoader->MakeTree("S");
285     TTree* treeS = fLoader->TreeS();
286     const Int_t kBufferSize = 4000;
287     treeS->Branch(GetName(), "AliZDCSDigit", &psdigit, kBufferSize);
288
289     // Create sdigits for ZN
290     sector[0] = 1; // Detector = ZN
291     sector[1] = 0; // Common PM ADC
292     new(psdigit) AliZDCSDigit(sector, pmCZN);
293     if (pmCZN > 0) treeS->Fill();
294     for (Int_t j = 0; j < 4; j++) {
295       sector[1] = j+1; // Towers PM ADCs
296       new(psdigit) AliZDCSDigit(sector, pmQZN[j]);
297       if (pmQZN[j] > 0) treeS->Fill();
298     }
299   
300     // Create sdigits for ZP
301     sector[0] = 2; // Detector = ZP
302     sector[1] = 0; // Common PM ADC
303     new(psdigit) AliZDCSDigit(sector, pmCZP);
304     if (pmCZP > 0) treeS->Fill();
305     for (Int_t j = 0; j < 4; j++) {
306       sector[1] = j+1; // Towers PM ADCs
307       new(psdigit) AliZDCSDigit(sector, pmQZP[j]);
308       if (pmQZP[j] > 0) treeS->Fill();
309     }
310
311     // Create sdigits for ZEM
312     sector[0] = 3; 
313     sector[1] = 1; // Detector = ZEM1
314     new(psdigit) AliZDCSDigit(sector, pmZEM1);
315     if (pmZEM1 > 0) treeS->Fill();
316     sector[1] = 2; // Detector = ZEM2
317     new(psdigit) AliZDCSDigit(sector, pmZEM2);
318     if (pmZEM2 > 0) treeS->Fill();
319
320     // write the output tree
321     fLoader->WriteSDigits("OVERWRITE");
322   }
323
324   fLoader->UnloadHits();
325   fLoader->UnloadSDigits();
326 }
327
328 //_____________________________________________________________________________
329 AliDigitizer* AliZDC::CreateDigitizer(AliRunDigitizer* manager) const
330 {
331   // Create the digitizer for ZDC
332
333   return new AliZDCDigitizer(manager);
334 }
335
336 //_____________________________________________________________________________
337 void AliZDC::Digits2Raw()
338 {
339   // Convert ZDC digits to raw data
340
341   // preliminary format: 12 interger values (ZNC, ZNQ1-4, ZPC, ZPQ1-4, ZEM1,2)
342   // For the CAEN module V965 we have an header, the Data Words and an End Of Block
343   UInt_t lADCHeader; 
344   UInt_t lADCData[24];
345   UInt_t lADCEndBlock;
346
347   // load the digits
348   fLoader->LoadDigits("read");
349   AliZDCDigit digit;
350   AliZDCDigit* pdigit = &digit;
351   TTree* treeD = fLoader->TreeD();
352   if (!treeD) return;
353   treeD->SetBranchAddress("ZDC", &pdigit);
354
355   // Fill data array
356   // ADC header
357   UInt_t lADCHeaderGEO = 0;
358   UInt_t lADCHeaderCRATE = 0;
359   UInt_t lADCHeaderCNT = (UInt_t) treeD->GetEntries();
360     
361   lADCHeader = lADCHeaderGEO << 27 | 0x1 << 25 | lADCHeaderCRATE << 16 |
362                lADCHeaderCNT << 8 ;
363
364   //printf("lADCHeader = %d\n",lADCHeader);
365       
366   // ADC data word
367   UInt_t lADCDataGEO = lADCHeaderGEO;
368   UInt_t lADCDataValue[24];
369   UInt_t lADCDataOvFlw[24];
370   for(Int_t i = 0; i < 24; i++){
371     lADCDataValue[i] = 0;
372     lADCDataOvFlw[i] = 0;
373   }
374   UInt_t lADCDataChannel = 0;
375   
376   // loop over digits
377   for (Int_t iDigit = 0; iDigit < treeD->GetEntries(); iDigit++) {
378     treeD->GetEntry(iDigit);
379     if (!pdigit) continue;
380
381     //ADC data
382     Int_t index = 0;
383     if(digit.GetSector(0)!=3){
384       index = (digit.GetSector(0)-1) + digit.GetSector(1)*4;
385       lADCDataChannel = (digit.GetSector(0)-1)*8 + digit.GetSector(1);
386     }
387     else {
388       index = 19 + digit.GetSector(1);
389       lADCDataChannel = 5 + digit.GetSector(1)*8;
390     }
391      
392     if ((index < 0) || (index >= 22)) {
393       Error("Digits2Raw", "sector[0] = %d, sector[1] = %d", 
394             digit.GetSector(0), digit.GetSector(1));
395       continue;
396     }
397     
398     lADCDataValue[index] = digit.GetADCValue(0);
399     if (lADCDataValue[index] > 2047) lADCDataOvFlw[index] = 1; 
400     lADCDataValue[index+2] = digit.GetADCValue(1);
401     if (lADCDataValue[index+2] > 2047) lADCDataOvFlw[index+2] = 1; 
402     
403     lADCData[index] =   lADCDataGEO << 27 | lADCDataChannel << 17 | 
404                         lADCDataOvFlw[index] << 12 | (lADCDataValue[index] & 0xfff); 
405     lADCData[index+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
406                         lADCDataOvFlw[index+2] << 12 | (lADCDataValue[index+2] & 0xfff);                    
407   }
408   //for (Int_t i=0;i<24;i++)printf("ADCData[%d] = %d\n",i,lADCData[i]);
409   
410   // End of Block
411   UInt_t lADCEndBlockGEO = lADCHeaderGEO;
412   UInt_t lADCEndBlockEvCount = gAlice->GetEventNrInRun();
413   
414   lADCEndBlock = lADCEndBlockGEO << 27 | 0x1 << 26 | lADCEndBlockEvCount;
415   
416   //printf("ADCEndBlock = %d\n",lADCEndBlock);
417
418
419   // open the output file
420   char fileName[30];
421   sprintf(fileName, "ZDC_%d.ddl", AliZDCRawStream::kDDLOffset);
422 #ifndef __DECCXX
423   ofstream file(fileName, ios::binary);
424 #else
425   ofstream file(fileName);
426 #endif
427
428   // write the DDL data header
429   AliRawDataHeader header;
430   header.fSize = sizeof(header) + sizeof(lADCHeader) + 
431                  sizeof(lADCData) + sizeof(lADCEndBlock);
432   //printf("sizeof header = %d, ADCHeader = %d, ADCData = %d, ADCEndBlock = %d\n",
433   //        sizeof(header),sizeof(lADCHeader),sizeof(lADCData),sizeof(lADCEndBlock));
434   header.SetAttribute(0);  // valid data
435   file.write((char*)(&header), sizeof(header));
436
437   // write the raw data and close the file
438   file.write((char*) &lADCHeader, sizeof (lADCHeader));
439   file.write((char*)(lADCData), sizeof(lADCData));
440   file.write((char*) &lADCEndBlock, sizeof(lADCEndBlock));
441   file.close();
442
443   // unload the digits
444   fLoader->UnloadDigits();
445 }
446
447 //______________________________________________________________________
448 void AliZDC::SetTreeAddress(){
449   // Set branch address for the Trees.
450   // Inputs:
451   //      none.
452   // Outputs:
453   //      none.
454   // Return:
455   //      none.
456   if (fLoader->TreeH() && (fHits == 0x0))
457     fHits   = new TClonesArray("AliZDCHit",1000);
458       
459   AliDetector::SetTreeAddress();
460 }
461  
462  
463 //Calibration methods (by Alberto Colla)
464  
465  
466 //________________________________________________________________
467 void AliZDC::CreateCalibData()
468 {
469   // 
470   //if (fCalibData) delete fCalibData; // delete previous version
471   fCalibData = new AliZDCCalibData(GetName());
472 }
473 //________________________________________________________________
474 void AliZDC::WriteCalibData(Int_t option)
475 {
476   //
477   const int kCompressLevel = 9;
478   char* fnam = GetZDCCalibFName();
479   if (!fnam || fnam[0]=='\0') {
480     fnam = gSystem->ExpandPathName("$(ALICE)/$(ALICE_LEVEL)/data/AliZDCCalib.root");
481     Warning("WriteCalibData","No File Name is provided, using default %s",fnam);
482   }
483   TFile* cdfile = TFile::Open(fnam,"UPDATE","",kCompressLevel);
484
485   // Writes Calibration Data to current directory. 
486   // User MUST take care of corresponding file opening and ->cd()... !!!
487   // By default, the object is overwritten. Use 0 option for opposite.
488   if (option) option = TObject::kOverwrite;
489   if (fCalibData) fCalibData->Write(0,option);
490   else if (fCalibData) fCalibData->Write(0,option);
491
492   cdfile->Close();
493   delete cdfile;
494 }
495
496 //________________________________________________________________
497 void AliZDC::LoadCalibData()
498 {
499   //
500   char* fnam = GetZDCCalibFName();
501   if (!fnam || fnam[0]=='\0') return; 
502   if (!gAlice->IsFileAccessible(fnam)) {
503     Error("LoadCalibData","ZDC Calibration Data file is not accessible, %s",fnam);
504     exit(1);
505   }
506   TFile* cdfile = TFile::Open(fnam);
507
508   // Loads Calibration Data from current directory. 
509   // User MUST take care of corresponding file opening and ->cd()...!!!
510   //
511   if (fCalibData) delete fCalibData; // delete previous version
512   TString dtname = "Calib_";
513   dtname += GetName();
514   fCalibData = (AliZDCCalibData*) gDirectory->Get(dtname.Data());
515   if (!fCalibData) { 
516     Error("LoadCalibData","No Calibration data found for %s",GetName());
517     exit(1);
518   }
519
520   cdfile->Close();
521   delete cdfile;
522 }
523
524
525 //Calibration methods (by Alberto Colla)