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