]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITS.cxx
fixed dependencies of include files. Tryed but failed to get a root automaticly
[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 /*
17 $Log$
18 Revision 1.9.2.15  2000/10/04 16:56:40  nilsen
19 Needed to include stdlib.h
20
21 =======
22 Revision 1.22  2000/10/04 19:45:52  barbera
23 Corrected by F. Carminati for v3.04
24
25 Revision 1.21  2000/10/02 21:28:08  fca
26 Removal of useless dependecies via forward declarations
27
28 Revision 1.20  2000/10/02 16:31:39  barbera
29 General code clean-up
30
31 Revision 1.9.2.14  2000/10/02 15:43:51  barbera
32 General code clean-up (e.g., printf -> cout)
33
34 Revision 1.19  2000/09/22 12:13:25  nilsen
35 Patches and updates for fixes to this and other routines.
36
37 Revision 1.18  2000/07/12 05:32:20  fca
38 Correcting several syntax problem with static members
39
40 Revision 1.17  2000/07/10 16:07:18  fca
41 Release version of ITS code
42
43 Revision 1.9.2.3  2000/02/02 13:42:09  barbera
44 fixed AliITS.cxx for new AliRun structure. Added ITS hits list to list of hits which will have their track numbers updated
45
46 Revision 1.9.2.2  2000/01/23 03:03:13  nilsen
47 //fixed FillModule. Removed fi(fabs(xl)<dx....
48
49 Revision 1.9.2.1  2000/01/12 19:03:32  nilsen
50 This is the version of the files after the merging done in December 1999.
51 See the ReadMe110100.txt file for details
52
53 Revision 1.9  1999/11/14 14:33:25  fca
54 Correct problems with distructors and pointers, thanks to I.Hrivnacova
55
56 Revision 1.8  1999/09/29 09:24:19  fca
57 Introduction of the Copyright and cvs Log
58
59 */
60
61 ///////////////////////////////////////////////////////////////////////////////
62 //
63 //      An overview of the basic philosophy of the ITS code development
64 // and analysis is show in the figure below.
65 //Begin_Html
66 /*
67 <img src="picts/ITS/ITS_Analysis_schema.gif">
68 </pre>
69 <br clear=left>
70 <font size=+2 color=red>
71 <p>Roberto Barbera is in charge of the ITS Offline code (1999).
72 <a href="mailto:roberto.barbera@ct.infn.it">Roberto Barbera</a>.
73 </font>
74 <pre>
75 */
76 //End_Html
77 //
78 //  AliITS. Inner Traking System base class.
79 //  This class contains the base procedures for the Inner Tracking System
80 //
81 //Begin_Html
82 /*
83 <img src="picts/ITS/AliITS_Class_Diagram.gif">
84 </pre>
85 <br clear=left>
86 <font size=+2 color=red>
87 <p>This show the class diagram of the different elements that are part of
88 the AliITS class.
89 </font>
90 <pre>
91 */
92 //End_Html
93 //
94 // Version: 0
95 // Written by Rene Brun, Federico Carminati, and Roberto Barbera
96 //
97 // Version: 1
98 // Modified and documented by Bjorn S. Nilsen
99 // July 11 1999
100 //
101 // Version: 2
102 // Modified and documented by A. Bologna
103 // October 18 1999
104 //
105 // AliITS is the general base class for the ITS. Also see AliDetector for
106 // futher information.
107 //
108 ///////////////////////////////////////////////////////////////////////////////
109 #include <stdlib.h>
110 #include <TMath.h>
111 #include <TRandom.h>
112 #include <TBranch.h>
113 #include <TVector.h>
114 #include <TObjArray.h>
115 #include <TROOT.h>
116 #include <TObjectTable.h>
117 #include <TFile.h>
118 #include <TTree.h>
119 #include <TString.h>
120
121
122
123 #include "AliRun.h"
124 #include "AliITS.h"
125 #include "AliITSMap.h"
126 #include "AliITSDetType.h"
127 #include "AliITSClusterFinder.h"
128 #include "AliITSsimulation.h"
129 #include "AliITSresponse.h"
130 #include "AliITSsegmentationSPD.h"
131 #include "AliITSresponseSPD.h"
132 #include "AliITSsegmentationSDD.h"
133 #include "AliITSresponseSDD.h"
134 #include "AliITSsegmentationSSD.h"
135 #include "AliITSresponseSSD.h"
136 #include "AliITShit.h"
137 #include "AliITSgeom.h"
138 #include "AliITSdigit.h"
139 #include "AliITSmodule.h"
140 #include "AliITSRecPoint.h"
141 #include "AliITSRawCluster.h"
142 #include "AliMC.h"
143 #include "stdlib.h"
144
145 const Int_t AliITS::fgkNTYPES=3;
146
147 ClassImp(AliITS)
148  
149 //_____________________________________________________________________________
150 AliITS::AliITS() : AliDetector() {
151   //
152   // Default initialiser for ITS
153   //     The default constructor of the AliITS class. In addition to
154   // creating the AliITS class it zeros the variables fIshunt (a member
155   // of AliDetector class), fEuclidOut, and fIdN, and zeros the pointers
156   // fITSpoints, fIdSens, and fIdName. The AliDetector default constructor
157   // is also called.
158   //
159
160
161   fIshunt     = 0;
162   fEuclidOut  = 0;
163
164   //fNDetTypes = fgkNTYPES;
165   fIdN        = 0;
166   fIdName     = 0;
167   fIdSens     = 0;
168   fITSmodules = 0;
169   //
170   fDetTypes   = 0;
171   //
172   fDtype  = 0;
173   fNdtype = 0;
174   fCtype  = 0;
175   fNctype = 0;
176   fRecPoints = 0;
177   fNRecPoints = 0;
178   fTreeC = 0;
179   //
180   fITSgeom=0;
181 }
182
183 //_____________________________________________________________________________
184 AliITS::AliITS(const char *name, const char *title):AliDetector(name,title){
185   //
186   // Default initialiser for ITS
187   //     The constructor of the AliITS class. In addition to creating the
188   // AliITS class, it allocates memory for the TClonesArrays fHits and
189   // fDigits, and for the TObjArray fITSpoints. It also zeros the variables
190   // fIshunt (a member of AliDetector class), fEuclidOut, and fIdN, and zeros
191   // the pointers fIdSens and fIdName. To help in displaying hits via the ROOT
192   // macro display.C AliITS also sets the marker color to red. The variables
193   // passes with this constructor, const char *name and *title, are used by
194   // the constructor of AliDetector class. See AliDetector class for a
195   // description of these parameters and its constructor functions.
196   //
197
198
199   fHits       = new TClonesArray("AliITShit", 1560);
200   gAlice->AddHitList(fHits);
201
202   //fNDetTypes = fgkNTYPES;
203
204   fNdtype = new Int_t[fgkNTYPES];
205   fDtype = new TObjArray(fgkNTYPES);
206
207   fNctype = new Int_t[fgkNTYPES];
208   fCtype = new TObjArray(fgkNTYPES);
209
210
211   fRecPoints = 0;
212   fNRecPoints = 0;
213
214   fTreeC = 0;
215
216   fITSmodules = 0; 
217
218   fIshunt     = 0;
219   fEuclidOut  = 0;
220   fIdN        = 0;
221   fIdName     = 0;
222   fIdSens     = 0;
223  
224   fDetTypes = new TObjArray(fgkNTYPES);  
225
226   Int_t i;
227   for(i=0;i<fgkNTYPES;i++) {
228     (*fDetTypes)[i]=new AliITSDetType(); 
229     fNdtype[i]=0;
230     fNctype[i]=0;
231    }
232   //
233
234   SetMarkerColor(kRed);
235
236   fITSgeom=0;
237 }
238 //___________________________________________________________________________
239 AliITS::AliITS(AliITS &source){
240   // copy constructor
241   if(this==&source) return;
242   Error("AliITS::Copy constructor",
243         "You are not allowed to make a copy of the AliITS");
244   exit(1);
245 }
246 //____________________________________________________________________________
247 AliITS& AliITS::operator=(AliITS &source){
248   // assignment operator
249   if(this==&source) return *this;
250   Error("AliITS::operator=",
251         "You are not allowed to make a copy of the AliITS");
252   exit(1);
253   return *this; //fake return
254 }
255 //____________________________________________________________________________
256 void AliITS::ClearModules(){
257   //clear the modules TObjArray
258
259   if(fITSmodules) fITSmodules->Delete();
260
261 }
262 //_____________________________________________________________________________
263 AliITS::~AliITS(){
264   //
265   // Default distructor for ITS
266   //     The default destructor of the AliITS class. In addition to deleting
267   // the AliITS class it deletes the memory pointed to by the fHits, fDigits,
268   // fIdSens, fIdName, and fITSpoints.
269   //
270
271
272   delete fHits;
273   delete fDigits;
274   delete fRecPoints;
275 //  delete fIdName;        // TObjArray of TObjStrings
276   if(fIdName!=0) delete[] fIdName;  // Array of TStrings
277   if(fIdSens!=0) delete[] fIdSens;
278   if(fITSmodules!=0) {
279       this->ClearModules();
280       delete fITSmodules;
281   }// end if fITSmodules!=0
282
283   //
284   if(fDtype) {
285     fDtype->Delete();
286     delete fDtype;
287   }
288   delete [] fNdtype;
289   if (fCtype) {
290     fCtype->Delete();
291     delete fCtype;
292   }
293   delete [] fNctype;
294   //
295
296   if (fDetTypes) {
297     fDetTypes->Delete();
298     delete fDetTypes;
299   }
300
301   if (fTreeC) delete fTreeC;
302
303   if (fITSgeom) delete fITSgeom;
304
305 }
306
307 //___________________________________________
308 AliITSDetType* AliITS::DetType(Int_t id)
309 {
310   //return pointer to id detector type
311     return ((AliITSDetType*) (*fDetTypes)[id]);
312
313 }
314 //___________________________________________
315 void AliITS::SetClasses(Int_t id, const char *digit, const char *cluster)
316 {
317   //set the digit and cluster classes to be used for the id detector type
318     ((AliITSDetType*) (*fDetTypes)[id])->ClassNames(digit,cluster);
319
320 }
321 //___________________________________________
322 void AliITS::SetResponseModel(Int_t id, AliITSresponse *response)
323 {
324   //set the response model for the id detector type
325
326     ((AliITSDetType*) (*fDetTypes)[id])->ResponseModel(response);
327
328 }
329
330 //___________________________________________
331 void AliITS::SetSegmentationModel(Int_t id, AliITSsegmentation *seg)
332 {
333   //set the segmentation model for the id detector type
334
335     ((AliITSDetType*) (*fDetTypes)[id])->SegmentationModel(seg);
336
337 }
338
339 //___________________________________________
340 void AliITS::SetSimulationModel(Int_t id, AliITSsimulation *sim)
341 {
342   //set the simulation model for the id detector type
343
344    ((AliITSDetType*) (*fDetTypes)[id])->SimulationModel(sim);
345
346 }
347 //___________________________________________
348 void AliITS::SetReconstructionModel(Int_t id, AliITSClusterFinder *reconst)
349 {
350   //set the cluster finder model for the id detector type
351
352    ((AliITSDetType*) (*fDetTypes)[id])->ReconstructionModel(reconst);
353
354 }
355
356 //_____________________________________________________________________________
357 void AliITS::AddHit(Int_t track, Int_t *vol, Float_t *hits){
358   //
359   // Add an ITS hit
360   //     The function to add information to the AliITShit class. See the
361   // AliITShit class for a full description. This function allocates the
362   // necessary new space for the hit information and passes the variable
363   // track, and the pointers *vol and *hits to the AliITShit constructor
364   // function.
365   //
366   TClonesArray &lhits = *fHits;
367   new(lhits[fNhits++]) AliITShit(fIshunt,track,vol,hits);
368 }
369 //_____________________________________________________________________________
370 void AliITS::AddRealDigit(Int_t id, Int_t *digits) 
371 {
372   // add a real digit - as coming from data
373
374   TClonesArray &ldigits = *((TClonesArray*)(*fDtype)[id]);
375   new(ldigits[fNdtype[id]++]) AliITSdigit(digits);
376
377 }
378 //_____________________________________________________________________________
379 void AliITS::AddSimDigit(Int_t id, AliITSdigit *d) 
380 {
381
382   // add a simulated digit
383
384   TClonesArray &ldigits = *((TClonesArray*)(*fDtype)[id]);
385
386   switch(id)
387   {
388   case 0:
389      new(ldigits[fNdtype[id]++]) AliITSdigitSPD(*((AliITSdigitSPD*)d));
390      break;
391   case 1:
392      new(ldigits[fNdtype[id]++]) AliITSdigitSDD(*((AliITSdigitSDD*)d));
393      break;
394   case 2:
395      new(ldigits[fNdtype[id]++]) AliITSdigitSSD(*((AliITSdigitSSD*)d));
396      break;
397   }
398
399 }
400
401 //_____________________________________________________________________________
402 void AliITS::AddSimDigit(Int_t id,Float_t phys,Int_t *digits,Int_t *tracks,Int_t *hits,Float_t *charges){
403
404   // add a simulated digit to the list
405
406   TClonesArray &ldigits = *((TClonesArray*)(*fDtype)[id]);
407   switch(id)
408   {
409   case 0:
410      new(ldigits[fNdtype[id]++]) AliITSdigitSPD(digits,tracks,hits);
411      break;
412   case 1:
413      new(ldigits[fNdtype[id]++]) AliITSdigitSDD(phys,digits,tracks,hits,charges);
414      break;
415   case 2:
416      new(ldigits[fNdtype[id]++]) AliITSdigitSSD(digits,tracks,hits);
417      break;
418   }
419  
420 }
421
422 //_____________________________________________________________________________
423 void AliITS::AddCluster(Int_t id, AliITSRawCluster *c) 
424 {
425
426   // add a cluster to the list
427
428   TClonesArray &lcl = *((TClonesArray*)(*fCtype)[id]);
429
430   switch(id)
431   {
432   case 0:
433      new(lcl[fNctype[id]++]) AliITSRawClusterSPD(*((AliITSRawClusterSPD*)c));
434      break;
435   case 1:
436      new(lcl[fNctype[id]++]) AliITSRawClusterSDD(*((AliITSRawClusterSDD*)c));
437      break;
438   case 2:
439      new(lcl[fNctype[id]++]) AliITSRawClusterSSD(*((AliITSRawClusterSSD*)c));
440      break;
441   }
442
443 }
444
445
446 //_____________________________________________________________________________
447 void AliITS::AddRecPoint(const AliITSRecPoint &r)
448 {
449   //
450   // Add a reconstructed space point to the list
451   //
452   TClonesArray &lrecp = *fRecPoints;
453   new(lrecp[fNRecPoints++]) AliITSRecPoint(r);
454 }
455  
456
457 //____________________________________________
458 void AliITS::ResetDigits()
459 {
460     //
461     // Reset number of digits and the digits array for the ITS detector
462     //
463
464     if (!fDtype) return;
465
466     Int_t i;
467     for (i=0;i<fgkNTYPES;i++ ) {
468         if ((*fDtype)[i])    ((TClonesArray*)(*fDtype)[i])->Clear();
469         if (fNdtype)  fNdtype[i]=0;
470     }
471 }
472
473 //____________________________________________
474 void AliITS::ResetDigits(Int_t i)
475 {
476     //
477     // Reset number of digits and the digits array for this branch
478     //
479   if ((*fDtype)[i])    ((TClonesArray*)(*fDtype)[i])->Clear();
480   if (fNdtype)  fNdtype[i]=0;
481 }
482
483
484 //____________________________________________
485 void AliITS::ResetClusters()
486 {
487     //
488     // Reset number of clusters and the clusters array for ITS
489     //
490
491     Int_t i;
492     for (i=0;i<fgkNTYPES;i++ ) {
493         if ((*fCtype)[i])    ((TClonesArray*)(*fCtype)[i])->Clear();
494         if (fNctype)  fNctype[i]=0;
495     }
496
497 }
498
499 //____________________________________________
500 void AliITS::ResetClusters(Int_t i)
501 {
502     //
503     // Reset number of clusters and the clusters array for this branch
504     //
505         if ((*fCtype)[i])    ((TClonesArray*)(*fCtype)[i])->Clear();
506         if (fNctype)  fNctype[i]=0;
507
508 }
509
510
511 //____________________________________________
512 void AliITS::ResetRecPoints()
513 {
514     //
515     // Reset number of rec points and the rec points array 
516     //
517     if (fRecPoints) fRecPoints->Clear();
518     fNRecPoints = 0;
519
520 }
521
522 //_____________________________________________________________________________
523 Int_t AliITS::DistancetoPrimitive(Int_t , Int_t ){
524   //
525   // Distance from mouse to ITS on the screen. Dummy routine
526   //     A dummy routine used by the ROOT macro display.C to allow for the
527   // use of the mouse (pointing device) in the macro. In general this should
528   // never be called. If it is it returns the number 9999 for any value of
529   // x and y.
530   //
531   return 9999;
532 }
533
534 //_____________________________________________________________________________
535 void AliITS::Init(){
536   //
537   // Initialise ITS after it has been built
538   //     This routine initializes the AliITS class. It is intended to be called
539   // from the Init function in AliITSv?. Besides displaying a banner
540   // indicating that it has been called it initializes the array fIdSens
541   // and sets the default segmentation, response, digit and raw cluster classes
542   // Therefore it should be called after a call to CreateGeometry.
543   //
544   Int_t i;
545
546   cout << endl;
547   for(i=0;i<30;i++) cout << "*";cout << " ITS_INIT ";
548   for(i=0;i<30;i++) cout << "*";cout << endl;
549 //
550   SetDefaults();
551 // TObjArray of TObjStrings
552 //  for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId((fIdName->At(i))->GetName());
553 // Array of TStrings
554   for(i=0;i<fIdN;i++) fIdSens[i] = gMC->VolId(fIdName[i]);
555 //
556   for(i=0;i<70;i++) cout << "*";
557   cout << endl;
558 }
559
560 //_____________________________________________________________________________
561 void AliITS::SetDefaults()
562 {
563   // sets the default segmentation, response, digit and raw cluster classes
564
565   cout << "AliITS::SetDefaults" << endl;
566
567   AliITSDetType *iDetType;
568
569   //SPD 
570
571   iDetType = DetType(0);
572   AliITSresponseSPD *resp0 = (AliITSresponseSPD*) iDetType->GetResponseModel();
573   if (!iDetType->GetSegmentationModel()) {
574     AliITSsegmentationSPD *seg0 = new AliITSsegmentationSPD(fITSgeom);
575     SetSegmentationModel(0,seg0);
576   } // end if 
577   if (!resp0){
578     resp0 = new AliITSresponseSPD();
579     SetResponseModel(0,resp0);
580   } // end if
581   // set digit and raw cluster classes to be used
582
583   const char *kData0 = resp0->DataType();
584   if (strstr(kData0,"real")) {
585       iDetType->ClassNames("AliITSdigit","AliITSRawClusterSPD");
586   } else iDetType->ClassNames("AliITSdigitSPD","AliITSRawClusterSPD");
587
588   // SDD
589   iDetType = DetType(1);
590   AliITSresponseSDD *resp1 = (AliITSresponseSDD*)iDetType->GetResponseModel();
591   //printf("SetDefaults: iDetType %p\n",iDetType);
592   if (!iDetType->GetSegmentationModel()) {
593     AliITSsegmentationSDD *seg1 = new AliITSsegmentationSDD(fITSgeom,resp1);
594     SetSegmentationModel(1,seg1); 
595   } // end if
596   //printf("SetDefaults: segm %p\n",iDetType->GetSegmentationModel());
597   if (!resp1) {
598     resp1 = new AliITSresponseSDD();
599     SetResponseModel(1,resp1); 
600   }
601   //printf("SetDefaults: resp %p\n",iDetType->GetResponseModel());
602   const char *kData1 = resp1->DataType();
603   const char *kopt = resp1->ZeroSuppOption();
604   if ((!strstr(kopt,"2D")) && (!strstr(kopt,"1D")) || strstr(kData1,"real") ) {
605       iDetType->ClassNames("AliITSdigit","AliITSRawClusterSDD");
606   } else iDetType->ClassNames("AliITSdigitSDD","AliITSRawClusterSDD");
607
608   // SSD
609   iDetType = DetType(2); 
610   AliITSresponseSSD *resp2 = (AliITSresponseSSD*)iDetType->GetResponseModel();
611   if (!iDetType->GetSegmentationModel()) {
612     AliITSsegmentationSSD *seg2 = new AliITSsegmentationSSD(fITSgeom);
613     SetSegmentationModel(2,seg2); 
614   } // end if
615   if (!resp2) {
616     resp2 = new AliITSresponseSSD();
617     SetResponseModel(2,resp2); 
618   } // end if
619   const char *kData2 = resp2->DataType();
620   if (strstr(kData2,"real")) {
621       iDetType->ClassNames("AliITSdigit","AliITSRawClusterSSD");
622   } else iDetType->ClassNames("AliITSdigitSSD","AliITSRawClusterSSD");
623
624   if (fgkNTYPES>3) {
625     Warning("SetDefaults","Only the three basic detector types are initialised!");
626   } // end if
627
628 }
629 //_____________________________________________________________________________
630 void AliITS::SetDefaultSimulation()
631 {
632   // to be written
633
634 }
635 //_____________________________________________________________________________
636 void AliITS::SetDefaultClusterFinders()
637 {
638   // to be written
639
640 }
641 //_____________________________________________________________________________
642
643 void AliITS::MakeTreeC(Option_t *option)
644 {
645   // create a separate tree to store the clusters
646
647   cout << "AliITS::MakeTreeC" << endl;
648
649      char *optC = strstr(option,"C");
650      if (optC && !fTreeC) fTreeC = new TTree("TC","Clusters in ITS");
651      else return;
652
653      Int_t buffersize = 4000;
654      char branchname[30];
655
656      char *det[3] = {"SPD","SDD","SSD"};
657
658      // one branch for Clusters per type of detector
659      Int_t i;
660      for (i=0; i<fgkNTYPES ;i++) {
661         if (fgkNTYPES==3) sprintf(branchname,"%sClusters%s",GetName(),det[i]);
662         else  sprintf(branchname,"%sClusters%d",GetName(),i+1);
663         if (fCtype   && fTreeC) {
664            TreeC()->Branch(branchname,&((*fCtype)[i]), buffersize);
665            cout << "Making Branch " << branchname;
666            cout << " for Clusters of detector type " << i+1 << endl;
667         }       
668      }
669
670 }
671
672 //_____________________________________________________________________________
673 void AliITS::GetTreeC(Int_t event)
674 {
675
676   cout << "AliITS::GetTreeC" << endl;
677
678   // get the clusters tree for this event and set the branch address
679     char treeName[20];
680     char branchname[30];
681
682     char *det[3] = {"SPD","SDD","SSD"};
683
684     ResetClusters();
685     if (fTreeC) {
686           delete fTreeC;
687     }
688
689     sprintf(treeName,"TreeC%d",event);
690     fTreeC = (TTree*)gDirectory->Get(treeName);
691
692
693     TBranch *branch;
694     if (fTreeC) {
695         Int_t i;
696         for (i=0; i<fgkNTYPES; i++) {
697            if (fgkNTYPES==3) sprintf(branchname,"%sClusters%s",GetName(),det[i]);
698            else  sprintf(branchname,"%sClusters%d",GetName(),i+1);
699            if (fCtype) {
700                 branch = fTreeC->GetBranch(branchname);
701                 if (branch) branch->SetAddress(&((*fCtype)[i]));
702             }
703         }
704     } else {
705         Error("AliITS::GetTreeC",
706                 "cannot find Clusters Tree for event:%d\n",event);
707     }
708
709 }
710 //_____________________________________________________________________________
711 void AliITS::MakeBranch(Option_t* option){
712   //
713   // Creates Tree branches for the ITS.
714   //
715
716
717   Int_t buffersize = 4000;
718   char branchname[30];
719   sprintf(branchname,"%s",GetName());
720
721   AliDetector::MakeBranch(option);
722
723
724 // one branch for digits per type of detector
725   
726    char *det[3] = {"SPD","SDD","SSD"};
727
728    char digclass[40];
729    char clclass[40];
730
731    Int_t i;
732    for (i=0; i<fgkNTYPES ;i++) {
733        AliITSDetType *iDetType=DetType(i); 
734        iDetType->GetClassNames(digclass,clclass);
735        //printf("i, digclass, recclass %d %s %s\n",i,digclass,clclass); 
736        // digits
737        (*fDtype)[i] = new TClonesArray(digclass,10000); 
738        // clusters
739        (*fCtype)[i] = new TClonesArray(clclass,10000); 
740    }
741
742
743   for (i=0; i<fgkNTYPES ;i++) {
744       if (fgkNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
745       else  sprintf(branchname,"%sDigits%d",GetName(),i+1);
746       
747       if (fDtype   && gAlice->TreeD()) {
748           gAlice->TreeD()->Branch(branchname,&((*fDtype)[i]), buffersize);
749           cout << "Making Branch " << branchname;
750           cout << " for digits of type "<< i+1 << endl;
751       } 
752   }
753
754   // only one branch for rec points for all detector types
755   sprintf(branchname,"%sRecPoints",GetName());
756
757   fRecPoints=new TClonesArray("AliITSRecPoint",10000);
758
759   if (fRecPoints && gAlice->TreeR()) {
760     gAlice->TreeR()->Branch(branchname,&fRecPoints, buffersize);
761     cout << "Making Branch " << branchname;
762     cout << " for reconstructed space points" << endl;
763   }     
764
765
766 }
767
768 //___________________________________________
769 void AliITS::SetTreeAddress()
770 {
771
772   // Set branch address for the Trees.
773
774   char branchname[30];
775   AliDetector::SetTreeAddress();
776
777   char *det[3] = {"SPD","SDD","SSD"};
778
779   TBranch *branch;
780   TTree *treeD = gAlice->TreeD();
781   TTree *treeR = gAlice->TreeR();
782
783   Int_t i;
784   if (treeD) {
785       for (i=0; i<fgkNTYPES; i++) {
786           if (fgkNTYPES==3) sprintf(branchname,"%sDigits%s",GetName(),det[i]);
787           else  sprintf(branchname,"%sDigits%d",GetName(),i+1);
788           if (fDtype) {
789               branch = treeD->GetBranch(branchname);
790               if (branch) branch->SetAddress(&((*fDtype)[i]));
791           }
792       }
793   }
794
795  
796   if (treeR) {
797     sprintf(branchname,"%sRecPoints",GetName());
798     if (fRecPoints) {
799       branch = treeR->GetBranch(branchname);
800       if (branch) branch->SetAddress(&fRecPoints);
801     }
802   }
803   
804
805 }
806
807 //____________________________________________________________________________
808 void AliITS::InitModules(Int_t size,Int_t &nmodules){
809
810   //initialize the modules array
811
812   if(fITSmodules){ 
813       fITSmodules->Delete();
814       delete fITSmodules;
815   }
816
817     Int_t nl,indexMAX,index;
818
819     if(size<=0){ // default to using data stored in AliITSgeom
820         if(fITSgeom==0) {
821             Error("AliITS::InitModules",
822                 "in AliITS::InitModule fITSgeom not defined\n");
823             return;
824         } // end if fITSgeom==0
825         nl = fITSgeom->GetNlayers();
826         indexMAX = fITSgeom->GetModuleIndex(nl,fITSgeom->GetNladders(nl),
827                                             fITSgeom->GetNdetectors(nl))+1;
828         nmodules = indexMAX;
829         fITSmodules = new TObjArray(indexMAX);
830         for(index=0;index<indexMAX;index++){
831                 fITSmodules->AddAt( new AliITSmodule(index),index);
832         } // end for index
833     }else{
834         fITSmodules = new TObjArray(size);
835         for(index=0;index<size;index++) {
836             fITSmodules->AddAt( new AliITSmodule(index),index);
837         }
838
839         nmodules = size;
840     } // end i size<=0
841 }
842
843 //____________________________________________________________________________
844 void AliITS::FillModules(Int_t evnt,Int_t bgrev,Int_t nmodules,Option_t *option,Text_t *filename){
845
846   // fill the modules with the sorted by module hits; add hits from background
847   // if option=Add
848
849
850     static TTree *trH1;                 //Tree with background hits
851     static TClonesArray *fHits2;        //List of hits for one track only
852
853     static Bool_t first=kTRUE;
854     static TFile *file;
855     char *addBgr = strstr(option,"Add");
856
857
858     if (addBgr ) {
859         if(first) {
860             cout<<"filename "<<filename<<endl;
861             file=new TFile(filename);
862             cout<<"I have opened "<<filename<<" file "<<endl;
863             fHits2     = new TClonesArray("AliITShit",1000  );
864         }           
865         first=kFALSE;
866         file->cd();
867         file->ls();
868         // Get Hits Tree header from file
869         if(fHits2) fHits2->Clear();
870         if(trH1) delete trH1;
871         trH1=0;
872         
873         char treeName[20];
874         sprintf(treeName,"TreeH%d",bgrev);
875         trH1 = (TTree*)gDirectory->Get(treeName);
876         //printf("TrH1 %p of treename %s for event %d \n",trH1,treeName,bgrev);
877         
878         if (!trH1) {
879             Error("AliITS::FillModules",
880             "cannot find Hits Tree for event:%d\n",bgrev);
881         }
882         // Set branch addresses
883         TBranch *branch;
884         char branchname[20];
885         sprintf(branchname,"%s",GetName());
886         if (trH1 && fHits2) {
887             branch = trH1->GetBranch(branchname);
888             if (branch) branch->SetAddress(&fHits2);
889         }
890
891         // test
892         //Int_t ntracks1 =(Int_t)TrH1->GetEntries();
893         //printf("background - ntracks1 - %d\n",ntracks1);
894    }
895
896     Int_t npart = gAlice->GetEvent(evnt);
897     if(npart<=0) return;
898     TClonesArray *itsHits = this->Hits();
899     Int_t lay,lad,det,index;
900     AliITShit *itsHit=0;
901     AliITSmodule *mod=0;
902
903     TTree *iTH = gAlice->TreeH();
904     Int_t ntracks =(Int_t) iTH->GetEntries();
905
906     Int_t t,h;
907     for(t=0; t<ntracks; t++){
908         gAlice->ResetHits();
909         iTH->GetEvent(t);
910         Int_t nhits = itsHits->GetEntriesFast();
911         //printf("nhits %d\n",nhits);
912         if (!nhits) continue;
913         for(h=0; h<nhits; h++){
914             itsHit = (AliITShit *)itsHits->UncheckedAt(h);
915             itsHit->GetDetectorID(lay,lad,det);
916             // temporarily index=det-1 !!!
917             if(fITSgeom) index = fITSgeom->GetModuleIndex(lay,lad,det);
918             else index=det-1;
919             //
920             mod = this->GetModule(index);
921             mod->AddHit(itsHit,t,h);
922         } // end loop over hits 
923     } // end loop over tracks
924
925     // open the file with background
926     
927     if (addBgr ) {
928           Int_t track,i;
929           ntracks =(Int_t)trH1->GetEntries();
930             //printf("background - ntracks1 %d\n",ntracks);
931             //printf("background - Start loop over tracks \n");     
932             //   Loop over tracks
933
934             for (track=0; track<ntracks; track++) {
935
936                 if (fHits2)       fHits2->Clear();
937                 trH1->GetEvent(track);
938                 //   Loop over hits
939                 for(i=0;i<fHits2->GetEntriesFast();++i) {
940
941                     itsHit=(AliITShit*) (*fHits2)[i];
942                     itsHit->GetDetectorID(lay,lad,det);
943                     // temporarily index=det-1 !!!
944                     if(fITSgeom) index = fITSgeom->GetModuleIndex(lay,lad,det);
945                     else index=det-1;
946                     //
947                     mod = this->GetModule(index);
948                     mod->AddHit(itsHit,track,i);
949                }  // end loop over hits
950             } // end loop over tracks
951
952             TTree *fAli=gAlice->TreeK();
953             TFile *fileAli=0;
954             
955             if (fAli) fileAli =fAli->GetCurrentFile();
956             fileAli->cd();
957
958     } // end if add
959
960     //gObjectTable->Print();
961
962 }
963
964
965 //____________________________________________________________________________
966 void AliITS::HitsToDigits(Int_t evNumber,Int_t bgrev,Int_t size, Option_t *option, Option_t *opt,Text_t *filename)
967 {
968     // keep galice.root for signal and name differently the file for 
969     // background when add! otherwise the track info for signal will be lost !
970   
971    // the condition below will disappear when the geom class will be
972    // initialised for all versions - for the moment it is only for v5 !
973    // 7 is the SDD beam test version  
974    Int_t ver = this->IsVersion(); 
975    if(ver!=5 && ver!=7) return; 
976
977    char *all = strstr(opt,"All");
978    char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),strstr(opt,"SSD")};
979
980    Int_t nmodules;
981    InitModules(size,nmodules); 
982    FillModules(evNumber,bgrev,nmodules,option,filename);
983
984    //TBranch *branch;
985    AliITSsimulation* sim;
986    //TObjArray *branches=gAlice->TreeD()->GetListOfBranches();
987    AliITSgeom *geom = GetITSgeom();
988
989    Int_t id,module;
990    Int_t first,last;
991    for (id=0;id<fgkNTYPES;id++) {
992         if (!all && !det[id]) continue;
993         //branch = (TBranch*)branches->UncheckedAt(id);
994         AliITSDetType *iDetType=DetType(id); 
995         sim = (AliITSsimulation*)iDetType->GetSimulationModel();
996         if (!sim) {
997            Error("HitsToDigits","The simulation class was not instantiated!");
998            exit(1);
999            // or SetDefaultSimulation();
1000         }
1001         if(geom) {
1002           first = geom->GetStartDet(id);
1003           last = geom->GetLastDet(id);
1004         } else first=last=0;
1005         cout << "det type " << id << " first, last "<< first << last << endl;
1006         for(module=first;module<=last;module++) {
1007             AliITSmodule *mod = (AliITSmodule *)fITSmodules->At(module);
1008             sim->DigitiseModule(mod,module,evNumber);
1009             // fills all branches - wasted disk space
1010             gAlice->TreeD()->Fill(); 
1011             ResetDigits();
1012             // try and fill only the branch 
1013             //branch->Fill();
1014             //ResetDigits(id);
1015         } // loop over modules
1016    } // loop over detector types
1017
1018    ClearModules();
1019
1020    Int_t nentries=(Int_t)gAlice->TreeD()->GetEntries();
1021    cout << "nentries in TreeD" << nentries << endl;
1022
1023    char hname[30];
1024    sprintf(hname,"TreeD%d",evNumber);
1025    gAlice->TreeD()->Write(hname);
1026    // reset tree
1027    gAlice->TreeD()->Reset();
1028
1029 }
1030
1031
1032 //____________________________________________________________________________
1033 void AliITS::DigitsToRecPoints(Int_t evNumber,Int_t lastentry,Option_t *opt)
1034 {
1035   // cluster finding and reconstruction of space points
1036   
1037    // the condition below will disappear when the geom class will be
1038    // initialised for all versions - for the moment it is only for v5 !
1039    // 7 is the SDD beam test version  
1040    Int_t ver = this->IsVersion(); 
1041    if(ver!=5 && ver!=7) return; 
1042
1043    char *all = strstr(opt,"All");
1044    char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),strstr(opt,"SSD")};
1045
1046    static Bool_t first=kTRUE;
1047    if (first) {
1048        MakeTreeC("C");
1049        first=kFALSE;
1050    }
1051  
1052    TTree *iTC=TreeC();
1053
1054    //TBranch *branch;
1055    AliITSClusterFinder* rec;
1056
1057    //TObjArray *branches=gAlice->TreeR()->GetListOfBranches();
1058    AliITSgeom *geom = GetITSgeom();
1059
1060    Int_t id,module;
1061    for (id=0;id<fgkNTYPES;id++) {
1062         if (!all && !det[id]) continue;
1063         //branch = (TBranch*)branches->UncheckedAt(id);
1064         AliITSDetType *iDetType=DetType(id); 
1065         rec = (AliITSClusterFinder*)iDetType->GetReconstructionModel();
1066         if (!rec) {
1067            Error("DigitsToRecPoints","The cluster finder class was not instantiated!");
1068            exit(1);
1069            // or SetDefaultClusterFinders();
1070         }
1071         TClonesArray *itsDigits  = this->DigitsAddress(id);
1072
1073         Int_t first,last;
1074         if(geom) {
1075           first = geom->GetStartDet(id);
1076           last = geom->GetLastDet(id);
1077         } else first=last=0;
1078         //printf("first last %d %d\n",first,last);
1079         for(module=first;module<=last;module++) {
1080               this->ResetDigits();
1081               if (all) gAlice->TreeD()->GetEvent(lastentry+module);
1082               else gAlice->TreeD()->GetEvent(lastentry+(module-first));
1083               Int_t ndigits = itsDigits->GetEntriesFast();
1084               if (ndigits) rec->FindRawClusters();
1085               gAlice->TreeR()->Fill(); 
1086               ResetRecPoints();
1087               iTC->Fill();
1088               ResetClusters();
1089               // try and fill only the branch 
1090               //branch->Fill();
1091               //ResetRecPoints(id);
1092         } // loop over modules
1093    } // loop over detector types
1094
1095
1096    Int_t nentries=(Int_t)gAlice->TreeR()->GetEntries();
1097    Int_t ncentries=(Int_t)iTC->GetEntries();
1098    cout << " nentries ncentries " << nentries << ncentries <<  endl;
1099
1100    char hname[30];
1101    sprintf(hname,"TreeR%d",evNumber);
1102    gAlice->TreeR()->Write(hname);
1103    // reset tree
1104    gAlice->TreeR()->Reset();
1105
1106    sprintf(hname,"TreeC%d",evNumber);
1107    iTC->Write(hname);
1108    iTC->Reset();
1109 }
1110
1111
1112 //____________________________________________________________________________
1113 void AliITS::HitsToFastRecPoints(Int_t evNumber,Int_t bgrev,Int_t size,
1114 Option_t *option,Option_t *opt,Text_t *filename)
1115 {
1116     // keep galice.root for signal and name differently the file for 
1117     // background when add! otherwise the track info for signal will be lost !
1118   
1119
1120    // the condition below will disappear when the geom class will be
1121    // initialised for all versions - for the moment it is only for v5 !  
1122    Int_t ver = this->IsVersion(); 
1123    if(ver!=5) return; 
1124
1125    char *all = strstr(opt,"All");
1126    char *det[3] = {strstr(opt,"SPD"),strstr(opt,"SDD"),strstr(opt,"SSD")};
1127
1128    Int_t nmodules;
1129    InitModules(size,nmodules);
1130    FillModules(evNumber,bgrev,nmodules,option,filename);
1131
1132
1133    AliITSsimulation* sim;
1134    AliITSgeom *geom = GetITSgeom();
1135
1136    TRandom *random=new TRandom[9];
1137    random[0].SetSeed(111);
1138    random[1].SetSeed(222);
1139    random[2].SetSeed(333);              
1140    random[3].SetSeed(444);
1141    random[4].SetSeed(555);
1142    random[5].SetSeed(666);              
1143    random[6].SetSeed(777);
1144    random[7].SetSeed(888);
1145    random[8].SetSeed(999);              
1146
1147
1148    Int_t id,module;
1149    for (id=0;id<fgkNTYPES;id++) {
1150         if (!all && !det[id]) continue;
1151         AliITSDetType *iDetType=DetType(id); 
1152         sim = (AliITSsimulation*)iDetType->GetSimulationModel();
1153         if (!sim) {
1154            Error("HitsToFastPoints","The simulation class was not instantiated!");
1155            exit(1);
1156            // or SetDefaultSimulation();
1157         }
1158         Int_t first = geom->GetStartDet(id);
1159         Int_t last = geom->GetLastDet(id);
1160         for(module=first;module<=last;module++) {
1161             AliITSmodule *mod = (AliITSmodule *)fITSmodules->At(module);
1162             sim->CreateFastRecPoints(mod,module,random);
1163             gAlice->TreeR()->Fill(); 
1164             ResetRecPoints();
1165         } // loop over modules
1166    } // loop over detector types
1167
1168
1169    ClearModules();
1170
1171    //Int_t nentries=(Int_t)gAlice->TreeR()->GetEntries();
1172
1173    char hname[30];
1174    sprintf(hname,"TreeR%d",evNumber);
1175    gAlice->TreeR()->Write(hname);
1176    // reset tree
1177    gAlice->TreeR()->Reset();
1178
1179    delete [] random;
1180
1181 }
1182
1183
1184 //____________________________________________________________________________
1185 void AliITS::Streamer(TBuffer &R__b){
1186    // Stream an object of class AliITS.
1187
1188    Int_t i;
1189
1190    if (R__b.IsReading()) {
1191       Version_t R__v = R__b.ReadVersion();
1192       if (R__v) {
1193           AliDetector::Streamer(R__b);
1194           R__b >> fIdN;
1195           R__b.ReadArray(fIdSens); 
1196           if(fIdName!=0) delete[] fIdName;  // Array of TStrings
1197           fIdName    = new TString[fIdN];
1198           for(i=0;i<fIdN;i++) fIdName[i].Streamer(R__b);
1199           R__b >> fITSgeom;
1200           R__b >> fITSmodules;
1201           R__b >> fEuclidOut;
1202           R__b >> fMajorVersion;
1203           R__b >> fMinorVersion;
1204           R__b >> fDetTypes;
1205           R__b >> fDtype;
1206           delete []fNdtype; 
1207           fNdtype = new Int_t[fgkNTYPES];
1208           R__b.ReadFastArray(fNdtype,fgkNTYPES);
1209           R__b >> fCtype;
1210           delete []fNctype; 
1211           fNctype = new Int_t[fgkNTYPES];
1212           R__b.ReadFastArray(fNctype,fgkNTYPES);
1213           R__b >> fRecPoints;
1214           R__b >> fNRecPoints;
1215           R__b >> fTreeC;
1216       } // end if R__v
1217    } else { // writing
1218       R__b.WriteVersion(AliITS::IsA());
1219       AliDetector::Streamer(R__b);
1220       R__b << fIdN;
1221       R__b.WriteArray(fIdSens,fIdN); 
1222       for(i=0;i<fIdN;i++) fIdName[i].Streamer(R__b);
1223       R__b << fITSgeom;
1224       R__b << fITSmodules;
1225       R__b << fEuclidOut;
1226       R__b << fMajorVersion;
1227       R__b << fMinorVersion;
1228       R__b << fDetTypes;
1229       R__b << fDtype;
1230       R__b.WriteFastArray(fNdtype,fgkNTYPES);
1231       R__b << fCtype;
1232       R__b.WriteFastArray(fNctype,fgkNTYPES);
1233       R__b << fRecPoints;
1234       R__b << fNRecPoints;
1235       R__b << fTreeC;
1236    } // end if
1237
1238 }