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 **************************************************************************/
20 #include <TStopwatch.h>
29 #include "AliITShit.h"
30 #include "AliITSdigit.h"
31 #include "AliITSmodule.h"
32 #include "AliITSMapA1.h"
33 #include "AliITSMapA2.h"
34 #include "AliITSsimulationSDD.h"
35 #include "AliITSetfSDD.h"
36 #include "AliITSRawData.h"
37 #include "AliITSHuffman.h"
38 #include "AliITSsegmentation.h"
39 #include "AliITSresponse.h"
41 ClassImp(AliITSsimulationSDD)
42 ////////////////////////////////////////////////////////////////////////
44 // Written by Piergiorgio Cerello
47 // AliITSsimulationSDD is the simulation of SDDs.
51 <img src="picts/ITS/AliITShit_Class_Diagram.gif">
54 <font size=+2 color=red>
55 <p>This show the relasionships between the ITS hit class and the rest of Aliroot.
60 //_____________________________________________________________________________
62 Int_t power(Int_t b, Int_t e) {
63 // compute b to the e power, where both b and e are Int_ts.
65 for(i=0; i<e; i++) power *= b;
69 //_____________________________________________
71 void FastFourierTransform(AliITSetfSDD *alisddetf,Double_t *real,
72 Double_t *imag,Int_t direction) {
73 // Do a Fast Fourier Transform
74 //printf("FFT: direction %d\n",direction);
76 Int_t samples = alisddetf->GetSamples();
77 Int_t l = (Int_t) ((log((Float_t) samples)/log(2.))+0.5);
80 Int_t m2 = samples/m1;
83 for(j=0; j<samples; j += m1) {
85 for(k=j; k<= j+m-1; k++) {
86 Double_t wsr = alisddetf->GetWeightReal(p);
87 Double_t wsi = alisddetf->GetWeightImag(p);
88 if(direction == -1) wsi = -wsi;
89 Double_t xr = *(real+k+m);
90 Double_t xi = *(imag+k+m);
91 *(real+k+m) = wsr*(*(real+k)-xr) - wsi*(*(imag+k)-xi);
92 *(imag+k+m) = wsr*(*(imag+k)-xi) + wsi*(*(real+k)-xr);
103 for(j=0; j<samples; j++) {
107 for(i1=1; i1<=l; i1++) {
110 p = p + p + j2 - j1 - j1;
113 Double_t xr = *(real+j);
114 Double_t xi = *(imag+j);
115 *(real+j) = *(real+p);
116 *(imag+j) = *(imag+p);
121 if(direction == -1) {
122 for(i=0; i<samples; i++) {
123 *(real+i) /= samples;
124 *(imag+i) /= samples;
129 //_____________________________________________________________________________
131 AliITSsimulationSDD::AliITSsimulationSDD(){
132 // Default constructor
148 SetPerpendTracksFlag();
160 //_____________________________________________________________________________
161 AliITSsimulationSDD::AliITSsimulationSDD(AliITSsimulationSDD &source)
163 // Copy constructor to satify Coding roules only.
164 if(this==&source) return;
165 printf("Not allowed to make a copy of AliITSsimulationSDD "
166 "Using default creater instead\n");
167 AliITSsimulationSDD();
169 //_____________________________________________________________________________
170 AliITSsimulationSDD& AliITSsimulationSDD::operator=(AliITSsimulationSDD &source)
172 // Assignment operator to satify Coding roules only.
173 if(this==&source) return *this;
174 printf("Not allowed to make a = with AliITSsimulationSDD "
175 "Using default creater instead\n");
178 //_____________________________________________________________________________
180 AliITSsimulationSDD::AliITSsimulationSDD(AliITSsegmentation *seg,AliITSresponse *resp)
182 // Standard Constructor
189 SetPerpendTracksFlag();
193 fHitMap2 = new AliITSMapA2(fSegmentation,fScaleSize,1);
194 fHitMap1 = new AliITSMapA1(fSegmentation);
197 fNofMaps=fSegmentation->Npz();
198 fMaxNofSamples=fSegmentation->Npx();
200 Float_t sddLength = fSegmentation->Dx();
201 Float_t sddWidth = fSegmentation->Dz();
204 Float_t anodePitch = fSegmentation->Dpz(dummy);
205 Double_t timeStep = (Double_t)fSegmentation->Dpx(dummy);
206 Float_t driftSpeed=fResponse->DriftSpeed();
208 if(anodePitch*(fNofMaps/2) > sddWidth) {
209 Warning("AliITSsimulationSDD",
210 "Too many anodes %d or too big pitch %f \n",fNofMaps/2,anodePitch);
213 if(timeStep*fMaxNofSamples < sddLength/driftSpeed) {
214 Error("AliITSsimulationSDD",
215 "Time Interval > Allowed Time Interval: exit\n");
219 fElectronics = new AliITSetfSDD(timeStep/fScaleSize);
221 char opt1[20], opt2[20];
222 fResponse->ParamOptions(opt1,opt2);
224 char *same = strstr(opt1,"same");
229 fNoise.Set(fNofMaps);
230 fBaseline.Set(fNofMaps);
234 const char *kopt=fResponse->ZeroSuppOption();
235 if (strstr(fParam,"file") ) {
238 if (strstr(kopt,"2D")) {
241 Init2D(); // desactivate if param change module by module
242 } else if(strstr(kopt,"1D")) {
245 Init1D(); // desactivate if param change module by module
256 Bool_t write=fResponse->OutputOption();
257 if(write && strstr(kopt,"2D")) MakeTreeB();
259 // call here if baseline does not change by module
262 fITS = (AliITS*)gAlice->GetModule("ITS");
263 Int_t size=fNofMaps*fMaxNofSamples;
264 fStream = new AliITSInStream(size);
266 fInZR = new Double_t [fScaleSize*fMaxNofSamples];
267 fInZI = new Double_t [fScaleSize*fMaxNofSamples];
268 fOutZR = new Double_t [fScaleSize*fMaxNofSamples];
269 fOutZI = new Double_t [fScaleSize*fMaxNofSamples];
274 //_____________________________________________________________________________
276 AliITSsimulationSDD::~AliITSsimulationSDD() {
296 if(fTreeB) delete fTreeB;
297 if(fInZR) delete [] fInZR;
298 if(fInZI) delete [] fInZI;
299 if(fOutZR) delete [] fOutZR;
300 if(fOutZI) delete [] fOutZI;
302 //_____________________________________________________________________________
304 void AliITSsimulationSDD::DigitiseModule(AliITSmodule *mod,Int_t md,Int_t ev){
305 // create maps to build the lists of tracks
311 TObjArray *fHits = mod->GetHits();
312 Int_t nhits = fHits->GetEntriesFast();
313 if (!nhits && fCheckNoise) {
316 fHitMap2->ClearMap();
318 } else if (!nhits) return;
320 //printf("simSDD: module nhits %d %d\n",md,nhits);
323 TObjArray *list=new TObjArray;
324 static TClonesArray *padr=0;
325 if(!padr) padr=new TClonesArray("TVector",1000);
326 Int_t arg[6] = {0,0,0,0,0,0};
327 fHitMap1->SetArray(list);
330 Int_t nofAnodes=fNofMaps/2;
332 Float_t sddLength = fSegmentation->Dx();
333 Float_t sddWidth = fSegmentation->Dz();
336 Float_t anodePitch = fSegmentation->Dpz(dummy);
337 Float_t timeStep = fSegmentation->Dpx(dummy);
339 Float_t driftSpeed=fResponse->DriftSpeed();
341 // Piergiorgio's part (apart for few variables which I made float
342 // when i thought that can be done
344 // Fill detector maps with GEANT hits
345 // loop over hits in the module
347 const Float_t kconv=1.0e+6; // GeV->KeV
350 for(ii=0; ii<nhits; ii++) {
351 AliITShit *hit = (AliITShit*) fHits->At(ii);
353 hit = (AliITShit*) fHits->At(ii);
354 hit->GetPositionL(xL[0],xL[1],xL[2]);
355 Int_t hitDetector = hit->GetDetector();
357 if(hit->StatusEntering()) idhit=ii;
359 // Deposited energy in keV
362 Float_t depEnergy = kconv*hit->GetIonization();
364 if(depEnergy != 0.) continue;
368 hit1 = (AliITShit*) fHits->At(ii);
369 hit1->GetPositionL(xL1[0],xL1[1],xL1[2]);
372 depEnergy = kconv*hit1->GetIonization();
374 // added 11.09.2000 - continue if the particle did not lose energy
375 // passing through detector
377 printf("This particle has passed without losing energy!\n");
382 // scale path to simulate a perpendicular track
385 hit->GetPositionL(lC[0],lC[1],lC[2]);
387 hit1->GetPositionL(lC1[0],lC1[1],lC1[2]);
388 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]));
389 if(pathInSDD) depEnergy *= (0.03/pathInSDD);
392 Float_t avDrft = xL[0]+avpath;
393 Float_t avAnode = xL[2]+avanod;
395 if(avpath != 0.) avDrft /= 2.;
396 if(avanod != 0.) avAnode /= 2.;
398 Float_t driftPath = 10000.*avDrft;
402 driftPath = -driftPath;
404 driftPath = sddLength-driftPath;
405 Int_t detector = 2*(hitDetector-1) + iWing;
407 cout << "Warning: negative drift path " << driftPath << endl;
412 Float_t driftTime = driftPath/driftSpeed;
413 Int_t timeSample = (Int_t) (fScaleSize*driftTime/timeStep + 1);
414 if(timeSample > fScaleSize*fMaxNofSamples) {
415 cout << "Warning: Wrong Time Sample: " << timeSample << endl;
420 Float_t xAnode = 10000.*(avAnode)/anodePitch + nofAnodes/2; // +1?
421 if((xAnode+1)*anodePitch > sddWidth || xAnode*anodePitch < 0.)
422 { cout << "Warning: Z = " << xAnode*anodePitch << endl; }
423 Int_t iAnode = (Int_t) (1.+xAnode); // xAnode?
424 if(iAnode < 0 || iAnode > nofAnodes) {
425 cout << "Warning: Wrong iAnode: " << iAnode << endl;
430 // work with the idtrack=entry number in the TreeH for the moment
431 //Int_t idhit,idtrack;
432 //mod->GetHitTrackAndHitIndex(ii,idtrack,idhit);
433 //Int_t idtrack=mod->GetHitTrackIndex(ii);
434 // or store straight away the particle position in the array
435 // of particles and take idhit=ii only when part is entering (this
436 // requires FillModules() in the macro for analysis) :
437 Int_t itrack = hit->GetTrack();
440 Float_t diffCoeff, s0;
441 fResponse->DiffCoeff(diffCoeff,s0);
443 // Squared Sigma along the anodes
444 Double_t sigma2A = 2.*diffCoeff*driftTime+s0*s0;
445 Double_t sigmaA = TMath::Sqrt(sigma2A);
446 Double_t sigmaT = sigmaA/driftSpeed;
448 // Peak amplitude in nanoAmpere
449 Double_t eVpairs = 3.6;
450 Double_t amplitude = fScaleSize*160.*depEnergy/(timeStep*eVpairs*2.*acos(-1.)*sigmaT*sigmaA);
452 Float_t nsigma=fResponse->NSigmaIntegration();
456 Int_t jt = timeSample;
458 Int_t nsplit = 4; // hard-wired
459 nsplit = (nsplit+1)/2*2;
461 Double_t aStep = anodePitch/(nsplit*fScaleSize);
462 Double_t tStep = timeStep/(nsplit*fScaleSize);
463 // Define SDD window corresponding to the hit
464 Int_t anodeWindow = (Int_t) (fScaleSize*nsigma*sigmaA/anodePitch + 1);
465 Int_t timeWindow = (Int_t) (fScaleSize*nsigma*sigmaT/timeStep + 1);
466 Int_t jamin = (ja - anodeWindow/2 - 1)*fScaleSize*nsplit + 1;
467 Int_t jamax = (ja + anodeWindow/2)*fScaleSize*nsplit;
468 if(jamin <= 0) jamin = 1;
469 if(jamax > fScaleSize*nofAnodes*nsplit) jamax = fScaleSize*nofAnodes*nsplit;
470 Int_t jtmin = (jt - timeWindow*3 - 1)*nsplit + 1; //hard-wired
471 Int_t jtmax = (jt + timeWindow*3)*nsplit; //hard-wired
472 if(jtmin <= 0) jtmin = 1;
473 if(jtmax > fScaleSize*fMaxNofSamples*nsplit) jtmax = fScaleSize*fMaxNofSamples*nsplit;
475 Double_t rlAnode = log(aStep*amplitude);
477 // Spread the charge in the anode-time window
479 for(ka=jamin; ka <=jamax; ka++) {
480 Int_t ia = (ka-1)/(fScaleSize*nsplit) + 1;
481 if(ia <= 0) { cout << "Warning: ia < 1: " << endl; continue; }
482 if(ia > nofAnodes) ia = nofAnodes;
483 Double_t aExpo = (aStep*(ka-0.5)-xAnode*anodePitch)/sigmaA;
484 Double_t anodeAmplitude = rlAnode - 0.5*aExpo*aExpo;
485 // Protect against overflows
486 if(anodeAmplitude > -87.3)
487 anodeAmplitude = exp(anodeAmplitude);
490 Int_t index = ((detector+1)%2)*nofAnodes+ia-1; // index starts from 0
492 Double_t rlTime = log(tStep*anodeAmplitude);
494 for(kt=jtmin; kt<=jtmax; kt++) {
495 Int_t it = (kt-1)/nsplit+1; // it starts from 1
496 if(it<=0) { cout << "Warning: it < 1: " << endl; continue; }
497 if(it>fScaleSize*fMaxNofSamples) it = fScaleSize*fMaxNofSamples;
498 Double_t tExpo = (tStep*(kt-0.5)-driftTime)/sigmaT;
499 Double_t timeAmplitude = rlTime - 0.5*tExpo*tExpo;
500 // Protect against overflows
501 if(timeAmplitude > -87.3){
502 timeAmplitude = exp(timeAmplitude);
506 // build the list of digits for this module
511 ListOfFiredCells(arg,timeAmplitude,list,padr);
512 } // loop over time in window
513 } // end if anodeAmplitude
514 } // loop over anodes in window
515 } // end loop over hits
517 // introduce the electronics effects and do zero-suppression if required
518 Int_t nentries=list->GetEntriesFast();
523 //timer.Stop(); timer.Print();
525 const char *kopt=fResponse->ZeroSuppOption();
526 ZeroSuppression(kopt);
535 fHitMap1->ClearMap();
536 fHitMap2->ClearMap();
538 //gObjectTable->Print();
542 //____________________________________________
544 void AliITSsimulationSDD::ListOfFiredCells(Int_t *arg,Double_t timeAmplitude,
545 TObjArray *list,TClonesArray *padr){
546 // Returns the list of "fired" cells.
550 Int_t idtrack=arg[2];
552 Int_t counter=arg[4];
553 Int_t countadr=arg[5];
555 Double_t charge=timeAmplitude;
556 charge += fHitMap2->GetSignal(index,ik-1);
557 fHitMap2->SetHit(index, ik-1, charge);
560 Int_t it=(Int_t)((ik-1)/fScaleSize);
564 digits[2]=(Int_t)timeAmplitude;
566 if (idtrack >= 0) phys=(Float_t)timeAmplitude;
569 Double_t cellcharge=0.;
570 AliITSTransientDigit* pdigit;
571 // build the list of fired cells and update the info
572 if (!fHitMap1->TestHit(index, it)) {
574 new((*padr)[countadr++]) TVector(3);
575 TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
576 trinfo(0)=(Float_t)idtrack;
577 trinfo(1)=(Float_t)idhit;
578 trinfo(2)=(Float_t)timeAmplitude;
580 list->AddAtAndExpand(
581 new AliITSTransientDigit(phys,digits),counter);
583 fHitMap1->SetHit(index, it, counter);
585 pdigit=(AliITSTransientDigit*)list->
588 TObjArray *trlist=(TObjArray*)pdigit->TrackList();
589 trlist->Add(&trinfo);
593 (AliITSTransientDigit*) fHitMap1->GetHit(index, it);
594 for(Int_t kk=0;kk<fScaleSize;kk++) {
595 cellcharge += fHitMap2->GetSignal(index,fScaleSize*it+kk);
598 (*pdigit).fSignal=(Int_t)cellcharge;
599 (*pdigit).fPhysics+=phys;
600 // update list of tracks
601 TObjArray* trlist=(TObjArray*)pdigit->TrackList();
602 Int_t lastentry=trlist->GetLast();
603 TVector *ptrkp=(TVector*)trlist->At(lastentry);
604 TVector &trinfo=*ptrkp;
605 Int_t lasttrack=Int_t(trinfo(0));
606 //Int_t lasthit=Int_t(trinfo(1));
607 Float_t lastcharge=(trinfo(2));
609 if (lasttrack==idtrack ) {
610 lastcharge+=(Float_t)timeAmplitude;
611 trlist->RemoveAt(lastentry);
613 //trinfo(1)=lasthit; // or idhit
615 trinfo(2)=lastcharge;
616 trlist->AddAt(&trinfo,lastentry);
619 new((*padr)[countadr++]) TVector(3);
620 TVector &trinfo=*((TVector*) (*padr)[countadr-1]);
621 trinfo(0)=(Float_t)idtrack;
622 trinfo(1)=(Float_t)idhit;
623 trinfo(2)=(Float_t)timeAmplitude;
625 trlist->Add(&trinfo);
629 // check the track list - debugging
630 Int_t trk[20], htrk[20];
632 Int_t nptracks=trlist->GetEntriesFast();
635 for (tr=0;tr<nptracks;tr++) {
636 TVector *pptrkp=(TVector*)trlist->At(tr);
637 TVector &pptrk=*pptrkp;
638 trk[tr]=Int_t(pptrk(0));
639 htrk[tr]=Int_t(pptrk(1));
640 chtrk[tr]=(pptrk(2));
641 printf("nptracks %d \n",nptracks);
655 //____________________________________________
657 void AliITSsimulationSDD::AddDigit(Int_t i, Int_t j, Int_t signal){
659 // tag with -1 signals coming from background tracks
660 // tag with -2 signals coming from pure electronic noise
662 Int_t digits[3], tracks[3], hits[3];
663 Float_t phys, charges[3];
665 Int_t trk[20], htrk[20];
668 Bool_t do10to8=fResponse->Do10to8();
670 if(do10to8) signal=Convert8to10(signal);
671 AliITSTransientDigit *obj = (AliITSTransientDigit*)fHitMap1->GetHit(i,j);
683 fITS->AddSimDigit(1,phys,digits,tracks,hits,charges);
686 TObjArray* trlist=(TObjArray*)obj->TrackList();
687 Int_t nptracks=trlist->GetEntriesFast();
690 cout<<"Attention - nptracks > 20 "<<nptracks<<endl;
694 for (tr=0;tr<nptracks;tr++) {
695 TVector &pp =*((TVector*)trlist->At(tr));
696 trk[tr]=Int_t(pp(0));
697 htrk[tr]=Int_t(pp(1));
701 //printf("nptracks > 2 -- %d\n",nptracks);
702 SortTracks(trk,chtrk,htrk,nptracks);
706 for (i=0; i<nptracks; i++) {
711 for (i=nptracks; i<3; i++) {
717 for (i=0; i<3; i++) {
724 fITS->AddSimDigit(1,phys,digits,tracks,hits,charges);
730 //____________________________________________
732 void AliITSsimulationSDD::SortTracks(Int_t *tracks,Float_t *charges,Int_t *hits,Int_t ntr){
734 // Sort the list of tracks contributing to a given digit
735 // Only the 3 most significant tracks are acctually sorted
739 // Loop over signals, only 3 times
745 Int_t idx[3] = {-3,-3,-3};
746 Float_t jch[3] = {-3,-3,-3};
747 Int_t jtr[3] = {-3,-3,-3};
748 Int_t jhit[3] = {-3,-3,-3};
759 if((i == 1 && j == idx[i-1] )
760 ||(i == 2 && (j == idx[i-1] || j == idx[i-2]))) continue;
762 if(charges[j] > qmax) {
770 jch[i]=charges[jmax];
790 //____________________________________________
791 void AliITSsimulationSDD::ChargeToSignal() {
792 // add baseline, noise, electronics and ADC saturation effects
795 Float_t maxadc = fResponse->MaxAdc();
796 Float_t topValue = fResponse->MagicValue();
797 Float_t norm = maxadc/topValue;
799 char opt1[20], opt2[20];
800 fResponse->ParamOptions(opt1,opt2);
801 char *read = strstr(opt1,"file");
803 Float_t baseline, noise;
806 static Bool_t readfile=kTRUE;
807 //read baseline and noise from file
808 if (readfile) ReadBaseline();
810 } else fResponse->GetNoiseParam(noise,baseline);
818 for (i=0;i<fNofMaps;i++) {
819 if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
820 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
821 fInZR[k] = fHitMap2->GetSignal(i,k);
822 contrib = baseline + noise*random.Gaus();
825 for(k=0; k<fMaxNofSamples; k++) {
826 Float_t newcont = 0.;
827 Float_t maxcont = 0.;
828 for(kk=0;kk<fScaleSize;kk++) {
829 newcont = fInZR[fScaleSize*k+kk];
830 if(newcont > maxcont) maxcont = newcont;
833 Double_t signal = newcont*norm;
834 if (signal >= maxadc) signal = maxadc -1;
837 fHitMap2->SetHit(i,k,signal);
839 } // loop over anodes
843 for (i=0;i<fNofMaps;i++) {
844 if (read && i<fNofMaps) GetAnodeBaseline(i,baseline,noise);
845 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
846 fInZR[k] = fHitMap2->GetSignal(i,k);
847 contrib = baseline + noise*random.Gaus();
851 FastFourierTransform(fElectronics,&fInZR[0],&fInZI[0],1);
852 for(k=0; k<fScaleSize*fMaxNofSamples; k++) {
853 Double_t rw = fElectronics->GetTraFunReal(k);
854 Double_t iw = fElectronics->GetTraFunImag(k);
855 fOutZR[k] = fInZR[k]*rw - fInZI[k]*iw;
856 fOutZI[k] = fInZR[k]*iw + fInZI[k]*rw;
858 FastFourierTransform(fElectronics,&fOutZR[0],&fOutZI[0],-1);
859 for(k=0; k<fMaxNofSamples; k++) {
860 Float_t newcont = 0.;
861 //Float_t totcont = 0.;
862 Float_t maxcont = 0.;
863 for(kk=0;kk<fScaleSize;kk++) {
864 newcont = fOutZR[fScaleSize*k+kk];
865 if(newcont > maxcont) maxcont = newcont;
866 // totcont += (0.25*Out_ZR[4*k+kk]);
869 Double_t signal = newcont*norm;
870 if (signal >= maxadc) signal = maxadc -1;
872 // comment the line below because you want to keep the signal in ADCs
873 // convert back to nA in cluster finder
875 fHitMap2->SetHit(i,k,signal);
877 } // loop over anodes
882 //____________________________________________
883 void AliITSsimulationSDD::GetAnodeBaseline(Int_t i,Float_t &baseline,
885 // Returns the Baseline for a particular anode.
886 baseline=fBaseline[i];
891 //____________________________________________
892 void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl,
894 // Returns the compression alogirthm parameters
895 Int_t size = fD.GetSize();
897 db=fD[i]; tl=fT1[i]; th=fT2[i];
899 if (size <= 2 && i>=fNofMaps/2) {
900 db=fD[1]; tl=fT1[1]; th=fT2[1];
902 db=fD[0]; tl=fT1[0]; th=fT2[0];
906 //____________________________________________
907 void AliITSsimulationSDD::CompressionParam(Int_t i,Int_t &db,Int_t &tl){
908 // returns the compression alogirthm parameters
909 Int_t size = fD.GetSize();
913 if (size <= 2 && i>=fNofMaps/2) {
921 //____________________________________________
922 void AliITSsimulationSDD::SetCompressParam(){
923 // Sets the compression alogirthm parameters
926 fResponse->GiveCompressParam(cp);
927 for (i=0; i<2; i++) {
932 printf("\n i, fD, fT1, fT2, fTol %d %d %d %d %d\n",
933 i,fD[i],fT1[i],fT2[i],fTol[i]);
937 //____________________________________________
938 void AliITSsimulationSDD::ReadBaseline(){
939 // read baseline and noise from file - either a .root file and in this
940 // case data should be organised in a tree with one entry for each
941 // module => reading should be done accordingly
942 // or a classic file and do smth. like this:
944 // Read baselines and noise for SDD
950 char input[100], base[100], param[100];
953 fResponse->Filenames(input,base,param);
956 filtmp = gSystem->ExpandPathName(fFileName.Data());
957 FILE *bline = fopen(filtmp,"r");
958 printf("filtmp %s\n",filtmp);
962 while(fscanf(bline,"%d %f %f",&pos, &bl, &n) != EOF) {
964 Error("ReadBaseline","Anode number not in increasing order!",
973 Error("ReadBaseline"," THE BASELINE FILE %s DOES NOT EXIST !",
982 //____________________________________________
983 Int_t AliITSsimulationSDD::Convert10to8(Int_t signal) {
984 // To the 10 to 8 bit lossive compression.
985 // code from Davide C. and Albert W.
987 if (signal < 128) return signal;
988 if (signal < 256) return (128+((signal-128)>>1));
989 if (signal < 512) return (192+((signal-256)>>3));
990 if (signal < 1024) return (224+((signal-512)>>4));
995 //____________________________________________
996 Int_t AliITSsimulationSDD::Convert8to10(Int_t signal) {
997 // Undo the lossive 10 to 8 bit compression.
998 // code from Davide C. and Albert W.
999 if (signal < 0 || signal > 255) {
1000 printf("<Convert8to10> out of range %d \n",signal);
1004 if (signal < 128) return signal;
1006 if (TMath::Odd(signal)) return (128+((signal-128)<<1));
1007 else return (128+((signal-128)<<1)+1);
1010 if (TMath::Odd(signal)) return (256+((signal-192)<<3)+3);
1011 else return (256+((signal-192)<<3)+4);
1013 if (TMath::Odd(signal)) return (512+((signal-224)<<4)+7);
1014 else return (512+((signal-224)<<4)+7);
1019 //____________________________________________
1020 AliITSMap* AliITSsimulationSDD::HitMap(Int_t i){
1021 //Return the correct map.
1022 return ((i==0)? fHitMap1 : fHitMap2);
1026 //____________________________________________
1027 void AliITSsimulationSDD::ZeroSuppression(const char *option) {
1028 // perform the zero suppresion
1029 if (strstr(option,"2D")) {
1030 //Init2D(); // activate if param change module by module
1032 } else if (strstr(option,"1D")) {
1033 //Init1D(); // activate if param change module by module
1035 } else StoreAllDigits();
1039 //____________________________________________
1040 void AliITSsimulationSDD::Init2D(){
1041 // read in and prepare arrays: fD, fT1, fT2
1042 // savemu[nanodes], savesigma[nanodes]
1043 // read baseline and noise from file - either a .root file and in this
1044 // case data should be organised in a tree with one entry for each
1045 // module => reading should be done accordingly
1046 // or a classic file and do smth. like this ( code from Davide C. and
1049 // Read 2D zero-suppression parameters for SDD
1052 if (!strstr(fParam,"file")) return;
1054 Int_t na,pos,tempTh;
1056 Float_t *savemu = new Float_t [fNofMaps];
1057 Float_t *savesigma = new Float_t [fNofMaps];
1058 char input[100],basel[100],par[100];
1062 Int_t minval = fResponse->MinVal();
1064 fResponse->Filenames(input,basel,par);
1068 filtmp = gSystem->ExpandPathName(fFileName.Data());
1069 FILE *param = fopen(filtmp,"r");
1073 while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
1075 Error("Init2D ","Anode number not in increasing order!",
1080 savesigma[na]=sigma;
1081 if ((2.*sigma) < mu) {
1082 fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
1085 tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
1086 if (tempTh < 0) tempTh=0;
1088 tempTh = (Int_t)floor(mu+3.0*sigma+0.5) - minval;
1089 if (tempTh < 0) tempTh=0;
1095 Error("Init2D "," THE FILE %s DOES NOT EXIST !",
1103 delete [] savesigma;
1106 //____________________________________________
1107 void AliITSsimulationSDD::Compress2D(){
1109 // simple ITS cluster finder -- online zero-suppression conditions
1114 Int_t minval = fResponse->MinVal();
1115 Bool_t write=fResponse->OutputOption();
1116 Bool_t do10to8=fResponse->Do10to8();
1118 Int_t nz, nl, nh, low, i, j;
1120 for (i=0; i<fNofMaps; i++) {
1121 CompressionParam(i,db,tl,th);
1126 for (j=0; j<fMaxNofSamples; j++) {
1127 Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
1128 signal -= db; // if baseline eq. is done here
1129 if (signal <= 0) {nz++; continue;}
1130 if ((signal - tl) < minval) low++;
1131 if ((signal - th) >= minval) {
1134 FindCluster(i,j,signal,minval,cond);
1135 if (cond && ((TMath::Abs(fHitMap2->GetSignal(i,j-1))-th)>=minval)) {
1136 if(do10to8) signal = Convert10to8(signal);
1137 AddDigit(i,j,signal);
1139 } else if ((signal - tl) >= minval) nl++;
1140 } // loop time samples
1141 if (write) TreeB()->Fill(nz,nl,nh,low,i+1);
1146 sprintf(hname,"TNtuple%d_%d",fModule,fEvent);
1147 TreeB()->Write(hname);
1154 //_____________________________________________________________________________
1155 void AliITSsimulationSDD::FindCluster(Int_t i,Int_t j,Int_t signal,
1156 Int_t minval,Bool_t &cond){
1158 // Find clusters according to the online 2D zero-suppression algorithm
1161 Bool_t do10to8=fResponse->Do10to8();
1165 fHitMap2->FlagHit(i,j);
1167 // check the online zero-suppression conditions
1169 const Int_t maxNeighbours = 4;
1173 Int_t xList[maxNeighbours], yList[maxNeighbours];
1174 fSegmentation->Neighbours(i,j,&nn,xList,yList);
1176 for (in=0; in<nn; in++) {
1179 if (fHitMap2->TestHit(ix,iy)==kUnused) {
1180 CompressionParam(ix,dbx,tlx,thx);
1181 Int_t qn = (Int_t)(fHitMap2->GetSignal(ix,iy));
1182 qn -= dbx; // if baseline eq. is done here
1183 if ((qn-tlx) < minval) {
1184 fHitMap2->FlagHit(ix,iy);
1187 if ((qn - thx) >= minval) high=kTRUE;
1189 if(do10to8) signal = Convert10to8(signal);
1190 AddDigit(i,j,signal);
1192 if(do10to8) qns = Convert10to8(qn);
1194 if (!high) AddDigit(ix,iy,qns);
1196 if(!high) fHitMap2->FlagHit(ix,iy);
1199 } // loop over neighbours
1203 //____________________________________________
1204 void AliITSsimulationSDD::Init1D(){
1205 // this is just a copy-paste of input taken from 2D algo
1206 // Torino people should give input
1208 // Read 1D zero-suppression parameters for SDD
1211 if (!strstr(fParam,"file")) return;
1213 Int_t na,pos,tempTh;
1215 Float_t *savemu = new Float_t [fNofMaps];
1216 Float_t *savesigma = new Float_t [fNofMaps];
1217 char input[100],basel[100],par[100];
1221 Int_t minval = fResponse->MinVal();
1222 fResponse->Filenames(input,basel,par);
1225 // set first the disable and tol param
1228 filtmp = gSystem->ExpandPathName(fFileName.Data());
1229 FILE *param = fopen(filtmp,"r");
1233 fscanf(param,"%d %d %d %d ", &fT2[0], &fT2[1], &fTol[0], &fTol[1]);
1234 while(fscanf(param,"%d %f %f",&pos, &mu, &sigma) != EOF) {
1236 Error("Init1D ","Anode number not in increasing order!",
1241 savesigma[na]=sigma;
1242 if ((2.*sigma) < mu) {
1243 fD[na] = (Int_t)floor(mu - 2.0*sigma + 0.5);
1246 tempTh = (Int_t)floor(mu+2.25*sigma+0.5) - minval;
1247 if (tempTh < 0) tempTh=0;
1252 Error("Init1D "," THE FILE %s DOES NOT EXIST !",
1260 delete [] savesigma;
1266 //____________________________________________
1267 void AliITSsimulationSDD::Compress1D(){
1268 // 1D zero-suppression algorithm (from Gianluca A.)
1270 Int_t dis,tol,thres,decr,diff;
1272 UChar_t *str=fStream->Stream();
1275 Bool_t do10to8=fResponse->Do10to8();
1279 for (k=0; k<2; k++) {
1282 for (i=0; i<fNofMaps/2; i++) {
1283 Bool_t firstSignal=kTRUE;
1284 Int_t idx=i+k*fNofMaps/2;
1285 CompressionParam(idx,decr,thres);
1286 for (j=0; j<fMaxNofSamples; j++) {
1287 Int_t signal=(Int_t)(fHitMap2->GetSignal(idx,j));
1288 signal -= decr; // if baseline eq.
1289 if(do10to8) signal = Convert10to8(signal);
1290 if (signal <= thres) {
1294 // write diff in the buffer for HuffT
1295 str[counter]=(UChar_t)diff;
1300 if (diff > 127) diff=127;
1301 if (diff < -128) diff=-128;
1304 // tol has changed to 8 possible cases ? - one can write
1305 // this if(TMath::Abs(diff)<tol) ... else ...
1306 if(TMath::Abs(diff)<tol) diff=0;
1307 // or keep it as it was before
1309 if (tol==1 && (diff >= -2 && diff <= 1)) diff=0;
1310 if (tol==2 && (diff >= -4 && diff <= 3)) diff=0;
1311 if (tol==3 && (diff >= -16 && diff <= 15)) diff=0;
1313 AddDigit(idx,j,last+diff);
1315 AddDigit(idx,j,signal);
1319 // write diff in the buffer used to compute Huffman tables
1320 if (firstSignal) str[counter]=(UChar_t)signal;
1321 else str[counter]=(UChar_t)diff;
1326 } // loop time samples
1327 } // loop anodes one half of detector
1331 fStream->CheckCount(counter);
1333 // open file and write out the stream of diff's
1335 static Bool_t open=kTRUE;
1336 static TFile *outFile;
1337 Bool_t write = fResponse->OutputOption();
1341 SetFileName("stream.root");
1342 cout<<"filename "<<fFileName<<endl;
1343 outFile=new TFile(fFileName,"recreate");
1344 cout<<"I have opened "<<fFileName<<" file "<<endl;
1351 fStream->ClearStream();
1353 // back to galice.root file
1355 TTree *fAli=gAlice->TreeK();
1358 if (fAli) file =fAli->GetCurrentFile();
1363 //____________________________________________
1364 void AliITSsimulationSDD::StoreAllDigits(){
1365 // if non-zero-suppressed data
1367 Bool_t do10to8=fResponse->Do10to8();
1369 Int_t i, j, digits[3];
1370 for (i=0; i<fNofMaps; i++) {
1371 for (j=0; j<fMaxNofSamples; j++) {
1372 Int_t signal=(Int_t)(fHitMap2->GetSignal(i,j));
1373 if(do10to8) signal = Convert10to8(signal);
1374 if(do10to8) signal = Convert8to10(signal);
1378 fITS->AddRealDigit(1,digits);
1382 //____________________________________________
1384 void AliITSsimulationSDD::CreateHistograms(Int_t scale){
1385 // Creates histograms of maps for debugging
1389 fHis=new TObjArray(fNofMaps);
1390 TString sddName("sdd_");
1391 for (i=0;i<fNofMaps;i++) {
1393 sprintf(candNum,"%d",i+1);
1394 sddName.Append(candNum);
1395 (*fHis)[i] = new TH1F(sddName.Data(),"SDD maps",
1396 scale*fMaxNofSamples,0.,(Float_t) scale*fMaxNofSamples);
1400 //____________________________________________
1401 void AliITSsimulationSDD::FillHistograms(){
1402 // fill 1D histograms from map
1405 for( Int_t i=0; i<fNofMaps; i++) {
1406 TH1F *hist =(TH1F *)fHis->UncheckedAt(i);
1407 Int_t nsamples = hist->GetNbinsX();
1408 for( Int_t j=0; j<nsamples; j++) {
1409 Double_t signal=fHitMap2->GetSignal(i,j);
1410 hist->Fill((Float_t)j,signal);
1415 //____________________________________________
1417 void AliITSsimulationSDD::ResetHistograms(){
1419 // Reset histograms for this detector
1422 for (i=0;i<fNofMaps;i++ ) {
1423 if ((*fHis)[i]) ((TH1F*)(*fHis)[i])->Reset();
1429 //____________________________________________
1431 TH1F *AliITSsimulationSDD::GetAnode(Int_t wing, Int_t anode) {
1432 // Fills a histogram from a give anode.
1433 if (!fHis) return 0;
1435 if(wing <=0 || wing > 2) {
1436 cout << "Wrong wing number: " << wing << endl;
1439 if(anode <=0 || anode > fNofMaps/2) {
1440 cout << "Wrong anode number: " << anode << endl;
1444 Int_t index = (wing-1)*fNofMaps/2 + anode-1;
1445 return (TH1F*)((*fHis)[index]);
1448 //____________________________________________
1450 void AliITSsimulationSDD::WriteToFile(TFile *hfile) {
1451 // Writes the histograms to a file
1456 for(i=0; i<fNofMaps; i++) (*fHis)[i]->Write(); //fAdcs[i]->Write();
1459 //____________________________________________
1460 Float_t AliITSsimulationSDD::GetNoise() {
1461 // Returns the noise value
1463 //Bool_t do10to8=fResponse->Do10to8();
1464 //noise will always be in the liniar part of the signal
1467 Int_t threshold=fT1[0];
1469 char opt1[20], opt2[20];
1470 fResponse->ParamOptions(opt1,opt2);
1472 char *same = strstr(opt1,"same");
1473 Float_t noise,baseline;
1475 fResponse->GetNoiseParam(noise,baseline);
1477 static Bool_t readfile=kTRUE;
1478 //read baseline and noise from file
1479 if (readfile) ReadBaseline();
1483 TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
1484 if(c2) delete c2->GetPrimitive("noisehist");
1485 if(c2) delete c2->GetPrimitive("anode");
1486 else c2=new TCanvas("c2");
1488 c2->SetFillColor(0);
1490 TH1F *noisehist = new TH1F("noisehist","noise",100,0.,(float)2*threshold);
1491 TH1F *anode = new TH1F("anode","Anode Projection",fMaxNofSamples,0.,(float)fMaxNofSamples);
1493 for (i=0;i<fNofMaps;i++) {
1494 CompressionParam(i,decr,threshold);
1495 if (!same) GetAnodeBaseline(i,baseline,noise);
1497 for (k=0;k<fMaxNofSamples;k++) {
1498 Float_t signal=(Float_t)fHitMap2->GetSignal(i,k);
1499 //if (signal <= (float)threshold) noisehist->Fill(signal-baseline);
1500 if (signal <= (float)threshold) noisehist->Fill(signal);
1501 anode->Fill((float)k,signal);
1506 TF1 *gnoise = new TF1("gnoise","gaus",0.,threshold);
1507 noisehist->Fit("gnoise","RQ");
1510 Float_t mnoise = gnoise->GetParameter(1);
1511 cout << "mnoise : " << mnoise << endl;
1512 Float_t rnoise = gnoise->GetParameter(2);
1513 cout << "rnoise : " << rnoise << endl;