]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSSDigitizer.cxx
8616449e2e356a37ade42ed5b55e882c801b5e41
[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 #include "TGeometry.h"
61
62 // --- Standard library ---
63 #include <iomanip.h>
64
65 // --- AliRoot header files ---
66 #include "AliRun.h"
67 #include "AliHeader.h"
68 #include "AliPHOSDigit.h"
69 #include "AliPHOSGeometry.h"
70 #include "AliPHOSGetter.h"
71 #include "AliPHOSHit.h"
72 #include "AliPHOSSDigitizer.h"
73
74
75 ClassImp(AliPHOSSDigitizer)
76
77            
78 //____________________________________________________________________________ 
79   AliPHOSSDigitizer::AliPHOSSDigitizer():TTask("","") 
80 {
81   // ctor
82   fA             = 0;
83   fB             = 10000000.;
84   fPrimThreshold = 0.01 ;
85   fSDigitsInRun  = 0 ; 
86 }
87
88 //____________________________________________________________________________ 
89 AliPHOSSDigitizer::AliPHOSSDigitizer(const char * headerFile, const char * sDigitsTitle):TTask(sDigitsTitle, headerFile)
90 {
91   // ctor
92   fA             = 0;
93   fB             = 10000000.;
94   fPrimThreshold = 0.01 ;
95   fSDigitsInRun  = 0 ;
96   Init();
97 }
98
99
100 //____________________________________________________________________________ 
101 void AliPHOSSDigitizer::Init()
102 {
103   // Initialization: open root-file, allocate arrays for hits and sdigits,
104   // attach task SDigitizer to the list of PHOS tasks
105   // 
106   // Initialization can not be done in the default constructor
107   //============================================================= YS
108   //  The initialisation is now done by AliPHOSGetter
109   
110   if( strcmp(GetTitle(), "") == 0 )
111     SetTitle("galice.root") ;
112   
113   AliPHOSGetter * gime = AliPHOSGetter::GetInstance(GetTitle(), GetName()) ;     
114   if ( gime == 0 ) {
115     cerr << "ERROR: AliPHOSSDigitizer::Init -> Could not obtain the Getter object !" << endl ; 
116     return ;
117   } 
118   
119   gime->PostSDigits( GetName(), GetTitle() ) ; 
120
121   TString sdname(GetName() );
122   sdname.Append(":") ;
123   sdname.Append(GetTitle() ) ;
124   SetName(sdname) ;
125   gime->PostSDigitizer(this) ;
126 }
127
128 //____________________________________________________________________________
129 void AliPHOSSDigitizer::Exec(Option_t *option) 
130
131   // Collects all hits in the same active volume into digit
132   if( strcmp(GetName(), "") == 0 )
133     Init() ;
134   
135   if (strstr(option, "print") ) {
136     Print("") ; 
137     return ; 
138   }
139
140   if(strstr(option,"tim"))
141     gBenchmark->Start("PHOSSDigitizer");
142
143   //Check, if this branch already exits
144   gAlice->GetEvent(0) ;
145
146   TString sdname(GetName()) ;
147   sdname.Remove(sdname.Index(GetTitle())-1) ;
148   
149   if(gAlice->TreeS() ) {
150     TObjArray * lob = static_cast<TObjArray*>(gAlice->TreeS()->GetListOfBranches()) ;
151     TIter next(lob) ; 
152     TBranch * branch = 0 ;  
153     Bool_t phosfound = kFALSE, sdigitizerfound = kFALSE ; 
154     
155     while ( (branch = (static_cast<TBranch*>(next()))) && (!phosfound || !sdigitizerfound) ) {
156       if ( (strcmp(branch->GetName(), "PHOS")==0) && (strcmp(branch->GetTitle(), GetName())==0) ) 
157         phosfound = kTRUE ;
158       
159       else if ( (strcmp(branch->GetName(), "AliPHOSSDigitizer")==0) && (strcmp(branch->GetTitle(), GetName())==0) ) 
160         sdigitizerfound = kTRUE ; 
161     }
162     
163     if ( phosfound || sdigitizerfound ) {
164       cerr << "WARNING: AliPHOSSDigitizer::Exec -> SDigits and/or SDigitizer branch with name " << GetName() 
165            << " already exits" << endl ;
166       return ; 
167     }   
168   }  
169
170   AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ; 
171
172   Int_t nevents = (Int_t) gAlice->TreeE()->GetEntries() ; 
173   
174   Int_t ievent ;
175   for(ievent = 0; ievent < nevents; ievent++){
176     gime->Event(ievent,"H") ;
177     const TClonesArray * hits = gime->Hits() ;
178     TClonesArray * sdigits = gime->SDigits(sdname.Data()) ;
179     sdigits->Clear();
180     Int_t nSdigits = 0 ;
181     
182     
183     //Now make SDigits from hits, for PHOS it is the same, so just copy    
184
185 //******************** CHECK HERE
186 //YS DOES NOT UNDERSTAND THE NEED FOR THE FOLLOWING LOOP
187 //     Int_t itrack ;
188 //     for (itrack=0; itrack < gAlice->GetNtrack(); itrack++){
189 //       //=========== Get the PHOS branch from Hits Tree for the Primary track itrack
190 //       gime->Track(itrack) ;
191       Int_t i;
192       for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
193         AliPHOSHit * hit = dynamic_cast<AliPHOSHit *>(hits->At(i)) ;
194         // Assign primary number only if contribution is significant
195         
196         if( hit->GetEnergy() > fPrimThreshold)
197           new((*sdigits)[nSdigits]) AliPHOSDigit(hit->GetPrimary(),hit->GetId(),
198                                                   Digitize(hit->GetEnergy()), hit->GetTime()) ;
199         else
200           new((*sdigits)[nSdigits]) AliPHOSDigit( -1              , hit->GetId(), 
201                                                    Digitize(hit->GetEnergy()), hit->GetTime()) ;
202         nSdigits++ ;    
203         
204       }
205 //    } // loop over tracks
206     
207     sdigits->Sort() ;
208     
209     nSdigits = sdigits->GetEntriesFast() ;
210     fSDigitsInRun += nSdigits ;  
211     sdigits->Expand(nSdigits) ;
212 //    Int_t i ;
213     for (i = 0 ; i < nSdigits ; i++) { 
214       AliPHOSDigit * digit = dynamic_cast<AliPHOSDigit *>(sdigits->At(i)) ; 
215       digit->SetIndexInList(i) ;     
216     }
217     
218     if(gAlice->TreeS() == 0)
219         gAlice->MakeTree("S") ;
220         
221     //First list of sdigits
222     Int_t bufferSize = 32000 ;    
223     TBranch * sdigitsBranch = gAlice->TreeS()->Branch("PHOS",&sdigits,bufferSize);
224     sdigitsBranch->SetTitle(sdname);
225       
226     //Next - SDigitizer
227     Int_t splitlevel = 0 ;
228     AliPHOSSDigitizer * sd = this ;
229     TBranch * sdigitizerBranch = gAlice->TreeS()->Branch("AliPHOSSDigitizer","AliPHOSSDigitizer",
230                                                &sd,bufferSize,splitlevel); 
231     sdigitizerBranch->SetTitle(sdname);
232     sdigitsBranch->Fill() ; 
233
234     gAlice->TreeS()->AutoSave() ;
235     
236     if(strstr(option,"deb"))
237       PrintSDigits(option) ;
238     
239   }
240   
241   if(strstr(option,"tim")){
242     gBenchmark->Stop("PHOSSDigitizer");
243     cout << "AliPHOSSDigitizer:" << endl ;
244     cout << "   took " << gBenchmark->GetCpuTime("PHOSSDigitizer") << " seconds for SDigitizing " 
245          <<  gBenchmark->GetCpuTime("PHOSSDigitizer")/nevents << " seconds per event " << endl ;
246     cout << endl ;
247   }
248   
249   
250 }
251
252 //__________________________________________________________________
253 void AliPHOSSDigitizer::SetSDigitsBranch(const char * title )
254 {
255   // Setting title to branch SDigits 
256
257   TString stitle(title) ;
258
259   // check if branch with title already exists
260   TBranch * sdigitsBranch    = 
261     static_cast<TBranch*>(gAlice->TreeS()->GetListOfBranches()->FindObject("PHOS")) ; 
262   TBranch * sdigitizerBranch =  
263     static_cast<TBranch*>(gAlice->TreeS()->GetListOfBranches()->FindObject("AliPHOSSDigitizer")) ;
264   const char * sdigitsTitle    = sdigitsBranch ->GetTitle() ;  
265   const char * sdigitizerTitle = sdigitizerBranch ->GetTitle() ;
266   if ( stitle.CompareTo(sdigitsTitle)==0 || stitle.CompareTo(sdigitizerTitle)==0 ){
267     cerr << "ERROR: AliPHOSSdigitizer::SetSDigitsBranch -> Cannot overwrite existing branch with title " << title << endl ;
268     return ;
269   }
270   
271   cout << "AliPHOSSdigitizer::SetSDigitsBranch -> Changing SDigits file from " << GetName() << " to " << title << endl ;
272
273   SetName(title) ; 
274     
275   // Post to the WhiteBoard
276   AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ; 
277   gime->PostSDigits( title, GetTitle()) ; 
278 }
279
280 //__________________________________________________________________
281 void AliPHOSSDigitizer::SetSplitFile(const TString splitFileName) const
282 {
283   // Diverts the SDigits in a file separate from the hits file
284   
285   TDirectory * cwd = gDirectory ;
286   TFile * splitFile = gAlice->InitTreeFile("S",splitFileName.Data());
287   splitFile->cd() ; 
288   gAlice->Write();
289   
290   TTree *treeE  = gAlice->TreeE();
291   if (!treeE) {
292     cerr<<"No TreeE found "<<endl;
293     abort() ;
294   }      
295   
296   // copy TreeE
297   AliHeader *header = new AliHeader();
298   treeE->SetBranchAddress("Header", &header);
299   treeE->SetBranchStatus("*",1);
300   TTree *treeENew =  treeE->CloneTree();
301   treeENew->Write();
302   
303   // copy AliceGeom
304   TGeometry *AliceGeom = static_cast<TGeometry*>(cwd->Get("AliceGeom"));
305   if (!AliceGeom) {
306     cerr<<"AliceGeom was not found in the input file "<<endl;
307     abort() ;
308   }
309   AliceGeom->Write();
310   cwd->cd() ; 
311   gAlice->MakeTree("S",splitFile);
312   cout << "INFO: AliPHOSSDigitizer::SetSPlitMode -> SDigits will be stored in " << splitFileName.Data() << endl ; 
313 }
314
315 //__________________________________________________________________
316 void AliPHOSSDigitizer::Print(Option_t* option)const
317 {
318   // Prints parameters of SDigitizer
319   cout << "------------------- "<< GetName() << " -------------" << endl ;
320   cout << "   Writing SDigits to branch with title  " << GetName() << endl ;
321   cout << "   with digitization parameters  A = " << fA << endl ;
322   cout << "                                 B = " << fB << endl ;
323   cout << "   Threshold for Primary assignment= " << fPrimThreshold << endl ; 
324   cout << "---------------------------------------------------"<<endl ;
325   
326 }
327 //__________________________________________________________________
328 Bool_t AliPHOSSDigitizer::operator==( AliPHOSSDigitizer const &sd )const
329 {
330   // Equal operator.
331   // SDititizers are equal if their pedestal, slope and threshold are equal
332
333   if( (fA==sd.fA)&&(fB==sd.fB)&&(fPrimThreshold==sd.fPrimThreshold))
334     return kTRUE ;
335   else
336     return kFALSE ;
337 }
338 //__________________________________________________________________
339 //__________________________________________________________________
340 void AliPHOSSDigitizer::PrintSDigits(Option_t * option)
341 {
342   // Prints list of digits produced in the current pass of AliPHOSDigitizer
343
344
345   AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ; 
346   TString sdname(GetName()) ;
347   sdname.Remove(sdname.Index(GetTitle())-1) ;
348   const TClonesArray * sdigits = gime->SDigits(sdname.Data()) ; 
349
350   cout << "AliPHOSSDigitiser: event " << gAlice->GetEvNumber() << endl ;
351   cout << "      Number of entries in SDigits list " << sdigits->GetEntriesFast() << endl ;
352   cout << endl ;
353   if(strstr(option,"all")||strstr(option,"EMC")){
354     
355     //loop over digits
356     AliPHOSDigit * digit;
357     cout << "EMC sdigits " << endl ;
358     cout << "Digit Id    Amplitude     Index     Nprim  Primaries list " <<  endl;      
359     Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
360     Int_t index ;
361     for (index = 0 ; (index < sdigits->GetEntriesFast()) && 
362          (((AliPHOSDigit * )  sdigits->At(index))->GetId() <= maxEmc) ; index++) {
363       digit = (AliPHOSDigit * )  sdigits->At(index) ;
364       if(digit->GetNprimary() == 0) continue;
365       cout << setw(6)  <<  digit->GetId() << "   "  <<  setw(10)  <<  digit->GetAmp() <<   "    "  
366            << setw(6)  <<  digit->GetIndexInList() << "    "   
367            << setw(5)  <<  digit->GetNprimary() <<"    ";
368       
369       Int_t iprimary;
370       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
371         cout << setw(5)  <<  digit->GetPrimary(iprimary+1) << "    ";
372       cout << endl;      
373     }    
374     cout << endl;
375   }
376
377   if(strstr(option,"all")||strstr(option,"CPV")){
378     
379     //loop over CPV digits
380     AliPHOSDigit * digit;
381     cout << "CPV sdigits " << endl ;
382     cout << "Digit Id    Amplitude     Index     Nprim  Primaries list " <<  endl;      
383     Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
384     Int_t index ;
385     for (index = 0 ; index < sdigits->GetEntriesFast(); index++) {
386       digit = (AliPHOSDigit * )  sdigits->At(index) ;
387       if(digit->GetId() > maxEmc){
388         cout << setw(6)  <<  digit->GetId() << "   "  <<        setw(10)  <<  digit->GetAmp() <<   "    "  
389              << setw(6)  <<  digit->GetIndexInList() << "    "   
390              << setw(5)  <<  digit->GetNprimary() <<"    ";
391         
392         Int_t iprimary;
393         for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
394           cout << setw(5)  <<  digit->GetPrimary(iprimary+1) << "    ";
395         cout << endl;    
396       }    
397     }
398   }
399
400 }
401
402 //____________________________________________________________________________ 
403 void AliPHOSSDigitizer::UseHitsFrom(const char * filename)
404 {
405   SetTitle(filename) ; 
406   Init() ; 
407 }