]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSSDigitizer.cxx
f400007c8d29f718261c400557780608c5873848
[u/mrichter/AliRoot.git] / PHOS / AliPHOSSDigitizer.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 // This is a TTask that makes SDigits out of Hits
20 // A Summable Digits is the sum of all hits originating 
21 // from one primary in one active cell
22 // A threshold for assignment of the primary to SDigit is applied 
23 // SDigits are written to TreeS, branch "PHOS"
24 // AliPHOSSDigitizer with all current parameters is written 
25 // to TreeS branch "AliPHOSSDigitizer".
26 // Both branches have the same title. If necessary one can produce 
27 // another set of SDigits with different parameters. Two versions
28 // can be distunguished using titles of the branches.
29 // User case:
30 // root [0] AliPHOSSDigitizer * s = new AliPHOSSDigitizer("galice.root")
31 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
32 // root [1] s->ExecuteTask()
33 //             // Makes SDigitis for all events stored in galice.root
34 // root [2] s->SetPedestalParameter(0.001)
35 //             // One can change parameters of digitization
36 // root [3] s->SetSDigitsBranch("Redestal 0.001")
37 //             // and write them into the new branch
38 // root [4] s->ExeciteTask("deb all tim")
39 //             // available parameters:
40 //             deb - print # of produced SDigitis
41 //             deb all  - print # and list of produced SDigits
42 //             tim - print benchmarking information
43 //
44 //*-- Author :  Dmitri Peressounko (SUBATECH & KI) 
45 //////////////////////////////////////////////////////////////////////////////
46
47
48 // --- ROOT system ---
49 #include "TFile.h"
50 #include "TTask.h"
51 #include "TTree.h"
52 #include "TSystem.h"
53 #include "TROOT.h"
54 #include "TFolder.h"
55 #include "TBenchmark.h"
56 // --- Standard library ---
57 #include <iomanip.h>
58
59 // --- AliRoot header files ---
60 #include "AliRun.h"
61 #include "AliPHOSDigit.h"
62 #include "AliPHOSHit.h"
63 #include "AliPHOSSDigitizer.h"
64
65
66 ClassImp(AliPHOSSDigitizer)
67
68            
69 //____________________________________________________________________________ 
70   AliPHOSSDigitizer::AliPHOSSDigitizer():TTask("AliPHOSSDigitizer","") 
71 {
72   // ctor
73   fA = 0;
74   fB = 10000000. ;
75   fPrimThreshold = 0.01 ;
76   fNevents = 0 ;     
77   fSDigits = 0 ;
78   fHits = 0 ;
79   fIsInitialized = kFALSE ;
80
81 }
82
83 //____________________________________________________________________________ 
84 AliPHOSSDigitizer::AliPHOSSDigitizer(const char* headerFile, const char *sDigitsTitle):TTask("AliPHOSSDigitizer","")
85 {
86   // ctor
87   fA = 0;
88   fB = 10000000.;
89   fPrimThreshold = 0.01 ;
90   fNevents = 0 ;      
91   fSDigitsTitle = sDigitsTitle ;
92   fHeadersFile = headerFile ;
93   fSDigits = new TClonesArray("AliPHOSDigit",1000);
94   fHits    = new TClonesArray("AliPHOSHit",1000);
95
96   TFile * file = (TFile*) gROOT->GetFile(fHeadersFile.Data() ) ;
97   
98   //File was not opened yet
99   if(file == 0){
100     if(fHeadersFile.Contains("rfio"))
101       file =    TFile::Open(fHeadersFile,"update") ;
102     else
103       file = new TFile(fHeadersFile.Data(),"update") ;
104     gAlice = (AliRun *) file->Get("gAlice") ;
105   }
106   
107   //add Task to //root/Tasks folder
108   TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ; 
109   roottasks->Add(this) ; 
110   
111   fIsInitialized = kTRUE ;
112 }
113
114 //____________________________________________________________________________ 
115 AliPHOSSDigitizer::~AliPHOSSDigitizer()
116 {
117   // dtor
118   if(fSDigits)
119     delete fSDigits ;
120   if(fHits)
121     delete fHits ;
122 }
123 //____________________________________________________________________________ 
124 void AliPHOSSDigitizer::Init(){
125   //Initialization can not be done in the default constructor
126
127   if(!fIsInitialized){
128
129     if(fHeadersFile.IsNull())
130       fHeadersFile="galice.root" ;
131
132     TFile * file = (TFile*) gROOT->GetFile(fHeadersFile.Data() ) ;
133     
134     //if file was not opened yet, read gAlice
135     if(file == 0){
136       file = new TFile(fHeadersFile.Data(),"update") ;
137       gAlice = (AliRun *) file->Get("gAlice") ;
138     }
139     
140     fHits    = new TClonesArray("AliPHOSHit",1000);
141     fSDigits = new TClonesArray("AliPHOSDigit",1000);
142     
143     // add Task to //root/Tasks folder
144     TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ; 
145     roottasks->Add(this) ; 
146     
147     fIsInitialized = kTRUE ;
148   }
149 }
150 //____________________________________________________________________________
151 void AliPHOSSDigitizer::Exec(Option_t *option) { 
152   //Collects all hits in the same active volume into digit
153   
154   if(!fIsInitialized)
155     Init() ;
156
157   if(strstr(option,"tim"))
158     gBenchmark->Start("PHOSSDigitizer");
159   
160   fNevents = (Int_t) gAlice->TreeE()->GetEntries() ; 
161   
162   Int_t ievent ;
163   for(ievent = 0; ievent < fNevents; ievent++){
164     gAlice->GetEvent(ievent) ;
165     gAlice->SetEvent(ievent) ;
166     
167     if(gAlice->TreeH()==0){
168       cout << "AliPHOSSDigitizer: There is no Hit Tree" << endl;
169       return ;
170     }
171     
172     //set address of the hits 
173     TBranch * branch = gAlice->TreeH()->GetBranch("PHOS");
174     if (branch) 
175       branch->SetAddress(&fHits);
176     else{
177       cout << "ERROR in AliPHOSSDigitizer: "<< endl ;
178       cout << "      no branch PHOS in TreeH"<< endl ;
179       cout << "      do nothing " << endl ;
180       return ;
181     }
182     
183     fSDigits->Clear();
184     Int_t nSdigits = 0 ;
185     
186     
187     //Now made SDigits from hits, for PHOS it is the same, so just copy    
188     Int_t itrack ;
189     for (itrack=0; itrack<gAlice->GetNtrack(); itrack++){
190       
191       //=========== Get the Hits Tree for the Primary track itrack
192       gAlice->ResetHits();    
193       gAlice->TreeH()->GetEvent(itrack);
194       
195       Int_t i;
196       for ( i = 0 ; i < fHits->GetEntries() ; i++ ) {
197         AliPHOSHit * hit = (AliPHOSHit*)fHits->At(i) ;
198         AliPHOSDigit * newdigit ;
199
200         // Assign primary number only if contribution is significant
201         if( hit->GetEnergy() > fPrimThreshold)
202           newdigit = new AliPHOSDigit( hit->GetPrimary(), hit->GetId(), Digitize( hit->GetEnergy() ) ) ;
203         else
204           newdigit = new AliPHOSDigit( -1               , hit->GetId(), Digitize( hit->GetEnergy() ) ) ;
205         
206         new((*fSDigits)[nSdigits]) AliPHOSDigit(* newdigit) ;
207         nSdigits++ ;  
208         
209         delete newdigit ;    
210       } 
211       
212     } // loop over tracks
213     
214     fSDigits->Sort() ;
215     
216     nSdigits = fSDigits->GetEntriesFast() ;
217     fSDigits->Expand(nSdigits) ;
218     
219     Int_t i ;
220     for (i = 0 ; i < nSdigits ; i++) { 
221       AliPHOSDigit * digit = (AliPHOSDigit *) fSDigits->At(i) ; 
222       digit->SetIndexInList(i) ;     
223     }
224
225     if(gAlice->TreeS() == 0)
226       gAlice->MakeTree("S") ;
227     
228     //check, if this branch already exits?
229     TBranch * sdigitsBranch = 0;
230     TBranch * sdigitizerBranch = 0;
231     
232     TObjArray * branches = gAlice->TreeS()->GetListOfBranches() ;
233     Int_t ibranch;
234     Bool_t phosNotFound = kTRUE ;
235     Bool_t sdigitizerNotFound = kTRUE ;
236     
237     for(ibranch = 0;ibranch <branches->GetEntries();ibranch++){
238       
239       if(phosNotFound){
240         sdigitsBranch=(TBranch *) branches->At(ibranch) ;
241         if( (strcmp("PHOS",sdigitsBranch->GetName())==0 ) &&
242             (fSDigitsTitle.CompareTo(sdigitsBranch->GetTitle()) == 0) )
243           phosNotFound = kFALSE ;
244       }
245       if(sdigitizerNotFound){
246         sdigitizerBranch = (TBranch *) branches->At(ibranch) ;
247         if( (strcmp(sdigitizerBranch->GetName(),"AliPHOSSDigitizer") == 0)&&
248             (fSDigitsTitle.CompareTo(sdigitizerBranch->GetTitle()) == 0) )
249           sdigitizerNotFound = kFALSE ;
250       }
251     }
252
253     if(!(sdigitizerNotFound && phosNotFound)){
254       cout << "AliPHOSSdigitizer error:" << endl ;
255       cout << "Can not overwrite existing branches: do not write" << endl ;
256       return ;
257     }
258     
259     //Make (if necessary) branches    
260     char * file =0;
261     if(gSystem->Getenv("CONFIG_SPLIT_FILE")){ //generating file name
262       file = new char[strlen(gAlice->GetBaseFile())+20] ;
263       sprintf(file,"%s/PHOS.SDigits.root",gAlice->GetBaseFile()) ;
264     }
265     
266     TDirectory *cwd = gDirectory;
267     
268     //First list of sdigits
269     Int_t bufferSize = 32000 ;    
270     sdigitsBranch = gAlice->TreeS()->Branch("PHOS",&fSDigits,bufferSize);
271     sdigitsBranch->SetTitle(fSDigitsTitle.Data());
272     if (file) {
273       sdigitsBranch->SetFile(file);
274       TIter next( sdigitsBranch->GetListOfBranches());
275       while ((sdigitsBranch=(TBranch*)next())) {
276         sdigitsBranch->SetFile(file);
277       }   
278       cwd->cd();
279     } 
280       
281     //second - SDigitizer
282     Int_t splitlevel = 0 ;
283     AliPHOSSDigitizer * sd = this ;
284     sdigitizerBranch = gAlice->TreeS()->Branch("AliPHOSSDigitizer","AliPHOSSDigitizer",
285                                                &sd,bufferSize,splitlevel); 
286     sdigitizerBranch->SetTitle(fSDigitsTitle.Data());
287     if (file) {
288       sdigitizerBranch->SetFile(file);
289       TIter next( sdigitizerBranch->GetListOfBranches());
290       while ((sdigitizerBranch=(TBranch*)next())) {
291         sdigitizerBranch->SetFile(file);
292       }   
293       cwd->cd();
294       delete file;
295     }
296
297     gAlice->TreeS()->Fill() ;
298     gAlice->TreeS()->Write(0,TObject::kOverwrite) ;
299     
300     if(strstr(option,"deb"))
301       PrintSDigits(option) ;
302     
303   }
304   
305   if(strstr(option,"tim")){
306     gBenchmark->Stop("PHOSSDigitizer");
307     cout << "AliPHOSSDigitizer:" << endl ;
308     cout << "   took " << gBenchmark->GetCpuTime("PHOSSDigitizer") << " seconds for SDigitizing " 
309          <<  gBenchmark->GetCpuTime("PHOSSDigitizer")/fNevents << " seconds per event " << endl ;
310     cout << endl ;
311   }
312   
313   
314 }
315 //__________________________________________________________________
316 void AliPHOSSDigitizer::SetSDigitsBranch(const char * title ){
317   //Seting title to branch SDigits 
318   if(!fSDigitsTitle.IsNull())
319     cout << "AliPHOSSdigitizer: changing SDigits file from " <<fSDigitsTitle.Data() << " to " << title << endl ;
320   fSDigitsTitle=title ;
321 }
322 //__________________________________________________________________
323 void AliPHOSSDigitizer::Print(Option_t* option)const{
324   cout << "------------------- "<< GetName() << " -------------" << endl ;
325   cout << "   Writing SDigitis to branch with title  " << fSDigitsTitle.Data() << endl ;
326   cout << "   with digitization parameters  A = " << fA << endl ;
327   cout << "                                 B = " << fB << endl ;
328   cout << "   Threshold for Primary assignment= " << fPrimThreshold << endl ; 
329   cout << "---------------------------------------------------"<<endl ;
330   
331 }
332 //__________________________________________________________________
333 Bool_t AliPHOSSDigitizer::operator==( AliPHOSSDigitizer const &sd )const{
334   if( (fA==sd.fA)&&(fB==sd.fB)&&(fPrimThreshold==sd.fPrimThreshold))
335     return kTRUE ;
336   else
337     return kFALSE ;
338 }
339 //__________________________________________________________________
340 void AliPHOSSDigitizer::PrintSDigits(Option_t * option){
341   //Prints list of digits produced at the current pass of AliPHOSDigitizer
342   
343   cout << "AliPHOSSDigitizer: " << endl ;
344   cout << "       Number of entries in SDigits list  " << fSDigits->GetEntriesFast() << endl ;
345   cout << endl ;
346   
347   if(strstr(option,"all")){// print all digits
348     
349     //loop over digits
350     AliPHOSDigit * digit;
351     cout << "SDigit Id " << " Amplitude " <<  " Index "  <<  " Nprim " << " Primaries list " <<  endl;    
352     Int_t index ;
353     for (index = 0 ; index < fSDigits->GetEntries() ; index++) {
354       digit = (AliPHOSDigit * )  fSDigits->At(index) ;
355       cout << setw(8)  <<  digit->GetId() << " "  <<    setw(3)  <<  digit->GetAmp() <<   "  "  
356            << setw(6)  <<  digit->GetIndexInList() << "  "   
357            << setw(5)  <<  digit->GetNprimary() <<"  ";
358       
359       Int_t iprimary;
360       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
361         cout << setw(5)  <<  digit->GetPrimary(iprimary+1) << "  ";
362       cout << endl;      
363     }
364     
365   }
366 }