]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITS.cxx
b505d465b9aa99b1f21a1c957328c49be52fc9ab
[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 "AliITSclusterV2.h"
89 #include "AliRun.h"
90 #include "AliLog.h"
91
92 ClassImp(AliITS)
93
94 //______________________________________________________________________
95 AliITS::AliITS() : AliDetector(){
96   // Default initializer for ITS
97   //      The default constructor of the AliITS class. In addition to
98   // creating the AliITS class it zeros the variables fIshunt (a member
99   // of AliDetector class), fEuclidOut, and fIdN, and zeros the pointers
100   // fIdSens, and fIdName. The AliDetector default constructor
101   // is also called.
102   
103   fEuclidOut=0;
104   fOpt="All";
105   fIdSens=0;
106   fIdName=0;
107   fDetTypeSim=0;
108   fIshunt     = 0;   // not zeroed in AliDetector.
109   fHits =0;
110   fNhits=0;
111   fITSmodules=0;
112  
113 //    SetDetectors(); // default to fOpt="All". This variable not written out.
114     SetMarkerColor(kRed);
115 }
116 //______________________________________________________________________
117 AliITS::AliITS(const char *name, const char *title):AliDetector(name,title){
118   //     The standard Constructor for the ITS class. 
119   // It also zeros the variables
120   // fIshunt (a member of AliDetector class), fEuclidOut, and zeros
121   // the pointers fIdSens and fIdName. To help in displaying hits via the
122   // ROOT macro display.C AliITS also sets the marker color to red. The
123   // variables passes with this constructor, const char *name and *title,
124   // are used by the constructor of AliDetector class. See AliDetector
125   // class for a description of these parameters and its constructor
126   // functions.
127   
128   fEuclidOut=0;
129   fOpt="All";
130   fIdSens=0;
131   fIdName=0;
132   fDetTypeSim=0;
133   fHits = new TClonesArray("AliITShit",1560);
134   if(gAlice->GetMCApp()) gAlice->GetMCApp()->AddHitList(fHits);
135   fNhits=0;
136   fITSmodules = 0;
137
138   fIshunt     = 0;  // not zeroed in AliDetector
139   // Not done in AliDetector.
140
141   fEuclidOut  = 0;
142   SetDetectors(); // default to fOpt="All". This variable not written out.
143   
144   fIdName     = 0;
145   fIdSens     = 0;
146   
147   fDetTypeSim   = new AliITSDetTypeSim();
148   
149   SetMarkerColor(kRed);
150
151 }
152 //______________________________________________________________________
153 AliITS::~AliITS(){
154     // Default destructor for ITS.
155     //     The default destructor of the AliITS class. In addition to deleting
156     // the AliITS class it deletes the memory pointed to by 
157     // fIdSens, fIdName, fDetTypeSim and it's contents.
158     // Inputs:
159     //      none.
160     // Outputs:
161     //      none.
162     // Return:
163     //      none.
164     if (fHits) {
165       fHits->Delete();
166       delete fHits;
167       fHits=0;
168     }
169     if(fITSmodules) {
170         this->ClearModules();
171         delete fITSmodules;
172         fITSmodules = 0;
173     }// end if fITSmodules!=0
174
175     delete[] fIdName;  // Array of TStrings
176     delete[] fIdSens;
177
178     if (fDetTypeSim){
179       delete fDetTypeSim;
180       fDetTypeSim = 0;
181     }
182 }
183 //______________________________________________________________________
184 AliITS::AliITS(const AliITS &source) : AliDetector(source){
185     // Copy constructor. This is a function which is not allowed to be
186     // done to the ITS. It exits with an error.
187     // Inputs:
188     //      AliITS &source  An AliITS class.
189     // Outputs:
190     //      none.
191     // Return:
192     //      none.
193
194     if(this==&source) return;
195     Error("Copy constructor",
196           "You are not allowed to make a copy of the AliITS");
197     exit(1);
198 }
199 //______________________________________________________________________
200 AliITS& AliITS::operator=(const AliITS &source){
201     // Assignment operator. This is a function which is not allowed to be
202     // done to the ITS. It exits with an error.
203     // Inputs:
204     //      AliITS &source  An AliITS class.
205     // Outputs:
206     //      none.
207     // Return:
208     //      none.
209
210     if(this==&source) return *this;
211     Error("operator=","You are not allowed to make a copy of the AliITS");
212     exit(1);
213     return *this; //fake return
214 }
215 //______________________________________________________________________
216 AliDigitizer* AliITS::CreateDigitizer(AliRunDigitizer* manager)const{
217     // Creates the AliITSDigitizer in a standard way for use via AliModule.
218     // This function can not be included in the .h file because of problems
219     // with the order of inclusion (recursive).
220     // Inputs:
221     //    AliRunDigitizer *manager  The Manger class for Digitization
222     // Output:
223     //    none.
224     // Return:
225     //    A new AliITSRunDigitizer (cast as a AliDigitizer).
226
227      return new AliITSDigitizer(manager);
228 }
229 //______________________________________________________________________
230 void AliITS::Init(){
231     // Initializer ITS after it has been built
232     //     This routine initializes the AliITS class. It is intended to be
233     // called from the Init function in AliITSv?. Besides displaying a banner
234     // indicating that it has been called it initializes the array fIdSens
235     // and sets the default segmentation, response, digit and raw cluster
236     // classes therefore it should be called after a call to CreateGeometry.
237     // Inputs:
238     //      none.
239     // Outputs:
240     //      none.
241     // Return:
242     //      none.
243     Int_t i;
244
245     SetDefaults();
246     // Array of TStrings
247     if(gMC) for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId(fIdName[i]);
248  
249     WriteGeometry();
250 }
251
252 //______________________________________________________________________
253 void AliITS::WriteGeometry(){
254   
255   //Writes ITS geomtry on gAlice
256
257   if(!fLoader) MakeLoader(AliConfig::GetDefaultEventFolderName());
258   AliRunLoader* rl  = fLoader->GetRunLoader();
259   rl->CdGAFile();
260   AliITSgeom* geom = GetITSgeom();
261   geom->Write();
262
263 }
264
265 //______________________________________________________________________
266 void AliITS::SetDefaults(){
267     // sets the default segmentation, response, digit and raw cluster classes.
268     // Inputs:
269     //      none.
270     // Outputs:
271     //      none.
272     // Return:
273     //      none.
274   AliInfoClass("Setting Defaults");
275
276     if(!fDetTypeSim) { 
277      Error("SetDefaults()","fDetTypeSim is 0!"); 
278      return;
279     }
280
281     fDetTypeSim->SetDefaults();
282
283
284 }
285 //______________________________________________________________________
286 void AliITS::SetDefaultSimulation(){
287     // sets the default simulation.
288     // Inputs:
289     //      none.
290     // Outputs:
291     //      none.
292     // Return:
293     //      none.
294     if(!fDetTypeSim) { 
295      Error("SetDefaultSimulation()","fDetTypeSim is 0!"); 
296      return;
297     }
298
299     fDetTypeSim->SetDefaultSimulation();
300
301 }
302
303
304 //______________________________________________________________________
305 void AliITS::MakeBranch(Option_t* option){
306     // Creates Tree branches for the ITS.
307     // Inputs:
308     //      Option_t *option    String of Tree types S,D, and/or R.
309     //      const char *file    String of the file name where these branches
310     //                          are to be stored. If blank then these branches
311     //                          are written to the same tree as the Hits were
312     //                          read from.
313     // Outputs:
314     //      none.
315     // Return:
316     //      none.
317   if(!fDetTypeSim) {
318     Error("MakeBranch","fDetTypeSim is 0!");
319     return;
320   }
321
322   Bool_t cH = (strstr(option,"H")!=0);
323   Bool_t cS = (strstr(option,"S")!=0);
324   Bool_t cD = (strstr(option,"D")!=0);
325   
326   if(cH && (fHits == 0x0)) fHits = new TClonesArray("AliITShit", 1560);
327   AliDetector::MakeBranch(option);
328   
329   if(cS) MakeBranchS(0);
330   if(cD) MakeBranchD(0);
331
332
333 }
334 //___________________________________________________________________
335 void AliITS::MakeBranchS(const char* fl){
336
337   // Creates Tree Branch for the ITS summable digits.
338   // Inputs:
339   //      cont char *fl  File name where SDigits branch is to be written
340   //                     to. If blank it write the SDigits to the same
341   //                     file in which the Hits were found.
342
343   
344   if(!fDetTypeSim){
345     Error("MakeBranchS","fDetTypeSim is 0!");
346   }
347   Int_t buffersize = 4000;
348   char branchname[30];
349
350   // only one branch for SDigits.
351   sprintf(branchname,"%s",GetName());
352
353   if(fLoader->TreeS()){
354     if(fDetTypeSim->GetSDigits()==0x0) fDetTypeSim->SetSDigits(new TClonesArray("AliITSpListItem",1000));
355     TClonesArray* sdig = (TClonesArray*)fDetTypeSim->GetSDigits();
356     MakeBranchInTree(fLoader->TreeS(),branchname,&sdig,buffersize,fl);
357   } 
358 }
359 //______________________________________________________________________
360 void AliITS::MakeBranchD(const char* file){
361
362   //Make branch for digits
363   if(!fDetTypeSim) {
364     Warning("MakeBranchD","fDetTypeSim is 0!");
365     return;
366   }
367   fDetTypeSim->SetLoader(fLoader);
368   MakeBranchInTreeD(fLoader->TreeD(),file);
369 }
370
371 //___________________________________________________________________
372 void AliITS:: MakeBranchInTreeD(TTree* treeD, const char* file){
373   // Creates Tree branches for the ITS.
374
375   if(!fDetTypeSim){
376     Error("MakeBranchS","fDetTypeSim is 0!");
377   }
378   fDetTypeSim->SetLoader(fLoader);
379
380   const Char_t *det[3] = {"SPD","SDD","SSD"};
381   Char_t* digclass;
382   Int_t buffersize = 4000;
383   Char_t branchname[30];
384   
385   if(!fDetTypeSim->GetDigits()){
386     fDetTypeSim->SetDigits(new TObjArray(fgkNTYPES));
387   }
388   for(Int_t i=0;i<fgkNTYPES;i++){
389     digclass = fDetTypeSim->GetDigitClassName(i);
390     TString classn = digclass;
391     if(!((fDetTypeSim->GetDigits())->At(i))){
392       (fDetTypeSim->GetDigits())->AddAt(new TClonesArray(classn.Data(),1000),i);
393     }
394     else ResetDigits(i);  
395     if(fgkNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
396     else sprintf(branchname,"%sDigits%d",GetName(),i+1);
397     TObjArray* dig = DigitsAddress(i);
398     if(GetDigits() && treeD) AliDetector::MakeBranchInTree(treeD,branchname, &dig,buffersize,file);
399   }
400
401 }
402 //______________________________________________________________________
403 void AliITS::SetTreeAddress(){
404     // Set branch address for the Trees.
405     // Inputs:
406     //      none.
407     // Outputs:
408     //      none.
409     // Return:
410     //      none.
411
412   if(!fDetTypeSim) {
413     Error("SetTreeAddress","fDetTypeSim is 0!");
414     return;
415   }
416
417   fDetTypeSim->SetLoader(fLoader);
418
419   TTree *treeS = fLoader->TreeS();
420   TTree *treeD = fLoader->TreeD();
421   if (fLoader->TreeH() && (fHits == 0x0)) {
422       fHits = new TClonesArray("AliITShit", 1560);
423   }
424   AliDetector::SetTreeAddress();
425
426   fDetTypeSim->SetTreeAddressS(treeS, (Char_t*)GetName());
427   fDetTypeSim->SetTreeAddressD(treeD, (Char_t*)GetName());
428
429
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(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(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(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(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(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   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   TBranch* branch = lTR->GetBranch("ITSRecPointsF");
861   TClonesArray* ptarray = new TClonesArray("AliITSRecPoint",1000);
862   if(branch)  branch->SetAddress(ptarray);
863   else lTR->Branch("ITSRecPointsF",&ptarray);
864  
865
866   //m.b. : this change is nothing but a nice way to make sure
867   //the CPU goes up !
868   AliDebug(1,Form("N mod = %d",geom->GetIndexMax()));    
869   for(module=0;module<geom->GetIndexMax();module++){
870     id       = geom->GetModuleType(module);
871     if (!all && !det[id]) continue;
872     sim      = (AliITSsimulation*)GetSimulationModel(id);
873     if (!sim) {
874       Error("HitsToFastPoints","The simulation class was not "
875             "instanciated for module %d type %x!",module,
876             geom->GetModuleTypeName(module));
877       exit(1);
878     } // end if !sim
879     mod      = (AliITSmodule *)fITSmodules->At(module);
880     sim->CreateFastRecPoints(mod,module,gRandom,ptarray);
881     lTR->Fill();
882   } // end for module
883
884   ClearModules();
885   fLoader->WriteRecPoints("OVERWRITE");
886   delete ptarray;
887 }
888 //_____________________________________________________________________
889 Int_t AliITS::Hits2Clusters(TTree *hTree, TTree *cTree) {
890   //------------------------------------------------------------
891   // This function creates ITS clusters
892   //------------------------------------------------------------
893   if(!GetITSgeom()){
894     Error("HitsToPreDigits","fGeom is null!");
895     return 1; // need transformations to do digitization.
896   }
897   AliITSgeom *geom=GetITSgeom();
898   Int_t mmax=geom->GetIndexMax();
899
900   InitModules(-1,mmax);
901   FillModules(hTree,0);
902
903   TClonesArray *clusters=new TClonesArray("AliITSclusterV2",1000);
904   TBranch *branch=cTree->GetBranch("Clusters");
905   if (!branch) cTree->Branch("Clusters",&clusters);
906   else branch->SetAddress(&clusters);
907
908   TClonesArray *points = new TClonesArray("AliITSRecPoint",1000);
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     RecPoints2Clusters(points, m, clusters);
916     ncl+=clusters->GetEntriesFast();
917     cTree->Fill();
918     clusters->Clear();
919     points->Clear();
920   }
921
922   Info("Hits2Clusters","Number of found fast clusters : %d",ncl);
923
924   //cTree->Write();
925
926   delete clusters;
927   delete points;
928   return 0;
929 }
930 //_____________________________________________________________________
931 void AliITS::RecPoints2Clusters
932 (const TClonesArray *points, Int_t idx, TClonesArray *clusters) {
933   //------------------------------------------------------------
934   // Conversion AliITSRecPoint -> AliITSclusterV2 for the ITS 
935   // subdetector indexed by idx 
936   //------------------------------------------------------------
937
938   if(!GetITSgeom()){
939     Error("HitsToPreDigits","fGeom is null!");
940     return; // need transformations to do digitization.
941   }
942   AliITSgeom *g=GetITSgeom();
943   Int_t mmax = g->GetIndexMax();
944   Float_t yshift[2200];
945   Float_t zshift[2200];
946   Int_t ndet[2200];
947   for (Int_t m=0; m<mmax; m++) {
948     Int_t lay,lad,det; g->GetModuleId(m,lay,lad,det);
949     Float_t x,y,z;     g->GetTrans(lay,lad,det,x,y,z); 
950     Double_t rot[9];   g->GetRotMatrix(lay,lad,det,rot);
951     Double_t alpha=TMath::ATan2(rot[1],rot[0])+TMath::Pi();
952     Double_t ca=TMath::Cos(alpha), sa=TMath::Sin(alpha);
953     yshift[m] = x*ca + y*sa;
954     zshift[m] = (Double_t)z;
955     ndet[m] = (lad-1)*g->GetNdetectors(lay) + (det-1);
956   }
957
958   //SPD geometry  
959   Float_t lastSPD1=g->GetModuleIndex(2,1,1)-1;
960
961   TClonesArray &cl=*clusters;
962   Int_t ncl=points->GetEntriesFast();
963   for (Int_t i=0; i<ncl; i++) {
964     AliITSRecPoint *p = (AliITSRecPoint *)points->UncheckedAt(i);
965     Float_t lp[5];
966     lp[0]=-(-p->GetX()+yshift[idx]); if (idx<=lastSPD1) lp[0]*=-1; //SPD1
967     lp[1]=  -p->GetZ()+zshift[idx];
968     lp[2]=p->GetSigmaX2();
969     lp[3]=p->GetSigmaZ2();
970     lp[4]=p->GetQ()*36./23333.;  //electrons -> ADC
971     Int_t lab[4]; 
972     lab[0]=p->GetLabel(0); lab[1]=p->GetLabel(1); lab[2]=p->GetLabel(2);
973     lab[3]=ndet[idx];
974     CheckLabels(lab);
975     Int_t dummy[3]={0,0,0};
976     new (cl[i]) AliITSclusterV2(lab,lp, dummy);
977   }  
978
979 //_____________________________________________________________________
980 void AliITS::CheckLabels(Int_t lab[3]) const {
981   //------------------------------------------------------------
982   // Tries to find mother's labels
983   //------------------------------------------------------------
984
985   if(lab[0]<0 && lab[1]<0 && lab[2]<0) return; // In case of no labels just exit
986
987   Int_t ntracks = gAlice->GetMCApp()->GetNtrack();
988   for (Int_t i=0;i<3;i++){
989     Int_t label = lab[i];
990     if (label>=0 && label<ntracks) {
991       TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
992       if (part->P() < 0.005) {
993         Int_t m=part->GetFirstMother();
994         if (m<0) {      
995           continue;
996         }
997         if (part->GetStatusCode()>0) {
998           continue;
999         }
1000         lab[i]=m;       
1001       }
1002     }    
1003   }
1004   
1005 }
1006
1007 //______________________________________________________________________
1008 void AliITS::SDigitsToDigits(Option_t *opt){
1009     // Standard Summable digits to Digits function.
1010     // Inputs:
1011     //      none.
1012     // Outputs:
1013     //      none.
1014     if(!fDetTypeSim) {
1015       Error("SDigitsToSDigits","fDetTypeSim is 0!");
1016       return;
1017     }
1018    
1019     fDetTypeSim->SetLoader(fLoader);
1020     SetDefaults();
1021     fDetTypeSim->SDigitsToDigits(opt,(Char_t*)GetName());
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::AddRealDigit(Int_t branch, Int_t *digits){
1076     //   Add a real digit - as coming from data.
1077     // Inputs:
1078     //      Int_t id        Detector type number.
1079     //      Int_t *digits   Integer array containing the digits info. See 
1080     //                      AliITSdigit.h
1081     // Outputs:
1082     //      none.
1083     // Return:
1084     //      none.
1085
1086     if(!fDetTypeSim) {
1087       Error("AddRealDigit","fDetTypeSim is 0!");
1088       return;
1089     }
1090     fDetTypeSim->AddRealDigit(branch,digits);
1091
1092 }
1093 //______________________________________________________________________
1094 void AliITS::AddSimDigit(Int_t branch, AliITSdigit *d){
1095     //    Add a simulated digit.
1096     // Inputs:
1097     //      Int_t id        Detector type number.
1098     //      AliITSdigit *d  Digit to be added to the Digits Tree. See 
1099     //                      AliITSdigit.h
1100     // Outputs:
1101     //      none.
1102     // Return:
1103     //      none.
1104
1105     if(!fDetTypeSim) {
1106       Error("AddSimDigit","fDetTypeSim is 0!");
1107       return;
1108     }
1109     fDetTypeSim->AddSimDigit(branch,d);
1110
1111 }
1112 //______________________________________________________________________
1113 void AliITS::AddSimDigit(Int_t branch,Float_t phys,Int_t *digits,Int_t *tracks,
1114                          Int_t *hits,Float_t *charges){
1115   //   Add a simulated digit to the list.
1116   // Inputs:
1117   //      Int_t id        Detector type number.
1118   //      Float_t phys    Physics indicator. See AliITSdigits.h
1119   //      Int_t *digits   Integer array containing the digits info. See 
1120   //                      AliITSdigit.h
1121   //      Int_t *tracks   Integer array [AliITSdigitS?D::GetNTracks()] 
1122   //                      containing the track numbers that contributed to
1123   //                      this digit.
1124   //      Int_t *hits     Integer array [AliITSdigitS?D::GetNTracks()]
1125   //                      containing the hit numbers, from AliITSmodule, that
1126   //                      contributed to this digit.
1127   //      Float_t *charge Floating point array of the signals contributed
1128   //                      to this digit by each track.
1129   // Outputs:
1130   //      none.
1131   // Return:
1132   //      none.
1133
1134     if(!fDetTypeSim) {
1135       Error("AddSimDigit","fDetTypeSim is 0!");
1136       return;
1137     }
1138     fDetTypeSim->AddSimDigit(branch,phys,digits,tracks,hits,charges);
1139
1140 }
1141 //______________________________________________________________________
1142 void AliITS::Digits2Raw(){
1143     // convert digits of the current event to raw data
1144
1145   if(!fDetTypeSim) {
1146     Error("Digits2Raw","fDetTypeSim is 0!");
1147     return;
1148   }
1149   fDetTypeSim->SetLoader(fLoader);
1150   SetDefaults();
1151   fDetTypeSim->GetLoader()->LoadDigits();
1152   TTree* digits = fDetTypeSim->GetLoader()->TreeD();
1153   if (!digits) {
1154       Error("Digits2Raw", "no digits tree");
1155       return;
1156   }
1157   fDetTypeSim->SetTreeAddressD(digits,(Char_t*)GetName());
1158
1159   AliITSDDLRawData rawWriter;
1160   //Verbose level
1161   // 0: Silent
1162   // 1: cout messages
1163   // 2: txt files with digits 
1164   //BE CAREFUL, verbose level 2 MUST be used only for debugging and
1165   //it is highly suggested to use this mode only for debugging digits files
1166   //reasonably small, because otherwise the size of the txt files can reach
1167   //quickly several MB wasting time and disk space.
1168   rawWriter.SetVerbose(0);
1169     
1170   //SILICON PIXEL DETECTOR
1171   Info("Digits2Raw", "Formatting raw data for SPD");
1172   rawWriter.RawDataSPD(digits->GetBranch("ITSDigitsSPD"));
1173     
1174   //SILICON DRIFT DETECTOR
1175   Info("Digits2Raw", "Formatting raw data for SDD");
1176   rawWriter.RawDataSDD(digits->GetBranch("ITSDigitsSDD"));
1177     
1178   //SILICON STRIP DETECTOR
1179   Info("Digits2Raw", "Formatting raw data for SSD");
1180   rawWriter.RawDataSSD(digits->GetBranch("ITSDigitsSSD"));
1181
1182   fLoader->UnloadDigits();
1183   
1184
1185
1186   
1187 }
1188
1189
1190
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     Info("MakeLoader","Creating AliITSLoader. Top folder is %s.",
1198          topfoldername);
1199     fLoader = new AliITSLoader(GetName(),topfoldername);
1200     return fLoader;
1201 }