Minor correction
[u/mrichter/AliRoot.git] / ZDC / AliZDCDigitizer.cxx
CommitLineData
8309c1ab 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// ZDC digitizer class //
21// //
22///////////////////////////////////////////////////////////////////////////////
23
24#include <stdlib.h>
25
26// --- ROOT system
27#include <TTree.h>
28#include <TFile.h>
29#include <TNtuple.h>
30#include <TRandom.h>
31
32// --- AliRoot header files
33#include "AliLog.h"
34#include "AliRun.h"
35#include "AliHeader.h"
36#include "AliGenHijingEventHeader.h"
37#include "AliRunDigitizer.h"
38#include "AliRunLoader.h"
78d18275 39#include "AliCDBManager.h"
48642b09 40#include "AliCDBStorage.h"
41#include "AliCDBEntry.h"
8309c1ab 42#include "AliZDCSDigit.h"
43#include "AliZDCDigit.h"
44#include "AliZDCFragment.h"
45#include "AliZDCDigitizer.h"
48642b09 46#include "AliZDCCalibData.h"
8309c1ab 47
48ClassImp(AliZDCDigitizer)
49
50
51//____________________________________________________________________________
52AliZDCDigitizer::AliZDCDigitizer()
53{
54// Default constructor
55
56}
57
58//____________________________________________________________________________
59AliZDCDigitizer::AliZDCDigitizer(AliRunDigitizer* manager):
60 AliDigitizer(manager)
61{
78d18275 62
48642b09 63 // Constructor
78d18275 64 fStorage = SetStorage("local://$ALICE_ROOT");
65
66 // Get calibration data
67 int runNumber = 0;
68 fCalibData = GetCalibData(runNumber);
69
8309c1ab 70}
71
72//____________________________________________________________________________
73AliZDCDigitizer::~AliZDCDigitizer()
74{
75// Destructor
76
77}
78
79
80//____________________________________________________________________________
81Bool_t AliZDCDigitizer::Init()
82{
48642b09 83 // Initialize the digitizer
84 // NB -> PM gain vs. HV & ADC resolutions will move to DCDB ***************
8309c1ab 85 for (Int_t i = 0; i < 3; i++) {
86 for(Int_t j = 0; j < 5; j++) {
87 fPMGain[i][j] = 100000.;
88 }
89 }
48642b09 90 // ADC Caen V965
8309c1ab 91 fADCRes[0] = 0.0000008; // ADC Resolution high gain: 200 fC/adcCh
92 fADCRes[1] = 0.0000064; // ADC Resolution low gain: 25 fC/adcCh
93
94 return kTRUE;
95}
96
97//____________________________________________________________________________
98void AliZDCDigitizer::Exec(Option_t* /*option*/)
99{
48642b09 100 // Execute digitization
8309c1ab 101
102 Float_t pm[3][5];
48642b09 103 // --- pm[0][...] = light in ZN [C, Q1, Q2, Q3, Q4]
104 // --- pm[1][...] = light in ZP [C, Q1, Q2, Q3, Q4]
105 // --- pm[2][...] = light in ZEM [x, 1, 2, x, x]
8309c1ab 106 for (Int_t iSector1 = 0; iSector1 < 3; iSector1++)
107 for (Int_t iSector2 = 0; iSector2 < 5; iSector2++) {
108 pm[iSector1][iSector2] = 0;
109 }
8309c1ab 110
111 // impact parameter and number of spectators
112 Float_t impPar = -1;
113 Int_t specN = 0;
114 Int_t specP = 0;
115
116 // loop over input streams
117 for (Int_t iInput = 0; iInput < fManager->GetNinputs(); iInput++) {
118
119 // get run loader and ZDC loader
120 AliRunLoader* runLoader =
121 AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
122 AliLoader* loader = runLoader->GetLoader("ZDCLoader");
123 if (!loader) continue;
124
125 // load sdigits
126 loader->LoadSDigits();
127 TTree* treeS = loader->TreeS();
128 if (!treeS) continue;
129 AliZDCSDigit sdigit;
130 AliZDCSDigit* psdigit = &sdigit;
131 treeS->SetBranchAddress("ZDC", &psdigit);
132
133 // loop over sdigits
134 for (Int_t iSDigit = 0; iSDigit < treeS->GetEntries(); iSDigit++) {
135 treeS->GetEntry(iSDigit);
136 if (!psdigit) continue;
137
138 if ((sdigit.GetSector(1) < 0) || (sdigit.GetSector(1) > 4)) {
139 AliError(Form("\nsector[0] = %d, sector[1] = %d\n",
140 sdigit.GetSector(0), sdigit.GetSector(1)));
141 continue;
142 }
48642b09 143 pm[(sdigit.GetSector(0))-1][sdigit.GetSector(1)] += sdigit.GetLightPM();
144 printf("\n\t Detector (%d, %d), pm[%d][%d] = %.0f \n",
145 sdigit.GetSector(0), sdigit.GetSector(1),sdigit.GetSector(0)-1,
146 sdigit.GetSector(1), pm[sdigit.GetSector(0)-1][sdigit.GetSector(1)]); // Chiara debugging!
8309c1ab 147 }
148
149 // unload sdigits
150 loader->UnloadSDigits();
151
152 // get the impact parameter and the number of spectators in case of hijing
153 if (!runLoader->GetAliRun()) runLoader->LoadgAlice();
154 AliHeader* header = runLoader->GetAliRun()->GetHeader();
155 if (!header) continue;
156 AliGenEventHeader* genHeader = header->GenEventHeader();
157 if (!genHeader) continue;
158 if (!genHeader->InheritsFrom(AliGenHijingEventHeader::Class())) continue;
159 impPar = ((AliGenHijingEventHeader*) genHeader)->ImpactParameter();
85a96223 160 //
161 specN = ((AliGenHijingEventHeader*) genHeader)->ProjSpectatorsn();
162 specP = ((AliGenHijingEventHeader*) genHeader)->ProjSpectatorsp();
78d18275 163 AliDebug(2, Form("\n AliZDCDigitizer -> b = %f fm, Nspecn = %d, Nspecp = %d\n",
8309c1ab 164 impPar, specN, specP));
78d18275 165 printf("\n\t AliZDCDigitizer -> b = %f fm, Nspecn = %d, Nspecp = %d\n",
48642b09 166 impPar, specN, specP);
8309c1ab 167 }
168
169 // add spectators
170 if (impPar >= 0) {
171 Int_t freeSpecN, freeSpecP;
172 Fragmentation(impPar, specN, specP, freeSpecN, freeSpecP);
78d18275 173 printf("\n\t AliZDCDigitizer ---- Adding signal for %d free spectator n\n",freeSpecN);
8309c1ab 174 SpectatorSignal(1, freeSpecN, pm);
78d18275 175 printf("\t AliZDCDigitizer ---- Adding signal for %d free spectator p\n\n",freeSpecP);
8309c1ab 176 SpectatorSignal(2, freeSpecP, pm);
177 }
178
179 // get the output run loader and loader
180 AliRunLoader* runLoader =
181 AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
182 AliLoader* loader = runLoader->GetLoader("ZDCLoader");
183 if (!loader) {
184 AliError("no ZDC loader found");
185 return;
186 }
187
188 // create the output tree
189 const char* mode = "update";
190 if (runLoader->GetEventNumber() == 0) mode = "recreate";
191 loader->LoadDigits(mode);
192 loader->MakeTree("D");
193 TTree* treeD = loader->TreeD();
194 AliZDCDigit digit;
195 AliZDCDigit* pdigit = &digit;
196 const Int_t kBufferSize = 4000;
197 treeD->Branch("ZDC", "AliZDCDigit", &pdigit, kBufferSize);
198
199 // Create digits
200 Int_t sector[2];
201 Int_t digi[2];
202 for (sector[0] = 1; sector[0] <= 3; sector[0]++)
203 for (sector[1] = 0; sector[1] < 5; sector[1]++) {
204 if ((sector[0] == 3) && ((sector[1] < 1) || (sector[1] > 2))) continue;
205 for (Int_t res = 0; res < 2; res++){
48642b09 206 digi[res] = Phe2ADCch(sector[0], sector[1], pm[sector[0]-1][sector[1]], res)
207 + Pedestal(sector[0], sector[1], res);
78d18275 208 //printf("\t DIGIT added -> det = %d, quad = %d - digi[%d] = %d\n\n",
209 // sector[0], sector[1], res, digi[res]); // Chiara debugging!
8309c1ab 210 }
211 new(pdigit) AliZDCDigit(sector, digi);
212 treeD->Fill();
213 }
214
215 // write the output tree
216 loader->WriteDigits("OVERWRITE");
217 loader->UnloadDigits();
218}
219
220
221//_____________________________________________________________________________
222void AliZDCDigitizer::Fragmentation(Float_t impPar, Int_t specN, Int_t specP,
223 Int_t &freeSpecN, Int_t &freeSpecP) const
224{
225// simulate fragmentation of spectators
226
227 Int_t zz[100], nn[100];
228 AliZDCFragment frag(impPar);
229 for (Int_t j=0; j<=99; j++){
230 zz[j] =0;
231 nn[j] =0;
232 }
233
234 // Fragments generation
235 Int_t nAlpha;
236 frag.GenerateIMF(zz, nAlpha);
237
238 // Attach neutrons
239 Int_t ztot=0;
240 Int_t ntot=0;
241 frag.AttachNeutrons(zz, nn, ztot, ntot);
242 freeSpecN = specN-ntot-2*nAlpha;
243 freeSpecP = specP-ztot-2*nAlpha;
244 if(freeSpecN<0) freeSpecN=0;
245 if(freeSpecP<0) freeSpecP=0;
246 AliDebug(2, Form("FreeSpn = %d, FreeSpp = %d", freeSpecN, freeSpecP));
247}
248
249//_____________________________________________________________________________
250void AliZDCDigitizer::SpectatorSignal(Int_t SpecType, Int_t numEvents,
251 Float_t pm[3][5]) const
252{
253// add signal of the spectators
254
255 TFile* file = NULL;
256 if (SpecType == 1) { // --- Signal for spectator neutrons
257 file = TFile::Open("$ALICE/$ALICE_LEVEL/ZDC/ZNsignalntu.root");
258 } else if (SpecType == 2) { // --- Signal for spectator protons
259 file = TFile::Open("$ALICE/$ALICE_LEVEL/ZDC/ZPsignalntu.root");
260 }
261 if (!file || !file->IsOpen()) {
262 AliError("Opening of file failed");
263 return;
264 }
265
266 TNtuple* zdcSignal = (TNtuple*) file->Get("ZDCSignal");
267 Int_t nentries = (Int_t) zdcSignal->GetEntries();
268
269 Float_t *entry, hitsSpec[7];
270 Int_t pl, i, j, k, iev=0, rnd[125], volume[2];
48642b09 271 for(pl=0;pl<125;pl++) rnd[pl] = 0;
8309c1ab 272 if (numEvents > 125) {
273 AliWarning(Form("numEvents (%d) is larger than 125", numEvents));
274 numEvents = 125;
275 }
276 for(pl=0;pl<numEvents;pl++){
277 rnd[pl] = (Int_t) (9999*gRandom->Rndm());
85a96223 278 if(rnd[pl] >= 9999) rnd[pl] = 9998;
8309c1ab 279 //printf(" rnd[%d] = %d\n",pl,rnd[pl]);
280 }
281 // Sorting vector in ascending order with C function QSORT
282 qsort((void*)rnd,numEvents,sizeof(Int_t),comp);
283 do{
284 for(i=0; i<nentries; i++){
285 zdcSignal->GetEvent(i);
286 entry = zdcSignal->GetArgs();
287 if(entry[0] == rnd[iev]){
288 for(k=0; k<2; k++) volume[k] = (Int_t) entry[k+1];
289 for(j=0; j<7; j++) hitsSpec[j] = entry[j+3];
48642b09 290 //
8309c1ab 291 Float_t lightQ = hitsSpec[4];
292 Float_t lightC = hitsSpec[5];
78d18275 293 AliDebug(3, Form("SpectatorSignal -> vol = (%d, %d), lightQ = %.0f, lightC = %.0f",
8309c1ab 294 volume[0], volume[1], lightQ, lightC));
48642b09 295 //printf("\n Volume = (%d, %d), lightQ = %.0f, lightC = %.0f",
296 // volume[0], volume[1], lightQ, lightC);
8309c1ab 297 if (volume[0] < 3) { // ZN or ZP
298 pm[volume[0]-1][0] += lightC;
299 pm[volume[0]-1][volume[1]] += lightQ;
48642b09 300 //printf("\n pm[%d][0] = %.0f, pm[%d][%d] = %.0f\n",(volume[0]-1),pm[volume[0]-1][0],
301 // (volume[0]-1),volume[1],pm[volume[0]-1][volume[1]]);
302 }
303 else {
304 if (volume[1] == 1) pm[2][1] += lightC; // ZEM 1
305 else pm[2][2] += lightQ; // ZEM 2
306 //printf("\n pm[2][1] = %.0f, pm[2][2] = %.0f\n",pm[2][1],pm[2][2]);
307 }
8309c1ab 308 }
309 else if(entry[0] > rnd[iev]){
310 iev++;
311 continue;
312 }
313 }
314 }while(iev<numEvents);
315
316 file->Close();
317 delete file;
318}
319
320
321//_____________________________________________________________________________
322Int_t AliZDCDigitizer::Phe2ADCch(Int_t Det, Int_t Quad, Float_t Light,
323 Int_t Res) const
324{
48642b09 325 // Evaluation of the ADC channel corresponding to the light yield Light
326 Int_t ADCch = (Int_t) (Light * fPMGain[Det-1][Quad] * fADCRes[Res]);
78d18275 327 //printf("\t Phe2ADCch -> det %d quad %d - phe %.0f ADC %d\n", Det,Quad,Light,ADCch);
48642b09 328 return ADCch;
329}
8309c1ab 330
48642b09 331//_____________________________________________________________________________
332Int_t AliZDCDigitizer::Pedestal(Int_t Det, Int_t Quad, Int_t Res) const
333{
48642b09 334
335 Float_t meanPed;
78d18275 336 if(Det != 3) meanPed = fCalibData->GetMeanPed(10*(Det-1)+Quad+5*Res);
337 else meanPed = fCalibData->GetMeanPed(10*(Det-1)+Quad+1*Res);
48642b09 338
78d18275 339 //printf("\t Pedestal -> det = %d, quad = %d, res = %d - Ped[%d] = %d\n",
340 // Det, Quad, Res,10*(Det-1)+Quad+5*Res,(Int_t) meanPed); // Chiara debugging!
48642b09 341
342 return (Int_t) meanPed;
8309c1ab 343}
344
345//_____________________________________________________________________________
78d18275 346AliCDBStorage* AliZDCDigitizer::SetStorage(const char *uri)
8309c1ab 347{
78d18275 348 //printf("\n\t AliZDCDigitizer::SetStorage \n");
8309c1ab 349
78d18275 350 Bool_t deleteManager = kFALSE;
48642b09 351
78d18275 352 AliCDBManager *manager = AliCDBManager::Instance();
353 AliCDBStorage *defstorage = manager->GetDefaultStorage();
48642b09 354
78d18275 355 if(!defstorage || !(defstorage->Contains("ZDC"))){
356 AliWarning("No default storage set or default storage doesn't contain ZDC!");
357 manager->SetDefaultStorage(uri);
358 deleteManager = kTRUE;
359 }
360
361 AliCDBStorage *storage = manager->GetDefaultStorage();
362
363 if(deleteManager){
364 AliCDBManager::Instance()->UnsetDefaultStorage();
365 defstorage = 0; // the storage is killed by AliCDBManager::Instance()->Destroy()
366 }
367
368 return storage;
369}
48642b09 370
78d18275 371//_____________________________________________________________________________
372AliZDCCalibData* AliZDCDigitizer::GetCalibData(int runNumber) const
373{
48642b09 374
78d18275 375 //printf("\n\t AliZDCDigitizer::GetCalibData \n");
376
377 AliCDBEntry *entry = fStorage->Get("ZDC/Calib/Data",runNumber);
378 AliZDCCalibData *calibdata = (AliZDCCalibData*) entry->GetObject();
379
380 if (!calibdata) AliWarning("No calibration data from calibration database !");
8309c1ab 381
78d18275 382 return calibdata;
8309c1ab 383}