]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/UPGRADE/AliITSU.cxx
Moving required CMake version from 2.8.4 to 2.8.8
[u/mrichter/AliRoot.git] / ITS / UPGRADE / AliITSU.cxx
1 /*
2 */
3
4 /**************************************************************************
5  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6  *                                                                        *
7  * Author: The ALICE Off-line Project.                                    *
8  * Contributors are mentioned in the code where appropriate.              *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /* $Id: AliITSU.cxx $ */
20
21
22 ///////////////////////////////////////////////////////////////////////////////
23 //                                                                           //
24 //      An overview of the basic philosophy of the ITS code development      //
25 // and analysis is show in the figure below.                                 //
26 //Begin_Html                                                                 //
27 /*                                               
28 <img src="picts/ITS/ITS_Analysis_schema.gif">
29 </pre>
30 <br clear=left>
31 <font size=+2 color=red>
32 <p>Roberto Barbera is in charge of the ITS Offline code (1999).
33 <a href="mailto:roberto.barbera@ct.infn.it">Roberto Barbera</a>.
34 </font>
35 <pre>
36 */
37 //End_Html
38 //
39 //  AliITSU. Inner Traking System base class.
40 //  This class contains the base procedures for the Inner Tracking System
41 //
42 //Begin_Html
43 /*
44 <img src="picts/ITS/AliITS_Class_Diagram.gif">
45 </pre>
46 <br clear=left>
47 <font size=+2 color=red>
48 <p>This show the class diagram of the different elements that are part of
49 the AliITS class.
50 </font>
51 <pre>
52 */
53 //End_Html
54 //
55 // Version: 0
56 // Written by Rene Brun, Federico Carminati, and Roberto Barbera
57 //
58 // Version: 1
59 // Modified and documented by Bjorn S. Nilsen
60 // July 11 1999
61 //
62 // Version: 2
63 // Modified and documented by A. Bologna
64 // October 18 1999
65 //
66 // AliITSU is the general base class for the ITS. Also see AliDetector for
67 // futher information.
68 //
69 ///////////////////////////////////////////////////////////////////////////////
70
71 #include <stdlib.h>
72 #include <TClonesArray.h>
73 #include <TFile.h>
74 #include <TParticle.h>
75 #include <TString.h>
76 #include <TTree.h>
77 #include <TVirtualMC.h>
78 #include <TMath.h>
79 #include "AliDetector.h"
80 #include "AliITSU.h"
81 #include "AliITSLoader.h"
82 #include "AliITSULoader.h"
83 #include "AliITSUHit.h"
84 #include "AliITSUSDigit.h"
85 #include "AliITSUSimulation.h"
86 #include "AliITSUSimulationPix.h"
87 #include "AliITSsimulationFastPoints.h"
88 #include "AliMC.h"
89 #include "AliITSUDigitizer.h"
90 #include "AliITSRecPoint.h"
91 #include "AliRawReader.h"
92 #include "AliRun.h"
93 #include "AliLog.h"
94 #include "AliITSdigit.h"
95 #include "AliITSUChip.h"
96 #include "AliITSUDigitPix.h"
97 #include "AliITSsegmentation.h"
98 #include "AliITSUSegmentationPix.h"
99 #include "AliITSUSimuParam.h"
100 #include "AliITSFOSignalsSPD.h"
101 #include "AliITSUParamList.h"
102 #include "AliCDBManager.h" // tmp! Later the simuparam should be loaded centrally
103 #include "AliCDBEntry.h"
104
105 using namespace TMath;
106
107 ClassImp(AliITSU)
108
109 //______________________________________________________________________
110 AliITSU::AliITSU() : 
111 AliDetector()
112   ,fEuclidOut(0)
113   ,fNLayers(0)
114   ,fIdSens(0)
115   ,fLayerName(0)
116   ,fTiming(kFALSE)
117   ,fGeomTGeo(0)
118   ,fSimuParam(0)
119   ,fModA(0)
120   ,fpSDigits(0)
121   ,fSDigits(0)
122   ,fDetHits(0)
123   ,fChipHits(0)
124   ,fDetDigits(0)
125   ,fSensMap(0)
126   ,fSimModelLr(0)
127   ,fSegModelLr(0)
128   ,fResponseLr(0)
129   ,fCalibration(0)
130   ,fRunNumber(0)
131   ,fSimInitDone(kFALSE)
132 {
133   // Default initializer for ITS  
134 }
135
136 //______________________________________________________________________
137 AliITSU::AliITSU(const Char_t *title, Int_t nlay) :
138   AliDetector("ITS",title)
139   ,fEuclidOut(0)
140   ,fNLayers(nlay)
141   ,fIdSens(0)
142   ,fLayerName(0)
143   ,fTiming(kFALSE)
144   ,fGeomTGeo(0)
145   ,fSimuParam(0)
146   ,fModA(0)
147   ,fpSDigits(0)
148   ,fSDigits(0)
149   ,fDetHits(0)
150   ,fChipHits(0)
151   ,fDetDigits(0)
152   ,fSensMap(0)
153   ,fSimModelLr(0)
154   ,fSegModelLr(0)
155   ,fResponseLr(0)
156   ,fCalibration(0)
157   ,fRunNumber(0)
158   ,fSimInitDone(kFALSE)
159 {
160   //     The standard Constructor for the ITS class. 
161   AliMC* mc = gAlice->GetMCApp();
162   if( mc && mc->GetHitLists() ) {
163     fHits = new TClonesArray("AliITSUHit",100); // from AliDetector
164     mc->AddHitList(fHits);  
165   }
166 }
167
168
169 //______________________________________________________________________
170 AliITSU::~AliITSU()
171 {
172   // Default destructor for ITS.
173   //  
174   delete fHits;
175   //  delete fSimuParam; // provided by the CDBManager
176   delete fSensMap;
177   if (fSimModelLr) {
178     for (int i=fNLayers;i--;) { // different layers may use the same simulation model
179       for (int j=i;j--;) if (fSimModelLr[j]==fSimModelLr[i]) fSimModelLr[j] = 0;
180       delete fSimModelLr[i];
181     }
182     delete[] fSimModelLr;
183   }
184   if (fSegModelLr) {
185     for (int i=fNLayers;i--;) { // different layers may use the same simulation model     
186       for (int j=i;j--;) if (fSegModelLr[j]==fSegModelLr[i]) fSegModelLr[j] = 0;
187       delete fSegModelLr[i];
188     }
189     delete[] fSegModelLr;
190   }
191   //
192   delete[] fResponseLr; // note: the response data is owned by the CDBManager, we don't delete them
193   //
194   delete[] fLayerName;  // Array of TStrings
195   delete[] fIdSens;
196   //
197   int nmod = fGeomTGeo ? fGeomTGeo->GetNChips() : 0;
198   if (fChipHits) fChipHits->Delete();
199   delete fChipHits;
200   if(fModA){
201     for(Int_t j=0; j<nmod; j++){
202       fModA[j]->Delete();
203       delete fModA[j];
204     }
205     delete[] fModA;
206   }
207   if (fpSDigits) { fpSDigits->Delete(); delete fpSDigits; }
208   if (fSDigits)  { fSDigits->Delete(); delete fSDigits; }
209   delete fGeomTGeo;
210   //
211 }
212
213 //______________________________________________________________________
214 AliDigitizer* AliITSU::CreateDigitizer(AliDigitizationInput* manager) const
215 {
216   // Creates the AliITSDigitizer in a standard way for use via AliChip.
217   // This function can not be included in the .h file because of problems
218   // with the order of inclusion (recursive).
219   // Inputs:
220   //    AliDigitizationInput *manager  The Manger class for Digitization
221   // Output:
222   //    none.
223   // Return:
224   //    A new AliITSRunDigitizer (cast as a AliDigitizer).
225   //
226   return new AliITSUDigitizer(manager);
227 }
228
229 //______________________________________________________________________
230 void AliITSU::Init() 
231 {
232   // Initializer ITS after it has been built
233   //     This routine initializes the AliITS class. It is intended to be
234   // called from the Init function in AliITSv?. Besides displaying a banner
235   // indicating that it has been called it initializes the array fIdSens
236   // and sets the default segmentation, response, digit and raw cluster
237   // classes therefore it should be called after a call to CreateGeometry.
238   //
239   if (!fIdSens) fIdSens = new Int_t[fNLayers];
240   for(int i=0;i<fNLayers;i++) fIdSens[i] = TVirtualMC::GetMC() ? TVirtualMC::GetMC()->VolId(fLayerName[i]) : 0;
241   fGeomTGeo     = new AliITSUGeomTGeo(kTRUE);
242   InitSimulation();
243   //
244 }
245
246 //______________________________________________________________________
247 void AliITSU::MakeBranch(Option_t* option)
248 {
249   // Creates Tree branches for the ITS.
250   // Inputs:
251   //      Option_t *option    String of Tree types S,D, and/or R.
252   //      const char *file    String of the file name where these branches
253   //                          are to be stored. If blank then these branches
254   //                          are written to the same tree as the Hits were
255   //                          read from.
256   // Outputs:
257   //      none.
258   // Return:
259   //      none.
260   
261   Bool_t cH = (strstr(option,"H")!=0);
262   Bool_t cS = (strstr(option,"S")!=0);
263   Bool_t cD = (strstr(option,"D")!=0);
264   
265   if(cH && (fHits == 0x0)) fHits = new TClonesArray("AliITSUHit", 1560);
266   AliDetector::MakeBranch(option);
267   
268   if(cS) MakeBranchS(0);
269   if(cD) MakeBranchD(0);
270   // 
271 }
272
273 //___________________________________________________________________
274 void AliITSU::MakeBranchS(const char* fl) 
275 {
276   // Creates Tree Branch for the ITS summable digits.
277   // Inputs:
278   //      cont char *fl  File name where SDigits branch is to be written
279   //                     to. If blank it write the SDigits to the same
280   //                     file in which the Hits were found.
281   //  
282   Int_t buffersize = 4000;
283   char branchname[31];
284   //
285   // only one branch for SDigits.
286   snprintf(branchname,30,"%s",GetName());
287   if(fLoader->TreeS()) MakeBranchInTree(fLoader->TreeS(),branchname,&fSDigits,buffersize,fl);
288   //
289 }
290
291 //______________________________________________________________________
292 void AliITSU::MakeBranchD(const char* file)
293 {
294   //Make branch for digits
295   MakeBranchInTreeD(fLoader->TreeD(),file);
296 }
297
298 //___________________________________________________________________
299 void AliITSU:: MakeBranchInTreeD(TTree* treeD, const char* file)
300 {
301   // Creates Tree branches for the ITS.
302   //
303   if (!treeD) {AliFatal("No tree provided");}
304   Int_t buffersize = 4000;
305   if (!fDetDigits) InitArrays();
306   //
307   for (Int_t i=0;i<kNChipTypes;i++) {
308     ResetDigits(i);
309     TClonesArray* darr = (TClonesArray*)fDetDigits->At(i);
310     AliDetector::MakeBranchInTree(treeD,Form("%sDigits%s",GetName(),fGeomTGeo->GetChipTypeName(i)),
311                                   &darr,buffersize,file);
312   }
313   //
314 }
315
316 //______________________________________________________________________
317 void AliITSU::InitArrays()
318 {
319   // initialize arrays
320   //
321   if(!fLoader) MakeLoader(AliConfig::GetDefaultEventFolderName());
322   //  
323   fDetDigits = new TObjArray(kNChipTypes);
324   for (Int_t i=0;i<kNChipTypes;i++) fDetDigits->AddAt(new TClonesArray(GetDigitClassName(i),100),i);
325   //
326   fSDigits = new TClonesArray("AliITSUSDigit",100);
327   //
328   fDetHits = new TClonesArray("AliITSUHit",100);
329   //
330   fChipHits = new TObjArray(fGeomTGeo->GetNChips());
331   for (int i=0;i<fGeomTGeo->GetNChips();i++) fChipHits->AddLast( new AliITSUChip(i,fGeomTGeo) );
332   //
333 }
334
335 //______________________________________________________________________
336 void AliITSU::SetTreeAddress()
337 {
338   // Set branch address for the Trees.
339   TTree *treeS = fLoader->TreeS();
340   if (treeS) {
341     TBranch* br = treeS->GetBranch(GetName());
342     if (br) br->SetAddress(&fSDigits);
343   }
344   //
345   TTree *treeD = fLoader->TreeD();
346   if (treeD) {
347     if (!fDetDigits) InitArrays();
348     for (int i=0;i<kNChipTypes;i++) {
349       TString brname = Form("%sDigits%s",GetName(),GetChipTypeName(i));
350       TBranch* br = treeD->GetBranch(brname.Data());
351       if (!br) continue;
352       TClonesArray* darr = (TClonesArray*)fDetDigits->At(i);
353       br->SetAddress(&darr);
354     }
355   }
356   if (fLoader->TreeH() && (fHits == 0x0)) fHits = new TClonesArray("AliITSUHit", 1560);
357   AliDetector::SetTreeAddress();
358   //
359 }
360
361 //______________________________________________________________________
362 void AliITSU::AddHit(Int_t track, Int_t *vol, Float_t *hits)
363 {
364   // Add an ITS hit
365   //     The function to add information to the AliITSUHit class. See the
366   // AliITSUHit class for a full description. This function allocates the
367   // necessary new space for the hit information and passes the variable
368   // track, and the pointers *vol and *hits to the AliITSUHit constructor
369   // function.
370   // Inputs:
371   //      Int_t   track   Track number which produced this hit.
372   //      Int_t   *vol    Array of Integer Hit information. See AliITSUHit.h
373   //      Float_t *hits   Array of Floating Hit information.  see AliITSUHit.h
374   TClonesArray &lhits = *fHits;
375   new(lhits[fNhits++]) AliITSUHit(fIshunt,track,vol,hits);
376   //
377 }
378
379 //______________________________________________________________________
380 void AliITSU::FillChips(Int_t bgrev, Option_t *option, const char *filename) 
381 {
382   // fill the chips with the sorted by chip hits; add hits from
383   // background if option=Add.
384   //
385   static TTree *trH1=0;                 //Tree with background hits
386   static Bool_t first=kTRUE;
387   static TFile *file = 0;
388   const char *addBgr = strstr(option,"Add");
389   //
390   if (addBgr ) {
391     if(first) {
392       file = new TFile(filename);    
393       first=kFALSE;
394     }
395     file->cd();
396     file->ls();
397     // Get Hits Tree header from file
398     if (trH1) {delete trH1; trH1=0;}
399     //
400     char treeName[21];
401     snprintf(treeName,20,"TreeH%d",bgrev);
402     trH1 = (TTree*)gDirectory->Get(treeName);
403     if (!trH1) Error("FillChips","cannot find Hits Tree for event:%d",bgrev);
404     // Set branch addresses
405   } // end if addBgr
406   
407   FillChips(fLoader->TreeH(),0); // fill from this file's tree.
408   //
409   if (addBgr ) {
410     FillChips(trH1,10000000); // Default mask 10M.
411     TTree *fAli=fLoader->GetRunLoader()->TreeK();
412     TFile *fileAli=0;
413     if (fAli) {
414       fileAli = fAli->GetCurrentFile();
415       fileAli->cd();
416     }
417   } // end if add
418   //  
419 }
420
421 //______________________________________________________________________
422 void AliITSU::FillChips(TTree *treeH, Int_t /*mask*/)
423 {
424   // fill the chips with the sorted by chip hits; 
425   // can be called many times to do a merging
426   // Inputs:
427   //      TTree *treeH  The tree containing the hits to be copied into
428   //                    the chips.
429   //      Int_t mask    The track number mask to indecate which file
430   //                    this hits came from.
431   //  
432   if (treeH == 0x0) { AliError("Tree H  is NULL"); return; }
433   //
434   Int_t lay,sta,ssta,mod,chip,index;
435   AliITSUHit *itsHit=0;
436   char branchname[21];
437   snprintf(branchname,20,"%s",GetName());
438   TBranch *branch = treeH->GetBranch(branchname);
439   if (!branch) {Error("FillChips","%s branch in TreeH not found",branchname); return;} // end if !branch
440   //
441   branch->SetAddress(&fHits);
442   Int_t nTracks =(Int_t) treeH->GetEntries();
443   Int_t iPrimTrack,h;
444   for (iPrimTrack=0; iPrimTrack<nTracks; iPrimTrack++) {
445     ResetHits();
446     Int_t nBytes = treeH->GetEvent(iPrimTrack);
447     if (nBytes <= 0) continue;
448     Int_t nHits = fHits->GetEntriesFast();
449     for (h=0; h<nHits; h++){
450       itsHit = (AliITSUHit *)fHits->UncheckedAt(h);
451       itsHit->GetChipID(lay,sta,ssta,mod,chip);
452       index = fGeomTGeo->GetChipIndex(lay,sta,ssta,mod,chip); // !!! AliITSHit counts indices from 1!
453       itsHit = new( (*fDetHits)[fDetHits->GetEntriesFast()] ) AliITSUHit(*itsHit);
454       itsHit->SetUniqueID(h);
455       GetChip(index)->AddHit(itsHit);
456       // do we need to add a mask?
457       // itsHit->SetTrack(itsHit->GetTrack()+mask);
458     } // end loop over hits 
459   } // end loop over tracks
460 }
461
462 //______________________________________________________________________
463 void AliITSU::ClearChips()
464 {
465   // clear accumulated hits
466   if (!fChipHits || !fDetHits) AliFatal("Hits accumulation arrays are not defined");
467   for (int i=fGeomTGeo->GetNChips();i--;) GetChip(i)->Clear();
468   fDetHits->Clear();
469 }
470
471 //______________________________________________________________________
472 void AliITSU::Hits2SDigits()
473 {
474   // Standard Hits to summable Digits function.
475   if (!IsSimInitDone()) InitSimulation();
476   fLoader->LoadHits("read");
477   fLoader->LoadSDigits("recreate");
478   AliRunLoader* rl = fLoader->GetRunLoader(); 
479   //
480   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
481     rl->GetEvent(iEvent);
482     if (!fLoader->TreeS()) fLoader->MakeTree("S");
483     MakeBranch("S");
484     SetTreeAddress();
485     Hits2SDigits(iEvent,0," "," ");
486   } // end for iEvent
487     //
488   fLoader->UnloadHits();
489   fLoader->UnloadSDigits();
490   // 
491 }
492
493 //______________________________________________________________________
494 void AliITSU::Hits2SDigits(Int_t evNumber,Int_t bgrev,Option_t *option,const char *filename)
495 {
496   // Keep galice.root for signal and name differently the file for 
497   // background when add! otherwise the track info for signal will be lost !
498   // Inputs:
499   //      Int_t evnt       Event to be processed.
500   //      Int_t bgrev      Background Hit tree number.
501   //      Int_t nchips   Not used.
502   //      Option_t *option String indicating if merging hits or not. To
503   //                       merge hits set equal to "Add". Otherwise no
504   //                       background hits are considered.
505   //      Test_t *filename File name containing the background hits..
506   //
507   if (!IsSimInitDone()) InitSimulation();
508   FillChips(bgrev,option,filename);
509   //
510   Int_t nchips = fGeomTGeo->GetNChips();
511   int prevLr = -1;
512   float roPhase=0; // synchronysation type between layers/chips
513   Bool_t randomyzeChips = kFALSE; // do we need to randomize layers
514   //
515   for(int chip=0;chip<nchips;chip++) {
516     int lr = fGeomTGeo->GetLayer(chip);
517     AliITSUSimulation* sim = GetSimulationModel(lr);
518     sim->InitSimulationChip(GetChip(chip),evNumber/*,gAlice->GetEvNumber()*/,GetSegmentation(lr),GetResponseParam(lr));
519     //
520     if (prevLr!=lr) { // new layer started)
521       roPhase = fSimuParam->GetLrROCycleShift(lr);
522       if (Abs(roPhase)<1.) roPhase = roPhase*sim->GetReadOutCycleLength(); // chips synchronized within layer with this offset
523       else                randomyzeChips = kTRUE;                     // chips have random offset
524     }
525     if (randomyzeChips) sim->GenerateReadOutCycleOffset();
526     else                  sim->SetReadOutCycleOffset(roPhase);
527     //
528     sim->SDigitiseChip();
529     fLoader->TreeS()->Fill();      // fills all branches - wasted disk space
530     ResetSDigits();
531     prevLr = lr;
532   } 
533   //
534   ClearChips();
535   //
536   fLoader->TreeS()->GetEntries();
537   fLoader->TreeS()->AutoSave();
538   fLoader->WriteSDigits("OVERWRITE");
539   fLoader->TreeS()->Reset();
540 }
541
542 //______________________________________________________________________
543 void AliITSU::Hits2Digits()
544 {
545   // Standard Hits to Digits function.
546   if (!IsSimInitDone()) InitSimulation();
547   fLoader->LoadHits("read");
548   fLoader->LoadDigits("recreate");
549   AliRunLoader* rl = fLoader->GetRunLoader(); 
550   //
551   for (Int_t iEvent = 0; iEvent < rl->GetNumberOfEvents(); iEvent++) {
552     rl->GetEvent(iEvent);
553     if (!fLoader->TreeD()) fLoader->MakeTree("D");
554     MakeBranch("D");
555     SetTreeAddress();
556     Hits2Digits(iEvent,0," "," ");
557   } // end for iEvent
558     //
559   fLoader->UnloadHits();
560   fLoader->UnloadSDigits();
561   // 
562 }
563
564 //______________________________________________________________________
565 void AliITSU::Hits2Digits(Int_t evNumber,Int_t bgrev,Option_t *option,const char *filename)
566 {
567   //   Keep galice.root for signal and name differently the file for 
568   // background when add! otherwise the track info for signal will be lost !
569   // Inputs:
570   //      Int_t evnt       Event to be processed.
571   //      Int_t bgrev      Background Hit tree number.
572   //      Option_t *option String indicating if merging hits or not. To
573   //                       merge hits set equal to "Add". Otherwise no
574   //                       background hits are considered.
575   //      Test_t *filename File name containing the background hits..
576   // Outputs:
577   //  
578   if (!IsSimInitDone()) InitSimulation();
579   FillChips(bgrev,option,filename);
580   // 
581   Int_t nchips = fGeomTGeo->GetNChips();
582   int prevLr = -1;
583   float roPhase=0; // synchronysation type between layers/chips
584   Bool_t randomyzeChips = kFALSE; // do we need to randomize layers
585   //
586   for (Int_t chip=0;chip<nchips;chip++) {
587     int lr = fGeomTGeo->GetLayer(chip);
588     AliITSUSimulation* sim = GetSimulationModel(lr);
589     //
590     sim->InitSimulationChip(GetChip(chip),evNumber/*gAlice->GetEvNumber()*/,GetSegmentation(lr),GetResponseParam(lr));
591     if (prevLr!=lr) { // new layer started)
592       roPhase = fSimuParam->GetLrROCycleShift(lr);
593       if (Abs(roPhase)<1.) roPhase = roPhase*sim->GenerateReadOutCycleOffset(); // chips synchronized within layer with this offset
594       else                randomyzeChips = kTRUE;                     // chips have random offset
595     }
596     if (randomyzeChips) sim->GenerateReadOutCycleOffset();
597     else                  sim->SetReadOutCycleOffset(roPhase);
598     sim->DigitiseChip();
599     // fills all branches - wasted disk space
600     fLoader->TreeD()->Fill(); 
601     ResetDigits();
602     prevLr = lr;
603   } // end for chip
604   //
605   ClearChips();
606   //
607   //    WriteFOSignals(); // Add Fast-OR signals to event (only one object per event)
608   fLoader->TreeD()->GetEntries();
609   fLoader->TreeD()->AutoSave();
610   fLoader->TreeD()->Reset(); 
611   //
612 }
613
614 //_____________________________________________________________________
615 void AliITSU::Hits2FastRecPoints(Int_t bgrev,Option_t *opt,const char *flnm)
616 {
617   // keep galice.root for signal and name differently the file for 
618   // background when add! otherwise the track info for signal will be lost !
619   // Inputs:
620   //      Int_t evnt       Event to be processed.
621   //      Int_t bgrev      Background Hit tree number.
622   //      Option_t *opt    Option passed to FillChips. See FillChips.
623   //      Test_t *flnm     File name containing the background hits..
624   // Outputs:
625   //      none.
626   // Return:
627   //      none.
628   if (!IsSimInitDone()) InitSimulation();
629   AliITSULoader *pITSloader = (AliITSULoader*)fLoader;
630   Int_t nchips = fGeomTGeo->GetNChips();
631   FillChips(bgrev,opt,flnm);
632   //
633   TTree *lTR = pITSloader->TreeR();
634   if(!lTR) {
635     pITSloader->MakeTree("R");
636     lTR = pITSloader->TreeR();
637   }
638   //
639   TClonesArray* ptarray = new TClonesArray("AliITSRecPoint",1000);
640   TBranch* branch = (TBranch*)lTR->Branch("ITSRecPointsF",&ptarray);
641   branch->SetAddress(&ptarray);
642   for (int chip=0;chip<nchips;chip++){
643     int id = fGeomTGeo->GetChipChipTypeID(chip);
644     AliITSUSimulation* sim = GetSimulationModel(id);
645     if (!sim) AliFatal(Form("The sim.class for chip %d of ChipTypeID %d is missing",chip,id));
646     sim->CreateFastRecPoints( GetChip(chip) ,chip,gRandom,ptarray);
647     lTR->Fill();
648     ptarray->Clear();
649   } // end for chip
650   //
651   ClearChips();
652   fLoader->WriteRecPoints("OVERWRITE");
653   delete ptarray;
654 }
655
656 //_____________________________________________________________________
657 Int_t AliITSU::Hits2Clusters(TTree */*hTree*/, TTree */*cTree*/)
658 {
659   /* RS: TODO
660   // This function creates ITS clusters
661   if (!IsSimInitDone()) InitSimulation();
662   Int_t mmax = 0;
663   FillChips(hTree,0);
664   //
665   TClonesArray *points = new TClonesArray("AliITSRecPoint",1000);
666   TBranch *branch=cTree->GetBranch("ITSRecPoints");
667   if (!branch) cTree->Branch("ITSRecPoints",&points);
668   else branch->SetAddress(&points);
669   //
670   AliITSsimulationFastPoints sim;
671   Int_t ncl=0;
672   for (Int_t m=0; m<mmax; m++) {
673     sim.CreateFastRecPoints(GetChip(m),m,gRandom,points);      
674     ncl+=points->GetEntriesFast();
675     cTree->Fill();
676     points->Clear();
677   }
678   //
679   ClearChips();
680   //
681   AliDebug(1,Form("Number of found fast clusters : %d",ncl));
682   //cTree->Write();
683   delete points;
684   */
685   return 0;
686 }
687
688 //_____________________________________________________________________
689 void AliITSU::CheckLabels(Int_t lab[3]) const //RSDONE
690 {
691   // Tries to find mother's labels
692   //
693   if(lab[0]<0 && lab[1]<0 && lab[2]<0) return; // In case of no labels just exit
694   //
695   Int_t ntracks = gAlice->GetMCApp()->GetNtrack();
696   for (Int_t i=0;i<3;i++){
697     Int_t label = lab[i];
698     if (label>=0 && label<ntracks) {
699       TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
700       if (part->P() < 0.005) {
701         Int_t m=part->GetFirstMother();
702         if (m<0) continue;
703         if (part->GetStatusCode()>0) continue;
704         lab[i]=m;       
705       }
706     }    
707   }
708   //
709 }
710
711 //______________________________________________________________________
712 void AliITSU::ResetDigits() //RSDONE?
713 {
714   // Reset number of digits and the digits array for the ITS detector.
715   if (fDetDigits) for (int i=kNChipTypes;i--;) ResetDigits(i);
716   //
717 }
718
719 //______________________________________________________________________
720 void AliITSU::ResetDigits(Int_t branch)
721 {
722   // Reset number of digits and the digits array for this branch.
723   if (fDetDigits) ((TClonesArray*)fDetDigits->At(branch))->Clear();
724   //
725 }
726
727 //______________________________________________________________________
728 void AliITSU::AddSumDigit(AliITSUSDigit &sdig)
729 {
730   // Adds the chip summable digits to the summable digits tree.
731   new( (*fSDigits)[fSDigits->GetEntriesFast()]) AliITSUSDigit(sdig);
732   //  
733 }
734
735 //______________________________________________________________________
736 void AliITSU::AddSimDigit(Int_t branch, AliITSdigit *d)
737 {
738   //    Add a simulated digit.
739   // Inputs:
740   //      Int_t id        Detector type number.
741   //      AliITSdigit *d  Digit to be added to the Digits Tree. See 
742   //                      AliITSdigit.h
743   TClonesArray &ldigits = *((TClonesArray*)fDetDigits->At(branch));
744   int nd = ldigits.GetEntriesFast();
745   switch(branch){
746   case AliITSUGeomTGeo::kChipTypePix:
747     new(ldigits[nd]) AliITSUDigitPix(*((AliITSUDigitPix*)d));
748     break;
749   default:
750     AliFatal(Form("Unknown digits branch %d",branch));
751   }
752 }
753
754 //______________________________________________________________________
755 void AliITSU::AddSimDigit(Int_t branch,Float_t /*phys*/,Int_t *digits,Int_t *tracks,
756                           Int_t *hits,Float_t */*charges*/, Int_t /*sigexpanded*/)
757 {
758   // Add a simulated digit to the list.
759   // Inputs:
760   //      Int_t id        Detector type number.
761   //      Float_t phys    Physics indicator. See AliITSdigits.h
762   //      Int_t *digits   Integer array containing the digits info. See 
763   //                      AliITSdigit.h
764   //      Int_t *tracks   Integer array [AliITSdigitS?D::GetNTracks()] 
765   //                      containing the track numbers that contributed to
766   //                      this digit.
767   //      Int_t *hits     Integer array [AliITSdigitS?D::GetNTracks()]
768   //                      containing the hit numbers, from AliITSchip, that
769   //                      contributed to this digit.
770   //      Float_t *charge Floating point array of the signals contributed
771   //                      to this digit by each track.
772   TClonesArray &ldigits = *((TClonesArray*)fDetDigits->At(branch));
773   int nd = ldigits.GetEntriesFast();
774   switch(branch){
775   case AliITSUGeomTGeo::kChipTypePix:
776     new(ldigits[nd]) AliITSUDigitPix(digits,tracks,hits);
777     break;
778   default:
779     AliFatal(Form("Unknown digits branch %d",branch));
780   }  
781   //
782 }
783
784 //______________________________________________________________________
785 void AliITSU::Digits2Raw()
786 {
787   AliError("Not ready");
788 }
789
790 //______________________________________________________________________
791 AliLoader* AliITSU::MakeLoader(const char* topfoldername)
792
793   //builds ITSgetter (AliLoader type)
794   //if detector wants to use castomized getter, it must overload this method
795   
796   AliDebug(1,Form("Creating AliITSULoader. Top folder is %s.",topfoldername));
797   fLoader = new AliITSULoader(GetName(),topfoldername);
798   return fLoader;
799 }
800
801 //______________________________________________________________________
802 Bool_t AliITSU::Raw2SDigits(AliRawReader* /*rawReader*/)
803 {
804   AliError("Not ready");
805   return kFALSE;
806 }
807
808 //______________________________________________________________________
809 /*
810 AliTriggerDetector* AliITSU::CreateTriggerDetector() const 
811 {
812   // create an AliITSTrigger object (and set trigger conditions as input)
813   return new AliITSTrigger(fChipTypeSim->GetTriggerConditions());
814 }
815 */
816
817 //______________________________________________________________________
818 void AliITSU::WriteFOSignals()
819 {
820   // This method write FO signals in Digits tree both in Hits2Digits
821   // or SDigits2Digits
822   AliError("Not ready");
823   //  fChipTypeSim->ProcessNoiseForFastOr();
824 }
825
826 //_______________________________________________________________________
827 void AliITSU::SDigits2Digits()
828 {
829   // Standard Summable digits to Digits function.
830   //
831   if (!IsSimInitDone()) InitSimulation();
832   TTree* trees = fLoader->TreeS();
833   if( !(trees && fSDigits) ) AliFatal("Error: No trees or SDigits.");
834   TBranch* brchSDigits = trees->GetBranch(GetName());
835   //
836   int nchips = fGeomTGeo->GetNChips();
837   int prevLr = -1;
838   float roPhase=0; // synchronysation type between layers/chips
839   Bool_t randomyzeChips = kFALSE; // do we need to randomize layers
840   //
841   for (int chip=0;chip<nchips;chip++) {
842     int lr = fGeomTGeo->GetLayer(chip);
843     AliITSUSimulation* sim = GetSimulationModel(lr);
844     sim->InitSimulationChip(GetChip(chip),gAlice->GetEvNumber(),GetSegmentation(lr),GetResponseParam(lr));
845     //
846     if (prevLr!=lr) { // new layer started)
847       roPhase = fSimuParam->GetLrROCycleShift(lr);
848       if (Abs(roPhase)<1.) roPhase = roPhase*sim->GenerateReadOutCycleOffset(); // chips synchronized within layer with this offset
849       else                randomyzeChips = kTRUE;                     // chips have random offset
850     }
851     if (randomyzeChips) sim->GenerateReadOutCycleOffset();
852     else                  sim->SetReadOutCycleOffset(roPhase);
853     //
854     fSDigits->Clear();
855     brchSDigits->GetEvent(chip);
856     sim->AddSDigitsToChip(fSDigits,0);
857     sim->FinishSDigitiseChip();
858     fLoader->TreeD()->Fill();
859     ResetDigits();
860     prevLr = lr;
861   }
862   //  WriteFOSignals(); 
863   fLoader->TreeD()->GetEntries();
864   fLoader->TreeD()->AutoSave();
865   fLoader->TreeD()->Reset();
866 }
867
868 //_______________________________________________________________________
869 void AliITSU::InitSimulation()
870 {
871   // Initialize arrays, segmentations ets, needed for simulation
872   // Equivalent of old AliITSChipTypeSim construction
873   //
874   if (fSimInitDone) {AliInfo("Already done"); return;}
875   //
876   AliCDBEntry* cdbEnt = AliCDBManager::Instance()->Get("ITS/Calib/SimuParam"); // tmp: load it centrally
877   if (!cdbEnt) {AliFatal("Failed to find ITS/Calib/SimuParam on CDB"); exit(1);}
878   fSimuParam    = (AliITSUSimuParam*)cdbEnt->GetObject();
879   //
880   fSensMap      = new AliITSUSensMap("AliITSUSDigit",0,0);
881   fSimModelLr   = new AliITSUSimulation*[fNLayers];
882   fSegModelLr   = new AliITSsegmentation*[fNLayers];
883   fResponseLr   = new AliITSUParamList*[fNLayers];
884   //
885   TObjArray arrSeg;
886   AliITSUSegmentationPix::LoadSegmentations(&arrSeg, AliITSUGeomTGeo::GetITSsegmentationFileName());
887   //
888   // add known simulation types used in the setup
889   for (int i=fNLayers;i--;) {
890     fSimModelLr[i] = 0;
891     fSegModelLr[i] = 0;
892     fResponseLr[i] = 0;
893     int dType = fGeomTGeo->GetLayerChipTypeID(i);           // fine detector type: class + segmentation
894     int sType = dType/AliITSUGeomTGeo::kMaxSegmPerChipType; // detector simulation class
895     //
896     // check if the simulation of this sType was already created for preceeding layers
897     AliITSUSimulation* simUpg = 0;
898     for (int j=fNLayers-1;j>i;j--) {
899       simUpg = GetSimulationModel(j);
900       if (simUpg && int(simUpg->GetUniqueID())==sType) break;
901       else simUpg = 0;
902     }
903     //
904     if (!simUpg) { // need to create simulation for detector class sType
905       switch (sType) 
906         {
907         case AliITSUGeomTGeo::kChipTypePix : 
908           simUpg = new AliITSUSimulationPix(fSimuParam,fSensMap); 
909           break;
910         default: AliFatal(Form("No %d detector type is defined",sType));
911         }
912     }
913     fSimModelLr[i] = simUpg;
914     //
915     // add segmentations used in the setup
916     if (!(fSegModelLr[i]=(AliITSsegmentation*)arrSeg[dType])) {AliFatal(Form("Segmentation for ChipType#%d is not found",dType)); exit(1);}
917     //
918     // add response function for the detectors of this layer
919     if ( !(fResponseLr[i]=(AliITSUParamList*)fSimuParam->FindRespFunParams(dType)) ) {AliFatal(Form("Response for ChipType#%d is not found in SimuParams",dType)); exit(1);}
920   }
921   // delete non needed segmentations
922   for (int i=fNLayers;i--;) arrSeg.Remove(fSegModelLr[i]);
923   arrSeg.Delete();
924   //
925   InitArrays();
926   //
927   fSimInitDone = kTRUE;
928   //
929 }