]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITS.cxx
merging RecPoints and ClustersV2. All ClusterFinders produce AliITSRecPoints objects...
[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 "AliRun.h"
89 #include "AliLog.h"
90
91 ClassImp(AliITS)
92
93 //______________________________________________________________________
94 AliITS::AliITS() : AliDetector(){
95   // Default initializer for ITS
96   //      The default constructor of the AliITS class. In addition to
97   // creating the AliITS class it zeros the variables fIshunt (a member
98   // of AliDetector class), fEuclidOut, and fIdN, and zeros the pointers
99   // fIdSens, and fIdName. The AliDetector default constructor
100   // is also called.
101   
102   fEuclidOut=0;
103   fOpt="All";
104   fIdSens=0;
105   fIdName=0;
106   fDetTypeSim=0;
107   fIshunt     = 0;   // not zeroed in AliDetector.
108   fHits =0;
109   fNhits=0;
110   fITSmodules=0;
111  
112 //    SetDetectors(); // default to fOpt="All". This variable not written out.
113     SetMarkerColor(kRed);
114 }
115 //______________________________________________________________________
116 AliITS::AliITS(const char *name, const char *title):AliDetector(name,title){
117   //     The standard Constructor for the ITS class. 
118   // It also zeros the variables
119   // fIshunt (a member of AliDetector class), fEuclidOut, and zeros
120   // the pointers fIdSens and fIdName. To help in displaying hits via the
121   // ROOT macro display.C AliITS also sets the marker color to red. The
122   // variables passes with this constructor, const char *name and *title,
123   // are used by the constructor of AliDetector class. See AliDetector
124   // class for a description of these parameters and its constructor
125   // functions.
126   
127   fEuclidOut=0;
128   fOpt="All";
129   fIdSens=0;
130   fIdName=0;
131   fDetTypeSim=0;
132   fHits = new TClonesArray("AliITShit",1560);
133   if(gAlice->GetMCApp()) gAlice->GetMCApp()->AddHitList(fHits);
134   fNhits=0;
135   fITSmodules = 0;
136
137   fIshunt     = 0;  // not zeroed in AliDetector
138   // Not done in AliDetector.
139
140   fEuclidOut  = 0;
141   SetDetectors(); // default to fOpt="All". This variable not written out.
142   
143   fIdName     = 0;
144   fIdSens     = 0;
145   
146   fDetTypeSim   = new AliITSDetTypeSim();
147   
148   SetMarkerColor(kRed);
149
150 }
151 //______________________________________________________________________
152 AliITS::~AliITS(){
153     // Default destructor for ITS.
154     //     The default destructor of the AliITS class. In addition to deleting
155     // the AliITS class it deletes the memory pointed to by 
156     // fIdSens, fIdName, fDetTypeSim and it's contents.
157     // Inputs:
158     //      none.
159     // Outputs:
160     //      none.
161     // Return:
162     //      none.
163     if (fHits) {
164       fHits->Delete();
165       delete fHits;
166       fHits=0;
167     }
168     if(fITSmodules) {
169         this->ClearModules();
170         delete fITSmodules;
171         fITSmodules = 0;
172     }// end if fITSmodules!=0
173
174     delete[] fIdName;  // Array of TStrings
175     delete[] fIdSens;
176
177     if (fDetTypeSim){
178       delete fDetTypeSim;
179       fDetTypeSim = 0;
180     }
181 }
182 //______________________________________________________________________
183 AliITS::AliITS(const AliITS &source) : AliDetector(source){
184     // Copy constructor. This is a function which is not allowed to be
185     // done to the ITS. It exits with an error.
186     // Inputs:
187     //      AliITS &source  An AliITS class.
188     // Outputs:
189     //      none.
190     // Return:
191     //      none.
192
193     if(this==&source) return;
194     Error("Copy constructor",
195           "You are not allowed to make a copy of the AliITS");
196     exit(1);
197 }
198 //______________________________________________________________________
199 AliITS& AliITS::operator=(const AliITS &source){
200     // Assignment operator. This is a function which is not allowed to be
201     // done to the ITS. It exits with an error.
202     // Inputs:
203     //      AliITS &source  An AliITS class.
204     // Outputs:
205     //      none.
206     // Return:
207     //      none.
208
209     if(this==&source) return *this;
210     Error("operator=","You are not allowed to make a copy of the AliITS");
211     exit(1);
212     return *this; //fake return
213 }
214 //______________________________________________________________________
215 AliDigitizer* AliITS::CreateDigitizer(AliRunDigitizer* manager)const{
216     // Creates the AliITSDigitizer in a standard way for use via AliModule.
217     // This function can not be included in the .h file because of problems
218     // with the order of inclusion (recursive).
219     // Inputs:
220     //    AliRunDigitizer *manager  The Manger class for Digitization
221     // Output:
222     //    none.
223     // Return:
224     //    A new AliITSRunDigitizer (cast as a AliDigitizer).
225
226      return new AliITSDigitizer(manager);
227 }
228 //______________________________________________________________________
229 void AliITS::Init(){
230     // Initializer ITS after it has been built
231     //     This routine initializes the AliITS class. It is intended to be
232     // called from the Init function in AliITSv?. Besides displaying a banner
233     // indicating that it has been called it initializes the array fIdSens
234     // and sets the default segmentation, response, digit and raw cluster
235     // classes therefore it should be called after a call to CreateGeometry.
236     // Inputs:
237     //      none.
238     // Outputs:
239     //      none.
240     // Return:
241     //      none.
242     Int_t i;
243
244     SetDefaults();
245     // Array of TStrings
246     if(gMC) for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId(fIdName[i]);
247  
248     WriteGeometry();
249 }
250
251 //______________________________________________________________________
252 void AliITS::WriteGeometry(){
253   
254   //Writes ITS geomtry on gAlice
255
256   if(!fLoader) MakeLoader(AliConfig::GetDefaultEventFolderName());
257   AliRunLoader* rl  = fLoader->GetRunLoader();
258   rl->CdGAFile();
259   AliITSgeom* geom = GetITSgeom();
260   geom->Write();
261
262 }
263
264 //______________________________________________________________________
265 void AliITS::SetDefaults(){
266     // sets the default segmentation, response, digit and raw cluster classes.
267     // Inputs:
268     //      none.
269     // Outputs:
270     //      none.
271     // Return:
272     //      none.
273   AliInfoClass("Setting Defaults");
274
275     if(!fDetTypeSim) { 
276      Error("SetDefaults()","fDetTypeSim is 0!"); 
277      return;
278     }
279
280     fDetTypeSim->SetDefaults();
281
282
283 }
284 //______________________________________________________________________
285 void AliITS::SetDefaultSimulation(){
286     // sets the default simulation.
287     // Inputs:
288     //      none.
289     // Outputs:
290     //      none.
291     // Return:
292     //      none.
293     if(!fDetTypeSim) { 
294      Error("SetDefaultSimulation()","fDetTypeSim is 0!"); 
295      return;
296     }
297
298     fDetTypeSim->SetDefaultSimulation();
299
300 }
301
302
303 //______________________________________________________________________
304 void AliITS::MakeBranch(Option_t* option){
305     // Creates Tree branches for the ITS.
306     // Inputs:
307     //      Option_t *option    String of Tree types S,D, and/or R.
308     //      const char *file    String of the file name where these branches
309     //                          are to be stored. If blank then these branches
310     //                          are written to the same tree as the Hits were
311     //                          read from.
312     // Outputs:
313     //      none.
314     // Return:
315     //      none.
316   if(!fDetTypeSim) {
317     Error("MakeBranch","fDetTypeSim is 0!");
318     return;
319   }
320
321   Bool_t cH = (strstr(option,"H")!=0);
322   Bool_t cS = (strstr(option,"S")!=0);
323   Bool_t cD = (strstr(option,"D")!=0);
324   
325   if(cH && (fHits == 0x0)) fHits = new TClonesArray("AliITShit", 1560);
326   AliDetector::MakeBranch(option);
327   
328   if(cS) MakeBranchS(0);
329   if(cD) MakeBranchD(0);
330
331
332 }
333 //___________________________________________________________________
334 void AliITS::MakeBranchS(const char* fl){
335
336   // Creates Tree Branch for the ITS summable digits.
337   // Inputs:
338   //      cont char *fl  File name where SDigits branch is to be written
339   //                     to. If blank it write the SDigits to the same
340   //                     file in which the Hits were found.
341
342   
343   if(!fDetTypeSim){
344     Error("MakeBranchS","fDetTypeSim is 0!");
345   }
346   Int_t buffersize = 4000;
347   char branchname[30];
348
349   // only one branch for SDigits.
350   sprintf(branchname,"%s",GetName());
351
352   if(fLoader->TreeS()){
353     if(fDetTypeSim->GetSDigits()==0x0) fDetTypeSim->SetSDigits(new TClonesArray("AliITSpListItem",1000));
354     TClonesArray* sdig = (TClonesArray*)fDetTypeSim->GetSDigits();
355     MakeBranchInTree(fLoader->TreeS(),branchname,&sdig,buffersize,fl);
356   } 
357 }
358 //______________________________________________________________________
359 void AliITS::MakeBranchD(const char* file){
360
361   //Make branch for digits
362   if(!fDetTypeSim) {
363     Warning("MakeBranchD","fDetTypeSim is 0!");
364     return;
365   }
366   fDetTypeSim->SetLoader(fLoader);
367   MakeBranchInTreeD(fLoader->TreeD(),file);
368 }
369
370 //___________________________________________________________________
371 void AliITS:: MakeBranchInTreeD(TTree* treeD, const char* file){
372   // Creates Tree branches for the ITS.
373
374   if(!fDetTypeSim){
375     Error("MakeBranchS","fDetTypeSim is 0!");
376   }
377   fDetTypeSim->SetLoader(fLoader);
378
379   const Char_t *det[3] = {"SPD","SDD","SSD"};
380   Char_t* digclass;
381   Int_t buffersize = 4000;
382   Char_t branchname[30];
383   
384   if(!fDetTypeSim->GetDigits()){
385     fDetTypeSim->SetDigits(new TObjArray(fgkNTYPES));
386   }
387   for(Int_t i=0;i<fgkNTYPES;i++){
388     digclass = fDetTypeSim->GetDigitClassName(i);
389     TString classn = digclass;
390     if(!((fDetTypeSim->GetDigits())->At(i))){
391       (fDetTypeSim->GetDigits())->AddAt(new TClonesArray(classn.Data(),1000),i);
392     }
393     else ResetDigits(i);  
394     if(fgkNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
395     else sprintf(branchname,"%sDigits%d",GetName(),i+1);
396     TObjArray* dig = DigitsAddress(i);
397     if(GetDigits() && treeD) AliDetector::MakeBranchInTree(treeD,branchname, &dig,buffersize,file);
398   }
399
400 }
401 //______________________________________________________________________
402 void AliITS::SetTreeAddress(){
403     // Set branch address for the Trees.
404     // Inputs:
405     //      none.
406     // Outputs:
407     //      none.
408     // Return:
409     //      none.
410
411   if(!fDetTypeSim) {
412     Error("SetTreeAddress","fDetTypeSim is 0!");
413     return;
414   }
415
416   fDetTypeSim->SetLoader(fLoader);
417
418   TTree *treeS = fLoader->TreeS();
419   TTree *treeD = fLoader->TreeD();
420   if (fLoader->TreeH() && (fHits == 0x0)) {
421       fHits = new TClonesArray("AliITShit", 1560);
422   }
423   AliDetector::SetTreeAddress();
424
425   fDetTypeSim->SetTreeAddressS(treeS, (Char_t*)GetName());
426   fDetTypeSim->SetTreeAddressD(treeD, (Char_t*)GetName());
427
428
429 }
430 //______________________________________________________________________
431 void AliITS::AddHit(Int_t track, Int_t *vol, Float_t *hits){
432     // Add an ITS hit
433     //     The function to add information to the AliITShit class. See the
434     // AliITShit class for a full description. This function allocates the
435     // necessary new space for the hit information and passes the variable
436     // track, and the pointers *vol and *hits to the AliITShit constructor
437     // function.
438     // Inputs:
439     //      Int_t   track   Track number which produced this hit.
440     //      Int_t   *vol    Array of Integer Hit information. See AliITShit.h
441     //      Float_t *hits   Array of Floating Hit information.  see AliITShit.h
442     // Outputs:
443     //      none.
444     // Return:
445     //      none.
446   TClonesArray &lhits = *fHits;
447   new(lhits[fNhits++]) AliITShit(fIshunt,track,vol,hits);
448 }
449
450 //______________________________________________________________________
451 void AliITS::FillModules(Int_t evnt,Int_t bgrev,Int_t nmodules,
452                          Option_t *option, const char *filename){
453   // fill the modules with the sorted by module hits; add hits from
454   // background if option=Add.
455
456   static TTree *trH1;                 //Tree with background hits
457   static Bool_t first=kTRUE;
458   static TFile *file;
459   const char *addBgr = strstr(option,"Add");
460   
461   evnt = nmodules; // Dummy use of variables to remove warnings
462   if (addBgr ) {
463     if(first) {
464       file=new TFile(filename);
465     } // end if first
466     first=kFALSE;
467     file->cd();
468     file->ls();
469     // Get Hits Tree header from file
470     if(trH1) delete trH1;
471     trH1=0;
472     
473     char treeName[20];
474     sprintf(treeName,"TreeH%d",bgrev);
475     trH1 = (TTree*)gDirectory->Get(treeName);
476     if (!trH1) {
477       Error("FillModules","cannot find Hits Tree for event:%d",bgrev);
478     } // end if !trH1
479     // Set branch addresses
480   } // end if addBgr
481   
482   FillModules(fLoader->TreeH(),0); // fill from this file's tree.
483     
484   if (addBgr ) {
485     FillModules(trH1,10000000); // Default mask 10M.
486     TTree *fAli=fLoader->GetRunLoader()->TreeK();
487     TFile *fileAli=0;
488     if (fAli) fileAli =fAli->GetCurrentFile();
489     fileAli->cd();
490   } // end if add
491   
492   
493 }
494
495
496 //______________________________________________________________________
497 void AliITS::FillModules(TTree *treeH, Int_t mask) {
498     // fill the modules with the sorted by module hits; 
499     // can be called many times to do a merging
500     // Inputs:
501     //      TTree *treeH  The tree containing the hits to be copied into
502     //                    the modules.
503     //      Int_t mask    The track number mask to indecate which file
504     //                    this hits came from.
505     // Outputs:
506     //      none.
507     // Return:
508     //      none.
509
510     if (treeH == 0x0)
511      {
512        Error("FillModules","Tree is NULL");
513      }
514     Int_t lay,lad,det,index;
515     AliITShit *itsHit=0;
516     AliITSmodule *mod=0;
517     char branchname[20];
518     sprintf(branchname,"%s",GetName());
519     TBranch *branch = treeH->GetBranch(branchname);
520     if (!branch) {
521         Error("FillModules","%s branch in TreeH not found",branchname);
522         return;
523     } // end if !branch
524     branch->SetAddress(&fHits);
525     Int_t nTracks =(Int_t) treeH->GetEntries();
526     Int_t iPrimTrack,h;
527     for(iPrimTrack=0; iPrimTrack<nTracks; iPrimTrack++){
528         ResetHits();
529         Int_t nBytes = treeH->GetEvent(iPrimTrack);
530         if (nBytes <= 0) continue;
531         Int_t nHits = fHits->GetEntriesFast();
532         for(h=0; h<nHits; h++){
533             itsHit = (AliITShit *)fHits->UncheckedAt(h);
534             itsHit->GetDetectorID(lay,lad,det);
535             if (GetITSgeom()) {
536                 index = GetITSgeom()->GetModuleIndex(lay,lad,det);
537             } else {
538                 index=det-1; // This should not be used.
539             } // end if [You must have fITSgeom for this to work!]
540             mod = GetModule(index);
541             itsHit->SetTrack(itsHit->GetTrack()+mask); // Set track mask.
542             mod->AddHit(itsHit,iPrimTrack,h);
543         } // end loop over hits 
544     } // end loop over tracks
545 }
546
547 //______________________________________________________________________
548 void AliITS::InitModules(Int_t size,Int_t &nmodules){
549     // Initialize the modules array.
550     // Inputs:
551     //      Int_t size  Size of array of the number of modules to be
552     //                  created. If size <=0 then the number of modules
553     //                  is gotten from AliITSgeom class kept in fITSgeom.
554     // Outputs:
555     //      Int_t &nmodules The number of modules existing.
556     // Return:
557     //      none.
558
559     if(fITSmodules){ 
560         fITSmodules->Delete();
561         delete fITSmodules;
562     } // end fir fITSmoudles
563
564     if(!fDetTypeSim) {
565       Error("InitModules","fDetTypeSim is null!");
566       return;
567     }
568
569     Int_t nl,indexMAX,index;
570
571     if(size<=0){ // default to using data stored in AliITSgeom
572         if(fDetTypeSim->GetITSgeom()==0) {
573             Error("InitModules","fITSgeom not defined");
574             return;
575         } // end if fITSgeom==0
576         nl = fDetTypeSim->GetITSgeom()->GetNlayers();
577         indexMAX = fDetTypeSim->GetITSgeom()->GetIndexMax();
578         nmodules = indexMAX;
579         fITSmodules = new TObjArray(indexMAX);
580         for(index=0;index<indexMAX;index++){
581             fITSmodules->AddAt( new AliITSmodule(index),index);
582         } // end for index
583     }else{
584         fITSmodules = new TObjArray(size);
585         for(index=0;index<size;index++) {
586             fITSmodules->AddAt( new AliITSmodule(index),index);
587         } // end for index
588
589         nmodules = size;
590     } // end i size<=0
591 }
592 //______________________________________________________________________
593 void AliITS::Hits2SDigits(){
594     // Standard Hits to summable Digits function.
595     // Inputs:
596     //      none.
597     // Outputs:
598     //      none.
599   
600
601    if(!fDetTypeSim) {
602      Error("Hits2SDigits","fDetTypeSim is null!");
603      return;
604     
605   } 
606      
607   SetDefaults();
608   fLoader->LoadHits("read");
609   fLoader->LoadSDigits("recreate");
610   AliRunLoader* rl = fLoader->GetRunLoader(); 
611   fDetTypeSim->SetLoader(fLoader);
612   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
613         // Do the Hits to Digits operation. Use Standard input values.
614         // Event number from file, no background hit merging , use size from
615         // AliITSgeom class, option="All", input from this file only.
616     rl->GetEvent(iEvent);
617     if (!fLoader->TreeS()) fLoader->MakeTree("S");
618     MakeBranch("S");
619     SetTreeAddress();
620     HitsToPreDigits(iEvent,0,-1," ",fOpt," ");
621   } // end for iEvent
622     
623   fLoader->UnloadHits();
624   fLoader->UnloadSDigits();
625   
626 }
627 //______________________________________________________________________
628 void AliITS::Hits2Digits(){
629
630   //Conversion from hits to digits
631   if(!fDetTypeSim) {
632     Error("Hits2SDigits","fDetTypeSim is 0!");
633     return;
634   }
635    
636   fDetTypeSim->SetLoader(fLoader);
637   SetDefaults();
638
639   fLoader->LoadHits("read");
640   fLoader->LoadDigits("recreate");
641   AliRunLoader* rl = fLoader->GetRunLoader(); 
642   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
643     rl->GetEvent(iEvent);
644     if (!fLoader->TreeD()) fLoader->MakeTree("D");
645     MakeBranch("D");
646     SetTreeAddress();   
647     HitsToDigits(iEvent,0,-1," ",fOpt," ");
648   } 
649   
650   fLoader->UnloadHits();
651   fLoader->UnloadDigits();
652   
653 }
654
655 //______________________________________________________________________
656 void AliITS::HitsToDigits(Int_t evNumber,Int_t bgrev,Int_t size,
657                           Option_t *option,Option_t *opt,
658                           const char *filename){
659     //   Keep galice.root for signal and name differently the file for 
660     // background when add! otherwise the track info for signal will be lost !
661     // the condition below will disappear when the geom class will be
662     // initialized for all versions - for the moment it is only for v5 !
663     // 7 is the SDD beam test version.
664     // Inputs:
665     //      Int_t evnt       Event to be processed.
666     //      Int_t bgrev      Background Hit tree number.
667     //      Int_t nmodules   Not used.
668     //      Option_t *option String indicating if merging hits or not. To
669     //                       merge hits set equal to "Add". Otherwise no
670     //                       background hits are considered.
671     //      Test_t *filename File name containing the background hits..
672     // Outputs:
673     //      none.
674     // Return:
675     //      none.
676
677   if(!fDetTypeSim) {
678     Error("HitsToDigits","fDetTypeSim is null!");
679     return;
680   }
681   fDetTypeSim->SetLoader(fLoader);
682   if(!GetITSgeom()) return; // need transformations to do digitization.
683   AliITSgeom *geom = GetITSgeom();
684
685   const char *all = strstr(opt,"All");
686   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
687                         strstr(opt,"SSD")};
688   static Bool_t setDef=kTRUE;
689   if (setDef) SetDefaultSimulation();
690   setDef=kFALSE;
691   
692   Int_t nmodules;
693   InitModules(size,nmodules);
694   FillModules(evNumber,bgrev,nmodules,option,filename);
695
696   AliITSsimulation *sim      = 0;
697   AliITSmodule     *mod      = 0;
698   Int_t id;
699   for(Int_t module=0;module<geom->GetIndexMax();module++){
700     id       = geom->GetModuleType(module);
701     if (!all && !det[id]) continue;
702     sim      = (AliITSsimulation*)fDetTypeSim->GetSimulationModel(id);
703     if (!sim) {
704       Error("HitsToDigits","The simulation class was not "
705             "instanciated for module %d type %s!",module,
706             geom->GetModuleTypeName(module));
707       exit(1);
708     } // end if !sim
709     mod      = (AliITSmodule *)fITSmodules->At(module);
710     sim->DigitiseModule(mod,module,evNumber);
711     // fills all branches - wasted disk space
712     fLoader->TreeD()->Fill(); 
713     ResetDigits();
714   } // end for module
715   
716   ClearModules();
717   
718   fLoader->TreeD()->GetEntries();
719   fLoader->TreeD()->AutoSave();
720   // reset tree
721   fLoader->TreeD()->Reset();
722 }
723 //_____________________________________________________________________
724 void AliITS::Hits2PreDigits(){ 
725   // Turn hits into SDigits
726
727   if(!fDetTypeSim) {
728     Error("Hits2SDigits","fDetTypeSim is 0!");
729     return;
730   }
731    
732   fDetTypeSim->SetLoader(fLoader);
733   SetDefaults();
734   
735   HitsToPreDigits(fLoader->GetRunLoader()->GetEventNumber(),
736                   0,-1," ",fOpt," ");
737 }
738
739 //______________________________________________________________________
740 void AliITS::HitsToPreDigits(Int_t evNumber,Int_t bgrev,Int_t size,
741                              Option_t *option,Option_t *opt,
742                              const char *filename){
743     //   Keep galice.root for signal and name differently the file for 
744     // background when add! otherwise the track info for signal will be lost !
745     // the condition below will disappear when the geom class will be
746     // initialized for all versions - for the moment it is only for v5 !
747     // 7 is the SDD beam test version.
748     // Inputs:
749     //      Int_t evnt       Event to be processed.
750     //      Int_t bgrev      Background Hit tree number.
751     //      Int_t nmodules   Not used.
752     //      Option_t *option String indicating if merging hits or not. To
753     //                       merge hits set equal to "Add". Otherwise no
754     //                       background hits are considered.
755     //      Test_t *filename File name containing the background hits..
756     // Outputs:
757     //      none.
758     // Return:
759     //      none.
760
761  
762   if(!fDetTypeSim) {
763     Error("HitsToPreDigits","fDetTypeSim is null!");
764     return;
765   }
766   fDetTypeSim->SetLoader(fLoader);
767
768   if(!GetITSgeom()){
769     Error("HitsToPreDigits","fGeom is null!");
770     return; // need transformations to do digitization.
771   }
772   AliITSgeom *geom = GetITSgeom();
773
774   const char *all = strstr(opt,"All");
775   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
776                         strstr(opt,"SSD")};
777   static Bool_t setDef=kTRUE;
778   if (setDef) SetDefaultSimulation();
779   setDef=kFALSE;
780   
781   Int_t nmodules;
782   InitModules(size,nmodules);
783   FillModules(evNumber,bgrev,nmodules,option,filename);
784   
785
786   AliITSsimulation *sim      = 0;
787   AliITSmodule     *mod      = 0;
788   Int_t id,module;
789   for(module=0;module<geom->GetIndexMax();module++){
790     id       = geom->GetModuleType(module);
791     if (!all && !det[id]) continue;
792     sim      = (AliITSsimulation*)GetSimulationModel(id);
793     if (!sim) {
794       Error("HitsToPreDigits","The simulation class was not "
795             "instanciated for module %d type %s!",module,
796             geom->GetModuleTypeName(module));
797       exit(1);
798     } // end if !sim
799     mod      = (AliITSmodule *)fITSmodules->At(module);
800     sim->SDigitiseModule(mod,module,evNumber);
801     // fills all branches - wasted disk space
802     fLoader->TreeS()->Fill(); 
803     fDetTypeSim->ResetSDigits();
804   } // end for module
805
806   ClearModules();
807
808   
809   fLoader->TreeS()->GetEntries();
810   fLoader->TreeS()->AutoSave();
811   fLoader->WriteSDigits("OVERWRITE");
812   // reset tree
813   fLoader->TreeS()->Reset();
814 }
815
816 //_____________________________________________________________________
817 void AliITS::HitsToFastRecPoints(Int_t evNumber,Int_t bgrev,Int_t size,
818                                   Option_t *opt0,Option_t *opt1,
819                                  const char *flnm){
820     // keep galice.root for signal and name differently the file for 
821     // background when add! otherwise the track info for signal will be lost !
822     // the condition below will disappear when the geom class will be
823     // initialized for all versions - for the moment it is only for v5 !
824     // Inputs:
825     //      Int_t evnt       Event to be processed.
826     //      Int_t bgrev      Background Hit tree number.
827     //      Int_t size       Size used by InitModules. See InitModules.
828     //      Option_t *opt0   Option passed to FillModules. See FillModules.
829     //      Option_t *opt1   String indicating if merging hits or not. To
830     //                       merge hits set equal to "Add". Otherwise no
831     //                       background hits are considered.
832     //      Test_t *flnm     File name containing the background hits..
833     // Outputs:
834     //      none.
835     // Return:
836     //      none.
837
838
839
840   if(!GetITSgeom()){
841     Error("HitsToPreDigits","fGeom is null!");
842     return; // need transformations to do digitization.
843   }
844   AliITSgeom *geom = GetITSgeom();
845
846   AliITSLoader *pITSloader = (AliITSLoader*)fLoader;
847
848   const char *all = strstr(opt1,"All");
849   const char *det[3] ={strstr(opt1,"SPD"),strstr(opt1,"SDD"),
850                        strstr(opt1,"SSD")};
851   Int_t nmodules;
852   InitModules(size,nmodules);
853   FillModules(evNumber,bgrev,nmodules,opt0,flnm);
854
855   AliITSsimulation *sim      = 0;
856   AliITSmodule     *mod      = 0;
857   Int_t id,module;
858
859   TTree *lTR = pITSloader->TreeR();
860   if(!lTR) {
861     pITSloader->MakeTree("R");
862     lTR = pITSloader->TreeR();
863   }
864   
865   TClonesArray* ptarray = new TClonesArray("AliITSRecPoint",1000);
866   TBranch* branch = (TBranch*)lTR->Branch("ITSRecPointsF",&ptarray);
867   branch->SetAddress(&ptarray);
868   //m.b. : this change is nothing but a nice way to make sure
869   //the CPU goes up !
870   AliDebug(1,Form("N mod = %d",geom->GetIndexMax()));    
871   for(module=0;module<geom->GetIndexMax();module++){
872     id       = geom->GetModuleType(module);
873     if (!all && !det[id]) continue;
874     sim      = (AliITSsimulation*)GetSimulationModel(id);
875     if (!sim) {
876       Error("HitsToFastPoints","The simulation class was not "
877             "instanciated for module %d type %x!",module,
878             geom->GetModuleTypeName(module));
879       exit(1);
880     } // end if !sim
881     mod      = (AliITSmodule *)fITSmodules->At(module);
882     sim->CreateFastRecPoints(mod,module,gRandom,ptarray);
883     lTR->Fill();
884   } // end for module
885
886   ClearModules();
887   fLoader->WriteRecPoints("OVERWRITE");
888   delete ptarray;
889 }
890 //_____________________________________________________________________
891 Int_t AliITS::Hits2Clusters(TTree *hTree, TTree *cTree) {
892   //------------------------------------------------------------
893   // This function creates ITS clusters
894   //------------------------------------------------------------
895   if(!GetITSgeom()){
896     Error("HitsToPreDigits","fGeom is null!");
897     return 1; // need transformations to do digitization.
898   }
899   AliITSgeom *geom=GetITSgeom();
900   Int_t mmax=geom->GetIndexMax();
901
902   InitModules(-1,mmax);
903   FillModules(hTree,0);
904
905   TClonesArray *points = new TClonesArray("AliITSRecPoint",1000);
906   TBranch *branch=cTree->GetBranch("ITSRecPoints");
907   if (!branch) cTree->Branch("ITSRecPoints",&points);
908   else branch->SetAddress(&points);
909
910   AliITSsimulationFastPoints sim;
911   Int_t ncl=0;
912   for (Int_t m=0; m<mmax; m++) {
913     AliITSmodule *mod=GetModule(m);      
914     sim.CreateFastRecPoints(mod,m,gRandom,points);      
915     ncl+=points->GetEntriesFast();
916     cTree->Fill();
917     points->Clear();
918   }
919
920   Info("Hits2Clusters","Number of found fast clusters : %d",ncl);
921
922   //cTree->Write();
923
924   delete points;
925   return 0;
926 }
927
928 //_____________________________________________________________________
929 void AliITS::CheckLabels(Int_t lab[3]) const {
930   //------------------------------------------------------------
931   // Tries to find mother's labels
932   //------------------------------------------------------------
933
934   if(lab[0]<0 && lab[1]<0 && lab[2]<0) return; // In case of no labels just exit
935
936   Int_t ntracks = gAlice->GetMCApp()->GetNtrack();
937   for (Int_t i=0;i<3;i++){
938     Int_t label = lab[i];
939     if (label>=0 && label<ntracks) {
940       TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
941       if (part->P() < 0.005) {
942         Int_t m=part->GetFirstMother();
943         if (m<0) {      
944           continue;
945         }
946         if (part->GetStatusCode()>0) {
947           continue;
948         }
949         lab[i]=m;       
950       }
951     }    
952   }
953   
954 }
955
956 //______________________________________________________________________
957 void AliITS::SDigitsToDigits(Option_t *opt){
958     // Standard Summable digits to Digits function.
959     // Inputs:
960     //      none.
961     // Outputs:
962     //      none.
963     if(!fDetTypeSim) {
964       Error("SDigitsToSDigits","fDetTypeSim is 0!");
965       return;
966     }
967    
968     fDetTypeSim->SetLoader(fLoader);
969     SetDefaults();
970     fDetTypeSim->SDigitsToDigits(opt,(Char_t*)GetName());
971
972 }
973
974 //______________________________________________________________________
975 void AliITS::ResetDigits(){
976     // Reset number of digits and the digits array for the ITS detector.
977     // Inputs:
978     //      none.
979     // Outputs:
980     //      none.
981     if(!fDetTypeSim) {
982       Error("ResetDigits","fDetTypeSim is 0!");
983       return;
984     }
985    
986     fDetTypeSim->ResetDigits();
987
988
989 }
990 //______________________________________________________________________
991 void AliITS::ResetDigits(Int_t branch){
992     // Reset number of digits and the digits array for this branch.
993     // Inputs:
994     //      none.
995     // Outputs:
996     //      none.
997
998     if(!fDetTypeSim) {
999       Error("ResetDigits","fDetTypeSim is 0!");
1000       return;
1001     }
1002    
1003     fDetTypeSim->ResetDigits(branch);
1004
1005 }
1006 //______________________________________________________________________
1007 void AliITS::AddSumDigit(AliITSpListItem &sdig){
1008     // Adds the a module full of summable digits to the summable digits tree.
1009     // Inputs:
1010     //      AliITSpListItem &sdig   SDigit to be added to SDigits tree.
1011     // Outputs:
1012     //      none.
1013     // Return:
1014     //      none.
1015
1016     if(!fDetTypeSim) {
1017       Error("AddSumDigit","fDetTypeSim is 0!");
1018       return;
1019     }
1020     fDetTypeSim->AddSumDigit(sdig);
1021     
1022 }
1023 //______________________________________________________________________
1024 void AliITS::AddRealDigit(Int_t branch, Int_t *digits){
1025     //   Add a real digit - as coming from data.
1026     // Inputs:
1027     //      Int_t id        Detector type number.
1028     //      Int_t *digits   Integer array containing the digits info. See 
1029     //                      AliITSdigit.h
1030     // Outputs:
1031     //      none.
1032     // Return:
1033     //      none.
1034
1035     if(!fDetTypeSim) {
1036       Error("AddRealDigit","fDetTypeSim is 0!");
1037       return;
1038     }
1039     fDetTypeSim->AddRealDigit(branch,digits);
1040
1041 }
1042 //______________________________________________________________________
1043 void AliITS::AddSimDigit(Int_t branch, AliITSdigit *d){
1044     //    Add a simulated digit.
1045     // Inputs:
1046     //      Int_t id        Detector type number.
1047     //      AliITSdigit *d  Digit to be added to the Digits Tree. See 
1048     //                      AliITSdigit.h
1049     // Outputs:
1050     //      none.
1051     // Return:
1052     //      none.
1053
1054     if(!fDetTypeSim) {
1055       Error("AddSimDigit","fDetTypeSim is 0!");
1056       return;
1057     }
1058     fDetTypeSim->AddSimDigit(branch,d);
1059
1060 }
1061 //______________________________________________________________________
1062 void AliITS::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,Int_t *tracks,
1063                          Int_t *hits,Float_t *charges){
1064   //   Add a simulated digit to the list.
1065   // Inputs:
1066   //      Int_t id        Detector type number.
1067   //      Float_t phys    Physics indicator. See AliITSdigits.h
1068   //      Int_t *digits   Integer array containing the digits info. See 
1069   //                      AliITSdigit.h
1070   //      Int_t *tracks   Integer array [AliITSdigitS?D::GetNTracks()] 
1071   //                      containing the track numbers that contributed to
1072   //                      this digit.
1073   //      Int_t *hits     Integer array [AliITSdigitS?D::GetNTracks()]
1074   //                      containing the hit numbers, from AliITSmodule, that
1075   //                      contributed to this digit.
1076   //      Float_t *charge Floating point array of the signals contributed
1077   //                      to this digit by each track.
1078   // Outputs:
1079   //      none.
1080   // Return:
1081   //      none.
1082
1083     if(!fDetTypeSim) {
1084       Error("AddSimDigit","fDetTypeSim is 0!");
1085       return;
1086     }
1087     fDetTypeSim->AddSimDigit(branch,phys,digits,tracks,hits,charges);
1088
1089 }
1090 //______________________________________________________________________
1091 void AliITS::Digits2Raw(){
1092     // convert digits of the current event to raw data
1093
1094   if(!fDetTypeSim) {
1095     Error("Digits2Raw","fDetTypeSim is 0!");
1096     return;
1097   }
1098   fDetTypeSim->SetLoader(fLoader);
1099   SetDefaults();
1100   fDetTypeSim->GetLoader()->LoadDigits();
1101   TTree* digits = fDetTypeSim->GetLoader()->TreeD();
1102   if (!digits) {
1103       Error("Digits2Raw", "no digits tree");
1104       return;
1105   }
1106   fDetTypeSim->SetTreeAddressD(digits,(Char_t*)GetName());
1107
1108   AliITSDDLRawData rawWriter;
1109   //Verbose level
1110   // 0: Silent
1111   // 1: cout messages
1112   // 2: txt files with digits 
1113   //BE CAREFUL, verbose level 2 MUST be used only for debugging and
1114   //it is highly suggested to use this mode only for debugging digits files
1115   //reasonably small, because otherwise the size of the txt files can reach
1116   //quickly several MB wasting time and disk space.
1117   rawWriter.SetVerbose(0);
1118     
1119   //SILICON PIXEL DETECTOR
1120   Info("Digits2Raw", "Formatting raw data for SPD");
1121   rawWriter.RawDataSPD(digits->GetBranch("ITSDigitsSPD"));
1122     
1123   //SILICON DRIFT DETECTOR
1124   Info("Digits2Raw", "Formatting raw data for SDD");
1125   rawWriter.RawDataSDD(digits->GetBranch("ITSDigitsSDD"));
1126     
1127   //SILICON STRIP DETECTOR
1128   Info("Digits2Raw", "Formatting raw data for SSD");
1129   rawWriter.RawDataSSD(digits->GetBranch("ITSDigitsSSD"));
1130
1131   fLoader->UnloadDigits();
1132   
1133
1134
1135   
1136 }
1137
1138
1139
1140
1141 //______________________________________________________________________
1142 AliLoader* AliITS::MakeLoader(const char* topfoldername){ 
1143     //builds ITSgetter (AliLoader type)
1144     //if detector wants to use castomized getter, it must overload this method
1145
1146     Info("MakeLoader","Creating AliITSLoader. Top folder is %s.",
1147          topfoldername);
1148     fLoader = new AliITSLoader(GetName(),topfoldername);
1149     return fLoader;
1150 }