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