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