// HbookFile.h #ifndef dset__HbookFile_H #define dset__HbookFile_H // David Adams // May 2003 // // Class to provide easy access to a HBOOK file from C++. // Assigns a logical unit number and links the file to a // PAW-useable file name when the file is opened. The file // is opened in one of three states: // FORPAW - hropen is not called // READ - hropen called for readonly access // WRITE - hropen called for update // // Copy and assignment are hidden to avoid inconsistencies between // the actual file open status and the value recorded in each object. // With effort, we could introduce global tracking to avoid these // problems. // #include class HbookFile { public: // enums enum OpenStatus { INVALID, CLOSED, FORPAW, READ, WRITE }; private: // data // Input file name. std::string m_fname; // File name for use with PAW. std::string m_pname; // LUN. int m_lun; // HBOOK directory. std::string m_hbdir; // Open status. OpenStatus m_state; private: // Hidden methods // Copy constructor. HbookFile(const HbookFile& rhs); // Assignment. HbookFile& operator=(const HbookFile& rhs); public: // methods // Constructor. // Default is an invalid state. explicit HbookFile(std::string fname =""); // Destructor. ~HbookFile(); // Open the file. // If write is true, file is opened for appending. // Returns 0 for success. int open(OpenStatus ostat =READ); // Close the file // Returns 0 for success. int close(); // Return the open status. OpenStatus state() const { return m_state; } // Is this a valid file? // Exists and has appropriate read/write attributes. bool is_valid() const; // Is the file writeable? // Underlying file must be writeable. bool is_writeable() const; // Is the file open (including open for PAW). bool is_open() const; // Is the file opened for reading (hropen called). bool is_open_for_reading() const; // Is the file opened for writing? bool is_open_for_writing() const; // Is the file "opened" for use in PAW. // User must call hropen from paw. // Use the name paw_filename() and the LUN lun(). bool is_open_for_paw() const; // Physical file name. std::string full_filename() const { return m_fname; } // Return the logical unit number. // Zero if file is not open. int lun() const { return m_lun; } // Return the LUN as a string. std::string slun() const; // PAW file name. // This name should not have any case or length problems // for use with HBOOK/PAW. // Typically a link to the physical file. // Returns blank if not open. std::string hbook_filename() const { return m_pname; } // Return the HBOOK directory where this file is opened. // Return blank if not open. std::string hbdir() const { return m_hbdir; } // Return the PAW command to open this file. // Returns blank if not open for paw. // Opened for update if argument is true. // Method open(FORPAW) must be called first. std::string paw_open_command(bool write =false) const; // Return the PAW command to close this file. // call close() afterwards. std::string paw_close_command() const; // Generate the skeleton code for processing ntuple id. // Code is written to the file evcode.f. // This must be valid and not open for paw. // Returns zero for success. int evcode(int id); // Directory listing. // Arguments are those of hldir. void ls(std::string dir =" ", std::string opt =" ") const; // Change directory. // Arguments are those of hcdir. // Returns 0 for success. int cd(std::string dir =" ", std::string opt =" ") const; // Read in histograms. // Arguments the same as hrin. // Default reads everything. // Returns 0 for success. int hrin(int id =0, int cycle =999999, int offset =0) const; }; // Output stream. std::ostream& operator<<(std::ostream& lhs, const HbookFile& rhs); #endif