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