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