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