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