Remove unused functions GetNextXXXX
[u/mrichter/AliRoot.git] / STEER / AliRunDigitizer.cxx
CommitLineData
9ce40367 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$Log$
a90ecf3c 18Revision 1.3 2001/07/30 14:04:18 jchudoba
19correct bug in the initialization
20
af20391f 21Revision 1.2 2001/07/28 10:44:32 hristov
22Loop variable declared once; typos corrected
23
aee2ebe9 24Revision 1.1 2001/07/27 12:59:00 jchudoba
25Manager class for merging/digitization
26
9ce40367 27*/
28
29////////////////////////////////////////////////////////////////////////
30//
31// AliRunDigitizer.cxx
32//
33// Manager object for merging/digitization
34//
35// Instance of this class manages the digitization and/or merging of
36// Sdigits into Digits.
37//
38// Only one instance of this class is created in the macro:
a90ecf3c 39// AliRunDigitizer * manager =
40// new AliRunDigitizer(nInputStreams,SPERB);
41// where nInputStreams is number of input streams and SPERB is
42// signals per background variable, which determines how combinations
43// of signal and background events are generated.
9ce40367 44// Then instances of specific detector digitizers are created:
45// AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager)
a90ecf3c 46// and the I/O configured (you have to specify input files
47// and an output file). The manager connects appropriate trees from
48// the input files according a combination returned by AliMergeCombi
49// classcreates. It creates TreeD in the output and runs once per
50// event Digitize method of all existing AliDetDigitizers
51// (without any option). AliDetDigitizers ask manager
52// for a TTree with input (manager->GetInputTreeS(Int_t i),
9ce40367 53// merge all inputs, digitize it, and save it in the TreeD
54// obtained by manager->GetTreeD(). Output events are stored with
55// numbers from 0, this default can be changed by
56// manager->SetFirstOutputEventNr(Int_t) method. The particle numbers
57// in the output are shifted by MASK, which is taken from manager.
58//
a90ecf3c 59// Single input file is permitted. Maximum MAXSTREAMSTOMERGE can be merged.
9ce40367 60// Input from the memory (on-the-fly merging) is not yet
61// supported, as well as access to the input data by invoking methods
62// on the output data.
63//
a90ecf3c 64// Access to the some data is via gAlice for now (supposing the
65// same geometry in all input files), gAlice is taken from the first
66// input file on the first stream.
9ce40367 67//
68// Example with MUON digitizer:
69//
a90ecf3c 70// AliRunDigitizer * manager = new AliRunDigitizer(2,1);
71// manager->SetInputStream(0,"1track_10events_phi45_60.root");
72// manager->SetInputStream(1,"1track_10events_phi120_135.root");
73// manager->SetOutputDir("/tmp");
74// manager->SetOutputFile("digits.root");
75// AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager);
76// manager->SetNrOfEventsToWrite(1);
77// manager->Digitize();
9ce40367 78//
79////////////////////////////////////////////////////////////////////////
80
81// system includes
82
83#include <iostream.h>
84
85// ROOT includes
86
87#include "TFile.h"
88#include "TTree.h"
89
90// AliROOT includes
91
92#include "AliRunDigitizer.h"
93#include "AliDigitizer.h"
94#include "AliRun.h"
95#include "AliHeader.h"
96#include "TParticle.h"
a90ecf3c 97#include "AliStream.h"
98#include "AliMergeCombi.h"
9ce40367 99
100ClassImp(AliRunDigitizer)
101
102////////////////////////////////////////////////////////////////////////
103
a90ecf3c 104AliRunDigitizer::AliRunDigitizer() : TNamed("AliRunDigitizer","")
9ce40367 105{
106// default ctor
a90ecf3c 107 cerr<<"Don't use"<<endl;
108 fCombi = 0;
109 fInputFiles = 0;
110 fNDigitizers = 0;
111 fNinputs = 0;
112 fInputStreams = 0;
113}
9ce40367 114
a90ecf3c 115////////////////////////////////////////////////////////////////////////
116AliRunDigitizer::AliRunDigitizer(Int_t nInputStreams, Int_t sperb) : TNamed("AliRunDigitizer","")
117{
118// default ctor
119 if (nInputStreams == 0) {
120 Error("AliRunDigitizer","Specify nr of input streams");
121 return;
122 }
aee2ebe9 123 Int_t i;
aee2ebe9 124 for (i=0;i<MAXDETECTORS;i++) fDigitizers[i]=0;
9ce40367 125 fNDigitizers = 0;
a90ecf3c 126 fNinputs = nInputStreams;
9ce40367 127 fOutputFileName = "digits.root";
128 fOutputDirName = "/tmp/";
a90ecf3c 129 fCombination.Set(MAXSTREAMSTOMERGE);
130 for (i=0;i<MAXSTREAMSTOMERGE;i++) {
9ce40367 131 fArrayTreeS[i]=fArrayTreeH[i]=fArrayTreeTPCS[i]=NULL;
132 fCombination[i]=-1;
133 }
134 fkMASKSTEP = 10000000;
135 fkMASK[0] = 0;
a90ecf3c 136 for (i=1;i<MAXSTREAMSTOMERGE;i++) {
9ce40367 137 fkMASK[i] = fkMASK[i-1] + fkMASKSTEP;
138 }
a90ecf3c 139 fInputStreams = new TClonesArray("AliStream",nInputStreams);
140 TClonesArray &lInputStreams = *fInputStreams;
141 for (i=0;i<nInputStreams;i++) {
142 new(lInputStreams[i]) AliStream();
143 }
9ce40367 144 fInputFiles = new TClonesArray("TFile",1);
9ce40367 145 fOutput = 0;
146 fEvent = 0;
147 fNrOfEventsToWrite = 0;
148 fNrOfEventsWritten = 0;
149 fCopyTreesFromInput = -1;
a90ecf3c 150 fCombi = new AliMergeCombi(nInputStreams,sperb);
9ce40367 151 fDebug = 3;
152 if (GetDebug()>2)
153 cerr<<"AliRunDigitizer::AliRunDigitizer() called"<<endl;
154}
155
156////////////////////////////////////////////////////////////////////////
157
158AliRunDigitizer::~AliRunDigitizer() {
159// dtor
160
a90ecf3c 161 if (fInputFiles) {
162 delete fInputFiles;
163 fInputFiles = 0;
164 }
165 if (fInputStreams) {
166 delete fInputStreams;
167 fInputStreams = 0;
168 }
169 if (fCombi) {
170 delete fCombi;
171 fCombi = 0;
172 }
173
9ce40367 174}
175
176////////////////////////////////////////////////////////////////////////
177void AliRunDigitizer::AddDigitizer(AliDigitizer *digitizer)
178{
179// add digitizer to the list of active digitizers
180
181 if (fNDigitizers >= MAXDETECTORS) {
182 cerr<<"Too many detectors to digitize. Increase value of MAXDETECTORS"
183 <<" constant in AliRunDigitizer.h and recompile or decrease the"
184 <<" the number of detectors"<<endl;
185 } else {
186 fDigitizers[fNDigitizers++] = digitizer;
187 }
188}
189
190////////////////////////////////////////////////////////////////////////
191
a90ecf3c 192void AliRunDigitizer::SetInputStream(Int_t i, char *inputFile)
9ce40367 193{
a90ecf3c 194 if (i > fInputStreams->GetLast()) {
195 Error("SetInputStream","Input stream number too high");
196 return;
9ce40367 197 }
a90ecf3c 198 static_cast<AliStream*>(fInputStreams->At(i))->AddFile(inputFile);
9ce40367 199}
200
201////////////////////////////////////////////////////////////////////////
202void AliRunDigitizer::Digitize()
203{
204// get a new combination of inputs, connect input trees and loop
205// over all digitizers
206
207 if (!InitGlobal()) {
208 cerr<<"False from InitGlobal"<<endl;
209 return;
210 }
211// take gAlice from the first input file. It is needed to access
212// geometry data
a90ecf3c 213 if (!static_cast<AliStream*>(fInputStreams->At(0))->ImportgAlice()) {
214 cerr<<"gAlice object not found in the first file of "
215 <<"the 1st stream"<<endl;
216 return;
217 }
218 Int_t eventsCreated = 0;
219 while (eventsCreated++ < fNrOfEventsToWrite) {
220// if (GetDebug()>2) PrintCombination();
9ce40367 221 ConnectInputTrees();
a90ecf3c 222 InitEvent();
9ce40367 223// loop over all registered digitizers and let them do the work
224 for (Int_t i=0;i<fNDigitizers; i++) {
225 fDigitizers[i]->Digitize();
226 }
a90ecf3c 227 FinishEvent();
9ce40367 228 }
229 FinishGlobal();
230}
231
232////////////////////////////////////////////////////////////////////////
a90ecf3c 233Bool_t AliRunDigitizer::ConnectInputTrees()
9ce40367 234{
235// fill arrays fArrayTreeS, fArrayTreeH and fArrayTreeTPCS with
236// pointers to the correct events according fCombination values
237// null pointers can be in the output, AliDigitizer has to check it
238
9ce40367 239 TTree *tree;
240 char treeName[20];
a90ecf3c 241 Int_t serialNr;
242 Int_t eventNr[MAXSTREAMSTOMERGE], delta[MAXSTREAMSTOMERGE];
243 fCombi->Combination(eventNr, delta);
9ce40367 244 for (Int_t i=0;i<fNinputs;i++) {
a90ecf3c 245 if (delta[i] == 1) {
246 AliStream *iStream = static_cast<AliStream*>(fInputStreams->At(i));
247 if (!iStream->NextEventInStream(serialNr)) return kFALSE;
248 sprintf(treeName,"TreeS%d",serialNr);
249 tree = static_cast<TTree*>(iStream->CurrentFile()->Get(treeName));
250 fArrayTreeS[i] = tree;
251 sprintf(treeName,"TreeH%d",serialNr);
252 tree = static_cast<TTree*>(iStream->CurrentFile()->Get(treeName));
253 fArrayTreeH[i] = tree;
254 sprintf(treeName,"TreeS_75x40_100x60_%d",serialNr);
255 tree = static_cast<TTree*>(iStream->CurrentFile()->Get(treeName));
256 fArrayTreeTPCS[i] = tree;
257 } else if (delta[i] != 0) {
258 Error("ConnectInputTrees","Only delta 0 or 1 is implemented");
259 return kFALSE;
260 }
261 }
262 return kTRUE;
9ce40367 263}
264
265////////////////////////////////////////////////////////////////////////
266Bool_t AliRunDigitizer::InitGlobal()
267{
268// called once before Digitize() is called, initialize digitizers and output
269
270 if (!InitOutputGlobal()) return kFALSE;
271 for (Int_t i=0;i<fNDigitizers; i++) {
272 if (!fDigitizers[i]->Init()) return kFALSE;
273 }
274 return kTRUE;
275}
276
277////////////////////////////////////////////////////////////////////////
278Bool_t AliRunDigitizer::InitOutputGlobal()
279{
280// Creates the output file, called once by InitGlobal()
281
282 TString fn;
283 fn = fOutputDirName + '/' + fOutputFileName;
284 fOutput = new TFile(fn,"recreate");
285 if (GetDebug()>2) {
286 cerr<<"file "<<fn.Data()<<" was opened"<<endl;
287 cerr<<"fOutput = "<<fOutput<<endl;
288 }
289 if (fOutput) return kTRUE;
290 Error("InitOutputGlobal","Could not create output file.");
291 return kFALSE;
292}
293
294
295////////////////////////////////////////////////////////////////////////
a90ecf3c 296void AliRunDigitizer::InitEvent()
9ce40367 297{
298// Creates TreeDxx in the output file, called from Digitize() once for
299// each event. xx = fEvent
300
301 if (GetDebug()>2)
a90ecf3c 302 cerr<<"AliRunDigitizer::InitEvent: fEvent = "<<fEvent<<endl;
9ce40367 303 fOutput->cd();
304 char hname[30];
305 sprintf(hname,"TreeD%d",fEvent);
306 fTreeD = new TTree(hname,"Digits");
a90ecf3c 307 fTreeD->Write(); // Do I have to write it here???
9ce40367 308}
309
310////////////////////////////////////////////////////////////////////////
a90ecf3c 311void AliRunDigitizer::FinishEvent()
9ce40367 312{
313// called at the end of loop over digitizers
314
315 fOutput->cd();
316 if (fCopyTreesFromInput > -1) {
317 char treeName[20];
318 Int_t i = fCopyTreesFromInput;
319 sprintf(treeName,"TreeK%d",fCombination[i]);
320 ((TFile*)fInputFiles->At(i))->Get(treeName)->Clone()->Write();
321 sprintf(treeName,"TreeH%d",fCombination[i]);
322 ((TFile*)fInputFiles->At(i))->Get(treeName)->Clone()->Write();
323 }
324 fEvent++;
325 fNrOfEventsWritten++;
326 if (fTreeD) {
327 delete fTreeD;
328 fTreeD = 0;
329 }
330}
331////////////////////////////////////////////////////////////////////////
332void AliRunDigitizer::FinishGlobal()
333{
334// called at the end of Exec
335// save unique objects to the output file
336
337 fOutput->cd();
338 this->Write();
339 if (fCopyTreesFromInput > -1) {
340 ((TFile*)fInputFiles->At(fCopyTreesFromInput))->Get("TE")
341 ->Clone()->Write();
342 gAlice->Write();
343 }
344 fOutput->Close();
345}
346
347
348////////////////////////////////////////////////////////////////////////
349Int_t AliRunDigitizer::GetNParticles(Int_t event)
350{
351// return number of particles in all input files for a given
352// event (as numbered in the output file)
353// return -1 if some file cannot be accessed
354
355 Int_t sum = 0;
356 Int_t sumI;
357 for (Int_t i = 0; i < fNinputs; i++) {
358 sumI = GetNParticles(GetInputEventNumber(event,i), i);
359 if (sumI < 0) return -1;
360 sum += sumI;
361 }
362 return sum;
363}
364
365////////////////////////////////////////////////////////////////////////
366Int_t AliRunDigitizer::GetNParticles(Int_t event, Int_t input)
367{
368// return number of particles in input file input for a given
369// event (as numbered in this input file)
370// return -1 if some error
371
372 TFile *file = ConnectInputFile(input);
373 if (!file) {
374 Error("GetNParticles","Cannot open input file");
375 return -1;
376 }
377
378// find the header and get Nprimaries and Nsecondaries
379 TTree* tE = (TTree *)file->Get("TE") ;
380 if (!tE) {
381 Error("GetNParticles","input file does not contain TE");
382 return -1;
383 }
384 AliHeader* header;
385 header = 0;
386 tE->SetBranchAddress("Header", &header);
387 if (!tE->GetEntry(event)) {
388 Error("GetNParticles","event %d not found",event);
389 return -1;
390 }
391 if (GetDebug()>2) {
392 cerr<<"Nprimary: "<< header->GetNprimary()<<endl;
393 cerr<<"Nsecondary: "<<header->GetNsecondary()<<endl;
394 }
395 return header->GetNprimary() + header->GetNsecondary();
396}
397
398////////////////////////////////////////////////////////////////////////
399Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event)
400{
401// return pointer to an int array with input event numbers which were
402// merged in the output event event
403
404// simplified for now, implement later
a90ecf3c 405 Int_t a[MAXSTREAMSTOMERGE];
9ce40367 406 for (Int_t i = 0; i < fNinputs; i++) {
407 a[i] = event;
408 }
409 return a;
410}
411////////////////////////////////////////////////////////////////////////
412Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t input)
413{
414// return an event number of an eventInput from input file input
415// which was merged to create output event event
416
417// simplified for now, implement later
418 return event;
419}
9ce40367 420////////////////////////////////////////////////////////////////////////
421TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event)
422{
423// return pointer to particle with index i (index with mask)
424
425// decode the MASK
426 Int_t input = i/fkMASKSTEP;
427 return GetParticle(i,input,GetInputEventNumber(event,input));
428}
429
430////////////////////////////////////////////////////////////////////////
431TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t input, Int_t event)
432{
433// return pointer to particle with index i in the input file input
434// (index without mask)
435// event is the event number in the file input
436// return 0 i fit does not exist
437
438 TFile *file = ConnectInputFile(input);
439 if (!file) {
440 Error("GetParticle","Cannot open input file");
441 return 0;
442 }
443
444// find the header and get Nprimaries and Nsecondaries
445 TTree* tE = (TTree *)file->Get("TE") ;
446 if (!tE) {
447 Error("GetParticle","input file does not contain TE");
448 return 0;
449 }
450 AliHeader* header;
451 header = 0;
452 tE->SetBranchAddress("Header", &header);
453 if (!tE->GetEntry(event)) {
454 Error("GetParticle","event %d not found",event);
455 return 0;
456 }
457
458// connect TreeK
459 char treeName[30];
460 sprintf(treeName,"TreeK%d",event);
461 TTree* tK = static_cast<TTree*>(file->Get(treeName));
462 if (!tK) {
463 Error("GetParticle","input file does not contain TreeK%d",event);
464 return 0;
465 }
466 TParticle *particleBuffer;
467 particleBuffer = 0;
468 tK->SetBranchAddress("Particles", &particleBuffer);
469
470
471// algorithmic way of getting entry index
472// (primary particles are filled after secondaries)
473 Int_t entry;
474 if (i<header->GetNprimary())
475 entry = i+header->GetNsecondary();
476 else
477 entry = i-header->GetNprimary();
9ce40367 478 Int_t bytesRead = tK->GetEntry(entry);
479// new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
480 if (bytesRead)
481 return particleBuffer;
482 return 0;
483}
484////////////////////////////////////////////////////////////////////////
485