1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 //_______________________________________________________________________
20 // AliRunDigitizer.cxx
22 // Manager object for merging/digitization
24 // Instance of this class manages the digitization and/or merging of
25 // Sdigits into Digits.
27 // Only one instance of this class is created in the macro:
28 // AliRunDigitizer * manager =
29 // new AliRunDigitizer(nInputStreams,SPERB);
30 // where nInputStreams is number of input streams and SPERB is
31 // signals per background variable, which determines how combinations
32 // of signal and background events are generated.
33 // Then instances of specific detector digitizers are created:
34 // AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager)
35 // and the I/O configured (you have to specify input files
36 // and an output file). The manager connects appropriate trees from
37 // the input files according a combination returned by AliMergeCombi
38 // class. It creates TreeD in the output and runs once per
39 // event Digitize method of all existing AliDetDigitizers
40 // (without any option). AliDetDigitizers ask manager
41 // for a TTree with input (manager->GetInputTreeS(Int_t i),
42 // merge all inputs, digitize it, and save it in the TreeD
43 // obtained by manager->GetTreeD(). Output events are stored with
44 // numbers from 0, this default can be changed by
45 // manager->SetFirstOutputEventNr(Int_t) method. The particle numbers
46 // in the output are shifted by MASK, which is taken from manager.
48 // The default output is to the signal file (stream 0). This can be
49 // changed with the SetOutputFile(TString fn) method.
51 // Single input file is permitted. Maximum kMaxStreamsToMerge can be merged.
52 // Input from the memory (on-the-fly merging) is not yet
53 // supported, as well as access to the input data by invoking methods
54 // on the output data.
56 // Access to the some data is via gAlice for now (supposing the
57 // same geometry in all input files), gAlice is taken from the first
58 // input file on the first stream.
60 // Example with MUON digitizer, no merging, just digitization
62 // AliRunDigitizer * manager = new AliRunDigitizer(1,1);
63 // manager->SetInputStream(0,"galice.root");
64 // AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager);
67 // Example with MUON digitizer, merge all events from
68 // galice.root (signal) file with events from bgr.root
69 // (background) file. Number of merged events is
70 // min(number of events in galice.root, number of events in bgr.root)
72 // AliRunDigitizer * manager = new AliRunDigitizer(2,1);
73 // manager->SetInputStream(0,"galice.root");
74 // manager->SetInputStream(1,"bgr.root");
75 // AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager);
78 // Example with MUON digitizer, save digits in a new file digits.root,
79 // process only 1 event
81 // AliRunDigitizer * manager = new AliRunDigitizer(2,1);
82 // manager->SetInputStream(0,"galice.root");
83 // manager->SetInputStream(1,"bgr.root");
84 // manager->SetOutputFile("digits.root");
85 // AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager);
86 // manager->SetNrOfEventsToWrite(1);
89 //_______________________________________________________________________
93 #include <Riostream.h>
103 #include "AliDigitizer.h"
104 #include "AliMergeCombi.h"
105 #include "AliRunLoader.h"
106 #include "AliLoader.h"
108 #include "AliRunDigitizer.h"
109 #include "AliStream.h"
110 #include "AliHeader.h"
112 ClassImp(AliRunDigitizer)
114 const TString AliRunDigitizer::fgkDefOutFolderName("Output");
115 const TString AliRunDigitizer::fgkBaseInFolderName("Input");
118 //_______________________________________________________________________
119 AliRunDigitizer::AliRunDigitizer():
124 fNrOfEventsToWrite(0),
125 fNrOfEventsWritten(0),
126 fCopyTreesFromInput(0),
131 fOutputInitialized(kFALSE),
134 fCombinationFileName(0),
137 // root requires default ctor, where no new objects can be created
138 // do not use this ctor, it is supplied only for root needs
140 // fOutputStream = 0x0;
143 //_______________________________________________________________________
144 AliRunDigitizer::AliRunDigitizer(Int_t nInputStreams, Int_t sperb):
145 TTask("AliRunDigitizer","The manager for Merging"),
146 fkMASKSTEP(10000000),
150 fNrOfEventsToWrite(-1),
151 fNrOfEventsWritten(0),
152 fCopyTreesFromInput(-1),
153 fNinputs(nInputStreams),
155 fInputStreams(new TClonesArray("AliStream",nInputStreams)),
157 fOutputInitialized(kFALSE),
158 fCombi(new AliMergeCombi(nInputStreams,sperb)),
159 fCombination(kMaxStreamsToMerge),
160 fCombinationFileName(0),
164 // ctor which should be used to create a manager for merging/digitization
165 if (nInputStreams == 0)
167 Fatal("AliRunDigitizer","Specify nr of input streams");
174 for (i=1;i<kMaxStreamsToMerge;i++)
176 fkMASK[i] = fkMASK[i-1] + fkMASKSTEP;
179 TClonesArray &lInputStreams = *fInputStreams;
181 new(lInputStreams[0]) AliStream(fgkBaseInFolderName+(Long_t)0,"UPDATE");
182 for (i=1;i<nInputStreams;i++) {
183 new(lInputStreams[i]) AliStream(fgkBaseInFolderName+(Long_t)i,"READ");
186 //_______________________________________________________________________
188 AliRunDigitizer::AliRunDigitizer(const AliRunDigitizer& dig):
194 fNrOfEventsToWrite(0),
195 fNrOfEventsWritten(0),
196 fCopyTreesFromInput(0),
201 fOutputInitialized(kFALSE),
204 fCombinationFileName(0),
212 //_______________________________________________________________________
214 void AliRunDigitizer::Copy(TObject&) const
216 Fatal("Copy","Not installed\n");
219 //_______________________________________________________________________
221 AliRunDigitizer::~AliRunDigitizer() {
223 delete fInputStreams;
225 delete fOutRunLoader;
227 //_______________________________________________________________________
228 void AliRunDigitizer::AddDigitizer(AliDigitizer *digitizer)
230 // add digitizer to the list of active digitizers
231 this->Add(digitizer);
233 //_______________________________________________________________________
234 void AliRunDigitizer::SetInputStream(Int_t i, const char *inputFile, TString foldername)
237 // Sets the name of the input file
239 if (i > fInputStreams->GetLast()) {
240 Error("SetInputStream","Input stream number too high");
243 AliStream * stream = static_cast<AliStream*>(fInputStreams->At(i)) ;
244 if ( !foldername.IsNull() ) {
246 foldername += i ; // foldername should stay unchanged for the default output
247 stream->SetFolderName(foldername) ;
249 stream->AddFile(inputFile);
252 //_______________________________________________________________________
253 void AliRunDigitizer::Digitize(Option_t* option)
255 // get a new combination of inputs, loads events to folders
257 // take gAlice from the first input file. It is needed to access
259 // If gAlice is already in memory, use it
262 if (!static_cast<AliStream*>(fInputStreams->At(0))->ImportgAlice())
264 Error("Digitize","Error occured while getting gAlice from Input 0");
268 if (!InitGlobal()) //calls Init() for all (sub)digitizers
270 Error("Digitize","InitGlobal returned error");
274 Int_t eventsCreated = 0;
275 // loop until there is anything on the input in case fNrOfEventsToWrite < 0
276 while ((eventsCreated++ < fNrOfEventsToWrite) || (fNrOfEventsToWrite < 0))
278 if (!ConnectInputTrees()) break;
279 InitEvent();//this must be after call of Connect Input tress.
282 fOutRunLoader->SetEventNumber(eventsCreated-1);
284 static_cast<AliStream*>(fInputStreams->At(0))->ImportgAlice(); // use gAlice of the first input stream
285 ExecuteTasks(option);// loop over all registered digitizers and let them do the work
292 //_______________________________________________________________________
293 Bool_t AliRunDigitizer::ConnectInputTrees()
296 Int_t eventNr[kMaxStreamsToMerge], delta[kMaxStreamsToMerge];
297 fCombi->Combination(eventNr, delta);
298 for (Int_t i=0;i<fNinputs;i++)
302 AliStream *iStream = static_cast<AliStream*>(fInputStreams->At(i));//gets the "i" defined in combination
303 if (!iStream->NextEventInStream()) return kFALSE; //sets serial number
305 else if (delta[i] != 0)
307 Error("ConnectInputTrees","Only delta 0 or 1 is implemented");
315 //_______________________________________________________________________
316 Bool_t AliRunDigitizer::InitGlobal()
318 // called once before Digitize() is called, initialize digitizers and output
319 fOutputInitialized = kFALSE;
320 TList* subTasks = this->GetListOfTasks();
322 TIter next(subTasks);
323 while (AliDigitizer * dig = (AliDigitizer *) next())
329 //_______________________________________________________________________
331 void AliRunDigitizer::SetOutputFile(TString fn)
333 // the output will be to separate file, not to the signal file
334 //here should be protection to avoid setting the same file as any input
335 Info("SetOutputFile","Setting Output File Name %s ",fn.Data());
336 fOutputFileName = fn;
337 // InitOutputGlobal();
340 //_______________________________________________________________________
341 Bool_t AliRunDigitizer::InitOutputGlobal()
343 // Creates the output file, called by InitEvent()
344 //Needs to be called after all inputs are opened
345 if (fOutputInitialized) return kTRUE;
347 if ( !fOutputFileName.IsNull())
349 fOutRunLoader = AliRunLoader::Open(fOutputFileName,fgkDefOutFolderName,"recreate");
351 if (fOutRunLoader == 0x0)
353 Error("InitOutputGlobal","Can not open ooutput");
356 Info("InitOutputGlobal", " 1 %s = ", GetInputFolderName(0).Data()) ;
357 AliRunLoader* inrl = AliRunLoader::GetRunLoader(GetInputFolderName(0));
360 Error("InitOutputGlobal","Can not get Run Loader Input 0. Maybe yet not initialized?");
363 Info("InitOutputGlobal", " 2 %#x = ", inrl) ;
365 //Copy all detector loaders from input 0 to output
366 const TObjArray* inloaders = inrl->GetArrayOfLoaders();
367 TIter next(inloaders);
369 while((loader = (AliLoader*)next()))
371 AliLoader* cloneloader = (AliLoader*)loader->Clone();
372 cloneloader->Register(fOutRunLoader->GetEventFolder());//creates folders for this loader in Output Folder Structure
373 GetOutRunLoader()->AddLoader(cloneloader);//adds a loader for output
376 fOutRunLoader->MakeTree("E");
378 if (GetDebug()>2) Info("InitOutputGlobal","file %s was opened.",fOutputFileName.Data());
380 fOutputInitialized = kTRUE;
383 //_______________________________________________________________________
385 void AliRunDigitizer::InitEvent()
387 //redirects output properly
390 Info("InitEvent","fEvent = %d",fEvent);
391 Info("InitEvent","fOutputFileName \"%s\"",fOutputFileName.Data());
393 if (fOutputInitialized == kFALSE) InitOutputGlobal();
395 // if fOutputFileName was not given, write output to signal directory
397 //_______________________________________________________________________
399 void AliRunDigitizer::FinishEvent()
401 // called at the end of loop over digitizers
404 if (GetOutRunLoader() == 0x0)
406 Error("FinishEvent","fOutRunLoader is null");
411 fNrOfEventsWritten++;
415 AliRunLoader* inrl = AliRunLoader::GetRunLoader(GetInputFolderName(0));
416 AliHeader* outheader = fOutRunLoader->GetHeader();
417 AliHeader* inheader = inrl->GetHeader();
421 inheader = inrl->GetHeader();
422 if (inheader == 0x0) Fatal("FinishEvent","Can not get header from input 0");
425 outheader->SetNprimary(inheader->GetNprimary());
426 outheader->SetNtrack(inheader->GetNtrack());
427 outheader->SetEvent(inheader->GetEvent());
428 outheader->SetEventNrInRun(inheader->GetEventNrInRun());
429 outheader->SetNvertex(inheader->GetNvertex());
430 fOutRunLoader->TreeE()->Fill();
433 if (fCopyTreesFromInput > -1)
435 //this is sensless since no information would be coherent in case of merging
437 cout<<"Copy trees from input: Copy or link files manually"<<endl;
441 //_______________________________________________________________________
443 void AliRunDigitizer::FinishGlobal()
445 // called at the end of Exec
446 // save unique objects to the output file
448 if (GetOutRunLoader() == 0x0)
450 Error("FinishGlobal","Can not get RunLoader from Output Stream folder");
453 TFile* file = TFile::Open(fOutputDirName + "/digitizer.root", "recreate");
459 fOutRunLoader->WriteHeader("OVERWRITE");
460 fOutRunLoader->WriteRunLoader("OVERWRITE");
461 TFolder* outfolder = fOutRunLoader->GetEventFolder();
462 if (outfolder == 0x0)
464 Error("FinishEvent","Can not get Event Folder");
468 AliRunLoader* inRN = AliRunLoader::GetRunLoader(GetInputFolderName(0));
469 outfolder->Add(inRN->GetAliRun());
470 fOutRunLoader->WriteAliRun("OVERWRITE");
473 if (fCopyTreesFromInput > -1)
475 //copy files manually
478 //_______________________________________________________________________
480 Int_t AliRunDigitizer::GetNParticles(Int_t event) const
482 // return number of particles in all input files for a given
483 // event (as numbered in the output file)
484 // return -1 if some file cannot be accessed
488 for (Int_t i = 0; i < fNinputs; i++) {
489 sumI = GetNParticles(GetInputEventNumber(event,i), i);
490 if (sumI < 0) return -1;
495 //_______________________________________________________________________
497 Int_t AliRunDigitizer::GetNParticles(Int_t /*event*/, Int_t /*input*/) const
499 // return number of particles in input file input for a given
500 // event (as numbered in this input file)
501 // return -1 if some error
503 // Must be revised in the version with AliStream
509 //_______________________________________________________________________
510 Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event) const
512 // return pointer to an int array with input event numbers which were
513 // merged in the output event event
515 // simplified for now, implement later
516 Int_t * a = new Int_t[kMaxStreamsToMerge];
517 for (Int_t i = 0; i < fNinputs; i++) {
522 //_______________________________________________________________________
523 Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t /*input*/) const
525 // return an event number of an eventInput from input file input
526 // which was merged to create output event event
528 // simplified for now, implement later
531 //_______________________________________________________________________
532 TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event) const
534 // return pointer to particle with index i (index with mask)
537 Int_t input = i/fkMASKSTEP;
538 return GetParticle(i,input,GetInputEventNumber(event,input));
541 //_______________________________________________________________________
542 TParticle* AliRunDigitizer::GetParticle(Int_t /*i*/, Int_t /*input*/, Int_t /*event*/) const
544 // return pointer to particle with index i in the input file input
545 // (index without mask)
546 // event is the event number in the file input
547 // return 0 i fit does not exist
549 // Must be revised in the version with AliStream
554 //_______________________________________________________________________
555 void AliRunDigitizer::ExecuteTask(Option_t* option)
557 // overwrite ExecuteTask to do Digitize only
559 if (!IsActive()) return;
561 fHasExecuted = kTRUE;
565 //_______________________________________________________________________
566 const TString& AliRunDigitizer::GetInputFolderName(Int_t i) const
568 AliStream* stream = dynamic_cast<AliStream*>(fInputStreams->At(i));
571 Fatal("GetInputFolderName","Can not get the input stream. Index = %d. Exiting",i);
573 return stream->GetFolderName();
575 //_______________________________________________________________________
577 const char* AliRunDigitizer::GetOutputFolderName()
579 return GetOutRunLoader()->GetEventFolder()->GetName();
581 //_______________________________________________________________________
583 AliRunLoader* AliRunDigitizer::GetOutRunLoader()
585 if (fOutRunLoader) return fOutRunLoader;
587 if ( fOutputFileName.IsNull() )
588 {//guard that sombody calls it without settting file name
589 cout<<"Output file name is empty. Using Input 0 for output\n";
590 return AliRunLoader::GetRunLoader(GetInputFolderName(0));
592 // InitOutputGlobal();
593 return fOutRunLoader;
595 //_______________________________________________________________________
597 TString AliRunDigitizer::GetInputFileName(Int_t input, Int_t order) const
599 // returns file name of the order-th file in the input stream input
600 // returns empty string if such file does not exist
601 // first input stream is 0
602 // first file in the input stream is 0
603 TString fileName("");
604 if (input >= fNinputs) return fileName;
605 AliStream * stream = static_cast<AliStream*>(fInputStreams->At(input));
606 if (order > stream->GetNInputFiles()) return fileName;
607 fileName = stream->GetFileName(order);