Additional include path added in order to avoid compilation problems.
[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 <TFile.h>
33#include <TSystem.h>
fe4da5cc 34
f901045b 35// --- AliRoot header files
88cb7938 36#include "AliDetector.h"
fe4da5cc 37#include "AliZDC.h"
f901045b 38#include "AliZDCHit.h"
6de91202 39#include "AliZDCSDigit.h"
40#include "AliZDCDigit.h"
41#include "AliZDCDigitizer.h"
42#include "AliZDCRawStream.h"
8af17dba 43#include "AliZDCCalibData.h"
88cb7938 44
6de91202 45#include "AliRawDataHeader.h"
88cb7938 46#include "AliLoader.h"
47#include "AliRun.h"
5d12ce38 48#include "AliMC.h"
fe4da5cc 49
fe4da5cc 50
51ClassImp(AliZDC)
8af17dba 52
53AliZDC *gZDC;
fe4da5cc 54
55//_____________________________________________________________________________
56AliZDC::AliZDC()
57{
58 //
59 // Default constructor for the Zero Degree Calorimeter base class
60 //
2e6fc82a 61
1450a7cd 62 fIshunt = 1;
63 fNoShower = 0;
09630792 64
1450a7cd 65 fHits = 0;
66 fNhits = 0;
09630792 67
1450a7cd 68 fDigits = 0;
69 fNdigits = 0;
2685bf00 70
fe4da5cc 71}
72
73//_____________________________________________________________________________
74AliZDC::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
359cdddc 81 fIshunt = 1;
82 fNoShower = 0;
f901045b 83
359cdddc 84 // Allocate the hits array
f901045b 85 fHits = new TClonesArray("AliZDCHit",1000);
5d12ce38 86 gAlice->GetMCApp()->AddHitList(fHits);
8af17dba 87
88 char sensname[5],senstitle[25];
89 sprintf(sensname,"ZDC");
90 sprintf(senstitle,"ZDC dummy");
91 SetName(sensname); SetTitle(senstitle);
f901045b 92
6de91202 93 fDigits = 0;
94 fNdigits = 0;
8af17dba 95
96 gZDC=this;
866ab5a2 97
98}
99//____________________________________________________________________________
100AliZDC::~AliZDC()
101{
102 //
103 // ZDC destructor
104 //
105
106 fIshunt = 0;
8af17dba 107 gZDC=0;
2e6fc82a 108
fe4da5cc 109}
fe4da5cc 110//_____________________________________________________________________________
111void AliZDC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
112{
113 //
f901045b 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
fe4da5cc 119 //
f901045b 120
68ca986e 121 static Float_t primKinEn, xImpact, yImpact, sFlag;
122
43f34e8b 123 AliZDCHit *newquad, *curprimquad;
f901045b 124 newquad = new AliZDCHit(fIshunt, track, vol, hits);
65d4f2be 125 TClonesArray &lhits = *fHits;
cefa66cb 126
127 if(fNhits==0){
128 // First hit -> setting flag for primary or secondary particle
5d12ce38 129 Int_t primary = gAlice->GetMCApp()->GetPrimary(track);
cefa66cb 130 if(track != primary){
3ff116f2 131 newquad->SetSFlag(1); // SECONDARY particle entering the ZDC
cefa66cb 132 }
133 else if(track == primary){
3ff116f2 134 newquad->SetSFlag(0); // PRIMARY particle entering the ZDC
cefa66cb 135 }
3ff116f2 136 sFlag = newquad->GetSFlag();
137 primKinEn = newquad->GetPrimKinEn();
138 xImpact = newquad->GetXImpact();
139 yImpact = newquad->GetYImpact();
cefa66cb 140 }
141 else{
3ff116f2 142 newquad->SetPrimKinEn(primKinEn);
143 newquad->SetXImpact(xImpact);
144 newquad->SetYImpact(yImpact);
145 newquad->SetSFlag(sFlag);
cefa66cb 146 }
147
43f34e8b 148 Int_t j;
f901045b 149 for(j=0; j<fNhits; j++){
2e6fc82a 150 // If hits are equal (same track, same volume), sum them.
f901045b 151 curprimquad = (AliZDCHit*) lhits[j];
152 if(*curprimquad == *newquad){
153 *curprimquad = *curprimquad+*newquad;
154 delete newquad;
68ca986e 155 return;
f901045b 156 }
157 }
f901045b 158
159 //Otherwise create a new hit
160 new(lhits[fNhits]) AliZDCHit(newquad);
161 fNhits++;
162
866ab5a2 163 delete newquad;
cefa66cb 164}
165
866ab5a2 166//_____________________________________________________________________________
fe4da5cc 167void 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
68ca986e 175 TNode *node, *top;
176 TBRIK *brik;
359cdddc 177 const int kColorZDC = kBlue;
fe4da5cc 178
179 //
68ca986e 180 top=gAlice->GetGeometry()->GetNode("alice");
fe4da5cc 181
182 // ZDC
fe4da5cc 183 brik = new TBRIK("S_ZDC","ZDC box","void",300,300,5);
68ca986e 184 top->cd();
185 node = new TNode("ZDC","ZDC","S_ZDC",0,0,600,"");
186 node->SetLineColor(kColorZDC);
187 fNodes->Add(node);
fe4da5cc 188}
189
190//_____________________________________________________________________________
191Int_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}
359cdddc 199
200//____________________________________________________________________________
201Float_t AliZDC::ZMin(void) const
202{
203 // Minimum dimension of the ZDC module in z
6de91202 204 return -11600.;
359cdddc 205}
206
207//____________________________________________________________________________
208Float_t AliZDC::ZMax(void) const
209{
210 // Maximum dimension of the ZDC module in z
6de91202 211 return -11750.;
359cdddc 212}
213
214
fe4da5cc 215//_____________________________________________________________________________
6de91202 216void AliZDC::MakeBranch(Option_t *opt, const char * /*file*/)
fe4da5cc 217{
218 //
359cdddc 219 // Create Tree branches for the ZDC
fe4da5cc 220 //
359cdddc 221
222 char branchname[10];
223 sprintf(branchname,"%s",GetName());
88cb7938 224
225 const char *cH = strstr(opt,"H");
226
227 if (cH && fLoader->TreeH())
228 fHits = new TClonesArray("AliZDCHit",1000);
359cdddc 229
230 AliDetector::MakeBranch(opt);
b6d27557 231}
1450a7cd 232
1450a7cd 233//_____________________________________________________________________________
b6d27557 234void AliZDC::Hits2SDigits()
235{
6de91202 236 // Create summable digits from hits
237
238 if (GetDebug()) printf("\n Entering AliZDC::Hits2Digits() ");
b6d27557 239
85a5290f 240 fLoader->LoadHits("read");
241 fLoader->LoadSDigits("recreate");
6de91202 242 AliRunLoader* runLoader = fLoader->GetRunLoader();
243 AliZDCSDigit sdigit;
244 AliZDCSDigit* psdigit = &sdigit;
85a5290f 245
6de91202 246 // Event loop
85a5290f 247 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
6de91202 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;
85a5290f 250
6de91202 251 runLoader->GetEvent(iEvent);
252 TTree* treeH = fLoader->TreeH();
359cdddc 253 Int_t ntracks = (Int_t) treeH->GetEntries();
6de91202 254 ResetHits();
255
359cdddc 256 // Tracks loop
6de91202 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()) {
b6d27557 262
6de91202 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
359cdddc 284 }
6de91202 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();
b6d27557 301 }
359cdddc 302
6de91202 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");
85a5290f 325 }
326
327 fLoader->UnloadHits();
328 fLoader->UnloadSDigits();
359cdddc 329}
330
331//_____________________________________________________________________________
6de91202 332AliDigitizer* AliZDC::CreateDigitizer(AliRunDigitizer* manager) const
359cdddc 333{
6de91202 334 // Create the digitizer for ZDC
335
336 return new AliZDCDigitizer(manager);
359cdddc 337}
338
339//_____________________________________________________________________________
6de91202 340void AliZDC::Digits2Raw()
359cdddc 341{
6de91202 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();
1450a7cd 363
6de91202 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;
1450a7cd 399 }
1450a7cd 400
6de91202 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;
1450a7cd 405
6de91202 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);
1450a7cd 410 }
6de91202 411 //for (Int_t i=0;i<24;i++)printf("ADCData[%d] = %d\n",i,ADCData[i]);
1450a7cd 412
6de91202 413 // End of Block
414 UInt_t ADCEndBlockGEO = ADCHeaderGEO;
415 UInt_t ADCEndBlockEvCount = gAlice->GetEventNrInRun();
1450a7cd 416
6de91202 417 ADCEndBlock = ADCEndBlockGEO << 27 | 0x1 << 26 | ADCEndBlockEvCount;
1450a7cd 418
6de91202 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();
359cdddc 448}
449
b81c9821 450//______________________________________________________________________
451void 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
b81c9821 462 AliDetector::SetTreeAddress();
463}
359cdddc 464
8af17dba 465
466//Calibration methods (by Alberto Colla)
467
468
469//________________________________________________________________
470void AliZDC::CreateCalibData()
471{
472 //
473 //if (fCalibData) delete fCalibData; // delete previous version
474 fCalibData = new AliZDCCalibData(GetName());
475}
476//________________________________________________________________
477void 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//________________________________________________________________
501void 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)