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 MAXSTREAMSTOMERGE 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 //_______________________________________________________________________
96 #include <Riostream.h>
102 #include "AliDigitizer.h"
103 #include "AliHeader.h"
104 #include "AliLoader.h"
105 #include "AliMergeCombi.h"
107 #include "AliRunDigitizer.h"
108 #include "AliRunLoader.h"
110 ClassImp(AliRunDigitizer)
112 const TString AliRunDigitizer::fgkDefOutFolderName("Output");
113 const TString AliRunDigitizer::fgkBaseInFolderName("Input");
116 //_______________________________________________________________________
117 AliRunDigitizer::AliRunDigitizer(): TTask("AliRunDigitizer","The manager for Merging"),
122 fNrOfEventsToWrite(0),
123 fNrOfEventsWritten(0),
124 fCopyTreesFromInput(0),
129 fOutputInitialized(kFALSE),
132 fCombinationFileName(0)
135 // root requires default ctor, where no new objects can be created
136 // do not use this ctor, it is supplied only for root needs
137 for (Int_t i=0; i<MAXSTREAMSTOMERGE; ++i) fkMASK[i]=0;
140 //_______________________________________________________________________
141 AliRunDigitizer::AliRunDigitizer(Int_t nInputStreams, Int_t sperb):
142 TTask("AliRunDigitizer","The manager for Merging"),
143 fkMASKSTEP(10000000),
147 fNrOfEventsToWrite(-1),
148 fNrOfEventsWritten(0),
149 fCopyTreesFromInput(-1),
150 fNinputs(nInputStreams),
152 fInputStreams(new TClonesArray("AliStream",nInputStreams)),
154 fOutputInitialized(kFALSE),
155 fCombi(new AliMergeCombi(nInputStreams,sperb)),
156 fCombination(MAXSTREAMSTOMERGE),
157 fCombinationFileName(0)
161 // ctor which should be used to create
162 // a manager for merging/digitization
164 if (nInputStreams == 0)
166 AliFatal("Specify nr of input streams");
173 for (i=1;i<MAXSTREAMSTOMERGE;i++)
175 fkMASK[i] = fkMASK[i-1] + fkMASKSTEP;
178 TClonesArray &lInputStreams = *fInputStreams;
180 new(lInputStreams[0]) AliStream(fgkBaseInFolderName+(Long_t)0,"UPDATE");
181 for (i=1;i<nInputStreams;i++) {
182 new(lInputStreams[i]) AliStream(fgkBaseInFolderName+(Long_t)i,"READ");
185 //_______________________________________________________________________
187 void AliRunDigitizer::Copy(TObject&) const
190 // Non implemented copy function
192 AliFatal("Not installed");
195 //_______________________________________________________________________
197 AliRunDigitizer::~AliRunDigitizer()
202 delete fInputStreams;
204 delete fOutRunLoader;
206 //_______________________________________________________________________
207 void AliRunDigitizer::AddDigitizer(AliDigitizer *digitizer)
210 // add digitizer to the list of active digitizers
212 this->Add(digitizer);
214 //_______________________________________________________________________
215 void AliRunDigitizer::SetInputStream(Int_t i, const char *inputFile, TString foldername)
218 // Sets the name of the input file
220 if (i > fInputStreams->GetLast()) {
221 AliError("Input stream number too high");
224 AliStream * stream = static_cast<AliStream*>(fInputStreams->At(i)) ;
225 if ( !foldername.IsNull() ) {
227 foldername += i ; // foldername should stay unchanged for the default output
228 stream->SetFolderName(foldername) ;
230 stream->AddFile(inputFile);
233 //_______________________________________________________________________
234 void AliRunDigitizer::Digitize(Option_t* option)
236 // get a new combination of inputs, loads events to folders
238 // take gAlice from the first input file. It is needed to access
240 // If gAlice is already in memory, use it
242 if (!static_cast<AliStream*>(fInputStreams->At(0))->ImportgAlice())
244 AliError("Error occured while getting gAlice from Input 0");
248 if (!InitGlobal()) //calls Init() for all (sub)digitizers
250 AliError("InitGlobal returned error");
254 Int_t eventsCreated = 0;
255 // loop until there is anything on the input in case fNrOfEventsToWrite < 0
256 while ((eventsCreated++ < fNrOfEventsToWrite) || (fNrOfEventsToWrite < 0))
258 if (!ConnectInputTrees()) break;
259 InitEvent();//this must be after call of Connect Input tress.
262 fOutRunLoader->SetEventNumber(eventsCreated-1);
264 static_cast<AliStream*>(fInputStreams->At(0))->ImportgAlice(); // use gAlice of the first input stream
265 ExecuteTasks(option);// loop over all registered digitizers and let them do the work
272 //_______________________________________________________________________
273 Bool_t AliRunDigitizer::ConnectInputTrees()
278 Int_t eventNr[MAXSTREAMSTOMERGE], delta[MAXSTREAMSTOMERGE];
279 fCombi->Combination(eventNr, delta);
280 for (Int_t i=0;i<fNinputs;i++)
284 AliStream *iStream = static_cast<AliStream*>(fInputStreams->At(i));//gets the "i" defined in combination
285 if (!iStream->NextEventInStream()) return kFALSE; //sets serial number
287 else if (delta[i] != 0)
289 AliError("Only delta 0 or 1 is implemented");
297 //_______________________________________________________________________
298 Bool_t AliRunDigitizer::InitGlobal()
301 // Method called once before Digitize() is called
302 // initialize digitizers and output
304 fOutputInitialized = kFALSE;
305 TList* subTasks = this->GetListOfTasks();
307 TIter next(subTasks);
308 while (AliDigitizer * dig = (AliDigitizer *) next())
314 //_______________________________________________________________________
316 void AliRunDigitizer::SetOutputFile(TString fn)
319 // The output will be to separate file,
320 // not to the signal file here should be protection
321 //to avoid setting the same file as any input
323 AliInfo(Form("Setting Output File Name %s ",fn.Data()));
324 fOutputFileName = fn;
325 // InitOutputGlobal();
328 //_______________________________________________________________________
329 Bool_t AliRunDigitizer::InitOutputGlobal()
332 // Creates the output file, called by InitEvent()
333 // Needs to be called after all inputs are opened
335 if (fOutputInitialized) return kTRUE;
337 if ( !fOutputFileName.IsNull())
339 fOutRunLoader = AliRunLoader::Open(fOutputFileName,fgkDefOutFolderName,"recreate");
341 if (fOutRunLoader == 0x0)
343 AliError("Can not open output");
346 Info("InitOutputGlobal", " 1 %s = ", GetInputFolderName(0).Data()) ;
347 AliRunLoader* inrl = AliRunLoader::GetRunLoader(GetInputFolderName(0));
350 AliError("Can not get Run Loader Input 0. Maybe yet not initialized?");
353 AliDebug(2, Form(" 2 %p = ", inrl)) ;
355 //Copy all detector loaders from input 0 to output
356 const TObjArray* inloaders = inrl->GetArrayOfLoaders();
357 TIter next(inloaders);
359 while((loader = (AliLoader*)next()))
361 AliLoader* cloneloader = (AliLoader*)loader->Clone();
362 cloneloader->Register(fOutRunLoader->GetEventFolder());//creates folders for this loader in Output Folder Structure
363 GetOutRunLoader()->AddLoader(cloneloader);//adds a loader for output
366 fOutRunLoader->MakeTree("E");
368 AliDebug(3,Form("file %s was opened.",fOutputFileName.Data()));
370 fOutputInitialized = kTRUE;
373 //_______________________________________________________________________
375 void AliRunDigitizer::InitEvent()
378 // redirects output properly
380 AliDebug(3,Form("fEvent = %d",fEvent));
381 AliDebug(3,Form("fOutputFileName \"%s\"",fOutputFileName.Data()));
382 if (fOutputInitialized == kFALSE) InitOutputGlobal();
384 // if fOutputFileName was not given, write output to signal directory
386 //_______________________________________________________________________
388 void AliRunDigitizer::FinishEvent()
391 // called at the end of loop over digitizers
394 if (GetOutRunLoader() == 0x0)
396 AliError("fOutRunLoader is null");
401 fNrOfEventsWritten++;
405 AliRunLoader* inrl = AliRunLoader::GetRunLoader(GetInputFolderName(0));
406 AliHeader* outheader = fOutRunLoader->GetHeader();
407 AliHeader* inheader = inrl->GetHeader();
411 inheader = inrl->GetHeader();
412 if (inheader == 0x0) AliFatal("Can not get header from input 0");
415 outheader->SetNprimary(inheader->GetNprimary());
416 outheader->SetNtrack(inheader->GetNtrack());
417 outheader->SetEvent(inheader->GetEvent());
418 outheader->SetEventNrInRun(inheader->GetEventNrInRun());
419 outheader->SetNvertex(inheader->GetNvertex());
420 fOutRunLoader->TreeE()->Fill();
423 if (fCopyTreesFromInput > -1)
425 //this is sensless since no information would be coherent in case of merging
427 AliWarning("Copy trees from input: Copy or link files manually");
431 //_______________________________________________________________________
433 void AliRunDigitizer::FinishGlobal()
436 // called at the end of Exec
437 // save unique objects to the output file
439 if (GetOutRunLoader() == 0x0)
441 AliError("Can not get RunLoader from Output Stream folder");
444 GetOutRunLoader()->CdGAFile();
445 this->Write(NULL, TObject::kOverwrite);
448 fOutRunLoader->WriteHeader("OVERWRITE");
449 fOutRunLoader->WriteRunLoader("OVERWRITE");
450 TFolder* outfolder = fOutRunLoader->GetEventFolder();
451 if (outfolder == 0x0)
453 AliError("Can not get Event Folder");
457 AliRunLoader* inRN = AliRunLoader::GetRunLoader(GetInputFolderName(0));
458 outfolder->Add(inRN->GetAliRun());
459 fOutRunLoader->WriteAliRun("OVERWRITE");
462 if (fCopyTreesFromInput > -1)
464 //copy files manually
467 //_______________________________________________________________________
469 Int_t AliRunDigitizer::GetNParticles(Int_t event) const
471 // return number of particles in all input files for a given
472 // event (as numbered in the output file)
473 // return -1 if some file cannot be accessed
477 for (Int_t i = 0; i < fNinputs; i++) {
478 sumI = GetNParticles(GetInputEventNumber(event,i), i);
479 if (sumI < 0) return -1;
484 //_______________________________________________________________________
486 Int_t AliRunDigitizer::GetNParticles(Int_t /*event*/, Int_t /*input*/) const
488 // return number of particles in input file input for a given
489 // event (as numbered in this input file)
490 // return -1 if some error
492 // Must be revised in the version with AliStream
498 //_______________________________________________________________________
499 Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event) const
501 // return pointer to an int array with input event numbers which were
502 // merged in the output event event
504 // simplified for now, implement later
505 Int_t * a = new Int_t[MAXSTREAMSTOMERGE];
506 for (Int_t i = 0; i < fNinputs; i++) {
511 //_______________________________________________________________________
512 Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t /*input*/) const
514 // return an event number of an eventInput from input file input
515 // which was merged to create output event event
517 // simplified for now, implement later
520 //_______________________________________________________________________
521 TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event) const
523 // return pointer to particle with index i (index with mask)
526 Int_t input = i/fkMASKSTEP;
527 return GetParticle(i,input,GetInputEventNumber(event,input));
530 //_______________________________________________________________________
531 TParticle* AliRunDigitizer::GetParticle(Int_t /*i*/, Int_t /*input*/, Int_t /*event*/) const
533 // return pointer to particle with index i in the input file input
534 // (index without mask)
535 // event is the event number in the file input
536 // return 0 i fit does not exist
538 // Must be revised in the version with AliStream
543 //_______________________________________________________________________
544 void AliRunDigitizer::ExecuteTask(Option_t* option)
547 // overwrite ExecuteTask to do Digitize only
549 if (!IsActive()) return;
551 fHasExecuted = kTRUE;
555 //_______________________________________________________________________
556 const TString& AliRunDigitizer::GetInputFolderName(Int_t i) const
559 // Get the input Folder Name
561 AliStream* stream = dynamic_cast<AliStream*>(fInputStreams->At(i));
563 AliFatal(Form("Can not get the input stream. Index = %d. Exiting",i));
564 return stream->GetFolderName();
566 //_______________________________________________________________________
568 const char* AliRunDigitizer::GetOutputFolderName()
571 // Get output folder name
573 return GetOutRunLoader()->GetEventFolder()->GetName();
575 //_______________________________________________________________________
577 AliRunLoader* AliRunDigitizer::GetOutRunLoader()
580 // Returns Run Loader
582 if (fOutRunLoader) return fOutRunLoader;
584 if ( fOutputFileName.IsNull() )
585 {//guard that sombody calls it without settting file name
586 AliDebug(1,"Output file name is empty. Using Input 0 for output");
587 return AliRunLoader::GetRunLoader(GetInputFolderName(0));
589 // InitOutputGlobal();
590 return fOutRunLoader;
592 //_______________________________________________________________________
594 TString AliRunDigitizer::GetInputFileName(Int_t input, Int_t order) const
596 // returns file name of the order-th file in the input stream input
597 // returns empty string if such file does not exist
598 // first input stream is 0
599 // first file in the input stream is 0
600 TString fileName("");
601 if (input >= fNinputs) return fileName;
602 AliStream * stream = static_cast<AliStream*>(fInputStreams->At(input));
603 if (order > stream->GetNInputFiles()) return fileName;
604 fileName = stream->GetFileName(order);