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 **************************************************************************/
17 // Author: Andrei Gheata, 31/05/2006
19 //==============================================================================
20 // AliAnalysysDataContainer - Container of data of arbitrary type deriving
21 // from TObject used for analysis. A container must be connected to the
22 // output data slot of a single analysis task (producer) , but also as
23 // input slot for possibly several other tasks (consumers). The connected
24 // slots must enforce the same data type as the container (or a derived type).
25 // A container becomes the owner of the contained data once this was produced.
27 // Containers should be defined by the analysis module using:
29 // AliAnalysisModule::AddContainer(const char *name, TClass *type);
31 // A container should be connected to a producer:
33 // AliAnalysisModule::ConnectOutput(AliAnalysisTask *task,
34 // AliAnalysisDataContainer *cont)
35 // and to its consumers:
37 // AliAnalysisModule::ConnectInput(AliAnalysisTask *task, Int_t islot,
38 // AliAnalysisDataContainer *cont)
40 // The container will create an implicit connection between the producer task
41 // and all consumers, which will become sub-tasks of the producer.
43 //==============================================================================
45 #include <Riostream.h>
46 #include <TMethodCall.h>
55 #include "AliAnalysisManager.h"
56 #include "AliAnalysisDataContainer.h"
57 #include "AliAnalysisDataSlot.h"
58 #include "AliAnalysisTask.h"
62 ClassImp(AliAnalysisDataContainer)
64 //______________________________________________________________________________
65 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
79 //______________________________________________________________________________
80 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
92 // Default constructor.
93 SetTitle(fType->GetName());
96 //______________________________________________________________________________
97 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
99 fDataReady(cont.fDataReady),
101 fFileName(cont.fFileName),
102 fFolderName(cont.fFolderName),
106 fProducer(cont.fProducer),
111 if (cont.fConsumers) {
112 fConsumers = new TObjArray(2);
113 Int_t ncons = cont.fConsumers->GetEntriesFast();
114 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
118 //______________________________________________________________________________
119 AliAnalysisDataContainer::~AliAnalysisDataContainer()
121 // Destructor. Deletes data ! (What happens if data is a container ???)
122 if (fData && fOwnedData) delete fData;
123 if (fConsumers) delete fConsumers;
126 //______________________________________________________________________________
127 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
131 TNamed::operator=(cont);
132 fDataReady = cont.fDataReady;
134 fFileName = cont.fFileName;
135 fFolderName = cont.fFolderName;
139 fProducer = cont.fProducer;
140 if (cont.fConsumers) {
141 fConsumers = new TObjArray(2);
142 Int_t ncons = cont.fConsumers->GetEntriesFast();
143 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
149 //______________________________________________________________________________
150 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
152 // Add a consumer for contained data;
153 AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
154 if (!slot || !slot->GetType()) {
155 cout<<"Consumer task "<< consumer->GetName()<<" does not have an input/type #"<<islot<<endl;
156 //AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
159 if (!slot->GetType()->InheritsFrom(GetType())) {
160 cout<<"Data type "<<slot->GetTitle()<<" for input slot "<<islot<<" of task "<<consumer->GetName()<<" does not match container type "<<GetTitle()<<endl;
161 //AliError(Form("Data type %s for input slot %i of task %s does not match container type %s", slot->GetType()->GetName(),islot,consumer->GetName(),fType->GetName()));
165 if (!fConsumers) fConsumers = new TObjArray(2);
166 fConsumers->Add(consumer);
167 // Add the consumer task to the list of task of the producer
168 if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer))
169 fProducer->Add(consumer);
172 //______________________________________________________________________________
173 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
175 // Check if all client tasks have executed.
176 TIter next(fConsumers);
177 AliAnalysisTask *task;
178 while ((task=(AliAnalysisTask*)next())) {
179 if (!task->HasExecuted()) return kFALSE;
184 //______________________________________________________________________________
185 void AliAnalysisDataContainer::DeleteData()
187 // Delete data if not needed anymore.
188 if (!fDataReady || !ClientsExecuted()) {
189 cout<<"Data not ready or not all clients of container "<<GetName()<<" executed. Data not deleted."<<endl;
190 //AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
194 cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
195 //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
203 //______________________________________________________________________________
204 TClass *AliAnalysisDataContainer::GetType() const
206 // Get class type for this slot.
207 AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)this;
208 if (!fType) cont->SetType(gROOT->GetClass(fTitle.Data()));
209 if (!fType) printf("AliAnalysisDataContainer: Unknown class: %s\n", GetTitle());
213 //______________________________________________________________________________
214 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
216 // If data is ready and derives from TTree or from TBranch, this will get the
217 // requested entry in memory if not already loaded.
218 if (!fDataReady || !GetType()) return;
219 Bool_t istree = fType->InheritsFrom(TTree::Class());
221 TTree *tree = (TTree*)fData;
222 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
225 Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
227 TBranch *branch = (TBranch*)fData;
228 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
233 //______________________________________________________________________________
234 Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
236 // Merge a list of containers with this one. Containers in the list must have
237 // data of the same type.
238 if (!list || !fData) return 0;
239 printf("Merging %d containers %s\n", list->GetSize()+1, GetName());
242 callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
243 if (!callEnv.IsValid() && !list->IsEmpty()) {
244 cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
248 if (list->IsEmpty()) return 1;
251 AliAnalysisDataContainer *cont;
252 // Make a list where to temporary store the data to be merged.
253 TList *collectionData = new TList();
254 Int_t count = 0; // object counter
255 while ((cont=(AliAnalysisDataContainer*)next())) {
256 TObject *data = cont->GetData();
258 if (strcmp(cont->GetName(), GetName())) {
259 cout << "Not merging containers with different names !" << endl;
262 printf(" ... merging object %s\n", data->GetName());
263 collectionData->Add(data);
266 callEnv.SetParam((Long_t) collectionData);
267 callEnv.Execute(fData);
268 delete collectionData;
273 //______________________________________________________________________________
274 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
276 // Print info about this container.
278 for (Int_t i=0; i<indent; i++) ind += " ";
281 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
283 printf("%sContainer: %s type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
285 printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
287 printf("%s= No data producer",ind.Data());
288 printf("%s = Consumer tasks: ", ind.Data());
289 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
292 if (fFolderName.Length())
293 printf("Filename: %s folder: %s\n", fFileName.Data(), fFolderName.Data());
295 printf("Filename: %s\n", fFileName.Data());
296 TIter next(fConsumers);
297 AliAnalysisTask *task;
298 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
301 //______________________________________________________________________________
302 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
304 // Set the data as READY only if it was published by the producer.
305 // If there is no producer declared, this is a top level container.
306 AliAnalysisTask *task;
307 Bool_t init = kFALSE;
310 if (data != fData) init = kTRUE;
314 nc = fConsumers->GetEntriesFast();
315 for (i=0; i<nc; i++) {
316 task = (AliAnalysisTask*)fConsumers->At(i);
317 task->CheckNotify(init);
322 // Check if it is the producer who published the data
323 if (fProducer->GetPublishedData()==data) {
327 nc = fConsumers->GetEntriesFast();
328 for (i=0; i<nc; i++) {
329 task = (AliAnalysisTask*)fConsumers->At(i);
335 // Ignore data posting from other than the producer
336 // cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
337 //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));
342 //______________________________________________________________________________
343 void AliAnalysisDataContainer::SetFileName(const char *filename)
345 // The filename field can be actually composed by the actual file name followed
346 // by :dirname (optional):
347 // filename = file_name[:dirname]
348 // No slashes (/) allowed
349 fFileName = filename;
351 Int_t index = fFileName.Index(":");
352 // Fill the folder name
354 fFolderName = fFileName(index+1, fFileName.Length()-index);
355 fFileName.Remove(index);
357 if (!fFileName.Length())
358 Fatal("SetFileName", "Empty file name");
359 if (fFileName.Index("/")>=0)
360 Fatal("SetFileName", "No slashes (/) allowed in the file name");
363 //______________________________________________________________________________
364 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
366 // Set the producer of data. The slot number is required for data type checking.
368 cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
369 //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
372 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
373 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
376 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
378 cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
379 //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
382 if (!slot->GetType()->InheritsFrom(GetType())) {
383 cout<<"Data type "<<slot->GetTitle()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<GetTitle()<<endl;
384 //AliError(Form("Data type %s for output slot %i of task %s does not match container type %s", slot->GetType()->GetName(),islot,prod->GetName(),fType->GetName()));
389 // Add all consumers as daughter tasks
390 TIter next(fConsumers);
391 AliAnalysisTask *cons;
392 while ((cons=(AliAnalysisTask*)next())) {
393 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
397 //______________________________________________________________________________
398 AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
400 // Wraps data for sending it through the net.
401 AliAnalysisDataWrapper *pack = 0;
403 Error("ExportData", "Container %s - No data to be wrapped !", GetName());
406 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
407 if (mgr->GetDebugLevel() > 1) printf(" ExportData: Wrapping data %s for container %s\n", fData->GetName(),GetName());
408 pack = new AliAnalysisDataWrapper(fData);
409 pack->SetName(fName.Data());
413 //______________________________________________________________________________
414 void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
416 // Unwraps data from a data wrapper.
418 fData = pack->Data();
420 Error("ImportData", "No data was wrapped for container %s", GetName());
423 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
424 if (mgr->GetDebugLevel() > 1) printf(" ImportData: Unwrapping data %s for container %s\n", fData->GetName(),GetName());
426 // Imported wrappers do not own data anymore (AG 13-11-07)
427 pack->SetDeleteData(kFALSE);
431 ClassImp (AliAnalysisDataWrapper)
433 //______________________________________________________________________________
434 AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
439 if (data) SetName(data->GetName());
442 //______________________________________________________________________________
443 AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
446 if (fData && TObject::TestBit(kDeleteData)) delete fData;
449 //______________________________________________________________________________
450 AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
453 if (&other != this) {
454 TNamed::operator=(other);
460 //______________________________________________________________________________
461 Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
463 // Merge a list of containers with this one. Containers in the list must have
464 // data of the same type.
465 if (TH1::AddDirectoryStatus()) TH1::AddDirectory(kFALSE);
466 if (!fData) return 0;
467 if (!list || list->IsEmpty()) return 1;
473 callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
474 if (!callEnv.IsValid()) {
475 cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
480 AliAnalysisDataWrapper *cont;
481 // Make a list where to temporary store the data to be merged.
482 TList *collectionData = new TList();
483 Int_t count = 0; // object counter
484 // printf("Wrapper %s 0x%lx (data=%s) merged with:\n", GetName(), (ULong_t)this, fData->ClassName());
485 while ((cont=(AliAnalysisDataWrapper*)next1())) {
486 cont->SetDeleteData();
487 TObject *data = cont->Data();
489 // printf(" - %s 0x%lx (data=%s)\n", cont->GetName(), (ULong_t)cont, data->ClassName());
490 collectionData->Add(data);
493 callEnv.SetParam((Long_t) collectionData);
494 callEnv.Execute(fData);
495 delete collectionData;
500 ClassImp(AliAnalysisFileDescriptor)
502 //______________________________________________________________________________
503 AliAnalysisFileDescriptor::AliAnalysisFileDescriptor()
504 :TObject(), fLfn(), fGUID(), fUrl(), fPfn(), fSE(),
505 fIsArchive(kFALSE), fImage(0), fNreplicas(0),
506 fStartBytes(0), fReadBytes(0), fSize(0), fOpenedAt(0),
507 fOpenTime(0.), fProcessingTime(0.), fThroughput(0.), fTimer()
512 //______________________________________________________________________________
513 AliAnalysisFileDescriptor::AliAnalysisFileDescriptor(const TFile *file)
514 :TObject(), fLfn(), fGUID(), fUrl(), fPfn(), fSE(),
515 fIsArchive(kFALSE), fImage(0), fNreplicas(0),
516 fStartBytes(0), fReadBytes(0), fSize(0), fOpenedAt(0),
517 fOpenTime(0.), fProcessingTime(0.), fThroughput(0.), fTimer()
519 // Normal constructor
520 if (file->InheritsFrom("TAlienFile")) {
521 fLfn =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetLfn();", file));
522 fGUID =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetGUID();", file));
523 fUrl =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetUrl();", file));
524 fPfn =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetPfn();", file));
525 fSE = (const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetSE();", file));
526 fImage = (Int_t)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetImage();", file));
527 fNreplicas = (Int_t)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetNreplicas();", file));
528 fOpenedAt = gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetOpenTime();", file));
529 gROOT->ProcessLine(Form("((AliAnalysisFileDescriptor*)%p)->SetOpenTime(((TAlienFile*)%p)->GetElapsed());", this, file));
531 fLfn = file->GetName();
532 fPfn = file->GetName();
533 fUrl = file->GetName();
535 if (!fPfn.BeginsWith("/")) fPfn.Prepend(Form("%s/",gSystem->WorkingDirectory()));
538 fStartBytes = TFile::GetFileBytesRead();
539 fIsArchive = file->IsArchive();
540 fSize = file->GetSize();
543 //______________________________________________________________________________
544 AliAnalysisFileDescriptor::AliAnalysisFileDescriptor(const AliAnalysisFileDescriptor &other)
545 :TObject(other), fLfn(other.fLfn), fGUID(other.fGUID),
546 fUrl(other.fUrl), fPfn(other.fPfn), fSE(other.fSE),
547 fIsArchive(other.fIsArchive), fImage(other.fImage),
548 fNreplicas(other.fNreplicas), fStartBytes(other.fStartBytes), fReadBytes(other.fReadBytes),
549 fSize(other.fSize), fOpenedAt(other.fOpenedAt), fOpenTime(other.fOpenTime),
550 fProcessingTime(other.fProcessingTime), fThroughput(other.fThroughput), fTimer()
555 //______________________________________________________________________________
556 AliAnalysisFileDescriptor &AliAnalysisFileDescriptor::operator=(const AliAnalysisFileDescriptor &other)
559 if (&other == this) return *this;
560 TObject::operator=(other);
566 fIsArchive = other.fIsArchive;
567 fImage = other.fImage;
568 fNreplicas = other.fNreplicas;
569 fStartBytes = other.fStartBytes;;
570 fReadBytes = other.fReadBytes;
572 fOpenedAt = other.fOpenedAt;
573 fOpenTime = other.fOpenTime;
574 fProcessingTime = other.fProcessingTime;
575 fThroughput = other.fThroughput;
579 //______________________________________________________________________________
580 AliAnalysisFileDescriptor::~AliAnalysisFileDescriptor()
585 //______________________________________________________________________________
586 void AliAnalysisFileDescriptor::Done()
588 // Must be called at the end of processing, providing file->GetBytesRead() as argument.
590 const Double_t megabyte = 1048576.;
591 // Long64_t stampnow = time(0);
592 fReadBytes = TFile::GetFileBytesRead()-fStartBytes;
593 // fProcessingTime = stampnow-fOpenedAt;
594 fProcessingTime = fTimer.RealTime();
595 Double_t readsize = fReadBytes/megabyte;
596 fThroughput = readsize/fProcessingTime;
599 //______________________________________________________________________________
600 void AliAnalysisFileDescriptor::Print(Option_t*) const
602 // Print info about the file descriptor
603 const Double_t megabyte = 1048576.;
604 printf("===== Logical file name: %s =====\n", fLfn.Data());
605 printf(" Pfn: %s\n", fPfn.Data());
606 printf(" url: %s\n", fUrl.Data());
607 printf(" access time: %lld from SE: %s image %d/%d\n", fOpenedAt, fSE.Data(), fImage, fNreplicas);
608 printf(" open time: %g [sec]\n", fOpenTime);
609 printf(" file size: %g [MB], read size: %g [MB]\n", fSize/megabyte, fReadBytes/megabyte);
610 printf(" processing time [sec]: %g\n", fProcessingTime);
611 printf(" average throughput: %g [MB/sec]\n", fThroughput);
614 //______________________________________________________________________________
615 void AliAnalysisFileDescriptor::SavePrimitive(ostream &out, Option_t *)
617 // Stream info to file
618 const Double_t megabyte = 1048576.;
619 out << "#################################################################" << endl;
620 out << "pfn " << fPfn.Data() << endl;
621 out << "url " << fUrl.Data() << endl;
622 out << "se " << fSE.Data() << endl;
623 out << "image " << fImage << endl;
624 out << "nreplicas " << fNreplicas << endl;
625 out << "openstamp " << fOpenedAt << endl;
626 out << setiosflags(ios::fixed) << setprecision(3);
627 out << "opentime " << fOpenTime << endl;
628 out << "runtime " << fProcessingTime << endl;
629 out << "filesize " << fSize/megabyte << endl;
630 out << "readsize " << fReadBytes/megabyte << endl;
631 out << "throughput " << fThroughput << endl;