]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITS.cxx
Fixed bug in read "new" and old .det files. Fix missing init of
[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   if(!fLoader) MakeLoader(AliConfig::GetDefaultEventFolderName());
150   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
151
152 }
153 //______________________________________________________________________
154 AliITS::~AliITS(){
155     // Default destructor for ITS.
156     //     The default destructor of the AliITS class. In addition to deleting
157     // the AliITS class it deletes the memory pointed to by 
158     // fIdSens, fIdName, fDetTypeSim and it's contents.
159     // Inputs:
160     //      none.
161     // Outputs:
162     //      none.
163     // Return:
164     //      none.
165
166     if (fHits) {
167       fHits->Delete();
168       delete fHits;
169       fHits=0;
170     }
171     if(fITSmodules) {
172         this->ClearModules();
173         delete fITSmodules;
174         fITSmodules = 0;
175     }// end if fITSmodules!=0
176
177     delete[] fIdName;  // Array of TStrings
178     delete[] fIdSens;
179
180     if (fDetTypeSim){
181       delete fDetTypeSim;
182       fDetTypeSim = 0;
183     }
184 }
185 //______________________________________________________________________
186 AliITS::AliITS(const AliITS &source) : AliDetector(source){
187     // Copy constructor. This is a function which is not allowed to be
188     // done to the ITS. It exits with an error.
189     // Inputs:
190     //      AliITS &source  An AliITS class.
191     // Outputs:
192     //      none.
193     // Return:
194     //      none.
195
196     if(this==&source) return;
197     Error("Copy constructor",
198           "You are not allowed to make a copy of the AliITS");
199     exit(1);
200 }
201 //______________________________________________________________________
202 AliITS& AliITS::operator=(const AliITS &source){
203     // Assignment operator. This is a function which is not allowed to be
204     // done to the ITS. It exits with an error.
205     // Inputs:
206     //      AliITS &source  An AliITS class.
207     // Outputs:
208     //      none.
209     // Return:
210     //      none.
211
212     if(this==&source) return *this;
213     Error("operator=","You are not allowed to make a copy of the AliITS");
214     exit(1);
215     return *this; //fake return
216 }
217 //______________________________________________________________________
218 AliDigitizer* AliITS::CreateDigitizer(AliRunDigitizer* manager)const{
219     // Creates the AliITSDigitizer in a standard way for use via AliModule.
220     // This function can not be included in the .h file because of problems
221     // with the order of inclusion (recursive).
222     // Inputs:
223     //    AliRunDigitizer *manager  The Manger class for Digitization
224     // Output:
225     //    none.
226     // Return:
227     //    A new AliITSRunDigitizer (cast as a AliDigitizer).
228
229      return new AliITSDigitizer(manager);
230 }
231 //______________________________________________________________________
232 void AliITS::Init(){
233     // Initializer ITS after it has been built
234     //     This routine initializes the AliITS class. It is intended to be
235     // called from the Init function in AliITSv?. Besides displaying a banner
236     // indicating that it has been called it initializes the array fIdSens
237     // and sets the default segmentation, response, digit and raw cluster
238     // classes therefore it should be called after a call to CreateGeometry.
239     // Inputs:
240     //      none.
241     // Outputs:
242     //      none.
243     // Return:
244     //      none.
245     Int_t i;
246
247     SetDefaults();
248     // Array of TStrings
249     if(gMC) for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId(fIdName[i]);
250  
251     WriteGeometry();
252 }
253
254 //______________________________________________________________________
255 void AliITS::WriteGeometry(){
256   
257   //Writes ITS geometry on gAlice
258
259   if(!fLoader) MakeLoader(AliConfig::GetDefaultEventFolderName());
260   AliRunLoader* rl  = fLoader->GetRunLoader();
261   rl->CdGAFile();
262   AliITSgeom* geom = GetITSgeom();
263   geom->Write();
264
265 }
266
267 //______________________________________________________________________
268 void AliITS::SetDefaults(){
269     // sets the default segmentation, response, digit and raw cluster classes.
270     // Inputs:
271     //      none.
272     // Outputs:
273     //      none.
274     // Return:
275     //      none.
276     AliInfoClass("AliITS::Setting Defaults");
277
278     if(!fDetTypeSim) { 
279      Error("SetDefaults()","fDetTypeSim is 0!"); 
280      return;
281     }
282
283     fDetTypeSim->SetDefaults();
284
285
286 }
287 //______________________________________________________________________
288 void AliITS::SetDefaultSimulation(){
289     // sets the default simulation.
290     // Inputs:
291     //      none.
292     // Outputs:
293     //      none.
294     // Return:
295     //      none.
296     if(!fDetTypeSim) { 
297      Error("SetDefaultSimulation()","fDetTypeSim is 0!"); 
298      return;
299     }
300
301     fDetTypeSim->SetDefaultSimulation();
302
303 }
304
305
306 //______________________________________________________________________
307 void AliITS::MakeBranch(Option_t* option){
308     // Creates Tree branches for the ITS.
309     // Inputs:
310     //      Option_t *option    String of Tree types S,D, and/or R.
311     //      const char *file    String of the file name where these branches
312     //                          are to be stored. If blank then these branches
313     //                          are written to the same tree as the Hits were
314     //                          read from.
315     // Outputs:
316     //      none.
317     // Return:
318     //      none.
319   if(!fDetTypeSim) {
320     Error("MakeBranch","fDetTypeSim is 0!");
321     return;
322   }
323
324   Bool_t cH = (strstr(option,"H")!=0);
325   Bool_t cS = (strstr(option,"S")!=0);
326   Bool_t cD = (strstr(option,"D")!=0);
327   
328   if(cH && (fHits == 0x0)) fHits = new TClonesArray("AliITShit", 1560);
329   AliDetector::MakeBranch(option);
330   
331   if(cS) MakeBranchS(0);
332   if(cD) MakeBranchD(0);
333
334
335 }
336 //___________________________________________________________________
337 void AliITS::MakeBranchS(const char* fl){
338
339   // Creates Tree Branch for the ITS summable digits.
340   // Inputs:
341   //      cont char *fl  File name where SDigits branch is to be written
342   //                     to. If blank it write the SDigits to the same
343   //                     file in which the Hits were found.
344
345   
346   if(!fDetTypeSim){
347     Error("MakeBranchS","fDetTypeSim is 0!");
348   }
349   Int_t buffersize = 4000;
350   char branchname[30];
351
352   // only one branch for SDigits.
353   sprintf(branchname,"%s",GetName());
354
355   if(fLoader->TreeS()){
356     if(fDetTypeSim->GetSDigits()==0x0) fDetTypeSim->SetSDigits(new TClonesArray("AliITSpListItem",1000));
357     TClonesArray* sdig = (TClonesArray*)fDetTypeSim->GetSDigits();
358     MakeBranchInTree(fLoader->TreeS(),branchname,&sdig,buffersize,fl);
359   } 
360 }
361 //______________________________________________________________________
362 void AliITS::MakeBranchD(const char* file){
363
364   //Make branch for digits
365   if(!fDetTypeSim) {
366     Warning("MakeBranchD","fDetTypeSim is 0!");
367     return;
368   }
369   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
370   MakeBranchInTreeD(fLoader->TreeD(),file);
371 }
372
373 //___________________________________________________________________
374 void AliITS:: MakeBranchInTreeD(TTree* treeD, const char* file){
375   // Creates Tree branches for the ITS.
376
377   if(!fDetTypeSim){
378     Error("MakeBranchS","fDetTypeSim is 0!");
379   }
380   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
381
382   const Char_t *det[3] = {"SPD","SDD","SSD"};
383   Char_t* digclass;
384   Int_t buffersize = 4000;
385   Char_t branchname[30];
386   
387   if(!fDetTypeSim->GetDigits()){
388     fDetTypeSim->SetDigits(new TObjArray(fgkNTYPES));
389   }
390   for(Int_t i=0;i<fgkNTYPES;i++){
391     digclass = fDetTypeSim->GetDigitClassName(i);
392     TString classn = digclass;
393     if(!((fDetTypeSim->GetDigits())->At(i))){
394       (fDetTypeSim->GetDigits())->AddAt(new TClonesArray(classn.Data(),1000),i);
395     }
396     else ResetDigits(i);  
397     if(fgkNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
398     else sprintf(branchname,"%sDigits%d",GetName(),i+1);
399     TObjArray* dig = DigitsAddress(i);
400     if(GetDigits() && treeD) AliDetector::MakeBranchInTree(treeD,branchname, &dig,buffersize,file);
401   }
402
403 }
404 //______________________________________________________________________
405 void AliITS::SetTreeAddress(){
406     // Set branch address for the Trees.
407     // Inputs:
408     //      none.
409     // Outputs:
410     //      none.
411     // Return:
412     //      none.
413     
414   if(!fDetTypeSim) {
415     Error("SetTreeAddress","fDetTypeSim is 0!");
416     return;
417   }
418
419   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
420
421   TTree *treeS = fLoader->TreeS();
422   TTree *treeD = fLoader->TreeD();
423   if (fLoader->TreeH() && (fHits == 0x0)) {
424       fHits = new TClonesArray("AliITShit", 1560);
425   }
426   AliDetector::SetTreeAddress();
427
428   fDetTypeSim->SetTreeAddressS(treeS, (Char_t*)GetName());
429   fDetTypeSim->SetTreeAddressD(treeD, (Char_t*)GetName());
430 }
431 //______________________________________________________________________
432 void AliITS::AddHit(Int_t track, Int_t *vol, Float_t *hits){
433     // Add an ITS hit
434     //     The function to add information to the AliITShit class. See the
435     // AliITShit class for a full description. This function allocates the
436     // necessary new space for the hit information and passes the variable
437     // track, and the pointers *vol and *hits to the AliITShit constructor
438     // function.
439     // Inputs:
440     //      Int_t   track   Track number which produced this hit.
441     //      Int_t   *vol    Array of Integer Hit information. See AliITShit.h
442     //      Float_t *hits   Array of Floating Hit information.  see AliITShit.h
443     // Outputs:
444     //      none.
445     // Return:
446     //      none.
447   TClonesArray &lhits = *fHits;
448   new(lhits[fNhits++]) AliITShit(fIshunt,track,vol,hits);
449 }
450
451 //______________________________________________________________________
452 void AliITS::FillModules(Int_t evnt,Int_t bgrev,Int_t nmodules,
453                          Option_t *option, const char *filename){
454   // fill the modules with the sorted by module hits; add hits from
455   // background if option=Add.
456
457   static TTree *trH1;                 //Tree with background hits
458   static Bool_t first=kTRUE;
459   static TFile *file;
460   const char *addBgr = strstr(option,"Add");
461   
462   evnt = nmodules; // Dummy use of variables to remove warnings
463   if (addBgr ) {
464     if(first) {
465       file=new TFile(filename);
466     } // end if first
467     first=kFALSE;
468     file->cd();
469     file->ls();
470     // Get Hits Tree header from file
471     if(trH1) delete trH1;
472     trH1=0;
473     
474     char treeName[20];
475     sprintf(treeName,"TreeH%d",bgrev);
476     trH1 = (TTree*)gDirectory->Get(treeName);
477     if (!trH1) {
478       Error("FillModules","cannot find Hits Tree for event:%d",bgrev);
479     } // end if !trH1
480     // Set branch addresses
481   } // end if addBgr
482   
483   FillModules(fLoader->TreeH(),0); // fill from this file's tree.
484     
485   if (addBgr ) {
486     FillModules(trH1,10000000); // Default mask 10M.
487     TTree *fAli=fLoader->GetRunLoader()->TreeK();
488     TFile *fileAli=0;
489     if (fAli) fileAli =fAli->GetCurrentFile();
490     fileAli->cd();
491   } // end if add
492   
493   
494 }
495
496
497 //______________________________________________________________________
498 void AliITS::FillModules(TTree *treeH, Int_t mask) {
499     // fill the modules with the sorted by module hits; 
500     // can be called many times to do a merging
501     // Inputs:
502     //      TTree *treeH  The tree containing the hits to be copied into
503     //                    the modules.
504     //      Int_t mask    The track number mask to indecate which file
505     //                    this hits came from.
506     // Outputs:
507     //      none.
508     // Return:
509     //      none.
510
511     if (treeH == 0x0)
512      {
513        Error("FillModules","Tree is NULL");
514      }
515     Int_t lay,lad,det,index;
516     AliITShit *itsHit=0;
517     AliITSmodule *mod=0;
518     char branchname[20];
519     sprintf(branchname,"%s",GetName());
520     TBranch *branch = treeH->GetBranch(branchname);
521     if (!branch) {
522         Error("FillModules","%s branch in TreeH not found",branchname);
523         return;
524     } // end if !branch
525     branch->SetAddress(&fHits);
526     Int_t nTracks =(Int_t) treeH->GetEntries();
527     Int_t iPrimTrack,h;
528     for(iPrimTrack=0; iPrimTrack<nTracks; iPrimTrack++){
529         ResetHits();
530         Int_t nBytes = treeH->GetEvent(iPrimTrack);
531         if (nBytes <= 0) continue;
532         Int_t nHits = fHits->GetEntriesFast();
533         for(h=0; h<nHits; h++){
534             itsHit = (AliITShit *)fHits->UncheckedAt(h);
535             itsHit->GetDetectorID(lay,lad,det);
536             if (GetITSgeom()) {
537                 index = GetITSgeom()->GetModuleIndex(lay,lad,det);
538             } else {
539                 index=det-1; // This should not be used.
540             } // end if [You must have fITSgeom for this to work!]
541             mod = GetModule(index);
542             itsHit->SetTrack(itsHit->GetTrack()+mask); // Set track mask.
543             mod->AddHit(itsHit,iPrimTrack,h);
544         } // end loop over hits 
545     } // end loop over tracks
546 }
547
548 //______________________________________________________________________
549 void AliITS::InitModules(Int_t size,Int_t &nmodules){
550     // Initialize the modules array.
551     // Inputs:
552     //      Int_t size  Size of array of the number of modules to be
553     //                  created. If size <=0 then the number of modules
554     //                  is gotten from AliITSgeom class kept in fITSgeom.
555     // Outputs:
556     //      Int_t &nmodules The number of modules existing.
557     // Return:
558     //      none.
559
560     if(fITSmodules){ 
561         fITSmodules->Delete();
562         delete fITSmodules;
563     } // end fir fITSmoudles
564
565     if(!fDetTypeSim) {
566       Error("InitModules","fDetTypeSim is null!");
567       return;
568     }
569
570     Int_t nl,indexMAX,index;
571
572     if(size<=0){ // default to using data stored in AliITSgeom
573         if(fDetTypeSim->GetITSgeom()==0) {
574             Error("InitModules","fITSgeom not defined");
575             return;
576         } // end if fITSgeom==0
577         nl = fDetTypeSim->GetITSgeom()->GetNlayers();
578         indexMAX = fDetTypeSim->GetITSgeom()->GetIndexMax();
579         nmodules = indexMAX;
580         fITSmodules = new TObjArray(indexMAX);
581         for(index=0;index<indexMAX;index++){
582             fITSmodules->AddAt( new AliITSmodule(index),index);
583         } // end for index
584     }else{
585         fITSmodules = new TObjArray(size);
586         for(index=0;index<size;index++) {
587             fITSmodules->AddAt( new AliITSmodule(index),index);
588         } // end for index
589
590         nmodules = size;
591     } // end i size<=0
592 }
593 //______________________________________________________________________
594 void AliITS::Hits2SDigits(){
595     // Standard Hits to summable Digits function.
596     // Inputs:
597     //      none.
598     // Outputs:
599     //      none.
600   
601
602    if(!fDetTypeSim) {
603      Error("Hits2SDigits","fDetTypeSim is null!");
604      return;
605     
606   } 
607      
608   SetDefaults();
609   fLoader->LoadHits("read");
610   fLoader->LoadSDigits("recreate");
611   AliRunLoader* rl = fLoader->GetRunLoader(); 
612   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
613   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
614         // Do the Hits to Digits operation. Use Standard input values.
615         // Event number from file, no background hit merging , use size from
616         // AliITSgeom class, option="All", input from this file only.
617     rl->GetEvent(iEvent);
618     if (!fLoader->TreeS()) fLoader->MakeTree("S");
619     MakeBranch("S");
620     SetTreeAddress();
621     HitsToPreDigits(iEvent,0,-1," ",fOpt," ");
622   } // end for iEvent
623     
624   fLoader->UnloadHits();
625   fLoader->UnloadSDigits();
626   
627 }
628 //______________________________________________________________________
629 void AliITS::Hits2Digits(){
630
631   //Conversion from hits to digits
632   if(!fDetTypeSim) {
633     Error("Hits2SDigits","fDetTypeSim is 0!");
634     return;
635   }
636    
637   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
638   SetDefaults();
639
640   fLoader->LoadHits("read");
641   fLoader->LoadDigits("recreate");
642   AliRunLoader* rl = fLoader->GetRunLoader(); 
643   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
644     rl->GetEvent(iEvent);
645     if (!fLoader->TreeD()) fLoader->MakeTree("D");
646     MakeBranch("D");
647     SetTreeAddress();   
648     HitsToDigits(iEvent,0,-1," ",fOpt," ");
649   } 
650   
651   fLoader->UnloadHits();
652   fLoader->UnloadDigits();
653   
654 }
655
656 //______________________________________________________________________
657 void AliITS::HitsToDigits(Int_t evNumber,Int_t bgrev,Int_t size,
658                           Option_t *option,Option_t *opt,
659                           const char *filename){
660     //   Keep galice.root for signal and name differently the file for 
661     // background when add! otherwise the track info for signal will be lost !
662     // the condition below will disappear when the geom class will be
663     // initialized for all versions - for the moment it is only for v5 !
664     // 7 is the SDD beam test version.
665     // Inputs:
666     //      Int_t evnt       Event to be processed.
667     //      Int_t bgrev      Background Hit tree number.
668     //      Int_t nmodules   Not used.
669     //      Option_t *option String indicating if merging hits or not. To
670     //                       merge hits set equal to "Add". Otherwise no
671     //                       background hits are considered.
672     //      Test_t *filename File name containing the background hits..
673     // Outputs:
674     //      none.
675     // Return:
676     //      none.
677
678   if(!fDetTypeSim) {
679     Error("HitsToDigits","fDetTypeSim is null!");
680     return;
681   }
682   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
683   if(!GetITSgeom()) return; // need transformations to do digitization.
684   AliITSgeom *geom = GetITSgeom();
685
686   const char *all = strstr(opt,"All");
687   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
688                         strstr(opt,"SSD")};
689   static Bool_t setDef=kTRUE;
690   if (setDef) SetDefaultSimulation();
691   setDef=kFALSE;
692   
693   Int_t nmodules;
694   InitModules(size,nmodules);
695   FillModules(evNumber,bgrev,nmodules,option,filename);
696
697   AliITSsimulation *sim      = 0;
698   AliITSmodule     *mod      = 0;
699   Int_t id;
700   for(Int_t module=0;module<geom->GetIndexMax();module++){
701     id       = geom->GetModuleType(module);
702     if (!all && !det[id]) continue;
703     sim      = (AliITSsimulation*)fDetTypeSim->GetSimulationModel(id);
704     if (!sim) {
705       Error("HitsToDigits","The simulation class was not "
706             "instanciated for module %d type %s!",module,
707             geom->GetModuleTypeName(module));
708       exit(1);
709     } // end if !sim
710     mod      = (AliITSmodule *)fITSmodules->At(module);
711     sim->DigitiseModule(mod,module,evNumber);
712     // fills all branches - wasted disk space
713     fLoader->TreeD()->Fill(); 
714     ResetDigits();
715   } // end for module
716   
717   ClearModules();
718   
719   fLoader->TreeD()->GetEntries();
720   fLoader->TreeD()->AutoSave();
721   // reset tree
722   fLoader->TreeD()->Reset();
723 }
724 //_____________________________________________________________________
725 void AliITS::Hits2PreDigits(){ 
726   // Turn hits into SDigits
727
728   if(!fDetTypeSim) {
729     Error("Hits2SDigits","fDetTypeSim is 0!");
730     return;
731   }
732    
733   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
734   SetDefaults();
735   
736   HitsToPreDigits(fLoader->GetRunLoader()->GetEventNumber(),
737                   0,-1," ",fOpt," ");
738 }
739
740 //______________________________________________________________________
741 void AliITS::HitsToPreDigits(Int_t evNumber,Int_t bgrev,Int_t size,
742                              Option_t *option,Option_t *opt,
743                              const char *filename){
744     //   Keep galice.root for signal and name differently the file for 
745     // background when add! otherwise the track info for signal will be lost !
746     // the condition below will disappear when the geom class will be
747     // initialized for all versions - for the moment it is only for v5 !
748     // 7 is the SDD beam test version.
749     // Inputs:
750     //      Int_t evnt       Event to be processed.
751     //      Int_t bgrev      Background Hit tree number.
752     //      Int_t nmodules   Not used.
753     //      Option_t *option String indicating if merging hits or not. To
754     //                       merge hits set equal to "Add". Otherwise no
755     //                       background hits are considered.
756     //      Test_t *filename File name containing the background hits..
757     // Outputs:
758     //      none.
759     // Return:
760     //      none.
761
762  
763   if(!fDetTypeSim) {
764     Error("HitsToPreDigits","fDetTypeSim is null!");
765     return;
766   }
767   fDetTypeSim->SetLoader((AliITSLoader*)fLoader);
768
769   if(!GetITSgeom()){
770     Error("HitsToPreDigits","fGeom is null!");
771     return; // need transformations to do digitization.
772   }
773   AliITSgeom *geom = GetITSgeom();
774
775   const char *all = strstr(opt,"All");
776   const char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),
777                         strstr(opt,"SSD")};
778   static Bool_t setDef=kTRUE;
779   if (setDef) SetDefaultSimulation();
780   setDef=kFALSE;
781   
782   Int_t nmodules;
783   InitModules(size,nmodules);
784   FillModules(evNumber,bgrev,nmodules,option,filename);
785   
786
787   AliITSsimulation *sim      = 0;
788   AliITSmodule     *mod      = 0;
789   Int_t id,module;
790   for(module=0;module<geom->GetIndexMax();module++){
791     id       = geom->GetModuleType(module);
792     if (!all && !det[id]) continue;
793     sim      = (AliITSsimulation*)GetSimulationModel(id);
794     if (!sim) {
795       Error("HitsToPreDigits","The simulation class was not "
796             "instanciated for module %d type %s!",module,
797             geom->GetModuleTypeName(module));
798       exit(1);
799     } // end if !sim
800     mod      = (AliITSmodule *)fITSmodules->At(module);
801     sim->SDigitiseModule(mod,module,evNumber);
802     // fills all branches - wasted disk space
803     fLoader->TreeS()->Fill(); 
804     fDetTypeSim->ResetSDigits();
805   } // end for module
806
807   ClearModules();
808
809   
810   fLoader->TreeS()->GetEntries();
811   fLoader->TreeS()->AutoSave();
812   fLoader->WriteSDigits("OVERWRITE");
813   // reset tree
814   fLoader->TreeS()->Reset();
815 }
816
817 //_____________________________________________________________________
818 void AliITS::HitsToFastRecPoints(Int_t evNumber,Int_t bgrev,Int_t size,
819                                   Option_t *opt0,Option_t *opt1,
820                                  const char *flnm){
821     // keep galice.root for signal and name differently the file for 
822     // background when add! otherwise the track info for signal will be lost !
823     // the condition below will disappear when the geom class will be
824     // initialized for all versions - for the moment it is only for v5 !
825     // Inputs:
826     //      Int_t evnt       Event to be processed.
827     //      Int_t bgrev      Background Hit tree number.
828     //      Int_t size       Size used by InitModules. See InitModules.
829     //      Option_t *opt0   Option passed to FillModules. See FillModules.
830     //      Option_t *opt1   String indicating if merging hits or not. To
831     //                       merge hits set equal to "Add". Otherwise no
832     //                       background hits are considered.
833     //      Test_t *flnm     File name containing the background hits..
834     // Outputs:
835     //      none.
836     // Return:
837     //      none.
838
839
840
841   if(!GetITSgeom()){
842     Error("HitsToPreDigits","fGeom is null!");
843     return; // need transformations to do digitization.
844   }
845   AliITSgeom *geom = GetITSgeom();
846
847   AliITSLoader *pITSloader = (AliITSLoader*)fLoader;
848
849   const char *all = strstr(opt1,"All");
850   const char *det[3] ={strstr(opt1,"SPD"),strstr(opt1,"SDD"),
851                        strstr(opt1,"SSD")};
852   Int_t nmodules;
853   InitModules(size,nmodules);
854   FillModules(evNumber,bgrev,nmodules,opt0,flnm);
855
856   AliITSsimulation *sim      = 0;
857   AliITSmodule     *mod      = 0;
858   Int_t id,module;
859
860   TTree *lTR = pITSloader->TreeR();
861   if(!lTR) {
862     pITSloader->MakeTree("R");
863     lTR = pITSloader->TreeR();
864   }
865   
866   TClonesArray* ptarray = new TClonesArray("AliITSRecPoint",1000);
867   TBranch* branch = (TBranch*)lTR->Branch("ITSRecPointsF",&ptarray);
868   branch->SetAddress(&ptarray);
869   //m.b. : this change is nothing but a nice way to make sure
870   //the CPU goes up !    
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((AliITSLoader*)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((AliITSLoader*)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 AliLoader* AliITS::MakeLoader(const char* topfoldername){ 
1135     //builds ITSgetter (AliLoader type)
1136     //if detector wants to use castomized getter, it must overload this method
1137
1138     Info("MakeLoader","Creating AliITSLoader. Top folder is %s.",
1139          topfoldername);
1140     fLoader = new AliITSLoader(GetName(),topfoldername);
1141     return fLoader;
1142 }