1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
22 #include <TStopwatch.h>
34 #include "AliITShit.h"
35 #include "AliITSdigit.h"
36 #include "AliITSmodule.h"
37 #include "AliITSMapA1.h"
38 #include "AliITSMapA2.h"
39 #include "AliITSetfSDD.h"
40 #include "AliITSRawData.h"
41 #include "AliITSHuffman.h"
42 #include "AliITSsegmentation.h"
43 #include "AliITSresponse.h"
44 #include "AliITSsimulationSDD.h"
46 ClassImp(AliITSsimulationSDD)
47 ////////////////////////////////////////////////////////////////////////
49 // Written by Piergiorgio Cerello
52 // AliITSsimulationSDD is the simulation of SDDs.
56 <img src="picts/ITS/AliITShit_Class_Diagram.gif">
59 <font size=+2 color=red>
60 <p>This show the relasionships between the ITS hit class and the rest of Aliroot.
65 //_____________________________________________________________________________
67 Int_t power(Int_t b, Int_t e) {
68 // compute b to the e power, where both b and e are Int_ts.
70 for(i=0; i<e; i++) power *= b;
74 //_____________________________________________
76 void FastFourierTransform(AliITSetfSDD *alisddetf,Double_t *real,
77 Double_t *imag,Int_t direction) {
78 // Do a Fast Fourier Transform
79 //printf("FFT: direction %d\n",direction);
81 Int_t samples = alisddetf->GetSamples();
82 Int_t l = (Int_t) ((log((Float_t) samples)/log(2.))+0.5);
85 Int_t m2 = samples/m1;
88 for(j=0; j<samples; j += m1) {
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);
108 for(j=0; j<samples; j++) {
112 for(i1=1; i1<=l; i1++) {
115 p = p + p + j2 - j1 - j1;
118 Double_t xr = *(real+j);
119 Double_t xi = *(imag+j);
120 *(real+j) = *(real+p);
121 *(imag+j) = *(imag+p);
126 if(direction == -1) {
127 for(i=0; i<samples; i++) {
128 *(real+i) /= samples;
129 *(imag+i) /= samples;
134 //_____________________________________________________________________________
136 AliITSsimulationSDD::AliITSsimulationSDD(){
137 // Default constructor
153 SetPerpendTracksFlag();
165 //_____________________________________________________________________________
166 AliITSsimulationSDD::AliITSsimulationSDD(AliITSsimulationSDD &source)
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();
174 //_____________________________________________________________________________
175 AliITSsimulationSDD& AliITSsimulationSDD::operator=(AliITSsimulationSDD &source)
177 // Assignment operator to satify Coding roules only.
178 if(this==&source) return *this;
179 printf("Not allowed to make a = with AliITSsimulationSDD "
180 "Using default creater instead\n");
183 //_____________________________________________________________________________
185 AliITSsimulationSDD::AliITSsimulationSDD(AliITSsegmentation *seg,AliITSresponse *resp)
187 // Standard Constructor
194 SetPerpendTracksFlag();
198 fHitMap2 = new AliITSMapA2(fSegmentation,fScaleSize,1);
199 fHitMap1 = new AliITSMapA1(fSegmentation);
202 fNofMaps=fSegmentation->Npz();
203 fMaxNofSamples=fSegmentation->Npx();
205 Float_t sddLength = fSegmentation->Dx();
206 Float_t sddWidth = fSegmentation->Dz();
209 Float_t anodePitch = fSegmentation->Dpz(dummy);
210 Double_t timeStep = (Double_t)fSegmentation->Dpx(dummy);
211 Float_t driftSpeed=fResponse->DriftSpeed();
213 if(anodePitch*(fNofMaps/2) > sddWidth) {
214 Warning("AliITSsimulationSDD",
215 "Too many anodes %d or too big pitch %f \n",fNofMaps/2,anodePitch);
218 if(timeStep*fMaxNofSamples < sddLength/driftSpeed) {
219 Error("AliITSsimulationSDD",
220 "Time Interval > Allowed Time Interval: exit\n");
224 fElectronics = new AliITSetfSDD(timeStep/fScaleSize);
226 char opt1[20], opt2[20];
227 fResponse->ParamOptions(opt1,opt2);
229 char *same = strstr(opt1,"same");
234 fNoise.Set(fNofMaps);
235 fBaseline.Set(fNofMaps);
239 const char *kopt=fResponse->ZeroSuppOption();
240 if (strstr(fParam,"file") ) {
243 if (strstr(kopt,"2D")) {
246 Init2D(); // desactivate if param change module by module
247 } else if(strstr(kopt,"1D")) {
250 Init1D(); // desactivate if param change module by module
261 Bool_t write=fResponse->OutputOption();
262 if(write && strstr(kopt,"2D")) MakeTreeB();
264 // call here if baseline does not change by module
267 fITS = (AliITS*)gAlice->GetModule("ITS");
268 Int_t size=fNofMaps*fMaxNofSamples;
269 fStream = new AliITSInStream(size);
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];
279 //_____________________________________________________________________________
281 AliITSsimulationSDD::~AliITSsimulationSDD() {
301 if(fTreeB) delete fTreeB;
302 if(fInZR) delete [] fInZR;
303 if(fInZI) delete [] fInZI;
304 if(fOutZR) delete [] fOutZR;
305 if(fOutZI) delete [] fOutZI;
307 //_____________________________________________________________________________
309 void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
310 // create maps to build the lists of tracks
316 TObjArray *fHits = mod->GetHits();
317 Int_t nhits = fHits->GetEntriesFast();
318 if (!nhits && fCheckNoise) {
321 fHitMap2->ClearMap();
323 } else if (!nhits) return;
325 //printf("simSDD: module nhits %d %d\n",md,nhits);
328 TObjArray *list=new TObjArray;
329 static TClonesArray *padr=0;
330 if(!padr) padr=new TClonesArray("TVector",1000);
331 Int_t arg[6] = {0,0,0,0,0,0};
332 fHitMap1->SetArray(list);
335 Int_t nofAnodes=fNofMaps/2;
337 Float_t sddLength = fSegmentation->Dx();
338 Float_t sddWidth = fSegmentation->Dz();
341 Float_t anodePitch = fSegmentation->Dpz(dummy);
342 Float_t timeStep = fSegmentation->Dpx(dummy);
344 Float_t driftSpeed=fResponse->DriftSpeed();
346 // Piergiorgio's part (apart for few variables which I made float
347 // when i thought that can be done
349 // Fill detector maps with GEANT hits
350 // loop over hits in the module
352 const Float_t kconv=1.0e+6; // GeV->KeV
355 for(ii=0; ii<nhits; ii++) {
356 AliITShit *hit = (AliITShit*) fHits->At(ii);
358 hit = (AliITShit*) fHits->At(ii);
359 hit->GetPositionL(xL[0],xL[1],xL[2]);
360 Int_t hitDetector = hit->GetDetector();
362 if(hit->StatusEntering()) idhit=ii;
364 // Deposited energy in keV
367 Float_t depEnergy = kconv*hit->GetIonization();
369 if(depEnergy != 0.) continue;
373 hit1 = (AliITShit*) fHits->At(ii);
374 hit1->GetPositionL(xL1[0],xL1[1],xL1[2]);
377 depEnergy = kconv*hit1->GetIonization();
379 // added 11.09.2000 - continue if the particle did not lose energy
380 // passing through detector
382 printf("This particle has passed without losing energy!\n");
387 // scale path to simulate a perpendicular track
390 hit->GetPositionL(lC[0],lC[1],lC[2]);
392 hit1->GetPositionL(lC1[0],lC1[1],lC1[2]);
393 Float_t pathInSDD = TMath::Sqrt((lC[0]-lC1[0])*(lC[0]-lC1[0])+(lC[1]-lC1[1])*(lC[1]-lC1[1])+(lC[2]-lC1[2])*(lC[2]-lC1[2]));
394 if(pathInSDD) depEnergy *= (0.03/pathInSDD);
397 Float_t avDrft = xL[0]+avpath;
398 Float_t avAnode = xL[2]+avanod;
400 if(avpath != 0.) avDrft /= 2.;
401 if(avanod != 0.) avAnode /= 2.;
403 Float_t driftPath = 10000.*avDrft;
407 driftPath = -driftPath;
409 driftPath = sddLength-driftPath;
410 Int_t detector = 2*(hitDetector-1) + iWing;
412 cout << "Warning: negative drift path " << driftPath << endl;
417 Float_t driftTime = driftPath/driftSpeed;
418 Int_t timeSample = (Int_t) (fScaleSize*driftTime/timeStep + 1);
419 if(timeSample > fScaleSize*fMaxNofSamples) {
420 cout << "Warning: Wrong Time Sample: " << timeSample << endl;
425 Float_t xAnode = 10000.*(avAnode)/anodePitch + nofAnodes/2; // +1?
426 if((xAnode+1)*anodePitch > sddWidth || xAnode*anodePitch < 0.)
427 { cout << "Warning: Z = " << xAnode*anodePitch << endl; }
428 Int_t iAnode = (Int_t) (1.+xAnode); // xAnode?
429 if(iAnode < 0 || iAnode > nofAnodes) {
430 cout << "Warning: Wrong iAnode: " << iAnode << endl;
435 // work with the idtrack=entry number in the TreeH for the moment
436 //Int_t idhit,idtrack;
437 //mod->GetHitTrackAndHitIndex(ii,idtrack,idhit);
438 //Int_t idtrack=mod->GetHitTrackIndex(ii);
439 // or store straight away the particle position in the array
440 // of particles and take idhit=ii only when part is entering (this
441 // requires FillModules() in the macro for analysis) :
442 Int_t itrack = hit->GetTrack();
445 Float_t diffCoeff, s0;
446 fResponse->DiffCoeff(diffCoeff,s0);
448 // Squared Sigma along the anodes
449 Double_t sigma2A = 2.*diffCoeff*driftTime+s0*s0;
450 Double_t sigmaA = TMath::Sqrt(sigma2A);
451 Double_t sigmaT = sigmaA/driftSpeed;
453 // Peak amplitude in nanoAmpere
454 Double_t eVpairs = 3.6;
455 Double_t amplitude = fScaleSize*160.*depEnergy/(timeStep*eVpairs*2.*acos(-1.)*sigmaT*sigmaA);
457 Float_t nsigma=fResponse->NSigmaIntegration();
461 Int_t jt = timeSample;
463 Int_t nsplit = 4; // hard-wired
464 nsplit = (nsplit+1)/2*2;
466 Double_t aStep = anodePitch/(nsplit*fScaleSize);
467 Double_t tStep = timeStep/(nsplit*fScaleSize);
468 // Define SDD window corresponding to the hit
469 Int_t anodeWindow = (Int_t) (fScaleSize*nsigma*sigmaA/anodePitch + 1);
470 Int_t timeWindow = (Int_t) (fScaleSize*nsigma*sigmaT/timeStep + 1);
471 Int_t jamin = (ja - anodeWindow/2 - 1)*fScaleSize*nsplit + 1;
472 Int_t jamax = (ja + anodeWindow/2)*fScaleSize*nsplit;
473 if(jamin <= 0) jamin = 1;
474 if(jamax > fScaleSize*nofAnodes*nsplit) jamax = fScaleSize*nofAnodes*nsplit;
475 Int_t jtmin = (jt - timeWindow*3 - 1)*nsplit + 1; //hard-wired
476 Int_t jtmax = (jt + timeWindow*3)*nsplit; //hard-wired
477 if(jtmin <= 0) jtmin = 1;
478 if(jtmax > fScaleSize*fMaxNofSamples*nsplit) jtmax = fScaleSize*fMaxNofSamples*nsplit;
480 Double_t rlAnode = log(aStep*amplitude);
482 // Spread the charge in the anode-time window
484 for(ka=jamin; ka <=jamax; ka++) {
485 Int_t ia = (ka-1)/(fScaleSize*nsplit) + 1;
486 if(ia <= 0) { cout << "Warning: ia < 1: " << endl; continue; }
487 if(ia > nofAnodes) ia = nofAnodes;
488 Double_t aExpo = (aStep*(ka-0.5)-xAnode*anodePitch)/sigmaA;
489 Double_t anodeAmplitude = rlAnode - 0.5*aExpo*aExpo;
490 // Protect against overflows
491 if(anodeAmplitude > -87.3)
492 anodeAmplitude = exp(anodeAmplitude);
495 Int_t index = ((detector+1)%2)*nofAnodes+ia-1; // index starts from 0
497 Double_t rlTime = log(tStep*anodeAmplitude);
499 for(kt=jtmin; kt<=jtmax; kt++) {
500 Int_t it = (kt-1)/nsplit+1; // it starts from 1
501 if(it<=0) { cout << "Warning: it < 1: " << endl; continue; }
502 if(it>fScaleSize*fMaxNofSamples) it = fScaleSize*fMaxNofSamples;
503 Double_t tExpo = (tStep*(kt-0.5)-driftTime)/sigmaT;
504 Double_t timeAmplitude = rlTime - 0.5*tExpo*tExpo;
505 // Protect against overflows
506 if(timeAmplitude > -87.3){
507 timeAmplitude = exp(timeAmplitude);
511 // build the list of digits for this module
516 ListOfFiredCells(arg,timeAmplitude,list,padr);
517 } // loop over time in window
518 } // end if anodeAmplitude
519 } // loop over anodes in window
520 } // end loop over hits
522 // introduce the electronics effects and do zero-suppression if required
523 Int_t nentries=list->GetEntriesFast();
528 //timer.Stop(); timer.Print();
530 const char *kopt=fResponse->ZeroSuppOption();
531 ZeroSuppression(kopt);
540 fHitMap1->ClearMap();
541 fHitMap2->ClearMap();
543 //gObjectTable->Print();
547 //____________________________________________
549 void AliITSsimulationSDD::ListOfFiredCells(Int_t *arg,Double_t timeAmplitude,
550 TObjArray *list,TClonesArray *padr){
551 // Returns the list of "fired" cells.
555 Int_t idtrack=arg[2];
557 Int_t counter=arg[4];
558 Int_t countadr=arg[5];
560 Double_t charge=timeAmplitude;
561 charge += fHitMap2->GetSignal(index,ik-1);
562 fHitMap2->SetHit(index, ik-1, charge);
565 Int_t it=(Int_t)((ik-1)/fScaleSize);
569 digits[2]=(Int_t)timeAmplitude;
571 if (idtrack >= 0) phys=(Float_t)timeAmplitude;
574 Double_t cellcharge=0.;
575 AliITSTransientDigit* pdigit;
576 // build the list of fired cells and update the info
577 if (!fHitMap1->TestHit(index, it)) {
579 new((*padr)[countadr++]) TVector(3);
580 TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
581 trinfo(0)=(Float_t)idtrack;
582 trinfo(1)=(Float_t)idhit;
583 trinfo(2)=(Float_t)timeAmplitude;
585 list->AddAtAndExpand(
586 new AliITSTransientDigit(phys,digits),counter);
588 fHitMap1->SetHit(index, it, counter);
590 pdigit=(AliITSTransientDigit*)list->
593 TObjArray *trlist=(TObjArray*)pdigit->TrackList();
594 trlist->Add(&trinfo);
598 (AliITSTransientDigit*) fHitMap1->GetHit(index, it);
599 for(Int_t kk=0;kk<fScaleSize;kk++) {
600 cellcharge += fHitMap2->GetSignal(index,fScaleSize*it+kk);
603 (*pdigit).fSignal=(Int_t)cellcharge;
604 (*pdigit).fPhysics+=phys;
605 // update list of tracks
606 TObjArray* trlist=(TObjArray*)pdigit->TrackList();
607 Int_t lastentry=trlist->GetLast();
608 TVector *ptrkp=(TVector*)trlist->At(lastentry);
609 TVector &trinfo=*ptrkp;
610 Int_t lasttrack=Int_t(trinfo(0));
611 //Int_t lasthit=Int_t(trinfo(1));
612 Float_t lastcharge=(trinfo(2));
614 if (lasttrack==idtrack ) {
615 lastcharge+=(Float_t)timeAmplitude;
616 trlist->RemoveAt(lastentry);
618 //trinfo(1)=lasthit; // or idhit
620 trinfo(2)=lastcharge;
621 trlist->AddAt(&trinfo,lastentry);
624 new((*padr)[countadr++]) TVector(3);
625 TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
626 trinfo(0)=(Float_t)idtrack;
627 trinfo(1)=(Float_t)idhit;
628 trinfo(2)=(Float_t)timeAmplitude;
630 trlist->Add(&trinfo);
634 // check the track list - debugging
635 Int_t trk[20], htrk[20];
637 Int_t nptracks=trlist->GetEntriesFast();
640 for (tr=0;tr<nptracks;tr++) {
641 TVector *pptrkp=(TVector*)trlist->At(tr);
642 TVector &pptrk=*pptrkp;
643 trk[tr]=Int_t(pptrk(0));
644 htrk[tr]=Int_t(pptrk(1));
645 chtrk[tr]=(pptrk(2));
646 printf("nptracks %d \n",nptracks);
660 //____________________________________________
662 void AliITSsimulationSDD::AddDigit(Int_t i, Int_t j, Int_t signal){
664 // tag with -1 signals coming from background tracks
665 // tag with -2 signals coming from pure electronic noise
667 Int_t digits[3], tracks[3], hits[3];
668 Float_t phys, charges[3];
670 Int_t trk[20], htrk[20];
673 Bool_t do10to8=fResponse->Do10to8();
675 if(do10to8) signal=Convert8to10(signal);
676 AliITSTransientDigit *obj = (AliITSTransientDigit*)fHitMap1->GetHit(i,j);
688 fITS->AddSimDigit(1,phys,digits,tracks,hits,charges);
691 TObjArray* trlist=(TObjArray*)obj->TrackList();
692 Int_t nptracks=trlist->GetEntriesFast();
695 cout<<"Attention - nptracks > 20 "<<nptracks<<endl;
699 for (tr=0;tr<nptracks;tr++) {
700 TVector &pp =*((TVector*)trlist->At(tr));
701 trk[tr]=Int_t(pp(0));
702 htrk[tr]=Int_t(pp(1));
706 //printf("nptracks > 2 -- %d\n",nptracks);
707 SortTracks(trk,chtrk,htrk,nptracks);
711 for (i=0; i<nptracks; i++) {
716 for (i=nptracks; i<3; i++) {
722 for (i=0; i<3; i++) {
729 fITS->AddSimDigit(1,phys,digits,tracks,hits,charges);
735 //____________________________________________
737 void AliITSsimulationSDD::SortTracks(Int_t *tracks,Float_t *charges,Int_t *hits,Int_t ntr){
739 // Sort the list of tracks contributing to a given digit
740 // Only the 3 most significant tracks are acctually sorted
744 // Loop over signals, only 3 times
750 Int_t idx[3] = {-3,-3,-3};
751 Float_t jch[3] = {-3,-3,-3};
752 Int_t jtr[3] = {-3,-3,-3};
753 Int_t jhit[3] = {-3,-3,-3};
764 if((i == 1 && j == idx[i-1] )
765 ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
767 if(charges[j] > qmax) {
775 jch[i]=charges[jmax];
795 //____________________________________________
796 void AliITSsimulationSDD::ChargeToSignal() {
797 // add baseline, noise, electronics and ADC saturation effects
800 Float_t maxadc = fResponse->MaxAdc();
801 Float_t topValue = fResponse->MagicValue();
802 Float_t norm = maxadc/topValue;
804 char opt1[20], opt2[20];
805 fResponse->ParamOptions(opt1,opt2);
806 char *read = strstr(opt1,"file");
808 Float_t baseline, noise;
811 static Bool_t readfile=kTRUE;
812 //read baseline and noise from file
813 if (readfile) ReadBaseline();
815 } else fResponse->GetNoiseParam(noise,baseline);
823 for (i=0;i<fNofMaps;i++) {
824 if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
825 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
826 fInZR[k] = fHitMap2->GetSignal(i,k);
827 contrib = baseline + noise*random.Gaus();
830 for(k=0; k<fMaxNofSamples; k++) {
831 Float_t newcont = 0.;
832 Float_t maxcont = 0.;
833 for(kk=0;kk<fScaleSize;kk++) {
834 newcont = fInZR[fScaleSize*k+kk];
835 if(newcont > maxcont) maxcont = newcont;
838 Double_t signal = newcont*norm;
839 if (signal >= maxadc) signal = maxadc -1;
842 fHitMap2->SetHit(i,k,signal);
844 } // loop over anodes
848 for (i=0;i<fNofMaps;i++) {
849 if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
850 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
851 fInZR[k] = fHitMap2->GetSignal(i,k);
852 contrib = baseline + noise*random.Gaus();
856 FastFourierTransform(fElectronics,&fInZR[0],&fInZI[0],1);
857 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
858 Double_t rw = fElectronics->GetTraFunReal(k);
859 Double_t iw = fElectronics->GetTraFunImag(k);
860 fOutZR[k] = fInZR[k]*rw - fInZI[k]*iw;
861 fOutZI[k] = fInZR[k]*iw + fInZI[k]*rw;
863 FastFourierTransform(fElectronics,&fOutZR[0],&fOutZI[0],-1);
864 for(k=0; k<fMaxNofSamples; k++) {
865 Float_t newcont = 0.;
866 //Float_t totcont = 0.;
867 Float_t maxcont = 0.;
868 for(kk=0;kk<fScaleSize;kk++) {
869 newcont = fOutZR[fScaleSize*k+kk];
870 if(newcont > maxcont) maxcont = newcont;
871 // totcont += (0.25*Out_ZR[4*k+kk]);
874 Double_t signal = newcont*norm;
875 if (signal >= maxadc) signal = maxadc -1;
877 // comment the line below because you want to keep the signal in ADCs
878 // convert back to nA in cluster finder
880 fHitMap2->SetHit(i,k,signal);
882 } // loop over anodes
887 //____________________________________________
888 void AliITSsimulationSDD::GetAnodeBaseline(Int_t i,Float_t &baseline,
890 // Returns the Baseline for a particular anode.
891 baseline=fBaseline[i];
896 //____________________________________________
897 void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl,
899 // Returns the compression alogirthm parameters
900 Int_t size = fD.GetSize();
902 db=fD[i]; tl=fT1[i]; th=fT2[i];
904 if (size <= 2 && i>=fNofMaps/2) {
905 db=fD[1]; tl=fT1[1]; th=fT2[1];
907 db=fD[0]; tl=fT1[0]; th=fT2[0];
911 //____________________________________________
912 void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl){
913 // returns the compression alogirthm parameters
914 Int_t size = fD.GetSize();
918 if (size <= 2 && i>=fNofMaps/2) {
926 //____________________________________________
927 void AliITSsimulationSDD::SetCompressParam(){
928 // Sets the compression alogirthm parameters
931 fResponse->GiveCompressParam(cp);
932 for (i=0; i<2; i++) {
937 printf("\n i, fD, fT1, fT2, fTol %d %d %d %d %d\n",
938 i,fD[i],fT1[i],fT2[i],fTol[i]);
942 //____________________________________________
943 void AliITSsimulationSDD::ReadBaseline(){
944 // read baseline and noise from file - either a .root file and in this
945 // case data should be organised in a tree with one entry for each
946 // module => reading should be done accordingly
947 // or a classic file and do smth. like this:
949 // Read baselines and noise for SDD
955 char input[100], base[100], param[100];
958 fResponse->Filenames(input,base,param);
961 filtmp = gSystem->ExpandPathName(fFileName.Data());
962 FILE *bline = fopen(filtmp,"r");
963 printf("filtmp %s\n",filtmp);
967 while(fscanf(bline,"%d %f %f",&pos, &bl, &n) != EOF) {
969 Error("ReadBaseline","Anode number not in increasing order!",
978 Error("ReadBaseline"," THE BASELINE FILE %s DOES NOT EXIST !",
987 //____________________________________________
988 Int_t AliITSsimulationSDD::Convert10to8(Int_t signal) {
989 // To the 10 to 8 bit lossive compression.
990 // code from Davide C. and Albert W.
992 if (signal < 128) return signal;
993 if (signal < 256) return (128+((signal-128)>>1));
994 if (signal < 512) return (192+((signal-256)>>3));
995 if (signal < 1024) return (224+((signal-512)>>4));
1000 //____________________________________________
1001 Int_t AliITSsimulationSDD::Convert8to10(Int_t signal) {
1002 // Undo the lossive 10 to 8 bit compression.
1003 // code from Davide C. and Albert W.
1004 if (signal < 0 || signal > 255) {
1005 printf("<Convert8to10> out of range %d \n",signal);
1009 if (signal < 128) return signal;
1011 if (TMath::Odd(signal)) return (128+((signal-128)<<1));
1012 else return (128+((signal-128)<<1)+1);
1015 if (TMath::Odd(signal)) return (256+((signal-192)<<3)+3);
1016 else return (256+((signal-192)<<3)+4);
1018 if (TMath::Odd(signal)) return (512+((signal-224)<<4)+7);
1019 else return (512+((signal-224)<<4)+7);
1024 //____________________________________________
1025 AliITSMap* AliITSsimulationSDD::HitMap(Int_t i){
1026 //Return the correct map.
1027 return ((i==0)? fHitMap1 : fHitMap2);
1031 //____________________________________________
1032 void AliITSsimulationSDD::ZeroSuppression(const char *option) {
1033 // perform the zero suppresion
1034 if (strstr(option,"2D")) {
1035 //Init2D(); // activate if param change module by module
1037 } else if (strstr(option,"1D")) {
1038 //Init1D(); // activate if param change module by module
1040 } else StoreAllDigits();
1044 //____________________________________________
1045 void AliITSsimulationSDD::Init2D(){
1046 // read in and prepare arrays: fD, fT1, fT2
1047 // savemu[nanodes], savesigma[nanodes]
1048 // read baseline and noise from file - either a .root file and in this
1049 // case data should be organised in a tree with one entry for each
1050 // module => reading should be done accordingly
1051 // or a classic file and do smth. like this ( code from Davide C. and
1054 // Read 2D zero-suppression parameters for SDD
1057 if (!strstr(fParam,"file")) return;
1059 Int_t na,pos,tempTh;
1061 Float_t *savemu = new Float_t [fNofMaps];
1062 Float_t *savesigma = new Float_t [fNofMaps];
1063 char input[100],basel[100],par[100];
1067 Int_t minval = fResponse->MinVal();
1069 fResponse->Filenames(input,basel,par);
1073 filtmp = gSystem->ExpandPathName(fFileName.Data());
1074 FILE *param = fopen(filtmp,"r");
1078 while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
1080 Error("Init2D ","Anode number not in increasing order!",
1085 savesigma[na]=sigma;
1086 if ((2.*sigma) < mu) {
1087 fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
1090 tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
1091 if (tempTh < 0) tempTh=0;
1093 tempTh = (Int_t)floor(mu+3.0*sigma+0.5) - minval;
1094 if (tempTh < 0) tempTh=0;
1100 Error("Init2D "," THE FILE %s DOES NOT EXIST !",
1108 delete [] savesigma;
1111 //____________________________________________
1112 void AliITSsimulationSDD::Compress2D(){
1114 // simple ITS cluster finder -- online zero-suppression conditions
1119 Int_t minval = fResponse->MinVal();
1120 Bool_t write=fResponse->OutputOption();
1121 Bool_t do10to8=fResponse->Do10to8();
1123 Int_t nz, nl, nh, low, i, j;
1125 for (i=0; i<fNofMaps; i++) {
1126 CompressionParam(i,db,tl,th);
1131 for (j=0; j<fMaxNofSamples; j++) {
1132 Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
1133 signal -= db; // if baseline eq. is done here
1134 if (signal <= 0) {nz++; continue;}
1135 if ((signal - tl) < minval) low++;
1136 if ((signal - th) >= minval) {
1139 FindCluster(i,j,signal,minval,cond);
1140 if (cond && ((TMath::Abs(fHitMap2->GetSignal(i,j-1))-th)>=minval)) {
1141 if(do10to8) signal = Convert10to8(signal);
1142 AddDigit(i,j,signal);
1144 } else if ((signal - tl) >= minval) nl++;
1145 } // loop time samples
1146 if (write) TreeB()->Fill(nz,nl,nh,low,i+1);
1151 sprintf(hname,"TNtuple%d_%d",fModule,fEvent);
1152 TreeB()->Write(hname);
1159 //_____________________________________________________________________________
1160 void AliITSsimulationSDD::FindCluster(Int_t i,Int_t j,Int_t signal,
1161 Int_t minval,Bool_t &cond){
1163 // Find clusters according to the online 2D zero-suppression algorithm
1166 Bool_t do10to8=fResponse->Do10to8();
1170 fHitMap2->FlagHit(i,j);
1172 // check the online zero-suppression conditions
1174 const Int_t maxNeighbours = 4;
1178 Int_t xList[maxNeighbours], yList[maxNeighbours];
1179 fSegmentation->Neighbours(i,j,&nn,xList,yList);
1181 for (in=0; in<nn; in++) {
1184 if (fHitMap2->TestHit(ix,iy)==kUnused) {
1185 CompressionParam(ix,dbx,tlx,thx);
1186 Int_t qn = (Int_t)(fHitMap2->GetSignal(ix,iy));
1187 qn -= dbx; // if baseline eq. is done here
1188 if ((qn-tlx) < minval) {
1189 fHitMap2->FlagHit(ix,iy);
1192 if ((qn - thx) >= minval) high=kTRUE;
1194 if(do10to8) signal = Convert10to8(signal);
1195 AddDigit(i,j,signal);
1197 if(do10to8) qns = Convert10to8(qn);
1199 if (!high) AddDigit(ix,iy,qns);
1201 if(!high) fHitMap2->FlagHit(ix,iy);
1204 } // loop over neighbours
1208 //____________________________________________
1209 void AliITSsimulationSDD::Init1D(){
1210 // this is just a copy-paste of input taken from 2D algo
1211 // Torino people should give input
1213 // Read 1D zero-suppression parameters for SDD
1216 if (!strstr(fParam,"file")) return;
1218 Int_t na,pos,tempTh;
1220 Float_t *savemu = new Float_t [fNofMaps];
1221 Float_t *savesigma = new Float_t [fNofMaps];
1222 char input[100],basel[100],par[100];
1226 Int_t minval = fResponse->MinVal();
1227 fResponse->Filenames(input,basel,par);
1230 // set first the disable and tol param
1233 filtmp = gSystem->ExpandPathName(fFileName.Data());
1234 FILE *param = fopen(filtmp,"r");
1238 fscanf(param,"%d %d %d %d ", &fT2[0], &fT2[1], &fTol[0], &fTol[1]);
1239 while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
1241 Error("Init1D ","Anode number not in increasing order!",
1246 savesigma[na]=sigma;
1247 if ((2.*sigma) < mu) {
1248 fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
1251 tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
1252 if (tempTh < 0) tempTh=0;
1257 Error("Init1D "," THE FILE %s DOES NOT EXIST !",
1265 delete [] savesigma;
1271 //____________________________________________
1272 void AliITSsimulationSDD::Compress1D(){
1273 // 1D zero-suppression algorithm (from Gianluca A.)
1275 Int_t dis,tol,thres,decr,diff;
1277 UChar_t *str=fStream->Stream();
1280 Bool_t do10to8=fResponse->Do10to8();
1284 for (k=0; k<2; k++) {
1287 for (i=0; i<fNofMaps/2; i++) {
1288 Bool_t firstSignal=kTRUE;
1289 Int_t idx=i+k*fNofMaps/2;
1290 CompressionParam(idx,decr,thres);
1291 for (j=0; j<fMaxNofSamples; j++) {
1292 Int_t signal=(Int_t)(fHitMap2->GetSignal(idx,j));
1293 signal -= decr; // if baseline eq.
1294 if(do10to8) signal = Convert10to8(signal);
1295 if (signal <= thres) {
1299 // write diff in the buffer for HuffT
1300 str[counter]=(UChar_t)diff;
1305 if (diff > 127) diff=127;
1306 if (diff < -128) diff=-128;
1309 // tol has changed to 8 possible cases ? - one can write
1310 // this if(TMath::Abs(diff)<tol) ... else ...
1311 if(TMath::Abs(diff)<tol) diff=0;
1312 // or keep it as it was before
1314 if (tol==1 && (diff >= -2 && diff <= 1)) diff=0;
1315 if (tol==2 && (diff >= -4 && diff <= 3)) diff=0;
1316 if (tol==3 && (diff >= -16 && diff <= 15)) diff=0;
1318 AddDigit(idx,j,last+diff);
1320 AddDigit(idx,j,signal);
1324 // write diff in the buffer used to compute Huffman tables
1325 if (firstSignal) str[counter]=(UChar_t)signal;
1326 else str[counter]=(UChar_t)diff;
1331 } // loop time samples
1332 } // loop anodes one half of detector
1336 fStream->CheckCount(counter);
1338 // open file and write out the stream of diff's
1340 static Bool_t open=kTRUE;
1341 static TFile *outFile;
1342 Bool_t write = fResponse->OutputOption();
1346 SetFileName("stream.root");
1347 cout<<"filename "<<fFileName<<endl;
1348 outFile=new TFile(fFileName,"recreate");
1349 cout<<"I have opened "<<fFileName<<" file "<<endl;
1356 fStream->ClearStream();
1358 // back to galice.root file
1360 TTree *fAli=gAlice->TreeK();
1363 if (fAli) file =fAli->GetCurrentFile();
1368 //____________________________________________
1369 void AliITSsimulationSDD::StoreAllDigits(){
1370 // if non-zero-suppressed data
1372 Bool_t do10to8=fResponse->Do10to8();
1374 Int_t i, j, digits[3];
1375 for (i=0; i<fNofMaps; i++) {
1376 for (j=0; j<fMaxNofSamples; j++) {
1377 Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
1378 if(do10to8) signal = Convert10to8(signal);
1379 if(do10to8) signal = Convert8to10(signal);
1383 fITS->AddRealDigit(1,digits);
1387 //____________________________________________
1389 void AliITSsimulationSDD::CreateHistograms(Int_t scale){
1390 // Creates histograms of maps for debugging
1394 fHis=new TObjArray(fNofMaps);
1395 TString sddName("sdd_");
1396 for (i=0;i<fNofMaps;i++) {
1398 sprintf(candNum,"%d",i+1);
1399 sddName.Append(candNum);
1400 (*fHis)[i] = new TH1F(sddName.Data(),"SDD maps",
1401 scale*fMaxNofSamples,0.,(Float_t) scale*fMaxNofSamples);
1405 //____________________________________________
1406 void AliITSsimulationSDD::FillHistograms(){
1407 // fill 1D histograms from map
1410 for( Int_t i=0; i<fNofMaps; i++) {
1411 TH1F *hist =(TH1F *)fHis->UncheckedAt(i);
1412 Int_t nsamples = hist->GetNbinsX();
1413 for( Int_t j=0; j<nsamples; j++) {
1414 Double_t signal=fHitMap2->GetSignal(i,j);
1415 hist->Fill((Float_t)j,signal);
1420 //____________________________________________
1422 void AliITSsimulationSDD::ResetHistograms(){
1424 // Reset histograms for this detector
1427 for (i=0;i<fNofMaps;i++ ) {
1428 if ((*fHis)[i]) ((TH1F*)(*fHis)[i])->Reset();
1434 //____________________________________________
1436 TH1F *AliITSsimulationSDD::GetAnode(Int_t wing, Int_t anode) {
1437 // Fills a histogram from a give anode.
1438 if (!fHis) return 0;
1440 if(wing <=0 || wing > 2) {
1441 cout << "Wrong wing number: " << wing << endl;
1444 if(anode <=0 || anode > fNofMaps/2) {
1445 cout << "Wrong anode number: " << anode << endl;
1449 Int_t index = (wing-1)*fNofMaps/2 + anode-1;
1450 return (TH1F*)((*fHis)[index]);
1453 //____________________________________________
1455 void AliITSsimulationSDD::WriteToFile(TFile *hfile) {
1456 // Writes the histograms to a file
1461 for(i=0; i<fNofMaps; i++) (*fHis)[i]->Write(); //fAdcs[i]->Write();
1464 //____________________________________________
1465 Float_t AliITSsimulationSDD::GetNoise() {
1466 // Returns the noise value
1468 //Bool_t do10to8=fResponse->Do10to8();
1469 //noise will always be in the liniar part of the signal
1472 Int_t threshold=fT1[0];
1474 char opt1[20], opt2[20];
1475 fResponse->ParamOptions(opt1,opt2);
1477 char *same = strstr(opt1,"same");
1478 Float_t noise,baseline;
1480 fResponse->GetNoiseParam(noise,baseline);
1482 static Bool_t readfile=kTRUE;
1483 //read baseline and noise from file
1484 if (readfile) ReadBaseline();
1488 TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
1489 if(c2) delete c2->GetPrimitive("noisehist");
1490 if(c2) delete c2->GetPrimitive("anode");
1491 else c2=new TCanvas("c2");
1493 c2->SetFillColor(0);
1495 TH1F *noisehist = new TH1F("noisehist","noise",100,0.,(float)2*threshold);
1496 TH1F *anode = new TH1F("anode","Anode Projection",fMaxNofSamples,0.,(float)fMaxNofSamples);
1498 for (i=0;i<fNofMaps;i++) {
1499 CompressionParam(i,decr,threshold);
1500 if (!same) GetAnodeBaseline(i,baseline,noise);
1502 for (k=0;k<fMaxNofSamples;k++) {
1503 Float_t signal=(Float_t)fHitMap2->GetSignal(i,k);
1504 //if (signal <= (float)threshold) noisehist->Fill(signal-baseline);
1505 if (signal <= (float)threshold) noisehist->Fill(signal);
1506 anode->Fill((float)k,signal);
1511 TF1 *gnoise = new TF1("gnoise","gaus",0.,threshold);
1512 noisehist->Fit("gnoise","RQ");
1515 Float_t mnoise = gnoise->GetParameter(1);
1516 cout << "mnoise : " << mnoise << endl;
1517 Float_t rnoise = gnoise->GetParameter(2);
1518 cout << "rnoise : " << rnoise << endl;