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