Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/wroot/key is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_wroot_key
0005 #define tools_wroot_key
0006 
0007 #include "seek"
0008 #include "date"
0009 #include "ifile"
0010 #include "wbuf"
0011 #include "../sout"
0012 
0013 #ifdef TOOLS_MEM
0014 #include "../mem"
0015 #endif
0016 
0017 #include <ostream>
0018 
0019 namespace tools {
0020 namespace wroot {
0021 
0022 class key {
0023   static uint32 class_version() {return 2;}
0024   static const std::string& s_class() {
0025     static const std::string s_v("tools::wroot::key");
0026     return s_v;
0027   }
0028 public:
0029   static unsigned int std_string_record_size(const std::string& x) {
0030     // Returns size string will occupy on I/O buffer.
0031     if (x.size() > 254)
0032       return uint32(x.size()+sizeof(unsigned char)+sizeof(int));
0033     else
0034       return uint32(x.size()+sizeof(unsigned char));
0035   }
0036 public:
0037   key(std::ostream& a_out,
0038       seek a_seek_directory,
0039       const std::string& a_object_name,
0040       const std::string& a_object_title,
0041       const std::string& a_object_class) //for basket cstor.
0042   :m_out(a_out)
0043   ,m_buf_size(0)
0044   ,m_buffer(0)
0045   // Record :
0046   ,m_nbytes(0)
0047   ,m_version(class_version())
0048   ,m_object_size(0)
0049   ,m_date(0)
0050   ,m_key_length(0)
0051   ,m_cycle(0)
0052   ,m_seek_key(0)
0053   ,m_seek_directory(0)
0054   ,m_object_class(a_object_class)
0055   ,m_object_name(a_object_name)
0056   ,m_object_title(a_object_title)
0057   {
0058 #ifdef TOOLS_MEM
0059     mem::increment(s_class().c_str());
0060 #endif
0061 
0062     if(a_seek_directory>START_BIG_FILE()) m_version += big_file_version_tag();
0063 
0064     m_key_length = record_size(m_version);
0065 
0066     initialize_zero();
0067 
0068     m_seek_directory = a_seek_directory;
0069   }
0070 
0071   key(std::ostream& a_out,
0072       ifile& a_file,
0073       seek a_seek_directory,
0074       const std::string& a_object_name,
0075       const std::string& a_object_title,
0076       const std::string& a_object_class,
0077       uint32 a_object_size) //uncompressed data size.
0078   :m_out(a_out)
0079   ,m_buf_size(0)
0080   ,m_buffer(0)
0081   // Record :
0082   ,m_nbytes(0)
0083   ,m_version(class_version())
0084   ,m_object_size(a_object_size)
0085   ,m_date(0)
0086   ,m_key_length(0)
0087   ,m_cycle(0)
0088   ,m_seek_key(0)
0089   ,m_seek_directory(0)
0090   ,m_object_class(a_object_class)
0091   ,m_object_name(a_object_name)
0092   ,m_object_title(a_object_title)
0093   {
0094 #ifdef TOOLS_MEM
0095     mem::increment(s_class().c_str());
0096 #endif
0097 
0098     if(a_object_size) {
0099       if(a_file.END()>START_BIG_FILE()) m_version += big_file_version_tag();
0100     }
0101     if(m_version>big_file_version_tag()) {
0102     } else {
0103       if(a_seek_directory>START_BIG_FILE()) m_version += big_file_version_tag();
0104     }
0105 
0106     m_key_length = record_size(m_version);
0107 
0108     initialize(a_file,a_object_size);
0109 
0110     m_seek_directory = a_seek_directory;
0111   }
0112   virtual ~key(){
0113     delete [] m_buffer;
0114 #ifdef TOOLS_MEM
0115     mem::decrement(s_class().c_str());
0116 #endif
0117   }
0118 protected:
0119   key(const key& a_from):m_out(a_from.m_out){
0120 #ifdef TOOLS_MEM
0121     mem::increment(s_class().c_str());
0122 #endif
0123   }
0124   key& operator=(const key &){return *this;}
0125 public:
0126   uint16 cycle() const {return m_cycle;}
0127   void set_cycle(uint16 a_cycle) {m_cycle = a_cycle;}
0128 
0129   const std::string& object_name() const {return m_object_name;}
0130   const std::string& object_title() const {return m_object_title;}
0131   const std::string& object_class() const {return m_object_class;}
0132 
0133   bool write_self(ifile& a_file) {
0134     char* buffer = m_buffer;
0135     wbuf wb(m_out,a_file.byte_swap(),eob(),buffer);
0136     return to_buffer(wb,a_file.verbose());
0137   }
0138 
0139   bool write_file(ifile& a_file,uint32& a_nbytes) {
0140     if(!a_file.set_pos(m_seek_key)) {
0141       a_nbytes = 0;
0142       return false;
0143     }
0144     if(!a_file.write_buffer(m_buffer,m_nbytes)) {
0145       a_nbytes = 0;
0146       return false;
0147     }
0148 
0149     if(a_file.verbose()) {
0150       m_out << "tools::wroot::key::write_file :"
0151             << " writing " << m_nbytes << " bytes"
0152             << " at address " << m_seek_key
0153             << " for ID=" << sout(m_object_name)
0154             << " Title=" << sout(m_object_title) << "."
0155             << std::endl;
0156     }
0157 
0158     delete [] m_buffer; //???
0159     m_buffer = 0;
0160     m_buf_size = 0;
0161 
0162     a_nbytes = m_nbytes;
0163     return true;
0164   }
0165 
0166   void set_number_of_bytes(uint32 a_n) {m_nbytes = a_n;}
0167   uint32 number_of_bytes() const {return m_nbytes;}
0168 
0169   uint32 object_size() const {return m_object_size;}
0170 
0171   seek seek_key() const {return m_seek_key;}
0172   short key_length() const {return m_key_length;}
0173 
0174   char* data_buffer() {return m_buffer + m_key_length;}
0175   const char* eob() const {return m_buffer + m_buf_size;}
0176 
0177   bool to_buffer(wbuf& a_wb,bool a_verbose) const {
0178     if(!a_wb.write(m_nbytes)) return false;
0179     short version = m_version;
0180     if(!a_wb.write(version)) return false;
0181     if(!a_wb.write(m_object_size)) return false;
0182     unsigned int _date = 0; //FIXME
0183     if(!a_wb.write(_date)) return false;
0184     if(!a_wb.write(m_key_length)) return false;
0185     if(!a_wb.write(m_cycle)) return false;
0186     if(version>(short)big_file_version_tag()) {
0187       if(!a_wb.write(m_seek_key)) return false;
0188       if(!a_wb.write(m_seek_directory)) return false;
0189     } else {
0190       if(m_seek_key>START_BIG_FILE()) {
0191         m_out << "tools::wroot::key::to_buffer :"
0192               << " attempt to write big seek "
0193               << m_seek_key << " on 32 bits."
0194               << std::endl;
0195         return false;
0196       }
0197       if(!a_wb.write((seek32)m_seek_key)) return false;
0198       if(m_seek_directory>START_BIG_FILE()) {
0199         m_out << "tools::wroot::key::to_buffer :"
0200               << " (2) attempt to write big seek "
0201               << m_seek_directory << " on 32 bits."
0202               << std::endl;
0203         return false;
0204       }
0205       if(!a_wb.write((seek32)m_seek_directory)) return false;
0206     }
0207     if(!a_wb.write(m_object_class)) return false;
0208     if(!a_wb.write(m_object_name)) return false;
0209     if(!a_wb.write(m_object_title)) return false;
0210     if(a_verbose) {
0211       m_out << "tools::wroot::key::to_buffer :"
0212             << " nbytes : " << m_nbytes
0213             << ", object class : " << sout(m_object_class)
0214             << ", object name : " << sout(m_object_name)
0215             << ", object title : " << sout(m_object_title)
0216             << ", object size : " << m_object_size
0217             << "."
0218             << std::endl;
0219     }
0220     return true;
0221   }
0222 
0223 protected:
0224   uint32 record_size(uint32 a_version) const {
0225     // Return the size in bytes of the key header structure.
0226     uint32 nbytes = sizeof(m_nbytes);
0227     nbytes += sizeof(short);
0228     nbytes += sizeof(m_object_size);
0229     nbytes += sizeof(date);
0230     nbytes += sizeof(m_key_length);
0231     nbytes += sizeof(m_cycle);
0232     if(a_version>big_file_version_tag()) {
0233       nbytes += sizeof(seek);
0234       nbytes += sizeof(seek);
0235     } else {
0236       nbytes += sizeof(seek32);
0237       nbytes += sizeof(seek32);
0238     }
0239     nbytes += std_string_record_size(m_object_class);
0240     nbytes += std_string_record_size(m_object_name);
0241     nbytes += std_string_record_size(m_object_title);
0242     return nbytes;
0243   }
0244 
0245   bool initialize_zero() {
0246     uint32 nsize = m_key_length;
0247     m_date = get_date();
0248     m_seek_key = 0;
0249     delete [] m_buffer;
0250     m_buffer = new char[nsize];
0251     m_buf_size = nsize;
0252     m_nbytes = nsize;
0253     return true;
0254   }
0255   bool initialize(ifile& a_file,uint32 a_nbytes) {
0256     uint32 nsize = m_key_length+a_nbytes;
0257 
0258     m_date = get_date();
0259 
0260     if(a_nbytes) {//GB
0261       m_seek_key = a_file.END();
0262       a_file.set_END(m_seek_key+nsize);
0263 
0264       //NOTE : the free segment logic found in CERN-ROOT/TKey
0265       //       is not yet needed right now for us, since
0266       //       we always write at end of file. The update
0267       //       of the eof free_seg is done in set_END.
0268     } else { //basket
0269       m_seek_key = 0;
0270     }
0271 
0272     delete [] m_buffer;
0273     m_buffer = new char[nsize];
0274     m_buf_size = nsize;
0275     m_nbytes = nsize;
0276 
0277     return true;
0278   }
0279 protected:
0280   std::ostream& m_out;
0281   uint32 m_buf_size;
0282   char* m_buffer;
0283   // Record (stored in file) :
0284   uint32 m_nbytes;            //Number of bytes for the object on file
0285   uint32 m_version;           //Key version identifier
0286   uint32 m_object_size;       //Length of uncompressed object in bytes
0287   date m_date;                //Date/Time of insertion in file
0288   uint16 m_key_length;        //Number of bytes for the key itself
0289   uint16 m_cycle;             //Cycle number
0290   seek m_seek_key;            //Location of object on file
0291   seek m_seek_directory;      //Location of parent directory on file
0292   std::string m_object_class; //Object Class name.
0293   std::string m_object_name;  //name of the object.
0294   std::string m_object_title; //title of the object.
0295 };
0296 
0297 }}
0298 
0299 #endif
0300 
0301 //doc :
0302 //////////////////////////////////////////////////////////////////////////
0303 //                                                                      //
0304 //  The Key class includes functions to book space on a file,           //
0305 //   to create I/O buffers, to fill these buffers                       //
0306 //   to compress/uncompress data buffers.                               //
0307 //                                                                      //
0308 //  Before saving (making persistent) an object on a file, a key must   //
0309 //  be created. The key structure contains all the information to       //
0310 //  uniquely identify a persistent object on a file.                    //
0311 //     fNbytes    = number of bytes for the compressed object+key       //
0312 //     version of the Key class                                         //
0313 //     fObjlen    = Length of uncompressed object                       //
0314 //     fDatime    = Date/Time when the object was written               //
0315 //     fKeylen    = number of bytes for the key structure               //
0316 //     fCycle     = cycle number of the object                          //
0317 //     fSeekKey   = Address of the object on file (points to fNbytes)   //
0318 //                  This is a redundant information used to cross-check //
0319 //                  the data base integrity.                            //
0320 //     fSeekPdir  = Pointer to the directory supporting this object     //
0321 //     fClassName = Object class name                                   //
0322 //     fName      = name of the object                                  //
0323 //     fTitle     = title of the object                                 //
0324 //                                                                      //
0325 //  The Key class is used by ROOT to:                                   //
0326 //    - to write an object in the Current Directory                     //
0327 //    - to write a new ntuple buffer                                    //
0328 //                                                                      //
0329 //////////////////////////////////////////////////////////////////////////