]> git.uio.no Git - u/mrichter/AliRoot.git/commitdiff
The file publisher now closes files when it is done with them during a given event...
authorszostak <szostak@f7af4fe6-9843-0410-8265-dc069ae4e863>
Fri, 28 Sep 2007 17:28:32 +0000 (17:28 +0000)
committerszostak <szostak@f7af4fe6-9843-0410-8265-dc069ae4e863>
Fri, 28 Sep 2007 17:28:32 +0000 (17:28 +0000)
HLT/BASE/util/AliHLTFilePublisher.cxx
HLT/BASE/util/AliHLTFilePublisher.h

index 323c1ee16fc012ee9b101a3c60febd139efedc89..b2bfa4d3b282583825fbb294511da1c4dabbb8d2 100644 (file)
@@ -26,6 +26,7 @@ using namespace std;
 #endif
 
 #include "AliHLTFilePublisher.h"
+#include "AliLog.h"
 #include <TObjString.h>
 #include <TMath.h>
 #include <TFile.h>
@@ -41,7 +42,8 @@ AliHLTFilePublisher::AliHLTFilePublisher()
   AliHLTDataSource(),
   fpCurrent(NULL),
   fEvents(),
-  fMaxSize(0)
+  fMaxSize(0),
+  fOpenFilesAtStart(false)
 {
   // see header file for class documentation
   // or
@@ -97,6 +99,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;
@@ -174,6 +177,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());
@@ -197,7 +202,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();
   }
@@ -244,10 +249,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;
@@ -263,6 +268,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());
@@ -310,6 +316,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();
@@ -334,6 +341,7 @@ int AliHLTFilePublisher::GetEvent( const AliHLTComponentEventData& /*evtData*/,
            fMaxSize=iCopy;
            iResult=-ENOSPC;
          }
+         if (not fOpenFilesAtStart) pFileDesc->CloseFile();
        } else {
          HLTError("no file available");
          iResult=-EFAULT;
@@ -372,9 +380,26 @@ AliHLTFilePublisher::FileDesc::FileDesc(const char* name, AliHLTComponentDataTyp
   fSpecification(spec)
 {
 }
+
 AliHLTFilePublisher::FileDesc::~FileDesc()
 {
-  if (fpInstance) delete fpInstance;
+  CloseFile();
+}
+
+void AliHLTFilePublisher::FileDesc::CloseFile()
+{
+  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;
 }
 
@@ -386,6 +411,11 @@ int AliHLTFilePublisher::FileDesc::OpenFile()
   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;
     }
index 36d00bfe2c82e0ca93cde3fe77c66cfedd68cf99..6e33592e061b3f0d856f1fc1ca9ba661215755c8 100644 (file)
  *      indicate files published by the next event
  *
  * Optional arguments:<br>
+ * \li -open_files_at_start
+ *      Opens all files during component initialisation rather than as needed
+ *      during event processing. Note: this feature may result in the system
+ *      running out of file handles if a large number of files was specified.
  *
  * The component needs at least one argument \em -datafile or \em -datafilelist.
  * Both can occur multiple times. The \em -datatype and \em -dataspec
@@ -64,9 +68,11 @@ class AliHLTFilePublisher : public AliHLTDataSource  {
   /**
    * Open all files.
    * Opens all files for all events from the event list @ref fEvents and adds TFile
-   * opjects to the internal list.
+   * objects to the internal list. It also calculates the maximum event size required.
+   * @param keepOpen  If this flag is true then the files are kept open, otherwise
+   *                  this method will close the files afterwards.
    */
-  int OpenFiles();
+  int OpenFiles(bool keepOpen);
 
  protected:
   /**
@@ -143,6 +149,11 @@ class AliHLTFilePublisher : public AliHLTDataSource  {
      */
     int OpenFile();
 
+    /**
+     * Close the file handle.
+     */
+    void CloseFile();
+
     /**
      * Get name of the file.
      */
@@ -229,6 +240,9 @@ class AliHLTFilePublisher : public AliHLTDataSource  {
 
   /** the maximum buffer size i.e. size of the biggest file */
   Int_t                   fMaxSize;                                //! transient
+  
+  /** Flag specifying if all the files should be opened during initialisation.  */
+  bool fOpenFilesAtStart;                                          //! transient
 
   ClassDef(AliHLTFilePublisher, 1)
 };