The deletion of TOF folders temporarily commented out
[u/mrichter/AliRoot.git] / TOF / AliTOFDigitizer.cxx
CommitLineData
68861244 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
17//_________________________________________________________________________
18// This is a TTask that makes TOF-Digits out of TOF-Hits.
19// A TOF Digit is essentially a TOF hit with the smearing for strip
20// time resolution and simulation of the ADC correlation signal.
21// Digits are written to TreeD in branch "TOF".
22// AliTOFDigitizer with all current parameters used for digitization
23// (time resolution and ADC parameter) is written
24// to TreeD branch "AliTOFDigitizer".
25// Both branches have the same title. If necessary one can produce
26// another set of Digits with different parameters. Two versions
27// can be distunguished using titles of the branches.
28// User case:
29// root [0] AliTOFDigitizer * s = new AliTOFDigitizer("galice.root")
30// Warning in <AliITSgeomSPD425Long::Default Creator>: Detector size may not be write.
31// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
32// root [1] s->ExecuteTask()
33// // Makes Digits for all events stored in galice.root
34// root [2] s->SetTimeRes(100.)
35// // One can change parameters of digitization
36// root [3] s->SetDigitsBranch("Time Resolution 100. ps")
37// // and write them into the new branch
38// root [4] s->ExecuteTask("deb all tim")
39// // available parameters:
40// deb - print number of produced Digits
41// deb all - print number and list of produced Digits
42// tim - print benchmarking information
43//
44// -- Author : F. Pierella (Bologna University) pierella@bo.infn.it
45//////////////////////////////////////////////////////////////////////////////
46
47#include "TFile.h"
48#include "TTask.h"
49#include "TTree.h"
50#include "TSystem.h"
51#include "TROOT.h"
52#include "TFolder.h"
53#include "TBenchmark.h"
0efc163f 54#include "TObjString.h"
68861244 55
56#include <iomanip.h>
57
58#include "AliRun.h"
0efc163f 59#include "AliTOF.h"
68861244 60#include "AliTOFdigit.h"
61#include "AliTOFhit.h"
62#include "AliTOFDigitizer.h"
63
64
65ClassImp(AliTOFDigitizer)
66
67
68//____________________________________________________________________________
69 AliTOFDigitizer::AliTOFDigitizer():TTask("AliTOFDigitizer","")
70{
71 // ctor
72 fTimeRes = 100; // ps
73 fChrgRes = 100.; // pC
74 fNevents = 0 ;
75 fDigits = 0 ;
76 fHits = 0 ;
77 fIsInitialized = kFALSE ;
78
79}
80
81//____________________________________________________________________________
82AliTOFDigitizer::AliTOFDigitizer(const char* headerFile, const char *digitsTitle):TTask("AliTOFDigitizer","")
83{
84 // ctor
85 fTimeRes = 100; // ps
86 fChrgRes = 100.; // pC
87 fNevents = 0 ;
88 fDigitsTitle = digitsTitle ;
89 fHeadersFile = headerFile ;
90 fIsInitialized = kFALSE ;
91 Init();
92}
93
94//____________________________________________________________________________
95AliTOFDigitizer::~AliTOFDigitizer()
96{
97 // dtor
98 if(fDigits)
99 delete fDigits ;
100 if(fHits)
101 delete fHits ;
102}
103//____________________________________________________________________________
104void AliTOFDigitizer::Init()
105{
106 // Initialization: open root-file, allocate arrays for hits and digits,
107 // attach task Digitizer to the list of TOF tasks
108 //
109 // Initialization can not be done in the default constructor
110
111 if(!fIsInitialized){
112
113 if(fHeadersFile.IsNull())
114 fHeadersFile="galice.root" ;
115
116 TFile * file = (TFile*) gROOT->GetFile(fHeadersFile.Data() ) ;
117
118 //if file was not opened yet, read gAlice
119 if(file == 0){
120 if(fHeadersFile.Contains("rfio"))
121 file = TFile::Open(fHeadersFile,"update") ;
122 else
123 file = new TFile(fHeadersFile.Data(),"update") ;
124 gAlice = (AliRun *) file->Get("gAlice") ;
125 }
126 fHits = new TClonesArray("AliTOFhit" , 405);
127 fDigits = new TClonesArray("AliTOFdigit", 405);
128
129 //add Task to //FPAlice/tasks/(S)Digitizer/TOF
130 TFolder * alice = (TFolder*)gROOT->GetListOfBrowsables()->FindObject("FPAlice") ;
131 TTask * aliceSD = (TTask*)alice->FindObject("tasks/(S)Digitizer") ;
132 TTask * tofD = (TTask*)aliceSD->GetListOfTasks()->FindObject("TOF") ;
133 tofD->Add(this) ;
134
135 fIsInitialized = kTRUE ;
136 }
137}
138//____________________________________________________________________________
139void AliTOFDigitizer::Exec(Option_t *option)
140{
141 Int_t tracks[3]; // track info
142 Int_t vol[5]; // dummy location for digit
143 Float_t digit[2]; // TOF digit variables
144
68861244 145 // Collects all hits in the same active volume into digit
146
147 if(!fIsInitialized)
148 Init() ;
149
150 if(strstr(option,"tim"))
151 gBenchmark->Start("TOFDigitizer");
152
153 fNevents = (Int_t) gAlice->TreeE()->GetEntries() ;
154
155 Int_t ievent ;
156 // start loop on events
157 for(ievent = 0; ievent < fNevents; ievent++){
158 gAlice->GetEvent(ievent) ;
159 gAlice->SetEvent(ievent) ;
160
161 if(gAlice->TreeH()==0){
162 cout << "AliTOFDigitizer: There is no Hit Tree" << endl;
163 return ;
164 }
165
166 //set address of the hits
167 TBranch * branch = gAlice->TreeH()->GetBranch("TOF");
168 if (branch)
169 branch->SetAddress(&fHits);
170 else{
171 cout << "ERROR in AliTOFDigitizer: "<< endl ;
172 cout << " no branch TOF in TreeH"<< endl ;
173 cout << " do nothing " << endl ;
174 return ;
175 }
176
177 fDigits->Clear();
178 Int_t ndigits = 0 ;
179
180 //Now made Digits from hits, for TOF it is the same a part for the tof smearing
181 // and some missing MC variables
182 Int_t itrack ;
183 for (itrack=0; itrack < gAlice->GetNtrack(); itrack++){
184
185 //=========== Get the TOF branch from Hits Tree for the Primary track itrack
186 branch->GetEntry(itrack,0);
187
188 Int_t i;
189 for ( i = 0 ; i < fHits->GetEntries() ; i++ ) {
190
191 AliTOFhit * hit = (AliTOFhit*)fHits->At(i) ;
192
193 vol[0] = hit->GetSector();
194 vol[1] = hit->GetPlate();
195 vol[2] = hit->GetPadx();
196 vol[3] = hit->GetPadz();
197 vol[4] = hit->GetStrip();
198 // 95% of efficiency to be inserted here
199 // edge effect to be inserted here
200 // cross talk to be inserted here
201 // simulation of the detector response
202 Float_t idealtime = hit->GetTof(); // unit s
203 idealtime *= 1.E+12; // conversion from s to ps
204 // fTimeRes is given usually in ps
0efc163f 205 Float_t tdctime = gRandom->Gaus(idealtime, fTimeRes);
68861244 206 digit[0] = tdctime;
207 // typical Landau Distribution to be inserted here
208 Float_t idealcharge = hit->GetEdep();
0efc163f 209 Float_t adccharge = gRandom->Gaus(idealcharge, fChrgRes);
68861244 210 digit[1] = adccharge;
211 // to be added a check for overlaps
212 new((*fDigits)[ndigits]) AliTOFdigit(tracks, vol, digit);
213 ndigits++ ;
214 }
215
216 } // end loop over tracks
217
68861244 218 ndigits = fDigits->GetEntriesFast() ;
219 printf("AliTOFDigitizer: Total number of digits %d\n",ndigits);
220
221 if(gAlice->TreeD() == 0)
222 gAlice->MakeTree("D") ;
223
224 //check, if this branch already exits
225 TBranch * digitsBranch = 0;
226 TBranch * digitizerBranch = 0;
227
228 TObjArray * branches = gAlice->TreeD()->GetListOfBranches() ;
229 Int_t ibranch;
230 Bool_t tofNotFound = kTRUE ;
231 Bool_t digitizerNotFound = kTRUE ;
232
233 for(ibranch = 0;ibranch <branches->GetEntries();ibranch++){
234
235 if(tofNotFound){
236 digitsBranch=(TBranch *) branches->At(ibranch) ;
237 if( (strcmp("TOF",digitsBranch->GetName())==0 ) &&
238 (fDigitsTitle.CompareTo(digitsBranch->GetTitle()) == 0) )
239 tofNotFound = kFALSE ;
240 }
241 if(digitizerNotFound){
242 digitizerBranch = (TBranch *) branches->At(ibranch) ;
243 if( (strcmp(digitizerBranch->GetName(),"AliTOFDigitizer") == 0)&&
244 (fDigitsTitle.CompareTo(digitizerBranch->GetTitle()) == 0) )
245 digitizerNotFound = kFALSE ;
246 }
247 }
248
249 if(!(digitizerNotFound && tofNotFound)){
250 cout << "AliTOFdigitizer error:" << endl ;
251 cout << "Can not overwrite existing branches: do not write" << endl ;
252 return ;
253 }
254
255 //Make (if necessary) branches
256 char * file =0;
257 if(gSystem->Getenv("CONFIG_SPLIT_FILE")){ //generating file name
258 file = new char[strlen(gAlice->GetBaseFile())+20] ;
259 sprintf(file,"%s/TOF.Digits.root",gAlice->GetBaseFile()) ;
260 }
261
262 TDirectory *cwd = gDirectory;
263
264 //First list of digits
265 Int_t bufferSize = 32000 ;
266 digitsBranch = gAlice->TreeD()->Branch("TOF",&fDigits,bufferSize);
267 digitsBranch->SetTitle(fDigitsTitle.Data());
268 if (file) {
269 digitsBranch->SetFile(file);
270 TIter next( digitsBranch->GetListOfBranches());
271 TBranch * subbr;
272 while ((subbr=(TBranch*)next())) {
273 subbr->SetFile(file);
274 }
275 cwd->cd();
276 }
277
278 //second - Digitizer
279 Int_t splitlevel = 0 ;
280 AliTOFDigitizer * digtz = this ;
281 digitizerBranch = gAlice->TreeD()->Branch("AliTOFDigitizer","AliTOFDigitizer",
282 &digtz,bufferSize,splitlevel);
283 digitizerBranch->SetTitle(fDigitsTitle.Data());
284 if (file) {
285 digitizerBranch->SetFile(file);
286 TIter next( digitizerBranch->GetListOfBranches());
287 TBranch * subbr ;
288 while ((subbr=(TBranch*)next())) {
289 subbr->SetFile(file);
290 }
291 cwd->cd();
292 delete file;
293 }
294
295 digitsBranch->Fill();
296 digitizerBranch->Fill();
297 gAlice->TreeD()->Write(0,TObject::kOverwrite) ;
298
299 if(strstr(option,"deb"))
300 PrintDigits(option) ;
301
302 }
303
304 if(strstr(option,"tim")){
305 gBenchmark->Stop("TOFDigitizer");
306 cout << "AliTOFDigitizer:" << endl ;
307 cout << " took " << gBenchmark->GetCpuTime("TOFDigitizer") << " seconds for Digitizing "
308 << gBenchmark->GetCpuTime("TOFDigitizer")/fNevents << " seconds per event " << endl ;
309 cout << endl ;
310 }
311
312
313}
314//__________________________________________________________________
315void AliTOFDigitizer::SetDigitsBranch(const char* title )
316{
317 // Setting title to branch Digits
318 if(!fDigitsTitle.IsNull())
319 cout << "AliTOFdigitizer: changing Digits file from " <<fDigitsTitle.Data() << " to " << title << endl ;
320 fDigitsTitle=title ;
321}
322//__________________________________________________________________
323void AliTOFDigitizer::Print(Option_t* option)const
324{
325 // Prints parameters of Digitizer
326 cout << "------------------- "<< GetName() << " -------------" << endl ;
327 cout << " Writing Digits to branch with title " << fDigitsTitle.Data() << endl ;
328 cout << " with digitization parameters Time resolution = " << fTimeRes << endl ;
329 cout << " Adc smearing parameter = " << fChrgRes << endl ;
330 cout << "---------------------------------------------------"<<endl ;
331
332}
333//__________________________________________________________________
334Bool_t AliTOFDigitizer::operator==( AliTOFDigitizer const &digtz )const
335{
336 // Equal operator.
337 // Dititizers are equal if their time resolution and Adc
338 // smearing parameter are equal
339
340 if( (fTimeRes==digtz.fTimeRes)&&(fChrgRes==digtz.fChrgRes))
341 return kTRUE ;
342 else
343 return kFALSE ;
344}
345//__________________________________________________________________
346void AliTOFDigitizer::PrintDigits(Option_t * option)
347{
348 // Prints list of digits produced in the current pass of AliTOFDigitizer
349
350 cout << "AliTOFDigitizer: " << endl ;
351 cout << " Number of entries in Digits list " << fDigits->GetEntriesFast() << endl ;
352 cout << endl ;
353
354 if(strstr(option,"all")){// print all digits
355
356 //loop over digits
357 AliTOFdigit * digit;
358 cout << "Digit # " << " Time of flight(ps) " <<
359 " ADC(pC)" << " Sector #" << " Plate #" <<
360 " Strip #" << " Pad # (x) " << " Pad # (z) " << endl;
361 Int_t index ;
362 for (index = 0 ; index < fDigits->GetEntries() ; index++) {
363 digit = (AliTOFdigit * ) fDigits->At(index) ;
364
365 // set the width of the output with the setw(int width) manipulator
366
367 cout << setw(7) << index << " "
368 << setw(13) << digit->GetTdc() << " "
369 << setw(13) << digit->GetAdc() << " "
370 << setw(6) << digit->GetSector() << " "
371 << setw(6) << digit->GetPlate() << " "
372 << setw(6) << digit->GetStrip() << " "
373 << setw(7) << digit->GetPadx() << " "
374 << setw(7) << digit->GetPadz() << " " << endl;
375
376 } // end loop on digits
377
378 } // close if "all" option selected
379}