]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITS.cxx
Fix for Coverity #10278
[u/mrichter/AliRoot.git] / ITS / AliITS.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
18
19 ///////////////////////////////////////////////////////////////////////////////
20 //                                                                           //
21 //      An overview of the basic philosophy of the ITS code development      //
22 // and analysis is show in the figure below.                                 //
23 //Begin_Html                                                                 //
24 /*                                               
25 <img src="picts/ITS/ITS_Analysis_schema.gif">
26 </pre>
27 <br clear=left>
28 <font size=+2 color=red>
29 <p>Roberto Barbera is in charge of the ITS Offline code (1999).
30 <a href="mailto:roberto.barbera@ct.infn.it">Roberto Barbera</a>.
31 </font>
32 <pre>
33 */
34 //End_Html
35 //
36 //  AliITS. Inner Traking System base class.
37 //  This class contains the base procedures for the Inner Tracking System
38 //
39 //Begin_Html
40 /*
41 <img src="picts/ITS/AliITS_Class_Diagram.gif">
42 </pre>
43 <br clear=left>
44 <font size=+2 color=red>
45 <p>This show the class diagram of the different elements that are part of
46 the AliITS class.
47 </font>
48 <pre>
49 */
50 //End_Html
51 //
52 // Version: 0
53 // Written by Rene Brun, Federico Carminati, and Roberto Barbera
54 //
55 // Version: 1
56 // Modified and documented by Bjorn S. Nilsen
57 // July 11 1999
58 //
59 // Version: 2
60 // Modified and documented by A. Bologna
61 // October 18 1999
62 //
63 // AliITS is the general base class for the ITS. Also see AliDetector for
64 // futher information.
65 //
66 ///////////////////////////////////////////////////////////////////////////////
67
68 #include <stdlib.h>
69 #include <TClonesArray.h>
70 #include <TFile.h>
71 #include <TParticle.h>
72 #include <TString.h>
73 #include <TTree.h>
74 #include <TVirtualMC.h>
75 #include "AliDetector.h"
76 #include "AliITS.h"
77 #include "AliITSDetTypeSim.h"
78 #include "AliITSDDLRawData.h"
79 #include "AliITSLoader.h"
80 #include "AliITShit.h"
81 #include "AliITSmodule.h"
82 #include "AliITSpListItem.h"
83 #include "AliITSsimulation.h"
84 #include "AliITSsimulationFastPoints.h"
85 #include "AliMC.h"
86 #include "AliITSDigitizer.h"
87 #include "AliITSRecPoint.h"
88 #include "AliITSsegmentationSPD.h"
89 #include "AliITSsegmentationSDD.h"
90 #include "AliITSsimulationSDD.h"
91 #include "AliITSCalibrationSDD.h"
92 #include "AliITSCalibrationSSD.h"
93 #include "AliITSsegmentationSSD.h"
94 #include "AliITSRawStreamSPD.h"
95 #include "AliITSRawStreamSSD.h"
96 #include "AliITSRawStreamSDD.h"
97 #include "AliRawReader.h"
98 #include "AliRun.h"
99 #include "AliLog.h"
100 #include "AliITSInitGeometry.h"
101 #include "AliITSFOSignalsSPD.h"
102
103 ClassImp(AliITS)
104
105 //______________________________________________________________________
106 AliITS::AliITS() : AliDetector(),
107 fDetTypeSim(0),
108 fEuclidOut(0),
109 fOpt("All"),
110 fIdN(0),
111 fIdSens(0),
112 fIdName(0),
113 fITSmodules(0),
114 fTiming(kFALSE),
115 fSimuParam(0),
116 fModA(0),
117 fpSDigits(0)
118 {
119   // Default initializer for ITS
120   //      The default constructor of the AliITS class. In addition to
121   // creating the AliITS class it zeros the variables fIshunt (a member
122   // of AliDetector class), fEuclidOut, and fIdN, and zeros the pointers
123   // fIdSens, and fIdName. The AliDetector default constructor
124   // is also called.
125   
126 //    SetDetectors(); // default to fOpt="All". This variable not written out.
127 //PH    SetMarkerColor(kRed);
128 }
129 //______________________________________________________________________
130 AliITS::AliITS(const Char_t *title):
131   AliDetector("ITS",title),
132   fDetTypeSim(0),
133   fEuclidOut(0),
134   fOpt("All"),
135   fIdN(0),
136   fIdSens(0),
137   fIdName(0),
138   fITSmodules(0),
139   fTiming(kFALSE),
140   fSimuParam(0),
141   fModA(0),
142   fpSDigits(0)
143 {
144     //     The standard Constructor for the ITS class. 
145     // It also zeros the variables
146     // fIshunt (a member of AliDetector class), fEuclidOut, and zeros
147     // the pointers fIdSens and fIdName. To help in displaying hits via the
148     // ROOT macro display.C AliITS also sets the marker color to red. The
149     // variables passes with this constructor, const char *name and *title,
150     // are used by the constructor of AliDetector class. See AliDetector
151     // class for a description of these parameters and its constructor
152     // functions.
153     // Inputs:
154     //   Char_t *title  Simulation title for the ITS
155     // Outputs:
156     //   none.
157     // Return:
158     //   none.
159   
160     fHits = new TClonesArray("AliITShit",1560); // from AliDetector
161     if(gAlice->GetMCApp()) gAlice->GetMCApp()->AddHitList(fHits);
162     //fNhits=0;  //done in AliDetector(name,title)
163     SetDetectors(); // default to fOpt="All". This variable not written out.
164     fDetTypeSim   = new AliITSDetTypeSim();
165     //PH  SetMarkerColor(kRed);
166     if(!fLoader) MakeLoader(AliConfig::GetDefaultEventFolderName());
167     fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
168 }
169 //______________________________________________________________________
170 AliITS::AliITS(const char *name, const char *title):
171   AliDetector(name,title),
172   fDetTypeSim(0),
173   fEuclidOut(0),
174   fOpt("All"),
175   fIdN(0),
176   fIdSens(0),
177   fIdName(0),
178   fITSmodules(0),
179   fTiming(kFALSE),
180   fSimuParam(0),
181   fModA(0),
182   fpSDigits(0)
183 {
184   //     The standard Constructor for the ITS class. 
185   // It also zeros the variables
186   // fIshunt (a member of AliDetector class), fEuclidOut, and zeros
187   // the pointers fIdSens and fIdName. To help in displaying hits via the
188   // ROOT macro display.C AliITS also sets the marker color to red. The
189   // variables passes with this constructor, const char *name and *title,
190   // are used by the constructor of AliDetector class. See AliDetector
191   // class for a description of these parameters and its constructor
192   // functions.
193   
194   fHits = new TClonesArray("AliITShit",1560);
195   if(gAlice->GetMCApp()) gAlice->GetMCApp()->AddHitList(fHits);
196   //fNhits=0;  //done in AliDetector(name,title)
197
198   SetDetectors(); // default to fOpt="All". This variable not written out.
199     
200   fDetTypeSim   = new AliITSDetTypeSim();
201   //PH  SetMarkerColor(kRed);
202   if(!fLoader) MakeLoader(AliConfig::GetDefaultEventFolderName());
203   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
204
205 }
206 //______________________________________________________________________
207 AliITS::~AliITS(){
208     // Default destructor for ITS.
209     //     The default destructor of the AliITS class. In addition to deleting
210     // the AliITS class it deletes the memory pointed to by 
211     // fIdSens, fIdName, fDetTypeSim and it's contents.
212     // Inputs:
213     //      none.
214     // Outputs:
215     //      none.
216     // Return:
217     //      none.
218
219     if (fHits) {
220       fHits->Delete();
221       delete fHits;
222       fHits=0;
223     }
224     if(fITSmodules) {
225         this->ClearModules();
226         delete fITSmodules;
227         fITSmodules = 0;
228     }// end if fITSmodules!=0
229
230     delete[] fIdName;  // Array of TStrings
231     delete[] fIdSens;
232     Int_t size   = AliITSgeomTGeo::GetNModules();
233     if (fDetTypeSim){
234       delete fDetTypeSim;
235       fDetTypeSim = 0;
236     }
237     if(fSimuParam){
238       delete fSimuParam;
239       fSimuParam=0;
240     }
241     if(fModA){
242       if(size>0){
243         for(Int_t j=0; j<size; j++){
244           fModA[j]->Delete();
245           delete fModA[j];
246         }
247       }
248       delete []fModA;
249     }
250     if(fpSDigits){
251       fpSDigits->Delete();
252       delete fpSDigits;
253     }
254 }
255
256 //______________________________________________________________________
257 AliDigitizer* AliITS::CreateDigitizer(AliRunDigitizer* manager)const{
258     // Creates the AliITSDigitizer in a standard way for use via AliModule.
259     // This function can not be included in the .h file because of problems
260     // with the order of inclusion (recursive).
261     // Inputs:
262     //    AliRunDigitizer *manager  The Manger class for Digitization
263     // Output:
264     //    none.
265     // Return:
266     //    A new AliITSRunDigitizer (cast as a AliDigitizer).
267
268      return new AliITSDigitizer(manager);
269 }
270 //______________________________________________________________________
271 void AliITS::Init(){
272     // Initializer ITS after it has been built
273     //     This routine initializes the AliITS class. It is intended to be
274     // called from the Init function in AliITSv?. Besides displaying a banner
275     // indicating that it has been called it initializes the array fIdSens
276     // and sets the default segmentation, response, digit and raw cluster
277     // classes therefore it should be called after a call to CreateGeometry.
278     // Inputs:
279     //      none.
280     // Outputs:
281     //      none.
282     // Return:
283     //      none.
284     Int_t i;
285     // Array of TStrings
286     if(gMC) for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId(fIdName[i]);
287  
288 }
289 //______________________________________________________________________
290 void AliITS::SetDefaults(){
291     // sets the default segmentation, response, digit and raw cluster classes.
292     // Inputs:
293     //      none.
294     // Outputs:
295     //      none.
296     // Return:
297     //      none.
298     AliInfoClass("AliITS::Setting Defaults");
299     if(!fDetTypeSim) { 
300      Error("SetDefaults()","fDetTypeSim is 0!"); 
301      return;
302     }
303
304     fDetTypeSim->SetDefaults();
305     if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
306
307 }
308 //______________________________________________________________________
309 void AliITS::SetDefaultSimulation(){
310     // sets the default simulation.
311     // Inputs:
312     //      none.
313     // Outputs:
314     //      none.
315     // Return:
316     //      none.
317     if(!fDetTypeSim) { 
318      Error("SetDefaultSimulation()","fDetTypeSim is 0!"); 
319      return;
320     }
321
322     fDetTypeSim->SetDefaultSimulation();
323     if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
324
325 }
326
327
328 //______________________________________________________________________
329 void AliITS::MakeBranch(Option_t* option){
330     // Creates Tree branches for the ITS.
331     // Inputs:
332     //      Option_t *option    String of Tree types S,D, and/or R.
333     //      const char *file    String of the file name where these branches
334     //                          are to be stored. If blank then these branches
335     //                          are written to the same tree as the Hits were
336     //                          read from.
337     // Outputs:
338     //      none.
339     // Return:
340     //      none.
341   if(!fDetTypeSim) {
342     Error("MakeBranch","fDetTypeSim is 0!");
343     return;
344   }
345
346   Bool_t cH = (strstr(option,"H")!=0);
347   Bool_t cS = (strstr(option,"S")!=0);
348   Bool_t cD = (strstr(option,"D")!=0);
349   
350   if(cH && (fHits == 0x0)) fHits = new TClonesArray("AliITShit", 1560);
351   AliDetector::MakeBranch(option);
352   
353   if(cS) MakeBranchS(0);
354   if(cD) MakeBranchD(0);
355
356
357 }
358 //___________________________________________________________________
359 void AliITS::MakeBranchS(const char* fl){
360
361   // Creates Tree Branch for the ITS summable digits.
362   // Inputs:
363   //      cont char *fl  File name where SDigits branch is to be written
364   //                     to. If blank it write the SDigits to the same
365   //                     file in which the Hits were found.
366
367   
368   if(!fDetTypeSim){
369     Error("MakeBranchS","fDetTypeSim is 0!");
370   }
371   Int_t buffersize = 4000;
372   char branchname[31];
373
374   // only one branch for SDigits.
375   snprintf(branchname,30,"%s",GetName());
376
377   if(fLoader->TreeS()){
378     TClonesArray* sdig = (TClonesArray*)fDetTypeSim->GetSDigits();
379     MakeBranchInTree(fLoader->TreeS(),branchname,&sdig,buffersize,fl);
380   } 
381 }
382 //______________________________________________________________________
383 void AliITS::MakeBranchD(const char* file){
384
385   //Make branch for digits
386   if(!fDetTypeSim) {
387     Warning("MakeBranchD","fDetTypeSim is 0!");
388     return;
389   }
390   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
391   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
392   MakeBranchInTreeD(fLoader->TreeD(),file);
393 }
394
395 //___________________________________________________________________
396 void AliITS:: MakeBranchInTreeD(TTree* treeD, const char* file){
397   // Creates Tree branches for the ITS.
398
399   if(!fDetTypeSim){
400     Error("MakeBranchS","fDetTypeSim is 0!");
401   }
402   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
403   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
404
405   const Char_t *det[3] = {"SPD","SDD","SSD"};
406   const Char_t* digclass;
407   Int_t buffersize = 4000;
408   Char_t branchname[31];
409   
410   if(!fDetTypeSim->GetDigits()){
411     fDetTypeSim->SetDigits(new TObjArray(fgkNTYPES));
412   }
413   for(Int_t i=0;i<fgkNTYPES;i++){
414     digclass = fDetTypeSim->GetDigitClassName(i);
415     TString classn = digclass;
416     if(!((fDetTypeSim->GetDigits())->At(i))){
417       (fDetTypeSim->GetDigits())->AddAt(new TClonesArray(classn.Data(),1000),i);
418     }
419     else ResetDigits(i);  
420     if(fgkNTYPES==3) snprintf(branchname,30,"%sDigits%s",GetName(),det[i]);
421     else sprintf(branchname,"%sDigits%d",GetName(),i+1);
422     TObjArray* dig = DigitsAddress(i);
423     if(GetDigits() && treeD) AliDetector::MakeBranchInTree(treeD,branchname, &dig,buffersize,file);
424   }
425
426 }
427 //______________________________________________________________________
428 void AliITS::SetTreeAddress(){
429     // Set branch address for the Trees.
430     // Inputs:
431     //      none.
432     // Outputs:
433     //      none.
434     // Return:
435     //      none.
436     
437   if(!fDetTypeSim) {
438     Error("SetTreeAddress","fDetTypeSim is 0!");
439     return;
440   }
441
442   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
443   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
444
445   TTree *treeS = fLoader->TreeS();
446   TTree *treeD = fLoader->TreeD();
447   if (fLoader->TreeH() && (fHits == 0x0)) {
448       fHits = new TClonesArray("AliITShit", 1560);
449   }
450   AliDetector::SetTreeAddress();
451
452   fDetTypeSim->SetTreeAddressS(treeS, (Char_t*)GetName());
453   fDetTypeSim->SetTreeAddressD(treeD, (Char_t*)GetName());
454 }
455 //______________________________________________________________________
456 void AliITS::AddHit(Int_t track, Int_t *vol, Float_t *hits){
457     // Add an ITS hit
458     //     The function to add information to the AliITShit class. See the
459     // AliITShit class for a full description. This function allocates the
460     // necessary new space for the hit information and passes the variable
461     // track, and the pointers *vol and *hits to the AliITShit constructor
462     // function.
463     // Inputs:
464     //      Int_t   track   Track number which produced this hit.
465     //      Int_t   *vol    Array of Integer Hit information. See AliITShit.h
466     //      Float_t *hits   Array of Floating Hit information.  see AliITShit.h
467     // Outputs:
468     //      none.
469     // Return:
470     //      none.
471   TClonesArray &lhits = *fHits;
472   new(lhits[fNhits++]) AliITShit(fIshunt,track,vol,hits);
473 }
474
475 //______________________________________________________________________
476 void AliITS::FillModules(Int_t /* evnt */,Int_t bgrev,Int_t /* nmodules */,
477                          Option_t *option, const char *filename){
478   // fill the modules with the sorted by module hits; add hits from
479   // background if option=Add.
480
481   static TTree *trH1;                 //Tree with background hits
482   static Bool_t first=kTRUE;
483   static TFile *file;
484   const char *addBgr = strstr(option,"Add");
485   
486   if (addBgr ) {
487     if(first) {
488       file=new TFile(filename);
489     } // end if first
490     first=kFALSE;
491     file->cd();
492     file->ls();
493     // Get Hits Tree header from file
494     if(trH1) delete trH1;
495     trH1=0;
496     
497     char treeName[21];
498     snprintf(treeName,20,"TreeH%d",bgrev);
499     trH1 = (TTree*)gDirectory->Get(treeName);
500     if (!trH1) {
501       Error("FillModules","cannot find Hits Tree for event:%d",bgrev);
502     } // end if !trH1
503     // Set branch addresses
504   } // end if addBgr
505   
506   FillModules(fLoader->TreeH(),0); // fill from this file's tree.
507     
508   if (addBgr ) {
509     FillModules(trH1,10000000); // Default mask 10M.
510     TTree *fAli=fLoader->GetRunLoader()->TreeK();
511     TFile *fileAli=0;
512     if (fAli) {
513       fileAli =fAli->GetCurrentFile();
514       fileAli->cd();
515     }
516   } // end if add
517   
518   
519 }
520 //______________________________________________________________________
521 void AliITS::FillModules(TTree *treeH, Int_t mask) {
522     // fill the modules with the sorted by module hits; 
523     // can be called many times to do a merging
524     // Inputs:
525     //      TTree *treeH  The tree containing the hits to be copied into
526     //                    the modules.
527     //      Int_t mask    The track number mask to indecate which file
528     //                    this hits came from.
529     // Outputs:
530     //      none.
531     // Return:
532     //      none.
533
534     if (treeH == 0x0)
535      {
536        AliError("Tree H  is NULL");
537        return;
538      }
539     Int_t lay,lad,det,index;
540     AliITShit *itsHit=0;
541     AliITSmodule *mod=0;
542     char branchname[21];
543     snprintf(branchname,20,"%s",GetName());
544     TBranch *branch = treeH->GetBranch(branchname);
545     if (!branch) {
546         Error("FillModules","%s branch in TreeH not found",branchname);
547         return;
548     } // end if !branch
549     branch->SetAddress(&fHits);
550     Int_t nTracks =(Int_t) treeH->GetEntries();
551     Int_t iPrimTrack,h;
552     for(iPrimTrack=0; iPrimTrack<nTracks; iPrimTrack++){
553         ResetHits();
554         Int_t nBytes = treeH->GetEvent(iPrimTrack);
555         if (nBytes <= 0) continue;
556         Int_t nHits = fHits->GetEntriesFast();
557         for(h=0; h<nHits; h++){
558             itsHit = (AliITShit *)fHits->UncheckedAt(h);
559             itsHit->GetDetectorID(lay,lad,det);
560             if (GetITSgeom()) {
561                 index = GetITSgeom()->GetModuleIndex(lay,lad,det);
562             } else {
563                 index=det-1; // This should not be used.
564             } // end if [You must have fITSgeom for this to work!]
565             mod = GetModule(index);
566             itsHit->SetTrack(itsHit->GetTrack()+mask); // Set track mask.
567             mod->AddHit(itsHit,iPrimTrack,h);
568         } // end loop over hits 
569     } // end loop over tracks
570 }
571
572 //______________________________________________________________________
573 Bool_t AliITS::InitModules(Int_t size,Int_t &nmodules){
574     // Initialize the modules array.
575     // Inputs:
576     //      Int_t size  Size of array of the number of modules to be
577     //                  created. If size <=0 then the number of modules
578     //                  is gotten from AliITSgeom class kept in fITSgeom.
579     // Outputs:
580     //      Int_t &nmodules The number of modules existing.
581     // Return:
582     //      none.
583
584     if(fITSmodules){ 
585         fITSmodules->Delete();
586         delete fITSmodules;
587     } // end fir fITSmoudles
588
589     if(!fDetTypeSim) {
590       Error("InitModules","fDetTypeSim is null!");
591       return kFALSE;
592     }
593     if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
594
595     Int_t nl,indexMAX,index;
596
597     if(size<=0){ // default to using data stored in AliITSgeom
598         if(fDetTypeSim->GetITSgeom()==0) {
599             Error("InitModules","fITSgeom not defined");
600             return kFALSE;
601         } // end if fITSgeom==0
602         nl = fDetTypeSim->GetITSgeom()->GetNlayers();
603         indexMAX = fDetTypeSim->GetITSgeom()->GetIndexMax();
604         nmodules = indexMAX;
605         fITSmodules = new TObjArray(indexMAX);
606         for(index=0;index<indexMAX;index++){
607             fITSmodules->AddAt( new AliITSmodule(index),index);
608         } // end for index
609     }else{
610         fITSmodules = new TObjArray(size);
611         for(index=0;index<size;index++) {
612             fITSmodules->AddAt( new AliITSmodule(index),index);
613         } // end for index
614
615         nmodules = size;
616     } // end i size<=0
617     return kTRUE;
618 }
619 //______________________________________________________________________
620 void AliITS::Hits2SDigits(){
621     // Standard Hits to summable Digits function.
622     // Inputs:
623     //      none.
624     // Outputs:
625     //      none.
626   
627
628    if(!fDetTypeSim) {
629      Error("Hits2SDigits","fDetTypeSim is null!");
630      return; 
631   } 
632      
633   SetDefaults();
634   fLoader->LoadHits("read");
635   fLoader->LoadSDigits("recreate");
636   AliRunLoader* rl = fLoader->GetRunLoader(); 
637   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
638   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
639
640   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
641         // Do the Hits to Digits operation. Use Standard input values.
642         // Event number from file, no background hit merging , use size from
643         // AliITSgeom class, option="All", input from this file only.
644     rl->GetEvent(iEvent);
645     if (!fLoader->TreeS()) fLoader->MakeTree("S");
646     MakeBranch("S");
647     SetTreeAddress();
648     HitsToPreDigits(iEvent,0,-1," ",fOpt," ");
649   } // end for iEvent
650     
651   fLoader->UnloadHits();
652   fLoader->UnloadSDigits();
653   
654 }
655 //______________________________________________________________________
656 void AliITS::Hits2Digits(){
657
658   //Conversion from hits to digits
659   if(!fDetTypeSim) {
660     Error("Hits2SDigits","fDetTypeSim is 0!");
661     return;
662   }
663    
664   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
665   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
666   SetDefaults();
667
668   fLoader->LoadHits("read");
669   fLoader->LoadDigits("recreate");
670   AliRunLoader* rl = fLoader->GetRunLoader(); 
671   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
672     rl->GetEvent(iEvent);
673     if (!fLoader->TreeD()) fLoader->MakeTree("D");
674     MakeBranch("D");
675     SetTreeAddress();   
676     HitsToDigits(iEvent,0,-1," ",fOpt," ");
677   } 
678   
679   fLoader->UnloadHits();
680   fLoader->UnloadDigits();
681   
682 }
683
684 //______________________________________________________________________
685 void AliITS::HitsToDigits(Int_t evNumber,Int_t bgrev,Int_t size,
686                           Option_t *option,Option_t *opt,
687                           const char *filename){
688     //   Keep galice.root for signal and name differently the file for 
689     // background when add! otherwise the track info for signal will be lost !
690     // the condition below will disappear when the geom class will be
691     // initialized for all versions - for the moment it is only for v5 !
692     // 7 is the SDD beam test version.
693     // Inputs:
694     //      Int_t evnt       Event to be processed.
695     //      Int_t bgrev      Background Hit tree number.
696     //      Int_t nmodules   Not used.
697     //      Option_t *option String indicating if merging hits or not. To
698     //                       merge hits set equal to "Add". Otherwise no
699     //                       background hits are considered.
700     //      Test_t *filename File name containing the background hits..
701     // Outputs:
702     //      none.
703     // Return:
704     //      none.
705
706   if(!fDetTypeSim) {
707     Error("HitsToDigits","fDetTypeSim is null!");
708     return;
709   }
710   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
711   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
712   if(!GetITSgeom()) return; // need transformations to do digitization.
713   AliITSgeom *geom = GetITSgeom();
714
715   const char *all = strstr(opt,"All");
716   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
717                         strstr(opt,"SSD")};
718   static Bool_t setDef=kTRUE;
719   if (setDef) SetDefaultSimulation();
720   setDef=kFALSE;
721   
722   Int_t nmodules;
723   InitModules(size,nmodules);
724   FillModules(evNumber,bgrev,nmodules,option,filename);
725  
726   // Reset Fast-OR signals for this event
727   fDetTypeSim->ResetFOSignals();
728
729   AliITSsimulation *sim      = 0;
730   AliITSmodule     *mod      = 0;
731   Int_t id;
732   for(Int_t module=0;module<geom->GetIndexMax();module++){
733     id       = geom->GetModuleType(module);
734     if (!all && !det[id]) continue;
735     sim      = (AliITSsimulation*)fDetTypeSim->GetSimulationModel(id);
736     if (!sim) {
737       Error("HitsToDigits","The simulation class was not "
738             "instanciated for module %d type %s!",module,
739             geom->GetModuleTypeName(module));
740       exit(1);
741     } // end if !sim
742     mod      = (AliITSmodule *)fITSmodules->At(module);
743     sim->DigitiseModule(mod,module,evNumber);
744     // fills all branches - wasted disk space
745     fLoader->TreeD()->Fill(); 
746     ResetDigits();
747   } // end for module
748   
749   ClearModules();
750  
751   // Add Fast-OR signals to event (only one object per event)
752   if (all || det[0]) { // SPD present
753     WriteFOSignals();
754   }
755   
756   fLoader->TreeD()->GetEntries();
757   fLoader->TreeD()->AutoSave();
758   // reset tree
759   fLoader->TreeD()->Reset();
760 }
761 //_____________________________________________________________________
762 void AliITS::Hits2PreDigits(){ 
763   // Turn hits into SDigits
764
765   if(!fDetTypeSim) {
766     Error("Hits2SDigits","fDetTypeSim is 0!");
767     return;
768   }
769    
770   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
771   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
772   SetDefaults();
773   
774   HitsToPreDigits(fLoader->GetRunLoader()->GetEventNumber(),
775                   0,-1," ",fOpt," ");
776 }
777
778 //______________________________________________________________________
779 void AliITS::HitsToPreDigits(Int_t evNumber,Int_t bgrev,Int_t size,
780                              Option_t *option,Option_t *opt,
781                              const char *filename){
782     //   Keep galice.root for signal and name differently the file for 
783     // background when add! otherwise the track info for signal will be lost !
784     // the condition below will disappear when the geom class will be
785     // initialized for all versions - for the moment it is only for v5 !
786     // 7 is the SDD beam test version.
787     // Inputs:
788     //      Int_t evnt       Event to be processed.
789     //      Int_t bgrev      Background Hit tree number.
790     //      Int_t nmodules   Not used.
791     //      Option_t *option String indicating if merging hits or not. To
792     //                       merge hits set equal to "Add". Otherwise no
793     //                       background hits are considered.
794     //      Test_t *filename File name containing the background hits..
795     // Outputs:
796     //      none.
797     // Return:
798     //      none.
799
800  
801   if(!fDetTypeSim) {
802     Error("HitsToPreDigits","fDetTypeSim is null!");
803     return;
804   }
805   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
806   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
807
808   if(!GetITSgeom()){
809     Error("HitsToPreDigits","fGeom is null!");
810     return; // need transformations to do digitization.
811   }
812   AliITSgeom *geom = GetITSgeom();
813
814   const char *all = strstr(opt,"All");
815   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
816                         strstr(opt,"SSD")};
817   static Bool_t setDef=kTRUE;
818   if (setDef) SetDefaultSimulation();
819   setDef=kFALSE;
820   
821   Int_t nmodules;
822   InitModules(size,nmodules);
823   FillModules(evNumber,bgrev,nmodules,option,filename);
824   
825
826   AliITSsimulation *sim      = 0;
827   AliITSmodule     *mod      = 0;
828   Int_t id,module;
829   for(module=0;module<geom->GetIndexMax();module++){
830     id       = geom->GetModuleType(module);
831     if (!all && !det[id]) continue;
832     sim      = (AliITSsimulation*)GetSimulationModel(id);
833     if (!sim) {
834       Error("HitsToPreDigits","The simulation class was not "
835             "instanciated for module %d type %s!",module,
836             geom->GetModuleTypeName(module));
837       exit(1);
838     } // end if !sim
839     mod      = (AliITSmodule *)fITSmodules->At(module);
840     sim->SDigitiseModule(mod,module,evNumber);
841     // fills all branches - wasted disk space
842     fLoader->TreeS()->Fill(); 
843     fDetTypeSim->ResetSDigits();
844   } // end for module
845
846   ClearModules();
847
848   
849   fLoader->TreeS()->GetEntries();
850   fLoader->TreeS()->AutoSave();
851   fLoader->WriteSDigits("OVERWRITE");
852   // reset tree
853   fLoader->TreeS()->Reset();
854 }
855
856 //_____________________________________________________________________
857 void AliITS::HitsToFastRecPoints(Int_t evNumber,Int_t bgrev,Int_t size,
858                                   Option_t *opt0,Option_t *opt1,
859                                  const char *flnm){
860     // keep galice.root for signal and name differently the file for 
861     // background when add! otherwise the track info for signal will be lost !
862     // the condition below will disappear when the geom class will be
863     // initialized for all versions - for the moment it is only for v5 !
864     // Inputs:
865     //      Int_t evnt       Event to be processed.
866     //      Int_t bgrev      Background Hit tree number.
867     //      Int_t size       Size used by InitModules. See InitModules.
868     //      Option_t *opt0   Option passed to FillModules. See FillModules.
869     //      Option_t *opt1   String indicating if merging hits or not. To
870     //                       merge hits set equal to "Add". Otherwise no
871     //                       background hits are considered.
872     //      Test_t *flnm     File name containing the background hits..
873     // Outputs:
874     //      none.
875     // Return:
876     //      none.
877
878
879
880   if(!GetITSgeom()){
881     Error("HitsToPreDigits","fGeom is null!");
882     return; // need transformations to do digitization.
883   }
884   AliITSgeom *geom = GetITSgeom();
885
886   AliITSLoader *pITSloader = (AliITSLoader*)fLoader;
887
888   const char *all = strstr(opt1,"All");
889   const char *det[3] ={strstr(opt1,"SPD"),strstr(opt1,"SDD"),
890                        strstr(opt1,"SSD")};
891   Int_t nmodules;
892   InitModules(size,nmodules);
893   FillModules(evNumber,bgrev,nmodules,opt0,flnm);
894
895   AliITSsimulation *sim      = 0;
896   AliITSmodule     *mod      = 0;
897   Int_t id,module;
898
899   TTree *lTR = pITSloader->TreeR();
900   if(!lTR) {
901     pITSloader->MakeTree("R");
902     lTR = pITSloader->TreeR();
903   }
904   
905   TClonesArray* ptarray = new TClonesArray("AliITSRecPoint",1000);
906   TBranch* branch = (TBranch*)lTR->Branch("ITSRecPointsF",&ptarray);
907   branch->SetAddress(&ptarray);
908   //m.b. : this change is nothing but a nice way to make sure
909   //the CPU goes up !    
910   for(module=0;module<geom->GetIndexMax();module++){
911     id       = geom->GetModuleType(module);
912     if (!all && !det[id]) continue;
913     sim      = (AliITSsimulation*)GetSimulationModel(id);
914     if (!sim) {
915       Error("HitsToFastPoints","The simulation class was not "
916             "instantiated for module %d type %s!",module,
917             geom->GetModuleTypeName(module));
918       exit(1);
919     } // end if !sim
920     mod      = (AliITSmodule *)fITSmodules->At(module);
921     sim->CreateFastRecPoints(mod,module,gRandom,ptarray);
922     lTR->Fill();
923     ptarray->Clear();
924   } // end for module
925
926   ClearModules();
927   fLoader->WriteRecPoints("OVERWRITE");
928   delete ptarray;
929 }
930 //_____________________________________________________________________
931 Int_t AliITS::Hits2Clusters(TTree *hTree, TTree *cTree) {
932   //------------------------------------------------------------
933   // This function creates ITS clusters
934   //------------------------------------------------------------
935   if(!GetITSgeom()){
936     Error("HitsToPreDigits","fGeom is null!");
937     return 1; // need transformations to do digitization.
938   }
939   AliITSgeom *geom=GetITSgeom();
940   Int_t mmax=geom->GetIndexMax();
941
942   InitModules(-1,mmax);
943   FillModules(hTree,0);
944
945   TClonesArray *points = new TClonesArray("AliITSRecPoint",1000);
946   TBranch *branch=cTree->GetBranch("ITSRecPoints");
947   if (!branch) cTree->Branch("ITSRecPoints",&points);
948   else branch->SetAddress(&points);
949
950   AliITSsimulationFastPoints sim;
951   Int_t ncl=0;
952   for (Int_t m=0; m<mmax; m++) {
953     AliITSmodule *mod=GetModule(m);      
954     sim.CreateFastRecPoints(mod,m,gRandom,points);      
955     ncl+=points->GetEntriesFast();
956     cTree->Fill();
957     points->Clear();
958   }
959
960   AliDebug(1,Form("Number of found fast clusters : %d",ncl));
961
962   //cTree->Write();
963
964   delete points;
965   return 0;
966 }
967
968 //_____________________________________________________________________
969 void AliITS::CheckLabels(Int_t lab[3]) const {
970   //------------------------------------------------------------
971   // Tries to find mother's labels
972   //------------------------------------------------------------
973
974   if(lab[0]<0 && lab[1]<0 && lab[2]<0) return; // In case of no labels just exit
975
976   Int_t ntracks = gAlice->GetMCApp()->GetNtrack();
977   for (Int_t i=0;i<3;i++){
978     Int_t label = lab[i];
979     if (label>=0 && label<ntracks) {
980       TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
981       if (part->P() < 0.005) {
982         Int_t m=part->GetFirstMother();
983         if (m<0) {      
984           continue;
985         }
986         if (part->GetStatusCode()>0) {
987           continue;
988         }
989         lab[i]=m;       
990       }
991     }    
992   }
993   
994 }
995
996 //______________________________________________________________________
997 void AliITS::SDigitsToDigits(Option_t *opt){
998   // Standard Summable digits to Digits function.
999   // Inputs:
1000   //      none.
1001   // Outputs:
1002   //      none.
1003   if (!fDetTypeSim) {
1004     AliError("fDetTypeSim is 0!");
1005     return;
1006   }
1007   const char *all = strstr(opt,"All");
1008   const char *det[3] ={strstr(opt,"SPD"),strstr(opt,"SDD"),
1009                        strstr(opt,"SSD")};
1010
1011   // Reset Fast-OR signals for this event
1012   fDetTypeSim->ResetFOSignals();
1013
1014   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
1015   SetDefaults();
1016   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
1017   fDetTypeSim->SDigitsToDigits(opt,(Char_t*)GetName());
1018
1019   // Add Fast-OR signals to event (only one object per event)
1020   if (all || det[0]) { // SPD present
1021    WriteFOSignals();
1022   }
1023 }
1024
1025 //______________________________________________________________________
1026 void AliITS::ResetDigits(){
1027     // Reset number of digits and the digits array for the ITS detector.
1028     // Inputs:
1029     //      none.
1030     // Outputs:
1031     //      none.
1032     if(!fDetTypeSim) {
1033       Error("ResetDigits","fDetTypeSim is 0!");
1034       return;
1035     }
1036    
1037     fDetTypeSim->ResetDigits();
1038
1039
1040 }
1041 //______________________________________________________________________
1042 void AliITS::ResetDigits(Int_t branch){
1043     // Reset number of digits and the digits array for this branch.
1044     // Inputs:
1045     //      none.
1046     // Outputs:
1047     //      none.
1048
1049     if(!fDetTypeSim) {
1050       Error("ResetDigits","fDetTypeSim is 0!");
1051       return;
1052     }
1053    
1054     fDetTypeSim->ResetDigits(branch);
1055
1056 }
1057 //______________________________________________________________________
1058 void AliITS::AddSumDigit(AliITSpListItem &sdig){
1059     // Adds the a module full of summable digits to the summable digits tree.
1060     // Inputs:
1061     //      AliITSpListItem &sdig   SDigit to be added to SDigits tree.
1062     // Outputs:
1063     //      none.
1064     // Return:
1065     //      none.
1066
1067     if(!fDetTypeSim) {
1068       Error("AddSumDigit","fDetTypeSim is 0!");
1069       return;
1070     }
1071     fDetTypeSim->AddSumDigit(sdig);
1072     
1073 }
1074 //______________________________________________________________________
1075 void AliITS::AddSimDigit(Int_t branch, AliITSdigit *d){
1076     //    Add a simulated digit.
1077     // Inputs:
1078     //      Int_t id        Detector type number.
1079     //      AliITSdigit *d  Digit to be added to the Digits Tree. See 
1080     //                      AliITSdigit.h
1081     // Outputs:
1082     //      none.
1083     // Return:
1084     //      none.
1085
1086     if(!fDetTypeSim) {
1087       Error("AddSimDigit","fDetTypeSim is 0!");
1088       return;
1089     }
1090     fDetTypeSim->AddSimDigit(branch,d);
1091
1092 }
1093 //______________________________________________________________________
1094 void AliITS::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,Int_t *tracks,
1095                          Int_t *hits,Float_t *charges, Int_t sigexpanded){
1096   //   Add a simulated digit to the list.
1097   // Inputs:
1098   //      Int_t id        Detector type number.
1099   //      Float_t phys    Physics indicator. See AliITSdigits.h
1100   //      Int_t *digits   Integer array containing the digits info. See 
1101   //                      AliITSdigit.h
1102   //      Int_t *tracks   Integer array [AliITSdigitS?D::GetNTracks()] 
1103   //                      containing the track numbers that contributed to
1104   //                      this digit.
1105   //      Int_t *hits     Integer array [AliITSdigitS?D::GetNTracks()]
1106   //                      containing the hit numbers, from AliITSmodule, that
1107   //                      contributed to this digit.
1108   //      Float_t *charge Floating point array of the signals contributed
1109   //                      to this digit by each track.
1110   // Outputs:
1111   //      none.
1112   // Return:
1113   //      none.
1114
1115     if(!fDetTypeSim) {
1116       Error("AddSimDigit","fDetTypeSim is 0!");
1117       return;
1118     }
1119     fDetTypeSim->AddSimDigit(branch,phys,digits,tracks,hits,charges,sigexpanded);
1120
1121 }
1122 //______________________________________________________________________
1123 void AliITS::Digits2Raw(){
1124     // convert digits of the current event to raw data
1125
1126   if(!fDetTypeSim) {
1127     Error("Digits2Raw","fDetTypeSim is 0!");
1128     return;
1129   }
1130   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
1131   SetDefaults();
1132   if(fSimuParam) fDetTypeSim->SetSimuParam(fSimuParam);
1133   fDetTypeSim->GetLoader()->LoadDigits();
1134   TTree* digits = fDetTypeSim->GetLoader()->TreeD();
1135   if (!digits) {
1136       Error("Digits2Raw", "no digits tree");
1137       return;
1138   }
1139   fDetTypeSim->SetTreeAddressD(digits,(Char_t*)GetName());
1140   
1141    // Get the FO signals for this event
1142   AliITSFOSignalsSPD* foSignals = NULL;
1143   AliRunLoader* runLoader = AliRunLoader::Instance();
1144   AliITSLoader* itsLoader = (AliITSLoader*) runLoader->GetLoader("ITSLoader");
1145   if (!itsLoader) {
1146     AliError("ITS loader is NULL.");
1147   }
1148    else {
1149       if(!itsLoader->TreeD()) AliError("   !!! No TreeD available !!!");
1150       foSignals = (AliITSFOSignalsSPD*)itsLoader->TreeD()->GetUserInfo()->FindObject("AliITSFOSignalsSPD");
1151       if(!foSignals) AliError("FO signals not retrieved");
1152      }
1153  
1154   Bool_t deleteFOsignalsLater = kFALSE;
1155   if (!foSignals) {
1156     AliError("FO signals not available. No FO bits will be written.");
1157     foSignals = new AliITSFOSignalsSPD(); // make a temporary dummy signals object
1158     deleteFOsignalsLater = kTRUE;
1159   }
1160   
1161   
1162   AliITSDDLModuleMapSDD* ddlsdd=fDetTypeSim->GetDDLModuleMapSDD();
1163   Char_t rawSDD=fDetTypeSim->GetSimuParam()->GetSDDRawDataFormat();
1164   AliITSDDLRawData rawWriter;
1165   
1166   rawWriter.SetSDDRawFormat(rawSDD);
1167   //Verbose level
1168   // 0: Silent
1169   // 1: cout messages
1170   // 2: txt files with digits 
1171   //BE CAREFUL, verbose level 2 MUST be used only for debugging and
1172   //it is highly suggested to use this mode only for debugging digits files
1173   //reasonably small, because otherwise the size of the txt files can reach
1174   //quickly several MB wasting time and disk space.
1175   rawWriter.SetVerbose(0);
1176     
1177   //SILICON PIXEL DETECTOR
1178   AliDebug(1,"Formatting raw data for SPD");
1179   rawWriter.RawDataSPD(digits->GetBranch("ITSDigitsSPD"),foSignals);
1180   if(deleteFOsignalsLater) delete foSignals;
1181     
1182   //SILICON DRIFT DETECTOR
1183   AliDebug(1,Form("Formatting raw data for SDD - Format code =%d",rawSDD));
1184   rawWriter.RawDataSDD(digits->GetBranch("ITSDigitsSDD"),ddlsdd);
1185     
1186   //SILICON STRIP DETECTOR
1187   AliDebug(1,"Formatting raw data for SSD");
1188   rawWriter.RawDataSSD(digits->GetBranch("ITSDigitsSSD"));
1189
1190   fLoader->UnloadDigits();
1191 }
1192 //______________________________________________________________________
1193 AliLoader* AliITS::MakeLoader(const char* topfoldername){ 
1194     //builds ITSgetter (AliLoader type)
1195     //if detector wants to use castomized getter, it must overload this method
1196
1197     AliDebug(1,Form("Creating AliITSLoader. Top folder is %s.",
1198          topfoldername));
1199     fLoader = new AliITSLoader(GetName(),topfoldername);
1200     return fLoader;
1201 }
1202 //______________________________________________________________________
1203 Bool_t AliITS::Raw2SDigits(AliRawReader* rawReader)
1204 {
1205   //
1206   // Converts RAW data to SDigits
1207   //
1208   // Get TreeS
1209   //
1210   Int_t last   = -1;
1211   Int_t size   = AliITSgeomTGeo::GetNModules();
1212   if(!fModA) {
1213     fModA = new TClonesArray*[size];
1214     for (Int_t mod = 0; mod < size; mod++) fModA[mod] = new TClonesArray("AliITSpListItem", 10000);
1215   }
1216   AliLoader* loader =  (AliRunLoader::Instance())->GetLoader("ITSLoader");
1217   if (!loader){
1218     Error("Open","Can not get ITS loader from Run Loader");
1219     return kFALSE;
1220   }
1221
1222   TTree* tree = 0;
1223   tree = loader->TreeS();
1224   if (!tree){
1225     loader->MakeTree("S");
1226     tree = loader->TreeS();
1227   }
1228   //
1229   // Array for SDigits
1230   // 
1231   if(!fpSDigits){
1232     fpSDigits = new TClonesArray("AliITSpListItem",10000);
1233   }
1234   TClonesArray& aSDigits = *fpSDigits;
1235   Int_t bufsize = 32000;
1236   tree->Branch("ITS", &fpSDigits, bufsize);
1237   Int_t npx = 0;
1238   //
1239   // SPD
1240   //
1241   AliITSsegmentationSPD* segSPD = (AliITSsegmentationSPD*) fDetTypeSim->GetSegmentationModel(0);
1242   if(!segSPD){
1243     AliWarning("Set AliITS defaults");
1244     SetDefaults();
1245     segSPD = (AliITSsegmentationSPD*) fDetTypeSim->GetSegmentationModel(0);
1246   }
1247   npx = segSPD->Npx();
1248   Double_t thr, sigma; 
1249     
1250   AliITSRawStreamSPD inputSPD(rawReader);
1251   while(1){
1252     Bool_t next  = inputSPD.Next();
1253     if (!next) break;
1254
1255     Int_t module = inputSPD.GetModuleID();
1256     Int_t column = inputSPD.GetColumn();
1257     Int_t row    = inputSPD.GetRow();
1258     Int_t index  = npx * column + row;
1259
1260     if (module >= size) continue;
1261  
1262     last = (fModA[module])->GetEntries();
1263     TClonesArray& dum = *fModA[module];
1264     fDetTypeSim->GetSimuParam()->SPDThresholds(module,thr,sigma);
1265     thr += 1.;
1266     new (dum[last]) AliITSpListItem(-1, -1, module, index, thr);
1267   }
1268   rawReader->Reset();
1269
1270   //
1271   // SDD
1272   // 
1273   AliITSsegmentationSDD* segSDD = (AliITSsegmentationSDD*) fDetTypeSim->GetSegmentationModel(1);
1274   npx = segSDD->Npx();
1275   Int_t scalef=AliITSsimulationSDD::ScaleFourier(segSDD);
1276   Int_t firstSDD=AliITSgeomTGeo::GetModuleIndex(3,1,1);
1277   Int_t firstSSD=AliITSgeomTGeo::GetModuleIndex(5,1,1);
1278
1279   AliITSRawStream* inputSDD=AliITSRawStreamSDD::CreateRawStreamSDD(rawReader);
1280   for(Int_t iMod=firstSDD; iMod<firstSSD; iMod++){
1281     AliITSCalibrationSDD* cal = (AliITSCalibrationSDD*)fDetTypeSim->GetCalibrationModel(iMod);
1282     Bool_t isZeroSupp=cal->GetZeroSupp();
1283     if(isZeroSupp){ 
1284       for(Int_t iSid=0; iSid<2; iSid++) inputSDD->SetZeroSuppLowThreshold(iMod-firstSDD,iSid,cal->GetZSLowThreshold(iSid));
1285     }else{
1286       for(Int_t iSid=0; iSid<2; iSid++) inputSDD->SetZeroSuppLowThreshold(iMod-firstSDD,iSid,0);
1287     }
1288   }
1289
1290   AliITSDDLModuleMapSDD* ddlmap=fDetTypeSim->GetDDLModuleMapSDD();
1291   inputSDD->SetDDLModuleMap(ddlmap);
1292   while(inputSDD->Next()){
1293     if(inputSDD->IsCompletedModule()==kFALSE && 
1294        inputSDD->IsCompletedDDL()==kFALSE){
1295
1296       Int_t module = inputSDD->GetModuleID();
1297       Int_t anode  = inputSDD->GetCoord1()+segSDD->NpzHalf()*inputSDD->GetChannel();
1298       Int_t time   = inputSDD->GetCoord2();
1299       Int_t signal10 = inputSDD->GetSignal();
1300       Int_t index = AliITSpList::GetIndex(anode,time,scalef*npx);
1301
1302       if (module >= size) continue;
1303       last = fModA[module]->GetEntries();
1304       TClonesArray& dum = *fModA[module];
1305       new (dum[last]) AliITSpListItem(-1, -1, module, index, Double_t(signal10));
1306       ((AliITSpListItem*) dum.At(last))->AddSignalAfterElect(module, index, Double_t(signal10));
1307     }
1308   }
1309   delete inputSDD;
1310   rawReader->Reset();
1311     
1312   //
1313   // SSD
1314   // 
1315   AliITSsegmentationSSD* segSSD = (AliITSsegmentationSSD*) fDetTypeSim->GetSegmentationModel(2);
1316   npx = segSSD->Npx();
1317   AliITSRawStreamSSD inputSSD(rawReader);
1318   while(1){
1319     Bool_t next  = inputSSD.Next();
1320     if (!next) break;
1321
1322     Int_t module  = inputSSD.GetModuleID();
1323     if(module<0)AliError(Form("Invalid SSD  module %d \n",module));
1324     if(module<0)continue;
1325     Int_t side    = inputSSD.GetSideFlag();
1326     Int_t strip   = inputSSD.GetStrip();
1327     Int_t signal  = inputSSD.GetSignal();
1328     Int_t index  = npx * side + strip;
1329
1330     if (module >= size) continue;
1331         
1332     last = fModA[module]->GetEntries();
1333     TClonesArray& dum = *fModA[module];
1334     new (dum[last]) AliITSpListItem(-1, -1, module, index, Double_t(signal));
1335   }
1336   rawReader->Reset();
1337   AliITSpListItem* sdig = 0;
1338     
1339   Int_t firstssd = GetITSgeom()->GetStartDet(kSSD);
1340   Double_t adcToEv = 1.;
1341   for (Int_t mod = 0; mod < size; mod++)
1342     {
1343       if(mod>=firstssd) {
1344         AliITSCalibrationSSD* calssd = (AliITSCalibrationSSD*)fDetTypeSim->GetCalibrationModel(mod);
1345         adcToEv = 1./calssd->GetSSDDEvToADC(1.);
1346       }
1347       Int_t nsdig =  fModA[mod]->GetEntries();
1348       for (Int_t ie = 0; ie < nsdig; ie++) {
1349         sdig = (AliITSpListItem*) (fModA[mod]->At(ie));
1350         Double_t digsig = sdig->GetSignal();
1351         if(mod>=firstssd) digsig*=adcToEv; // for SSD: convert back charge from ADC to electron
1352         new (aSDigits[ie]) AliITSpListItem(-1, -1, mod, sdig->GetIndex(), digsig);
1353         Float_t sig = sdig->GetSignalAfterElect();
1354         if(mod>=firstssd) sig*=adcToEv;
1355         if (sig > 0.) {
1356           sdig = (AliITSpListItem*)aSDigits[ie];
1357           sdig->AddSignalAfterElect(mod, sdig->GetIndex(), Double_t(sig));
1358         }
1359       }
1360         
1361       tree->Fill();
1362       aSDigits.Clear();
1363       fModA[mod]->Clear();
1364     }
1365   loader->WriteSDigits("OVERWRITE");    
1366   return kTRUE;
1367 }
1368
1369 //______________________________________________________________________
1370 void AliITS::UpdateInternalGeometry(){
1371   //reads new geometry from TGeo 
1372 //   AliDebug(1,"Delete ITSgeom and create a new one reading TGeo");
1373
1374   AliITSVersion_t version = (AliITSVersion_t)IsVersion();
1375   Int_t minor = 0;
1376   AliITSInitGeometry initgeom;
1377   AliITSgeom* geom = initgeom.CreateAliITSgeom(version,minor);
1378   SetITSgeom(geom);
1379 }
1380 //______________________________________________________________________
1381 AliTriggerDetector* AliITS::CreateTriggerDetector() const {
1382   // create an AliITSTrigger object (and set trigger conditions as input)
1383   return new AliITSTrigger(fDetTypeSim->GetTriggerConditions());
1384 }
1385 //______________________________________________________________________
1386 void AliITS::WriteFOSignals(){
1387 // This method write FO signals in Digits tree both in Hits2Digits
1388 // or SDigits2Digits
1389
1390   fDetTypeSim->ProcessNoiseForFastOr();
1391   fDetTypeSim->WriteFOSignals();
1392 }