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