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