New code from Piergiorgio added
[u/mrichter/AliRoot.git] / ITS / AliITSsimulationSDD.cxx
CommitLineData
b0f5e3fc 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
17#include <iostream.h>
18#include <stdlib.h>
19#include <stdio.h>
1ca7869b 20#include <string.h>
21
ece86d9a 22#include <TStopwatch.h>
23#include <TCanvas.h>
24#include <TF1.h>
25#include <TRandom.h>
1ca7869b 26#include <TH1.h>
27#include <TFile.h>
28#include <TVector.h>
29#include <TArrayI.h>
30#include <TArrayF.h>
ece86d9a 31
b0f5e3fc 32#include "AliRun.h"
e8189707 33#include "AliITS.h"
ece86d9a 34#include "AliITShit.h"
35#include "AliITSdigit.h"
36#include "AliITSmodule.h"
e8189707 37#include "AliITSMapA1.h"
38#include "AliITSMapA2.h"
e8189707 39#include "AliITSetfSDD.h"
40#include "AliITSRawData.h"
b0f5e3fc 41#include "AliITSHuffman.h"
ece86d9a 42#include "AliITSsegmentation.h"
43#include "AliITSresponse.h"
1ca7869b 44#include "AliITSsimulationSDD.h"
b0f5e3fc 45
b0f5e3fc 46ClassImp(AliITSsimulationSDD)
47////////////////////////////////////////////////////////////////////////
48// Version: 0
49// Written by Piergiorgio Cerello
50// November 23 1999
51//
52// AliITSsimulationSDD is the simulation of SDDs.
53 //
54//Begin_Html
55/*
56<img src="picts/ITS/AliITShit_Class_Diagram.gif">
57</pre>
58<br clear=left>
59<font size=+2 color=red>
60<p>This show the relasionships between the ITS hit class and the rest of Aliroot.
61</font>
62<pre>
63*/
64//End_Html
65//_____________________________________________________________________________
66
67Int_t power(Int_t b, Int_t e) {
e8189707 68 // compute b to the e power, where both b and e are Int_ts.
b0f5e3fc 69 Int_t power = 1,i;
70 for(i=0; i<e; i++) power *= b;
71 return power;
72}
73
74//_____________________________________________
75
76void FastFourierTransform(AliITSetfSDD *alisddetf,Double_t *real,
77 Double_t *imag,Int_t direction) {
78 // Do a Fast Fourier Transform
ece86d9a 79 //printf("FFT: direction %d\n",direction);
b0f5e3fc 80
81 Int_t samples = alisddetf->GetSamples();
82 Int_t l = (Int_t) ((log((Float_t) samples)/log(2.))+0.5);
83 Int_t m1 = samples;
84 Int_t m = samples/2;
85 Int_t m2 = samples/m1;
86 Int_t i,j,k;
87 for(i=1; i<=l; i++) {
88 for(j=0; j<samples; j += m1) {
89 Int_t p = 0;
90 for(k=j; k<= j+m-1; k++) {
91 Double_t wsr = alisddetf->GetWeightReal(p);
92 Double_t wsi = alisddetf->GetWeightImag(p);
93 if(direction == -1) wsi = -wsi;
94 Double_t xr = *(real+k+m);
95 Double_t xi = *(imag+k+m);
96 *(real+k+m) = wsr*(*(real+k)-xr) - wsi*(*(imag+k)-xi);
97 *(imag+k+m) = wsr*(*(imag+k)-xi) + wsi*(*(real+k)-xr);
98 *(real+k) += xr;
99 *(imag+k) += xi;
100 p += m2;
101 }
102 }
103 m1 = m;
104 m /= 2;
105 m2 += m2;
106 }
107
108 for(j=0; j<samples; j++) {
109 Int_t j1 = j;
110 Int_t p = 0;
111 Int_t i1;
112 for(i1=1; i1<=l; i1++) {
113 Int_t j2 = j1;
114 j1 /= 2;
115 p = p + p + j2 - j1 - j1;
116 }
117 if(p >= j) {
118 Double_t xr = *(real+j);
119 Double_t xi = *(imag+j);
120 *(real+j) = *(real+p);
121 *(imag+j) = *(imag+p);
122 *(real+p) = xr;
123 *(imag+p) = xi;
124 }
125 }
126 if(direction == -1) {
127 for(i=0; i<samples; i++) {
128 *(real+i) /= samples;
129 *(imag+i) /= samples;
130 }
131 }
132 return;
133}
134//_____________________________________________________________________________
135
136AliITSsimulationSDD::AliITSsimulationSDD(){
137 // Default constructor
138
139 fResponse = 0;
140 fSegmentation = 0;
141 fHis = 0;
ece86d9a 142 fHitMap1 = 0;
143 fHitMap2 = 0;
144 fElectronics = 0;
145 fStream = 0;
b0f5e3fc 146 fD.Set(0);
147 fT1.Set(0);
148 fT2.Set(0);
149 fTol.Set(0);
ece86d9a 150 fNoise.Set(0);
151 fBaseline.Set(0);
e8189707 152 SetScaleFourier();
153 SetPerpendTracksFlag();
ece86d9a 154 SetDoFFT();
155 SetCheckNoise();
5d18fa90 156 fInZR = 0;
157 fInZI = 0;
158 fOutZR = 0;
159 fOutZI = 0;
ece86d9a 160 fNofMaps = 0;
161 fMaxNofSamples = 0;
162 fITS = 0;
163 fTreeB=0;
b0f5e3fc 164}
165//_____________________________________________________________________________
e8189707 166AliITSsimulationSDD::AliITSsimulationSDD(AliITSsimulationSDD &source)
167{
b0f5e3fc 168 // Copy constructor to satify Coding roules only.
169 if(this==&source) return;
170 printf("Not allowed to make a copy of AliITSsimulationSDD "
171 "Using default creater instead\n");
172 AliITSsimulationSDD();
173}
174//_____________________________________________________________________________
e8189707 175AliITSsimulationSDD& AliITSsimulationSDD::operator=(AliITSsimulationSDD &source)
176{
177 // Assignment operator to satify Coding roules only.
b0f5e3fc 178 if(this==&source) return *this;
179 printf("Not allowed to make a = with AliITSsimulationSDD "
180 "Using default creater instead\n");
181 return *this ;
182}
183//_____________________________________________________________________________
184
e8189707 185AliITSsimulationSDD::AliITSsimulationSDD(AliITSsegmentation *seg,AliITSresponse *resp)
186{
187 // Standard Constructor
ece86d9a 188
189 fHis=0;
190 fTreeB=0;
b0f5e3fc 191 fResponse = resp;
192 fSegmentation = seg;
e8189707 193 SetScaleFourier();
194 SetPerpendTracksFlag();
ece86d9a 195 SetDoFFT();
196 SetCheckNoise();
b0f5e3fc 197
e8189707 198 fHitMap2 = new AliITSMapA2(fSegmentation,fScaleSize,1);
b0f5e3fc 199 fHitMap1 = new AliITSMapA1(fSegmentation);
200
201 //
202 fNofMaps=fSegmentation->Npz();
203 fMaxNofSamples=fSegmentation->Npx();
204
205 Float_t sddLength = fSegmentation->Dx();
206 Float_t sddWidth = fSegmentation->Dz();
207
208 Int_t dummy=0;
209 Float_t anodePitch = fSegmentation->Dpz(dummy);
210 Double_t timeStep = (Double_t)fSegmentation->Dpx(dummy);
211 Float_t driftSpeed=fResponse->DriftSpeed();
212
213 if(anodePitch*(fNofMaps/2) > sddWidth) {
214 Warning("AliITSsimulationSDD",
ece86d9a 215 "Too many anodes %d or too big pitch %f \n",fNofMaps/2,anodePitch);
b0f5e3fc 216 }
217
218 if(timeStep*fMaxNofSamples < sddLength/driftSpeed) {
219 Error("AliITSsimulationSDD",
ece86d9a 220 "Time Interval > Allowed Time Interval: exit\n");
b0f5e3fc 221 return;
222 }
223
e8189707 224 fElectronics = new AliITSetfSDD(timeStep/fScaleSize);
b0f5e3fc 225
e8189707 226 char opt1[20], opt2[20];
b0f5e3fc 227 fResponse->ParamOptions(opt1,opt2);
228 fParam=opt2;
229 char *same = strstr(opt1,"same");
230 if (same) {
231 fNoise.Set(0);
232 fBaseline.Set(0);
233 } else {
234 fNoise.Set(fNofMaps);
235 fBaseline.Set(fNofMaps);
236 }
237
238 //
e8189707 239 const char *kopt=fResponse->ZeroSuppOption();
b0f5e3fc 240 if (strstr(fParam,"file") ) {
241 fD.Set(fNofMaps);
242 fT1.Set(fNofMaps);
e8189707 243 if (strstr(kopt,"2D")) {
b0f5e3fc 244 fT2.Set(fNofMaps);
245 fTol.Set(0);
246 Init2D(); // desactivate if param change module by module
e8189707 247 } else if(strstr(kopt,"1D")) {
b0f5e3fc 248 fT2.Set(2);
249 fTol.Set(2);
250 Init1D(); // desactivate if param change module by module
251 }
252 } else {
253 fD.Set(2);
254 fTol.Set(2);
255 fT1.Set(2);
256 fT2.Set(2);
257 SetCompressParam();
258 }
259
260
261 Bool_t write=fResponse->OutputOption();
e8189707 262 if(write && strstr(kopt,"2D")) MakeTreeB();
b0f5e3fc 263
264 // call here if baseline does not change by module
265 // ReadBaseline();
266
267 fITS = (AliITS*)gAlice->GetModule("ITS");
268 Int_t size=fNofMaps*fMaxNofSamples;
269 fStream = new AliITSInStream(size);
270
e8189707 271 fInZR = new Double_t [fScaleSize*fMaxNofSamples];
272 fInZI = new Double_t [fScaleSize*fMaxNofSamples];
273 fOutZR = new Double_t [fScaleSize*fMaxNofSamples];
274 fOutZI = new Double_t [fScaleSize*fMaxNofSamples];
5d18fa90 275
b0f5e3fc 276}
277
278
279//_____________________________________________________________________________
280
281AliITSsimulationSDD::~AliITSsimulationSDD() {
282 // destructor
283
284 delete fHitMap1;
285 delete fHitMap2;
286 delete fStream;
ece86d9a 287 delete fElectronics;
b0f5e3fc 288
289 fD.Set(0);
290 fT1.Set(0);
291 fT2.Set(0);
292 fTol.Set(0);
293 fNoise.Set(0);
294 fBaseline.Set(0);
ece86d9a 295 fITS = 0;
b0f5e3fc 296
297 if (fHis) {
298 fHis->Delete();
299 delete fHis;
ece86d9a 300 }
301 if(fTreeB) delete fTreeB;
e8189707 302 if(fInZR) delete [] fInZR;
303 if(fInZI) delete [] fInZI;
304 if(fOutZR) delete [] fOutZR;
305 if(fOutZI) delete [] fOutZI;
b0f5e3fc 306}
307//_____________________________________________________________________________
308
309void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
310 // create maps to build the lists of tracks
311 // for each digit
312
313 fModule=md;
314 fEvent=ev;
315
316 TObjArray *fHits = mod->GetHits();
317 Int_t nhits = fHits->GetEntriesFast();
ece86d9a 318 if (!nhits && fCheckNoise) {
319 ChargeToSignal();
320 GetNoise();
321 fHitMap2->ClearMap();
322 return;
323 } else if (!nhits) return;
324
325 //printf("simSDD: module nhits %d %d\n",md,nhits);
b0f5e3fc 326
327
328 TObjArray *list=new TObjArray;
329 static TClonesArray *padr=0;
330 if(!padr) padr=new TClonesArray("TVector",1000);
e8189707 331 Int_t arg[6] = {0,0,0,0,0,0};
b0f5e3fc 332 fHitMap1->SetArray(list);
333
334
e8189707 335 Int_t nofAnodes=fNofMaps/2;
b0f5e3fc 336
337 Float_t sddLength = fSegmentation->Dx();
338 Float_t sddWidth = fSegmentation->Dz();
339
340 Int_t dummy=0;
341 Float_t anodePitch = fSegmentation->Dpz(dummy);
342 Float_t timeStep = fSegmentation->Dpx(dummy);
343
344 Float_t driftSpeed=fResponse->DriftSpeed();
345
346 // Piergiorgio's part (apart for few variables which I made float
347 // when i thought that can be done
348
349 // Fill detector maps with GEANT hits
350 // loop over hits in the module
351
e8189707 352 const Float_t kconv=1.0e+6; // GeV->KeV
b0f5e3fc 353 Int_t ii;
ece86d9a 354 Int_t idhit=-1;
b0f5e3fc 355 for(ii=0; ii<nhits; ii++) {
356 AliITShit *hit = (AliITShit*) fHits->At(ii);
b0f5e3fc 357 Float_t xL[3];
e8189707 358 hit = (AliITShit*) fHits->At(ii);
b0f5e3fc 359 hit->GetPositionL(xL[0],xL[1],xL[2]);
e8189707 360 Int_t hitDetector = hit->GetDetector();
361
ece86d9a 362 if(hit->StatusEntering()) idhit=ii;
4383d7f8 363
364 Int_t nOfSplits = 5;
365 if(fFlag) nOfSplits = 1;
b0f5e3fc 366 // Deposited energy in keV
367 Float_t avpath = 0.;
368 Float_t avanod = 0.;
4383d7f8 369 Float_t depEnergy = kconv*hit->GetIonization()/nOfSplits;
b0f5e3fc 370 AliITShit *hit1 = 0;
e8189707 371 Float_t xL1[3];
4383d7f8 372 if(fFlag && depEnergy != 0.) continue;
373 if(depEnergy == 0.) {
374 ii++;
375 hit1 = (AliITShit*) fHits->At(ii);
376 hit1->GetPositionL(xL1[0],xL1[1],xL1[2]);
377 } else {
378 xL1[0] = xL[0];
379 xL1[1] = xL[1];
380 xL1[2] = xL[2];
381 }
382
383 // scale path to simulate a perpendicular track
384
385 if(depEnergy == 0.) depEnergy = kconv*hit1->GetIonization()/nOfSplits;
386 if (fFlag) {
387 Float_t pathInSDD = TMath::Sqrt((xL[0]-xL1[0])*(xL[0]-xL1[0])+(xL[1]-xL1[1])*(xL[1]-xL1[1])+(xL[2]-xL1[2])*(xL[2]-xL1[2]));
388 depEnergy *= (0.03/pathInSDD);
389 }
390
391 for(Int_t kk=0;kk<nOfSplits;kk++) {
392 Float_t avDrft = xL[0]+(xL1[0]-xL[0])*((kk+0.5)/((Float_t) nOfSplits));
393 Float_t avAnode = xL[2]+(xL1[2]-xL[2])*((kk+0.5)/((Float_t) nOfSplits));
394 Float_t driftPath = 10000.*avDrft;
395
396 avpath = xL1[0];
e8189707 397 avanod = xL1[2];
398 depEnergy = kconv*hit1->GetIonization();
399
ece86d9a 400 // added 11.09.2000 - continue if the particle did not lose energy
401 // passing through detector
402 if (!depEnergy) {
403 printf("This particle has passed without losing energy!\n");
404 continue;
405 }
406 // end add
407
b0f5e3fc 408 Int_t iWing = 2;
409 if(driftPath < 0) {
410 iWing = 1;
411 driftPath = -driftPath;
412 }
413 driftPath = sddLength-driftPath;
414 Int_t detector = 2*(hitDetector-1) + iWing;
415 if(driftPath < 0) {
416 cout << "Warning: negative drift path " << driftPath << endl;
417 continue;
418 }
419
420 // Drift Time
421 Float_t driftTime = driftPath/driftSpeed;
e8189707 422 Int_t timeSample = (Int_t) (fScaleSize*driftTime/timeStep + 1);
423 if(timeSample > fScaleSize*fMaxNofSamples) {
b0f5e3fc 424 cout << "Warning: Wrong Time Sample: " << timeSample << endl;
425 continue;
426 }
427
428 // Anode
e8189707 429 Float_t xAnode = 10000.*(avAnode)/anodePitch + nofAnodes/2; // +1?
430 if((xAnode+1)*anodePitch > sddWidth || xAnode*anodePitch < 0.)
b0f5e3fc 431 { cout << "Warning: Z = " << xAnode*anodePitch << endl; }
432 Int_t iAnode = (Int_t) (1.+xAnode); // xAnode?
e8189707 433 if(iAnode < 0 || iAnode > nofAnodes) {
b0f5e3fc 434 cout << "Warning: Wrong iAnode: " << iAnode << endl;
435 continue;
436 }
437
438
e8189707 439 // work with the idtrack=entry number in the TreeH for the moment
ece86d9a 440 //Int_t idhit,idtrack;
441 //mod->GetHitTrackAndHitIndex(ii,idtrack,idhit);
e8189707 442 //Int_t idtrack=mod->GetHitTrackIndex(ii);
b0f5e3fc 443 // or store straight away the particle position in the array
ece86d9a 444 // of particles and take idhit=ii only when part is entering (this
445 // requires FillModules() in the macro for analysis) :
e8189707 446 Int_t itrack = hit->GetTrack();
b0f5e3fc 447
448 // Signal 2d Shape
e8189707 449 Float_t diffCoeff, s0;
450 fResponse->DiffCoeff(diffCoeff,s0);
b0f5e3fc 451
b0f5e3fc 452 // Squared Sigma along the anodes
e8189707 453 Double_t sigma2A = 2.*diffCoeff*driftTime+s0*s0;
454 Double_t sigmaA = TMath::Sqrt(sigma2A);
455 Double_t sigmaT = sigmaA/driftSpeed;
b0f5e3fc 456
457 // Peak amplitude in nanoAmpere
458 Double_t eVpairs = 3.6;
e8189707 459 Double_t amplitude = fScaleSize*160.*depEnergy/(timeStep*eVpairs*2.*acos(-1.)*sigmaT*sigmaA);
b0f5e3fc 460
e8189707 461 Float_t nsigma=fResponse->NSigmaIntegration();
b0f5e3fc 462 // Spread the charge
463 // Pixel index
464 Int_t ja = iAnode;
465 Int_t jt = timeSample;
466 // Sub-pixel index
e8189707 467 Int_t nsplit = 4; // hard-wired
b0f5e3fc 468 nsplit = (nsplit+1)/2*2;
469 // Sub-pixel size
e8189707 470 Double_t aStep = anodePitch/(nsplit*fScaleSize);
471 Double_t tStep = timeStep/(nsplit*fScaleSize);
b0f5e3fc 472 // Define SDD window corresponding to the hit
e8189707 473 Int_t anodeWindow = (Int_t) (fScaleSize*nsigma*sigmaA/anodePitch + 1);
474 Int_t timeWindow = (Int_t) (fScaleSize*nsigma*sigmaT/timeStep + 1);
475 Int_t jamin = (ja - anodeWindow/2 - 1)*fScaleSize*nsplit + 1;
476 Int_t jamax = (ja + anodeWindow/2)*fScaleSize*nsplit;
b0f5e3fc 477 if(jamin <= 0) jamin = 1;
e8189707 478 if(jamax > fScaleSize*nofAnodes*nsplit) jamax = fScaleSize*nofAnodes*nsplit;
479 Int_t jtmin = (jt - timeWindow*3 - 1)*nsplit + 1; //hard-wired
480 Int_t jtmax = (jt + timeWindow*3)*nsplit; //hard-wired
b0f5e3fc 481 if(jtmin <= 0) jtmin = 1;
e8189707 482 if(jtmax > fScaleSize*fMaxNofSamples*nsplit) jtmax = fScaleSize*fMaxNofSamples*nsplit;
483
b0f5e3fc 484 Double_t rlAnode = log(aStep*amplitude);
e8189707 485
b0f5e3fc 486 // Spread the charge in the anode-time window
487 Int_t ka;
488 for(ka=jamin; ka <=jamax; ka++) {
e8189707 489 Int_t ia = (ka-1)/(fScaleSize*nsplit) + 1;
b0f5e3fc 490 if(ia <= 0) { cout << "Warning: ia < 1: " << endl; continue; }
e8189707 491 if(ia > nofAnodes) ia = nofAnodes;
492 Double_t aExpo = (aStep*(ka-0.5)-xAnode*anodePitch)/sigmaA;
493 Double_t anodeAmplitude = rlAnode - 0.5*aExpo*aExpo;
b0f5e3fc 494 // Protect against overflows
495 if(anodeAmplitude > -87.3)
496 anodeAmplitude = exp(anodeAmplitude);
497 else
498 anodeAmplitude = 0;
ece86d9a 499 Int_t index = ((detector+1)%2)*nofAnodes+ia-1; // index starts from 0
b0f5e3fc 500 if(anodeAmplitude) {
501 Double_t rlTime = log(tStep*anodeAmplitude);
502 Int_t kt;
503 for(kt=jtmin; kt<=jtmax; kt++) {
e8189707 504 Int_t it = (kt-1)/nsplit+1; // it starts from 1
b0f5e3fc 505 if(it<=0) { cout << "Warning: it < 1: " << endl; continue; }
e8189707 506 if(it>fScaleSize*fMaxNofSamples) it = fScaleSize*fMaxNofSamples;
507 Double_t tExpo = (tStep*(kt-0.5)-driftTime)/sigmaT;
b0f5e3fc 508 Double_t timeAmplitude = rlTime - 0.5*tExpo*tExpo;
509 // Protect against overflows
e8189707 510 if(timeAmplitude > -87.3){
b0f5e3fc 511 timeAmplitude = exp(timeAmplitude);
e8189707 512 } else
b0f5e3fc 513 timeAmplitude = 0;
514
b0f5e3fc 515 // build the list of digits for this module
516 arg[0]=index;
517 arg[1]=it;
e8189707 518 arg[2]=itrack;
519 arg[3]=idhit;
b0f5e3fc 520 ListOfFiredCells(arg,timeAmplitude,list,padr);
521 } // loop over time in window
522 } // end if anodeAmplitude
523 } // loop over anodes in window
4383d7f8 524 } // end loop over "sub-hits"
b0f5e3fc 525 } // end loop over hits
526
b0f5e3fc 527 // introduce the electronics effects and do zero-suppression if required
e8189707 528 Int_t nentries=list->GetEntriesFast();
b0f5e3fc 529 if (nentries) {
ece86d9a 530
531 //TStopwatch timer;
b0f5e3fc 532 ChargeToSignal();
ece86d9a 533 //timer.Stop(); timer.Print();
b0f5e3fc 534
e8189707 535 const char *kopt=fResponse->ZeroSuppOption();
536 ZeroSuppression(kopt);
b0f5e3fc 537 }
538
539 // clean memory
540 list->Delete();
541 delete list;
542
543 padr->Delete();
544
545 fHitMap1->ClearMap();
546 fHitMap2->ClearMap();
547
548 //gObjectTable->Print();
549}
550
551
552//____________________________________________
553
554void AliITSsimulationSDD::ListOfFiredCells(Int_t *arg,Double_t timeAmplitude,
555 TObjArray *list,TClonesArray *padr){
556 // Returns the list of "fired" cells.
557
558 Int_t index=arg[0];
e8189707 559 Int_t ik=arg[1];
b0f5e3fc 560 Int_t idtrack=arg[2];
e8189707 561 Int_t idhit=arg[3];
562 Int_t counter=arg[4];
563 Int_t countadr=arg[5];
b0f5e3fc 564
e8189707 565 Double_t charge=timeAmplitude;
566 charge += fHitMap2->GetSignal(index,ik-1);
567 fHitMap2->SetHit(index, ik-1, charge);
b0f5e3fc 568
e8189707 569 Int_t digits[3];
570 Int_t it=(Int_t)((ik-1)/fScaleSize);
571
b0f5e3fc 572 digits[0]=index;
e8189707 573 digits[1]=it;
b0f5e3fc 574 digits[2]=(Int_t)timeAmplitude;
575 Float_t phys;
576 if (idtrack >= 0) phys=(Float_t)timeAmplitude;
577 else phys=0;
578
e8189707 579 Double_t cellcharge=0.;
b0f5e3fc 580 AliITSTransientDigit* pdigit;
581 // build the list of fired cells and update the info
e8189707 582 if (!fHitMap1->TestHit(index, it)) {
b0f5e3fc 583
e8189707 584 new((*padr)[countadr++]) TVector(3);
b0f5e3fc 585 TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
586 trinfo(0)=(Float_t)idtrack;
e8189707 587 trinfo(1)=(Float_t)idhit;
588 trinfo(2)=(Float_t)timeAmplitude;
b0f5e3fc 589
590 list->AddAtAndExpand(
591 new AliITSTransientDigit(phys,digits),counter);
592
e8189707 593 fHitMap1->SetHit(index, it, counter);
b0f5e3fc 594 counter++;
b0f5e3fc 595 pdigit=(AliITSTransientDigit*)list->
596 At(list->GetLast());
597 // list of tracks
598 TObjArray *trlist=(TObjArray*)pdigit->TrackList();
599 trlist->Add(&trinfo);
600
601 } else {
602 pdigit=
e8189707 603 (AliITSTransientDigit*) fHitMap1->GetHit(index, it);
604 for(Int_t kk=0;kk<fScaleSize;kk++) {
605 cellcharge += fHitMap2->GetSignal(index,fScaleSize*it+kk);
606 }
b0f5e3fc 607 // update charge
e8189707 608 (*pdigit).fSignal=(Int_t)cellcharge;
b0f5e3fc 609 (*pdigit).fPhysics+=phys;
610 // update list of tracks
611 TObjArray* trlist=(TObjArray*)pdigit->TrackList();
612 Int_t lastentry=trlist->GetLast();
613 TVector *ptrkp=(TVector*)trlist->At(lastentry);
614 TVector &trinfo=*ptrkp;
615 Int_t lasttrack=Int_t(trinfo(0));
ece86d9a 616 //Int_t lasthit=Int_t(trinfo(1));
e8189707 617 Float_t lastcharge=(trinfo(2));
b0f5e3fc 618
619 if (lasttrack==idtrack ) {
620 lastcharge+=(Float_t)timeAmplitude;
621 trlist->RemoveAt(lastentry);
622 trinfo(0)=lasttrack;
ece86d9a 623 //trinfo(1)=lasthit; // or idhit
624 trinfo(1)=idhit;
e8189707 625 trinfo(2)=lastcharge;
b0f5e3fc 626 trlist->AddAt(&trinfo,lastentry);
627 } else {
628
e8189707 629 new((*padr)[countadr++]) TVector(3);
b0f5e3fc 630 TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
631 trinfo(0)=(Float_t)idtrack;
e8189707 632 trinfo(1)=(Float_t)idhit;
633 trinfo(2)=(Float_t)timeAmplitude;
b0f5e3fc 634
635 trlist->Add(&trinfo);
636 }
637
638#ifdef print
639 // check the track list - debugging
e8189707 640 Int_t trk[20], htrk[20];
641 Float_t chtrk[20];
b0f5e3fc 642 Int_t nptracks=trlist->GetEntriesFast();
643 if (nptracks > 2) {
644 Int_t tr;
e8189707 645 for (tr=0;tr<nptracks;tr++) {
b0f5e3fc 646 TVector *pptrkp=(TVector*)trlist->At(tr);
647 TVector &pptrk=*pptrkp;
648 trk[tr]=Int_t(pptrk(0));
e8189707 649 htrk[tr]=Int_t(pptrk(1));
650 chtrk[tr]=(pptrk(2));
b0f5e3fc 651 printf("nptracks %d \n",nptracks);
652 // set printings
653 }
654 } // end if nptracks
655#endif
656 } // end if pdigit
657
e8189707 658 arg[4]=counter;
659 arg[5]=countadr;
b0f5e3fc 660
661
662}
663
664
665//____________________________________________
666
667void AliITSsimulationSDD::AddDigit(Int_t i, Int_t j, Int_t signal){
668 // Adds a Digit.
669 // tag with -1 signals coming from background tracks
670 // tag with -2 signals coming from pure electronic noise
671
e8189707 672 Int_t digits[3], tracks[3], hits[3];
b0f5e3fc 673 Float_t phys, charges[3];
674
e8189707 675 Int_t trk[20], htrk[20];
b0f5e3fc 676 Float_t chtrk[20];
677
ece86d9a 678 Bool_t do10to8=fResponse->Do10to8();
679
680 if(do10to8) signal=Convert8to10(signal);
b0f5e3fc 681 AliITSTransientDigit *obj = (AliITSTransientDigit*)fHitMap1->GetHit(i,j);
682 digits[0]=i;
683 digits[1]=j;
684 digits[2]=signal;
b0f5e3fc 685 if (!obj) {
686 phys=0;
687 Int_t k;
e8189707 688 for (k=0;k<3;k++) {
b0f5e3fc 689 tracks[k]=-2;
690 charges[k]=0;
ece86d9a 691 hits[k]=-1;
b0f5e3fc 692 }
e8189707 693 fITS->AddSimDigit(1,phys,digits,tracks,hits,charges);
b0f5e3fc 694 } else {
695 phys=obj->fPhysics;
b0f5e3fc 696 TObjArray* trlist=(TObjArray*)obj->TrackList();
697 Int_t nptracks=trlist->GetEntriesFast();
698
699 if (nptracks > 20) {
700 cout<<"Attention - nptracks > 20 "<<nptracks<<endl;
701 nptracks=20;
702 }
703 Int_t tr;
e8189707 704 for (tr=0;tr<nptracks;tr++) {
b0f5e3fc 705 TVector &pp =*((TVector*)trlist->At(tr));
706 trk[tr]=Int_t(pp(0));
e8189707 707 htrk[tr]=Int_t(pp(1));
708 chtrk[tr]=(pp(2));
b0f5e3fc 709 }
710 if (nptracks > 1) {
ece86d9a 711 //printf("nptracks > 2 -- %d\n",nptracks);
e8189707 712 SortTracks(trk,chtrk,htrk,nptracks);
b0f5e3fc 713 }
714 Int_t i;
715 if (nptracks < 3 ) {
e8189707 716 for (i=0; i<nptracks; i++) {
b0f5e3fc 717 tracks[i]=trk[i];
718 charges[i]=chtrk[i];
e8189707 719 hits[i]=htrk[i];
b0f5e3fc 720 }
e8189707 721 for (i=nptracks; i<3; i++) {
ece86d9a 722 tracks[i]=-3;
723 hits[i]=-1;
b0f5e3fc 724 charges[i]=0;
725 }
726 } else {
e8189707 727 for (i=0; i<3; i++) {
b0f5e3fc 728 tracks[i]=trk[i];
729 charges[i]=chtrk[i];
e8189707 730 hits[i]=htrk[i];
b0f5e3fc 731 }
732 }
733
e8189707 734 fITS->AddSimDigit(1,phys,digits,tracks,hits,charges);
b0f5e3fc 735
736 }
737
738}
739
740//____________________________________________
741
e8189707 742void AliITSsimulationSDD::SortTracks(Int_t *tracks,Float_t *charges,Int_t *hits,Int_t ntr){
b0f5e3fc 743 //
744 // Sort the list of tracks contributing to a given digit
745 // Only the 3 most significant tracks are acctually sorted
746 //
747
748 //
749 // Loop over signals, only 3 times
750 //
751
752
753 Float_t qmax;
754 Int_t jmax;
755 Int_t idx[3] = {-3,-3,-3};
756 Float_t jch[3] = {-3,-3,-3};
757 Int_t jtr[3] = {-3,-3,-3};
e8189707 758 Int_t jhit[3] = {-3,-3,-3};
b0f5e3fc 759 Int_t i,j,imax;
760
761 if (ntr<3) imax=ntr;
762 else imax=3;
763 for(i=0;i<imax;i++){
764 qmax=0;
765 jmax=0;
766
767 for(j=0;j<ntr;j++){
768
769 if((i == 1 && j == idx[i-1] )
770 ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
771
772 if(charges[j] > qmax) {
773 qmax = charges[j];
774 jmax=j;
775 }
776 }
777
778 if(qmax > 0) {
779 idx[i]=jmax;
780 jch[i]=charges[jmax];
781 jtr[i]=tracks[jmax];
e8189707 782 jhit[i]=hits[jmax];
b0f5e3fc 783 }
784
785 }
786
787 for(i=0;i<3;i++){
788 if (jtr[i] == -3) {
789 charges[i]=0;
e8189707 790 tracks[i]=-3;
791 hits[i]=-1;
b0f5e3fc 792 } else {
793 charges[i]=jch[i];
794 tracks[i]=jtr[i];
ece86d9a 795 hits[i]=jhit[i];
b0f5e3fc 796 }
797 }
798
799}
800//____________________________________________
801void AliITSsimulationSDD::ChargeToSignal() {
802 // add baseline, noise, electronics and ADC saturation effects
803
b0f5e3fc 804
805 Float_t maxadc = fResponse->MaxAdc();
e8189707 806 Float_t topValue = fResponse->MagicValue();
807 Float_t norm = maxadc/topValue;
b0f5e3fc 808
e8189707 809 char opt1[20], opt2[20];
b0f5e3fc 810 fResponse->ParamOptions(opt1,opt2);
811 char *read = strstr(opt1,"file");
812
813 Float_t baseline, noise;
814
815 if (read) {
816 static Bool_t readfile=kTRUE;
817 //read baseline and noise from file
818 if (readfile) ReadBaseline();
819 readfile=kFALSE;
820 } else fResponse->GetNoiseParam(noise,baseline);
821
822 Float_t contrib=0;
b0f5e3fc 823
ece86d9a 824 TRandom random;
e8189707 825 Int_t i,k,kk;
ece86d9a 826
827 if(!fDoFFT) {
828 for (i=0;i<fNofMaps;i++) {
829 if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
830 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
831 fInZR[k] = fHitMap2->GetSignal(i,k);
832 contrib = baseline + noise*random.Gaus();
833 fInZR[k] += contrib;
e8189707 834 }
ece86d9a 835 for(k=0; k<fMaxNofSamples; k++) {
836 Float_t newcont = 0.;
837 Float_t maxcont = 0.;
838 for(kk=0;kk<fScaleSize;kk++) {
839 newcont = fInZR[fScaleSize*k+kk];
840 if(newcont > maxcont) maxcont = newcont;
841 }
842 newcont = maxcont;
843 Double_t signal = newcont*norm;
844 if (signal >= maxadc) signal = maxadc -1;
845 // back to analog: ?
846 signal /=norm;
847 fHitMap2->SetHit(i,k,signal);
848 }
849 } // loop over anodes
850 return;
851 } // end if DoFFT
852
853 for (i=0;i<fNofMaps;i++) {
854 if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
855 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
856 fInZR[k] = fHitMap2->GetSignal(i,k);
857 contrib = baseline + noise*random.Gaus();
858 fInZR[k] += contrib;
859 fInZI[k] = 0.;
860 }
e8189707 861 FastFourierTransform(fElectronics,&fInZR[0],&fInZI[0],1);
ece86d9a 862 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
863 Double_t rw = fElectronics->GetTraFunReal(k);
e8189707 864 Double_t iw = fElectronics->GetTraFunImag(k);
ece86d9a 865 fOutZR[k] = fInZR[k]*rw - fInZI[k]*iw;
866 fOutZI[k] = fInZR[k]*iw + fInZI[k]*rw;
e8189707 867 }
ece86d9a 868 FastFourierTransform(fElectronics,&fOutZR[0],&fOutZI[0],-1);
869 for(k=0; k<fMaxNofSamples; k++) {
870 Float_t newcont = 0.;
871 //Float_t totcont = 0.;
872 Float_t maxcont = 0.;
873 for(kk=0;kk<fScaleSize;kk++) {
874 newcont = fOutZR[fScaleSize*k+kk];
875 if(newcont > maxcont) maxcont = newcont;
876 // totcont += (0.25*Out_ZR[4*k+kk]);
877 }
878 newcont = maxcont;
879 Double_t signal = newcont*norm;
880 if (signal >= maxadc) signal = maxadc -1;
881 // back to analog: ?
882 // comment the line below because you want to keep the signal in ADCs
883 // convert back to nA in cluster finder
884 signal /=norm;
885 fHitMap2->SetHit(i,k,signal);
886 }
b0f5e3fc 887 } // loop over anodes
ece86d9a 888 return;
889
b0f5e3fc 890}
891
892//____________________________________________
893void AliITSsimulationSDD::GetAnodeBaseline(Int_t i,Float_t &baseline,
894 Float_t &noise){
895 // Returns the Baseline for a particular anode.
896 baseline=fBaseline[i];
897 noise=fNoise[i];
898
899}
900
901//____________________________________________
902void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl,
903 Int_t &th){
904 // Returns the compression alogirthm parameters
905 Int_t size = fD.GetSize();
906 if (size > 2 ) {
907 db=fD[i]; tl=fT1[i]; th=fT2[i];
908 } else {
909 if (size <= 2 && i>=fNofMaps/2) {
910 db=fD[1]; tl=fT1[1]; th=fT2[1];
911 } else {
912 db=fD[0]; tl=fT1[0]; th=fT2[0];
913 }
914 }
915}
916//____________________________________________
917void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl){
918 // returns the compression alogirthm parameters
919 Int_t size = fD.GetSize();
920 if (size > 2 ) {
921 db=fD[i]; tl=fT1[i];
922 } else {
923 if (size <= 2 && i>=fNofMaps/2) {
924 db=fD[1]; tl=fT1[1];
925 } else {
926 db=fD[0]; tl=fT1[0];
927 }
928 }
929
930}
931//____________________________________________
932void AliITSsimulationSDD::SetCompressParam(){
933 // Sets the compression alogirthm parameters
934 Int_t cp[8],i;
935
936 fResponse->GiveCompressParam(cp);
e8189707 937 for (i=0; i<2; i++) {
b0f5e3fc 938 fD[i] =cp[i];
939 fT1[i] =cp[i+2];
940 fT2[i] =cp[i+4];
941 fTol[i]=cp[i+6];
b0f5e3fc 942 printf("\n i, fD, fT1, fT2, fTol %d %d %d %d %d\n",
943 i,fD[i],fT1[i],fT2[i],fTol[i]);
b0f5e3fc 944 }
945}
946
947//____________________________________________
948void AliITSsimulationSDD::ReadBaseline(){
949 // read baseline and noise from file - either a .root file and in this
950 // case data should be organised in a tree with one entry for each
951 // module => reading should be done accordingly
952 // or a classic file and do smth. like this:
953 //
954 // Read baselines and noise for SDD
955 //
956
957
958 Int_t na,pos;
959 Float_t bl,n;
e8189707 960 char input[100], base[100], param[100];
b0f5e3fc 961 char *filtmp;
962
e8189707 963 fResponse->Filenames(input,base,param);
b0f5e3fc 964 fFileName=base;
965//
966 filtmp = gSystem->ExpandPathName(fFileName.Data());
967 FILE *bline = fopen(filtmp,"r");
968 printf("filtmp %s\n",filtmp);
969 na = 0;
970
971 if(bline) {
972 while(fscanf(bline,"%d %f %f",&pos, &bl, &n) != EOF) {
b0f5e3fc 973 if (pos != na+1) {
974 Error("ReadBaseline","Anode number not in increasing order!",
975 filtmp);
976 exit(1);
977 }
978 fBaseline[na]=bl;
979 fNoise[na]=n;
980 na++;
981 }
982 } else {
983 Error("ReadBaseline"," THE BASELINE FILE %s DOES NOT EXIST !",
984 filtmp);
985 exit(1);
986 } // end if(bline)
987
988 fclose(bline);
989 delete [] filtmp;
b0f5e3fc 990}
991
992//____________________________________________
993Int_t AliITSsimulationSDD::Convert10to8(Int_t signal) {
994 // To the 10 to 8 bit lossive compression.
995 // code from Davide C. and Albert W.
996
997 if (signal < 128) return signal;
998 if (signal < 256) return (128+((signal-128)>>1));
999 if (signal < 512) return (192+((signal-256)>>3));
1000 if (signal < 1024) return (224+((signal-512)>>4));
1001 return 0;
1002
1003}
1004
1005//____________________________________________
1006Int_t AliITSsimulationSDD::Convert8to10(Int_t signal) {
1007 // Undo the lossive 10 to 8 bit compression.
1008 // code from Davide C. and Albert W.
1009 if (signal < 0 || signal > 255) {
1010 printf("<Convert8to10> out of range %d \n",signal);
1011 return 0;
1012 }
1013
1014 if (signal < 128) return signal;
1015 if (signal < 192) {
1016 if (TMath::Odd(signal)) return (128+((signal-128)<<1));
1017 else return (128+((signal-128)<<1)+1);
1018 }
1019 if (signal < 224) {
1020 if (TMath::Odd(signal)) return (256+((signal-192)<<3)+3);
1021 else return (256+((signal-192)<<3)+4);
1022 }
1023 if (TMath::Odd(signal)) return (512+((signal-224)<<4)+7);
1024 else return (512+((signal-224)<<4)+7);
1025 return 0;
1026
1027}
1028
1029//____________________________________________
1030AliITSMap* AliITSsimulationSDD::HitMap(Int_t i){
1031 //Return the correct map.
1032 return ((i==0)? fHitMap1 : fHitMap2);
1033}
1034
1035
1036//____________________________________________
e8189707 1037void AliITSsimulationSDD::ZeroSuppression(const char *option) {
b0f5e3fc 1038 // perform the zero suppresion
1039 if (strstr(option,"2D")) {
1040 //Init2D(); // activate if param change module by module
1041 Compress2D();
1042 } else if (strstr(option,"1D")) {
1043 //Init1D(); // activate if param change module by module
1044 Compress1D();
1045 } else StoreAllDigits();
1046
1047}
1048
1049//____________________________________________
1050void AliITSsimulationSDD::Init2D(){
1051 // read in and prepare arrays: fD, fT1, fT2
1052 // savemu[nanodes], savesigma[nanodes]
1053 // read baseline and noise from file - either a .root file and in this
1054 // case data should be organised in a tree with one entry for each
1055 // module => reading should be done accordingly
1056 // or a classic file and do smth. like this ( code from Davide C. and
1057 // Albert W.) :
1058 //
1059 // Read 2D zero-suppression parameters for SDD
1060 //
1061
1062 if (!strstr(fParam,"file")) return;
1063
1064 Int_t na,pos,tempTh;
1065 Float_t mu,sigma;
5d18fa90 1066 Float_t *savemu = new Float_t [fNofMaps];
e8189707 1067 Float_t *savesigma = new Float_t [fNofMaps];
1068 char input[100],basel[100],par[100];
b0f5e3fc 1069 char *filtmp;
1070
1071
1072 Int_t minval = fResponse->MinVal();
1073
e8189707 1074 fResponse->Filenames(input,basel,par);
1075 fFileName=par;
b0f5e3fc 1076
1077//
1078 filtmp = gSystem->ExpandPathName(fFileName.Data());
1079 FILE *param = fopen(filtmp,"r");
1080 na = 0;
1081
1082 if(param) {
1083 while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
1084 if (pos != na+1) {
1085 Error("Init2D ","Anode number not in increasing order!",
1086 filtmp);
1087 exit(1);
1088 }
1089 savemu[na]=mu;
1090 savesigma[na]=sigma;
1091 if ((2.*sigma) < mu) {
1092 fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
1093 mu = 2.0 * sigma;
1094 } else fD[na] = 0;
1095 tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
1096 if (tempTh < 0) tempTh=0;
1097 fT1[na] = tempTh;
1098 tempTh = (Int_t)floor(mu+3.0*sigma+0.5) - minval;
1099 if (tempTh < 0) tempTh=0;
1100 fT2[na] = tempTh;
1101 na++;
1102 } // end while
1103
1104 } else {
1105 Error("Init2D "," THE FILE %s DOES NOT EXIST !",
1106 filtmp);
1107 exit(1);
1108 } // end if(param)
1109
1110 fclose(param);
1111 delete [] filtmp;
5d18fa90 1112 delete [] savemu;
e8189707 1113 delete [] savesigma;
b0f5e3fc 1114}
e8189707 1115
b0f5e3fc 1116//____________________________________________
1117void AliITSsimulationSDD::Compress2D(){
1118 //
1119 // simple ITS cluster finder -- online zero-suppression conditions
1120 //
1121 //
1122
b0f5e3fc 1123 Int_t db,tl,th;
1124 Int_t minval = fResponse->MinVal();
1125 Bool_t write=fResponse->OutputOption();
ece86d9a 1126 Bool_t do10to8=fResponse->Do10to8();
b0f5e3fc 1127
1128 Int_t nz, nl, nh, low, i, j;
1129
e8189707 1130 for (i=0; i<fNofMaps; i++) {
b0f5e3fc 1131 CompressionParam(i,db,tl,th);
1132 nz=0;
1133 nl=0;
1134 nh=0;
1135 low=0;
e8189707 1136 for (j=0; j<fMaxNofSamples; j++) {
b0f5e3fc 1137 Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
1138 signal -= db; // if baseline eq. is done here
1139 if (signal <= 0) {nz++; continue;}
1140 if ((signal - tl) < minval) low++;
1141 if ((signal - th) >= minval) {
1142 nh++;
1143 Bool_t cond=kTRUE;
b0f5e3fc 1144 FindCluster(i,j,signal,minval,cond);
ece86d9a 1145 if (cond && ((TMath::Abs(fHitMap2->GetSignal(i,j-1))-th)>=minval)) {
1146 if(do10to8) signal = Convert10to8(signal);
1147 AddDigit(i,j,signal);
1148 }
b0f5e3fc 1149 } else if ((signal - tl) >= minval) nl++;
1150 } // loop time samples
1151 if (write) TreeB()->Fill(nz,nl,nh,low,i+1);
b0f5e3fc 1152 } // loop anodes
1153
1154 char hname[30];
1155 if (write) {
1156 sprintf(hname,"TNtuple%d_%d",fModule,fEvent);
1157 TreeB()->Write(hname);
1158 // reset tree
1159 TreeB()->Reset();
1160 }
1161
1162}
1163
1164//_____________________________________________________________________________
1165void AliITSsimulationSDD::FindCluster(Int_t i,Int_t j,Int_t signal,
ece86d9a 1166 Int_t minval,Bool_t &cond){
b0f5e3fc 1167//
1168// Find clusters according to the online 2D zero-suppression algorithm
1169//
1170
ece86d9a 1171 Bool_t do10to8=fResponse->Do10to8();
1172
b0f5e3fc 1173 Bool_t high=kFALSE;
1174
1175 fHitMap2->FlagHit(i,j);
1176//
1177// check the online zero-suppression conditions
1178//
e8189707 1179 const Int_t maxNeighbours = 4;
1180
b0f5e3fc 1181 Int_t nn;
1182 Int_t dbx,tlx,thx;
e8189707 1183 Int_t xList[maxNeighbours], yList[maxNeighbours];
1184 fSegmentation->Neighbours(i,j,&nn,xList,yList);
ece86d9a 1185 Int_t in,ix,iy,qns;
e8189707 1186 for (in=0; in<nn; in++) {
1187 ix=xList[in];
1188 iy=yList[in];
b0f5e3fc 1189 if (fHitMap2->TestHit(ix,iy)==kUnused) {
1190 CompressionParam(ix,dbx,tlx,thx);
1191 Int_t qn = (Int_t)(fHitMap2->GetSignal(ix,iy));
1192 qn -= dbx; // if baseline eq. is done here
1193 if ((qn-tlx) < minval) {
1194 fHitMap2->FlagHit(ix,iy);
1195 continue;
1196 } else {
1197 if ((qn - thx) >= minval) high=kTRUE;
1198 if (cond) {
ece86d9a 1199 if(do10to8) signal = Convert10to8(signal);
b0f5e3fc 1200 AddDigit(i,j,signal);
1201 }
ece86d9a 1202 if(do10to8) qns = Convert10to8(qn);
1203 else qns=qn;
b0f5e3fc 1204 if (!high) AddDigit(ix,iy,qns);
1205 cond=kFALSE;
1206 if(!high) fHitMap2->FlagHit(ix,iy);
1207 }
1208 } // TestHit
1209 } // loop over neighbours
1210
1211}
1212
1213//____________________________________________
1214void AliITSsimulationSDD::Init1D(){
1215 // this is just a copy-paste of input taken from 2D algo
1216 // Torino people should give input
1217 //
1218 // Read 1D zero-suppression parameters for SDD
1219 //
1220
1221 if (!strstr(fParam,"file")) return;
1222
1223 Int_t na,pos,tempTh;
1224 Float_t mu,sigma;
e8189707 1225 Float_t *savemu = new Float_t [fNofMaps];
1226 Float_t *savesigma = new Float_t [fNofMaps];
1227 char input[100],basel[100],par[100];
b0f5e3fc 1228 char *filtmp;
1229
1230
1231 Int_t minval = fResponse->MinVal();
e8189707 1232 fResponse->Filenames(input,basel,par);
1233 fFileName=par;
b0f5e3fc 1234
1235// set first the disable and tol param
1236 SetCompressParam();
1237//
1238 filtmp = gSystem->ExpandPathName(fFileName.Data());
1239 FILE *param = fopen(filtmp,"r");
1240 na = 0;
1241
1242 if (param) {
1243 fscanf(param,"%d %d %d %d ", &fT2[0], &fT2[1], &fTol[0], &fTol[1]);
1244 while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
1245 if (pos != na+1) {
1246 Error("Init1D ","Anode number not in increasing order!",
1247 filtmp);
1248 exit(1);
1249 }
1250 savemu[na]=mu;
1251 savesigma[na]=sigma;
1252 if ((2.*sigma) < mu) {
1253 fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
1254 mu = 2.0 * sigma;
1255 } else fD[na] = 0;
1256 tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
1257 if (tempTh < 0) tempTh=0;
1258 fT1[na] = tempTh;
1259 na++;
1260 } // end while
1261 } else {
1262 Error("Init1D "," THE FILE %s DOES NOT EXIST !",
1263 filtmp);
1264 exit(1);
1265 } // end if(param)
1266
1267 fclose(param);
1268 delete [] filtmp;
749bd21a 1269 delete [] savemu;
e8189707 1270 delete [] savesigma;
1271
1272
b0f5e3fc 1273
1274}
1275
1276//____________________________________________
1277void AliITSsimulationSDD::Compress1D(){
1278 // 1D zero-suppression algorithm (from Gianluca A.)
1279
1280 Int_t dis,tol,thres,decr,diff;
b0f5e3fc 1281
1282 UChar_t *str=fStream->Stream();
1283 Int_t counter=0;
1284
ece86d9a 1285 Bool_t do10to8=fResponse->Do10to8();
1286
e8189707 1287 Int_t last=0;
1288 Int_t k,i,j;
ece86d9a 1289 for (k=0; k<2; k++) {
1290 tol = Tolerance(k);
1291 dis = Disable(k);
e8189707 1292 for (i=0; i<fNofMaps/2; i++) {
b0f5e3fc 1293 Bool_t firstSignal=kTRUE;
ece86d9a 1294 Int_t idx=i+k*fNofMaps/2;
1295 CompressionParam(idx,decr,thres);
e8189707 1296 for (j=0; j<fMaxNofSamples; j++) {
ece86d9a 1297 Int_t signal=(Int_t)(fHitMap2->GetSignal(idx,j));
b0f5e3fc 1298 signal -= decr; // if baseline eq.
ece86d9a 1299 if(do10to8) signal = Convert10to8(signal);
1300 if (signal <= thres) {
b0f5e3fc 1301 signal=0;
1302 diff=128;
1303 last=0;
1304 // write diff in the buffer for HuffT
1305 str[counter]=(UChar_t)diff;
1306 counter++;
1307 continue;
1308 }
1309 diff=signal-last;
1310 if (diff > 127) diff=127;
1311 if (diff < -128) diff=-128;
1312
1313 if (signal < dis) {
ece86d9a 1314 // tol has changed to 8 possible cases ? - one can write
1315 // this if(TMath::Abs(diff)<tol) ... else ...
1316 if(TMath::Abs(diff)<tol) diff=0;
1317 // or keep it as it was before
1318 /*
b0f5e3fc 1319 if (tol==1 && (diff >= -2 && diff <= 1)) diff=0;
1320 if (tol==2 && (diff >= -4 && diff <= 3)) diff=0;
1321 if (tol==3 && (diff >= -16 && diff <= 15)) diff=0;
ece86d9a 1322 */
1323 AddDigit(idx,j,last+diff);
b0f5e3fc 1324 } else {
ece86d9a 1325 AddDigit(idx,j,signal);
b0f5e3fc 1326 }
1327
1328 diff += 128;
1329 // write diff in the buffer used to compute Huffman tables
1330 if (firstSignal) str[counter]=(UChar_t)signal;
1331 else str[counter]=(UChar_t)diff;
1332 counter++;
1333
1334 last=signal;
1335 firstSignal=kFALSE;
1336 } // loop time samples
1337 } // loop anodes one half of detector
1338 }
1339
1340 // check
1341 fStream->CheckCount(counter);
1342
1343 // open file and write out the stream of diff's
1344
1345 static Bool_t open=kTRUE;
e8189707 1346 static TFile *outFile;
b0f5e3fc 1347 Bool_t write = fResponse->OutputOption();
1348
1349 if (write ) {
1350 if(open) {
1351 SetFileName("stream.root");
1352 cout<<"filename "<<fFileName<<endl;
e8189707 1353 outFile=new TFile(fFileName,"recreate");
b0f5e3fc 1354 cout<<"I have opened "<<fFileName<<" file "<<endl;
1355 }
1356 open=kFALSE;
e8189707 1357 outFile->cd();
b0f5e3fc 1358 fStream->Write();
1359 } // endif write
1360
1361 fStream->ClearStream();
1362
1363 // back to galice.root file
1364
1365 TTree *fAli=gAlice->TreeK();
1366 TFile *file = 0;
1367
1368 if (fAli) file =fAli->GetCurrentFile();
1369 file->cd();
1370
1371
1372}
1373//____________________________________________
1374void AliITSsimulationSDD::StoreAllDigits(){
ece86d9a 1375 // if non-zero-suppressed data
b0f5e3fc 1376
ece86d9a 1377 Bool_t do10to8=fResponse->Do10to8();
b0f5e3fc 1378
ece86d9a 1379 Int_t i, j, digits[3];
e8189707 1380 for (i=0; i<fNofMaps; i++) {
1381 for (j=0; j<fMaxNofSamples; j++) {
b0f5e3fc 1382 Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
ece86d9a 1383 if(do10to8) signal = Convert10to8(signal);
1384 if(do10to8) signal = Convert8to10(signal);
b0f5e3fc 1385 digits[0]=i;
1386 digits[1]=j;
1387 digits[2]=signal;
ece86d9a 1388 fITS->AddRealDigit(1,digits);
b0f5e3fc 1389 }
1390 }
1391}
1392//____________________________________________
1393
ece86d9a 1394void AliITSsimulationSDD::CreateHistograms(Int_t scale){
b0f5e3fc 1395 // Creates histograms of maps for debugging
1396
1397 Int_t i;
ece86d9a 1398
1399 fHis=new TObjArray(fNofMaps);
1400 TString sddName("sdd_");
e8189707 1401 for (i=0;i<fNofMaps;i++) {
b0f5e3fc 1402 Char_t candNum[4];
1403 sprintf(candNum,"%d",i+1);
ece86d9a 1404 sddName.Append(candNum);
1405 (*fHis)[i] = new TH1F(sddName.Data(),"SDD maps",
1406 scale*fMaxNofSamples,0.,(Float_t) scale*fMaxNofSamples);
b0f5e3fc 1407 }
1408
1409}
1410//____________________________________________
ece86d9a 1411void AliITSsimulationSDD::FillHistograms(){
1412 // fill 1D histograms from map
1413 if (!fHis) return;
1414
1415 for( Int_t i=0; i<fNofMaps; i++) {
1416 TH1F *hist =(TH1F *)fHis->UncheckedAt(i);
1417 Int_t nsamples = hist->GetNbinsX();
1418 for( Int_t j=0; j<nsamples; j++) {
1419 Double_t signal=fHitMap2->GetSignal(i,j);
1420 hist->Fill((Float_t)j,signal);
1421 }
1422 }
1423}
1424
1425//____________________________________________
b0f5e3fc 1426
1427void AliITSsimulationSDD::ResetHistograms(){
1428 //
1429 // Reset histograms for this detector
1430 //
1431 Int_t i;
e8189707 1432 for (i=0;i<fNofMaps;i++ ) {
b0f5e3fc 1433 if ((*fHis)[i]) ((TH1F*)(*fHis)[i])->Reset();
1434 }
1435
1436}
1437
1438
1439//____________________________________________
1440
1441TH1F *AliITSsimulationSDD::GetAnode(Int_t wing, Int_t anode) {
1442 // Fills a histogram from a give anode.
1443 if (!fHis) return 0;
1444
1445 if(wing <=0 || wing > 2) {
1446 cout << "Wrong wing number: " << wing << endl;
1447 return NULL;
1448 }
1449 if(anode <=0 || anode > fNofMaps/2) {
1450 cout << "Wrong anode number: " << anode << endl;
1451 return NULL;
1452 }
1453
1454 Int_t index = (wing-1)*fNofMaps/2 + anode-1;
1455 return (TH1F*)((*fHis)[index]);
1456}
1457
1458//____________________________________________
1459
1460void AliITSsimulationSDD::WriteToFile(TFile *hfile) {
1461 // Writes the histograms to a file
1462 if (!fHis) return;
1463
1464 hfile->cd();
1465 Int_t i;
1466 for(i=0; i<fNofMaps; i++) (*fHis)[i]->Write(); //fAdcs[i]->Write();
1467 return;
1468}
1469//____________________________________________
ece86d9a 1470Float_t AliITSsimulationSDD::GetNoise() {
b0f5e3fc 1471 // Returns the noise value
b0f5e3fc 1472
ece86d9a 1473 //Bool_t do10to8=fResponse->Do10to8();
1474 //noise will always be in the liniar part of the signal
1475
1476 Int_t decr;
1477 Int_t threshold=fT1[0];
1478
1479 char opt1[20], opt2[20];
1480 fResponse->ParamOptions(opt1,opt2);
1481 fParam=opt2;
1482 char *same = strstr(opt1,"same");
1483 Float_t noise,baseline;
1484 if (same) {
1485 fResponse->GetNoiseParam(noise,baseline);
1486 } else {
1487 static Bool_t readfile=kTRUE;
1488 //read baseline and noise from file
1489 if (readfile) ReadBaseline();
1490 readfile=kFALSE;
1491 }
1492
1493 TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
1494 if(c2) delete c2->GetPrimitive("noisehist");
1495 if(c2) delete c2->GetPrimitive("anode");
1496 else c2=new TCanvas("c2");
1497 c2->cd();
1498 c2->SetFillColor(0);
1499
1500 TH1F *noisehist = new TH1F("noisehist","noise",100,0.,(float)2*threshold);
1501 TH1F *anode = new TH1F("anode","Anode Projection",fMaxNofSamples,0.,(float)fMaxNofSamples);
b0f5e3fc 1502 Int_t i,k;
e8189707 1503 for (i=0;i<fNofMaps;i++) {
ece86d9a 1504 CompressionParam(i,decr,threshold);
1505 if (!same) GetAnodeBaseline(i,baseline,noise);
1506 anode->Reset();
1507 for (k=0;k<fMaxNofSamples;k++) {
1508 Float_t signal=(Float_t)fHitMap2->GetSignal(i,k);
1509 //if (signal <= (float)threshold) noisehist->Fill(signal-baseline);
1510 if (signal <= (float)threshold) noisehist->Fill(signal);
1511 anode->Fill((float)k,signal);
b0f5e3fc 1512 }
ece86d9a 1513 anode->Draw();
1514 c2->Update();
b0f5e3fc 1515 }
ece86d9a 1516 TF1 *gnoise = new TF1("gnoise","gaus",0.,threshold);
b0f5e3fc 1517 noisehist->Fit("gnoise","RQ");
1518 noisehist->Draw();
ece86d9a 1519 c2->Update();
b0f5e3fc 1520 Float_t mnoise = gnoise->GetParameter(1);
1521 cout << "mnoise : " << mnoise << endl;
1522 Float_t rnoise = gnoise->GetParameter(2);
1523 cout << "rnoise : " << rnoise << endl;
1524 delete noisehist;
1525 return rnoise;
1526}