]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/util/AliHLTFilePublisher.cxx
correct handling of multiple output data types
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTFilePublisher.cxx
index d8143df5f73da8943e6988e403e48ab3815a840b..6bfb774b48ed8194dd8f68f60c8174601dc7cfbb 100644 (file)
@@ -26,12 +26,11 @@ using namespace std;
 #endif
 
 #include "AliHLTFilePublisher.h"
+#include "AliLog.h"
 #include <TObjString.h>
 #include <TMath.h>
 #include <TFile.h>
 
-/** the global object for component registration */
-AliHLTFilePublisher gAliHLTFilePublisher;
 
 /** ROOT macro for the implementation of ROOT specific class methods */
 ClassImp(AliHLTFilePublisher)
@@ -41,7 +40,9 @@ AliHLTFilePublisher::AliHLTFilePublisher()
   AliHLTDataSource(),
   fpCurrent(NULL),
   fEvents(),
-  fMaxSize(0)
+  fMaxSize(0),
+  fOpenFilesAtStart(false),
+  fOutputDataTypes()
 {
   // see header file for class documentation
   // or
@@ -71,7 +72,16 @@ const char* AliHLTFilePublisher::GetComponentID()
 AliHLTComponentDataType AliHLTFilePublisher::GetOutputDataType()
 {
   // see header file for class documentation
-  return kAliHLTVoidDataType;
+  if (fOutputDataTypes.size()==0) return kAliHLTVoidDataType;
+  else if (fOutputDataTypes.size()==1) return fOutputDataTypes[0];
+  return kAliHLTMultipleDataType;
+}
+
+int AliHLTFilePublisher::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
+{
+  tgtList.assign(fOutputDataTypes.begin(), fOutputDataTypes.end());
+  HLTInfo("%s %p provides %d output data types", GetComponentID(), this, fOutputDataTypes.size());
+  return fOutputDataTypes.size();
 }
 
 void AliHLTFilePublisher::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
@@ -97,6 +107,7 @@ int AliHLTFilePublisher::DoInit( int argc, const char** argv )
   int bMissingParam=0;
   int bHaveDatatype=0;
   int bHaveSpecification=0;
+  fOpenFilesAtStart = false;
   AliHLTComponentDataType currDataType=kAliHLTVoidDataType;
   AliHLTUInt32_t          currSpecification=kAliHLTVoidDataSpec;
   EventFiles*             pCurrEvent=NULL;
@@ -128,12 +139,17 @@ int AliHLTFilePublisher::DoInit( int argc, const char** argv )
       memcpy(&currDataType.fID, argv[i], TMath::Min(kAliHLTComponentDataTypefIDsize, (Int_t)strlen(argv[i])));
       if ((bMissingParam=(++i>=argc))) break;
       memcpy(&currDataType.fOrigin, argv[i], TMath::Min(kAliHLTComponentDataTypefOriginSize, (Int_t)strlen(argv[i])));
+
+      // add all different data types to the list
+      AliHLTComponentDataTypeList::iterator element=fOutputDataTypes.begin();
+      while (element!=fOutputDataTypes.end() && *element!=currDataType) element++;
+      if (element==fOutputDataTypes.end()) fOutputDataTypes.push_back(currDataType);
+
       if (bHaveDatatype==0 && pCurrEvent && iResult>=0) {
        // this is a workaround to make old tutorials working which contain
        // the arguments in the wrong sequence
        TList& files=*pCurrEvent; // type conversion operator defined
        TObjLink *flnk=files.FirstLink();
-       int eventSize=0;
        while (flnk) {
          FileDesc* pFileDesc=dynamic_cast<FileDesc*>(flnk->GetObject());
          if (pFileDesc) {
@@ -163,7 +179,6 @@ int AliHLTFilePublisher::DoInit( int argc, const char** argv )
        // the arguments in the wrong sequence
        TList& files=*pCurrEvent; // type conversion operator defined
        TObjLink *flnk=files.FirstLink();
-       int eventSize=0;
        while (flnk) {
          FileDesc* pFileDesc=dynamic_cast<FileDesc*>(flnk->GetObject());
          if (pFileDesc) {
@@ -176,6 +191,8 @@ int AliHLTFilePublisher::DoInit( int argc, const char** argv )
       // -nextevent
     } else if (argument.CompareTo("-nextevent")==0) {
       InsertEvent(pCurrEvent);
+    } else if (argument.CompareTo("-open_files_at_start")==0) {
+      fOpenFilesAtStart = true;
     } else {
       if ((iResult=ScanArgument(argc-i, &argv[i]))==-EINVAL) {
        HLTError("unknown argument %s", argument.Data());
@@ -199,7 +216,7 @@ int AliHLTFilePublisher::DoInit( int argc, const char** argv )
     HLTError("the publisher needs at least one file argument");
     iResult=-EINVAL;
   }
-  if (iResult>=0) iResult=OpenFiles();
+  if (iResult>=0) iResult=OpenFiles(fOpenFilesAtStart);
   if (iResult<0) {
     fEvents.Clear();
   }
@@ -208,6 +225,7 @@ int AliHLTFilePublisher::DoInit( int argc, const char** argv )
 
 int AliHLTFilePublisher::InsertFile(EventFiles* &pCurrEvent, FileDesc* pDesc)
 {
+  // see header file for class documentation
   int iResult=0;
   if (pDesc) {
     if (pCurrEvent==NULL) {
@@ -229,6 +247,7 @@ int AliHLTFilePublisher::InsertFile(EventFiles* &pCurrEvent, FileDesc* pDesc)
 
 int AliHLTFilePublisher::InsertEvent(EventFiles* &pEvent)
 {
+  // see header file for class documentation
   int iResult=0;
   if (pEvent) {
     HLTDebug("Inserted event %p", pEvent);
@@ -246,10 +265,10 @@ int AliHLTFilePublisher::ScanArgument(int argc, const char** argv)
   if (argc==0 && argv==NULL) {
     // this is just to get rid of the warning "unused parameter"
   }
-  return -EPROTO;
+  return -EINVAL;
 }
 
-int AliHLTFilePublisher::OpenFiles()
+int AliHLTFilePublisher::OpenFiles(bool keepOpen)
 {
   // see header file for class documentation
   int iResult=0;
@@ -265,6 +284,7 @@ int AliHLTFilePublisher::OpenFiles()
        FileDesc* pFileDesc=dynamic_cast<FileDesc*>(flnk->GetObject());
        if (pFileDesc) {
          int size=pFileDesc->OpenFile();
+         if (not keepOpen) pFileDesc->CloseFile();
          if (size<0) {
            iResult=size;
            HLTError("can not open file %s", pFileDesc->GetName());
@@ -299,6 +319,7 @@ int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& /*evtData*/,
                                   AliHLTUInt32_t& size,
                                   vector<AliHLTComponentBlockData>& outputBlocks )
 {
+  // see header file for class documentation
   int iResult=0;
   TObjLink *lnk=fpCurrent;
   if (lnk==NULL) lnk=fEvents.FirstLink();
@@ -312,6 +333,7 @@ int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& /*evtData*/,
       int iTotalSize=0;
       while (flnk && iResult>=0) {
        FileDesc* pFileDesc=dynamic_cast<FileDesc*>(flnk->GetObject());
+       if (not fOpenFilesAtStart) pFileDesc->OpenFile();
        TFile* pFile=NULL;
        if (pFileDesc && (pFile=*pFileDesc)!=NULL) {
          int iCopy=pFile->GetSize();
@@ -330,12 +352,17 @@ int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& /*evtData*/,
              bd.fSpecification=*pFileDesc; // type conversion operator defined
              outputBlocks.push_back(bd);
              iTotalSize+=iCopy;
+//           TString msg;
+//           msg.Form("get file %s ", pFile->GetName());
+//           msg+="data type \'%s\'";
+//           PrintDataTypeContent(bd.fDataType, msg.Data());
            }
          } else {
            // output buffer too small, update GetOutputDataSize for the second trial
            fMaxSize=iCopy;
            iResult=-ENOSPC;
          }
+         if (not fOpenFilesAtStart) pFileDesc->CloseFile();
        } else {
          HLTError("no file available");
          iResult=-EFAULT;
@@ -373,21 +400,51 @@ AliHLTFilePublisher::FileDesc::FileDesc(const char* name, AliHLTComponentDataTyp
   fDataType(dt),
   fSpecification(spec)
 {
+  // see header file for class documentation
+  // or
+  // refer to README to build package
+  // or
+  // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
 }
+
 AliHLTFilePublisher::FileDesc::~FileDesc()
 {
-  if (fpInstance) delete fpInstance;
+  // see header file for class documentation
+  CloseFile();
+}
+
+void AliHLTFilePublisher::FileDesc::CloseFile()
+{
+  // see header file for class documentation
+  if (fpInstance)
+  {
+    // Unfortunately had to use AliLog mechanisms rather that AliHLTLogging because
+    // AliHLTFilePublisher::FileDesc does not derive from AliHLTLogging. It would
+    // become a rather heavy class if it did.
+#ifdef __DEBUG
+    AliDebugGeneral("AliHLTFilePublisher::FileDesc",
+      2, Form("File %s has been closed.", fName.Data())
+    );
+#endif
+    delete fpInstance;
+  }
   fpInstance=NULL;
 }
 
 int AliHLTFilePublisher::FileDesc::OpenFile()
 {
+  // see header file for class documentation
   int iResult=0;
   TString fullFN= fName + "?filetype=raw";
   fpInstance = new TFile(fullFN);
   if (fpInstance) {
     if (fpInstance->IsZombie()==0) {
       iResult=fpInstance->GetSize();
+#ifdef __DEBUG
+      AliDebugGeneral("AliHLTFilePublisher::FileDesc",
+        2, Form("File %s has been opened.", fName.Data())
+      );
+#endif
     } else {
       iResult=-ENOENT;
     }