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