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>
54 #include <TStopwatch.h>
56 #include "AliAnalysisManager.h"
57 #include "AliAnalysisDataContainer.h"
58 #include "AliAnalysisDataSlot.h"
59 #include "AliAnalysisTask.h"
63 ClassImp(AliAnalysisDataContainer)
65 //______________________________________________________________________________
66 AliAnalysisDataContainer::AliAnalysisDataContainer() : TNamed(),
80 //______________________________________________________________________________
81 AliAnalysisDataContainer::AliAnalysisDataContainer(const char *name, TClass *type)
93 // Default constructor.
94 SetTitle(fType->GetName());
97 //______________________________________________________________________________
98 AliAnalysisDataContainer::AliAnalysisDataContainer(const AliAnalysisDataContainer &cont)
100 fDataReady(cont.fDataReady),
102 fFileName(cont.fFileName),
103 fFolderName(cont.fFolderName),
107 fProducer(cont.fProducer),
112 if (cont.fConsumers) {
113 fConsumers = new TObjArray(2);
114 Int_t ncons = cont.fConsumers->GetEntriesFast();
115 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
119 //______________________________________________________________________________
120 AliAnalysisDataContainer::~AliAnalysisDataContainer()
122 // Destructor. Deletes data ! (What happens if data is a container ???)
123 if (fData && fOwnedData) delete fData;
124 if (fConsumers) delete fConsumers;
127 //______________________________________________________________________________
128 AliAnalysisDataContainer &AliAnalysisDataContainer::operator=(const AliAnalysisDataContainer &cont)
132 TNamed::operator=(cont);
133 fDataReady = cont.fDataReady;
135 fFileName = cont.fFileName;
136 fFolderName = cont.fFolderName;
140 fProducer = cont.fProducer;
141 if (cont.fConsumers) {
142 fConsumers = new TObjArray(2);
143 Int_t ncons = cont.fConsumers->GetEntriesFast();
144 for (Int_t i=0; i<ncons; i++) fConsumers->Add(cont.fConsumers->At(i));
150 //______________________________________________________________________________
151 void AliAnalysisDataContainer::AddConsumer(AliAnalysisTask *consumer, Int_t islot)
153 // Add a consumer for contained data;
154 AliAnalysisDataSlot *slot = consumer->GetInputSlot(islot);
155 if (!slot || !slot->GetType()) {
156 cout<<"Consumer task "<< consumer->GetName()<<" does not have an input/type #"<<islot<<endl;
157 //AliError(Form("Consumer task %s does not have an input #%i", consumer->GetName(),islot));
160 if (!slot->GetType()->InheritsFrom(GetType())) {
161 cout<<"Data type "<<slot->GetTitle()<<" for input slot "<<islot<<" of task "<<consumer->GetName()<<" does not match container type "<<GetTitle()<<endl;
162 //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()));
166 if (!fConsumers) fConsumers = new TObjArray(2);
167 fConsumers->Add(consumer);
168 // Add the consumer task to the list of task of the producer
169 if (fProducer && !fProducer->GetListOfTasks()->FindObject(consumer))
170 fProducer->Add(consumer);
173 //______________________________________________________________________________
174 Bool_t AliAnalysisDataContainer::ClientsExecuted() const
176 // Check if all client tasks have executed.
177 TIter next(fConsumers);
178 AliAnalysisTask *task;
179 while ((task=(AliAnalysisTask*)next())) {
180 if (!task->HasExecuted()) return kFALSE;
185 //______________________________________________________________________________
186 void AliAnalysisDataContainer::DeleteData()
188 // Delete data if not needed anymore.
189 if (!fDataReady || !ClientsExecuted()) {
190 cout<<"Data not ready or not all clients of container "<<GetName()<<" executed. Data not deleted."<<endl;
191 //AliWarning(Form("Data not ready or not all clients of container %s executed. Data not deleted.", GetName()));
195 cout<<"Data not owned by container "<<GetName()<<". Not deleted."<<endl;
196 //AliWarning(Form("Data not owned by container %s. Not deleted.", GetName()));
204 //______________________________________________________________________________
205 TClass *AliAnalysisDataContainer::GetType() const
207 // Get class type for this slot.
208 AliAnalysisDataContainer *cont = (AliAnalysisDataContainer*)this;
209 if (!fType) cont->SetType(gROOT->GetClass(fTitle.Data()));
210 if (!fType) printf("AliAnalysisDataContainer: Unknown class: %s\n", GetTitle());
214 //______________________________________________________________________________
215 void AliAnalysisDataContainer::GetEntry(Long64_t ientry)
217 // If data is ready and derives from TTree or from TBranch, this will get the
218 // requested entry in memory if not already loaded.
219 if (!fDataReady || !GetType()) return;
220 Bool_t istree = fType->InheritsFrom(TTree::Class());
222 TTree *tree = (TTree*)fData;
223 if (tree->GetReadEntry() != ientry) tree->GetEntry(ientry);
226 Bool_t isbranch = fType->InheritsFrom(TBranch::Class());
228 TBranch *branch = (TBranch*)fData;
229 if (branch->GetReadEntry() != ientry) branch->GetEntry(ientry);
234 //______________________________________________________________________________
235 Long64_t AliAnalysisDataContainer::Merge(TCollection *list)
237 // Merge a list of containers with this one. Containers in the list must have
238 // data of the same type.
239 if (!list || !fData) return 0;
240 printf("Merging %d containers %s\n", list->GetSize()+1, GetName());
243 callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
244 if (!callEnv.IsValid() && !list->IsEmpty()) {
245 cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
249 if (list->IsEmpty()) return 1;
252 AliAnalysisDataContainer *cont;
253 // Make a list where to temporary store the data to be merged.
254 TList *collectionData = new TList();
255 Int_t count = 0; // object counter
256 while ((cont=(AliAnalysisDataContainer*)next())) {
257 TObject *data = cont->GetData();
259 if (strcmp(cont->GetName(), GetName())) {
260 cout << "Not merging containers with different names !" << endl;
263 printf(" ... merging object %s\n", data->GetName());
264 collectionData->Add(data);
267 callEnv.SetParam((Long_t) collectionData);
268 callEnv.Execute(fData);
269 delete collectionData;
274 //______________________________________________________________________________
275 void AliAnalysisDataContainer::PrintContainer(Option_t *option, Int_t indent) const
277 // Print info about this container.
279 for (Int_t i=0; i<indent; i++) ind += " ";
282 Bool_t dep = (opt.Contains("dep"))?kTRUE:kFALSE;
284 printf("%sContainer: %s type: %s POST_LOOP=%i", ind.Data(), GetName(), GetTitle(), IsPostEventLoop());
286 printf("%s = Data producer: task %s",ind.Data(),fProducer->GetName());
288 printf("%s= No data producer",ind.Data());
289 printf("%s = Consumer tasks: ", ind.Data());
290 if (!fConsumers || !fConsumers->GetEntriesFast()) printf("-none-\n");
293 if (fFolderName.Length())
294 printf("Filename: %s folder: %s\n", fFileName.Data(), fFolderName.Data());
296 printf("Filename: %s\n", fFileName.Data());
297 TIter next(fConsumers);
298 AliAnalysisTask *task;
299 while ((task=(AliAnalysisTask*)next())) task->PrintTask(option, indent+3);
302 //______________________________________________________________________________
303 Bool_t AliAnalysisDataContainer::SetData(TObject *data, Option_t *)
305 // Set the data as READY only if it was published by the producer.
306 // If there is no producer declared, this is a top level container.
307 AliAnalysisTask *task;
308 Bool_t init = kFALSE;
311 if (data != fData) init = kTRUE;
315 nc = fConsumers->GetEntriesFast();
316 for (i=0; i<nc; i++) {
317 task = (AliAnalysisTask*)fConsumers->At(i);
318 task->CheckNotify(init);
323 // Check if it is the producer who published the data
324 if (fProducer->GetPublishedData()==data) {
328 nc = fConsumers->GetEntriesFast();
329 for (i=0; i<nc; i++) {
330 task = (AliAnalysisTask*)fConsumers->At(i);
336 // Ignore data posting from other than the producer
337 // cout<<"Data for container "<<GetName()<<" can be published only by producer task "<<fProducer->GetName()<<endl;
338 //AliWarning(Form("Data for container %s can be published only by producer task %s", GetName(), fProducer->GetName()));
343 //______________________________________________________________________________
344 void AliAnalysisDataContainer::SetFileName(const char *filename)
346 // The filename field can be actually composed by the actual file name followed
347 // by :dirname (optional):
348 // filename = file_name[:dirname]
349 // No slashes (/) allowed
350 fFileName = filename;
352 Int_t index = fFileName.Index(":");
353 // Fill the folder name
355 fFolderName = fFileName(index+1, fFileName.Length()-index);
356 fFileName.Remove(index);
358 if (!fFileName.Length())
359 Fatal("SetFileName", "Empty file name");
360 if (fFileName.Index("/")>=0)
361 Fatal("SetFileName", "No slashes (/) allowed in the file name");
364 //______________________________________________________________________________
365 void AliAnalysisDataContainer::SetProducer(AliAnalysisTask *prod, Int_t islot)
367 // Set the producer of data. The slot number is required for data type checking.
369 cout<<"Data container "<<GetName()<<" already has a producer: "<<fProducer->GetName()<<endl;
370 //AliWarning(Form("Data container %s already has a producer: %s",GetName(),fProducer->GetName()));
373 cout<<GetName()<<" container contains data - cannot change producer!"<<endl;
374 //AliError(Form("%s container contains data - cannot change producer!", GetName()));
377 AliAnalysisDataSlot *slot = prod->GetOutputSlot(islot);
379 cout<<"Producer task "<<prod->GetName()<<" does not have an output #"<<islot<<endl;
380 //AliError(Form("Producer task %s does not have an output #%i", prod->GetName(),islot));
383 if (!slot->GetType()->InheritsFrom(GetType())) {
384 cout<<"Data type "<<slot->GetTitle()<<"for output slot "<<islot<<" of task "<<prod->GetName()<<" does not match container type "<<GetTitle()<<endl;
385 //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()));
390 // Add all consumers as daughter tasks
391 TIter next(fConsumers);
392 AliAnalysisTask *cons;
393 while ((cons=(AliAnalysisTask*)next())) {
394 if (!prod->GetListOfTasks()->FindObject(cons)) prod->Add(cons);
398 //______________________________________________________________________________
399 AliAnalysisDataWrapper *AliAnalysisDataContainer::ExportData() const
401 // Wraps data for sending it through the net.
402 AliAnalysisDataWrapper *pack = 0;
404 Error("ExportData", "Container %s - No data to be wrapped !", GetName());
407 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
408 if (mgr->GetDebugLevel() > 1) printf(" ExportData: Wrapping data %s for container %s\n", fData->GetName(),GetName());
409 pack = new AliAnalysisDataWrapper(fData);
410 pack->SetName(fName.Data());
414 //______________________________________________________________________________
415 void AliAnalysisDataContainer::ImportData(AliAnalysisDataWrapper *pack)
417 // Unwraps data from a data wrapper.
419 fData = pack->Data();
421 Error("ImportData", "No data was wrapped for container %s", GetName());
424 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
425 if (mgr->GetDebugLevel() > 1) printf(" ImportData: Unwrapping data %s for container %s\n", fData->GetName(),GetName());
427 // Imported wrappers do not own data anymore (AG 13-11-07)
428 pack->SetDeleteData(kFALSE);
432 ClassImp (AliAnalysisDataWrapper)
434 //______________________________________________________________________________
435 AliAnalysisDataWrapper::AliAnalysisDataWrapper(TObject *data)
440 if (data) SetName(data->GetName());
443 //______________________________________________________________________________
444 AliAnalysisDataWrapper::~AliAnalysisDataWrapper()
447 if (fData && TObject::TestBit(kDeleteData)) delete fData;
450 //______________________________________________________________________________
451 AliAnalysisDataWrapper &AliAnalysisDataWrapper::operator=(const AliAnalysisDataWrapper &other)
454 if (&other != this) {
455 TNamed::operator=(other);
461 //______________________________________________________________________________
462 Long64_t AliAnalysisDataWrapper::Merge(TCollection *list)
464 // Merge a list of containers with this one. Containers in the list must have
465 // data of the same type.
466 if (TH1::AddDirectoryStatus()) TH1::AddDirectory(kFALSE);
467 if (!fData) return 0;
468 if (!list || list->IsEmpty()) return 1;
474 callEnv.InitWithPrototype(fData->IsA(), "Merge", "TCollection*");
475 if (!callEnv.IsValid()) {
476 cout << "No merge interface for data stored by " << GetName() << ". Merging not possible !" << endl;
481 AliAnalysisDataWrapper *cont;
482 // Make a list where to temporary store the data to be merged.
483 TList *collectionData = new TList();
484 Int_t count = 0; // object counter
485 // printf("Wrapper %s 0x%lx (data=%s) merged with:\n", GetName(), (ULong_t)this, fData->ClassName());
486 while ((cont=(AliAnalysisDataWrapper*)next1())) {
487 cont->SetDeleteData();
488 TObject *data = cont->Data();
490 // printf(" - %s 0x%lx (data=%s)\n", cont->GetName(), (ULong_t)cont, data->ClassName());
491 collectionData->Add(data);
494 callEnv.SetParam((Long_t) collectionData);
495 callEnv.Execute(fData);
496 delete collectionData;
501 ClassImp(AliAnalysisFileDescriptor)
503 //______________________________________________________________________________
504 AliAnalysisFileDescriptor::AliAnalysisFileDescriptor()
505 :TObject(), fLfn(), fGUID(), fUrl(), fPfn(), fSE(),
506 fIsArchive(kFALSE), fImage(0), fNreplicas(0),
507 fStartBytes(0), fReadBytes(0), fSize(0), fOpenedAt(0),
508 fOpenTime(0.), fProcessingTime(0.), fThroughput(0.)
513 //______________________________________________________________________________
514 AliAnalysisFileDescriptor::AliAnalysisFileDescriptor(const TFile *file)
515 :TObject(), fLfn(), fGUID(), fUrl(), fPfn(), fSE(),
516 fIsArchive(kFALSE), fImage(0), fNreplicas(0),
517 fStartBytes(0), fReadBytes(0), fSize(0), fOpenedAt(0),
518 fOpenTime(0.), fProcessingTime(0.), fThroughput(0.)
520 // Normal constructor
521 if (file->InheritsFrom("TAlienFile")) {
522 fLfn =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetLfn();", file));
523 fGUID =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetGUID();", file));
524 fUrl =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetUrl();", file));
525 fPfn =(const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetPfn();", file));
526 fSE = (const char*)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetSE();", file));
527 fImage = (Int_t)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetImage();", file));
528 fNreplicas = (Int_t)gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetNreplicas();", file));
529 fOpenedAt = gROOT->ProcessLine(Form("((TAlienFile*)%p)->GetOpenTime();", file));
530 gROOT->ProcessLine(Form("((AliAnalysisFileDescriptor*)%p)->SetOpenTime(((TAlienFile*)%p)->GetElapsed());", this, file));
532 fLfn = file->GetName();
533 fPfn = file->GetName();
534 fUrl = file->GetName();
536 if (!fPfn.BeginsWith("/")) fPfn.Prepend(Form("%s/",gSystem->WorkingDirectory()));
539 fStartBytes = TFile::GetFileBytesRead();
540 fIsArchive = file->IsArchive();
541 fSize = file->GetSize();
544 //______________________________________________________________________________
545 AliAnalysisFileDescriptor::AliAnalysisFileDescriptor(const AliAnalysisFileDescriptor &other)
546 :TObject(other), fLfn(other.fLfn), fGUID(other.fGUID),
547 fUrl(other.fUrl), fPfn(other.fPfn), fSE(other.fSE),
548 fIsArchive(other.fIsArchive), fImage(other.fImage),
549 fNreplicas(other.fNreplicas), fStartBytes(other.fStartBytes), fReadBytes(other.fReadBytes),
550 fSize(other.fSize), fOpenedAt(other.fOpenedAt), fOpenTime(other.fOpenTime),
551 fProcessingTime(other.fProcessingTime), fThroughput(other.fThroughput)
556 //______________________________________________________________________________
557 AliAnalysisFileDescriptor &AliAnalysisFileDescriptor::operator=(const AliAnalysisFileDescriptor &other)
560 if (&other == this) return *this;
561 TObject::operator=(other);
567 fIsArchive = other.fIsArchive;
568 fImage = other.fImage;
569 fNreplicas = other.fNreplicas;
570 fStartBytes = other.fStartBytes;;
571 fReadBytes = other.fReadBytes;
573 fOpenedAt = other.fOpenedAt;
574 fOpenTime = other.fOpenTime;
575 fProcessingTime = other.fProcessingTime;
576 fThroughput = other.fThroughput;
580 //______________________________________________________________________________
581 AliAnalysisFileDescriptor::~AliAnalysisFileDescriptor()
586 //______________________________________________________________________________
587 void AliAnalysisFileDescriptor::Done()
589 // 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 Double_t readsize = fReadBytes/megabyte;
595 fThroughput = readsize/fProcessingTime;
598 //______________________________________________________________________________
599 void AliAnalysisFileDescriptor::Print(Option_t*) const
601 // Print info about the file descriptor
602 const Double_t megabyte = 1048576.;
603 printf("===== Logical file name: %s =====\n", fLfn.Data());
604 printf(" Pfn: %s\n", fPfn.Data());
605 printf(" url: %s\n", fUrl.Data());
606 printf(" access time: %lld from SE: %s image %d/%d\n", fOpenedAt, fSE.Data(), fImage, fNreplicas);
607 printf(" open time: %g [sec]\n", fOpenTime);
608 printf(" file size: %g [MB], read size: %g [MB]\n", fSize/megabyte, fReadBytes/megabyte);
609 printf(" processing time [sec]: %g\n", fProcessingTime);
610 printf(" average throughput: %g [MB/sec]\n", fThroughput);
613 //______________________________________________________________________________
614 void AliAnalysisFileDescriptor::SavePrimitive(ostream &out, Option_t *)
616 // Stream info to file
617 const Double_t megabyte = 1048576.;
618 out << "#################################################################" << endl;
619 out << "pfn " << fPfn.Data() << endl;
620 out << "url " << fUrl.Data() << endl;
621 out << "se " << fSE.Data() << endl;
622 out << "image " << fImage << endl;
623 out << "nreplicas " << fNreplicas << endl;
624 out << "openstamp " << fOpenedAt << endl;
625 out << setiosflags(ios::fixed) << setprecision(3);
626 out << "opentime " << fOpenTime << endl;
627 out << "runtime " << fProcessingTime << endl;
628 out << "filesize " << fSize/megabyte << endl;
629 out << "readsize " << fReadBytes/megabyte << endl;
630 out << "throughput " << fThroughput << endl;