Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/wroot/leaf 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_leaf
0005 #define tools_wroot_leaf
0006 
0007 #include "base_leaf"
0008 
0009 #include "../cids"
0010 
0011 namespace tools {
0012 namespace wroot {
0013 
0014 inline const std::string& leaf_store_class(char) {
0015   static const std::string s_v("TLeafB");
0016   return s_v;
0017 }
0018 inline const std::string& leaf_store_class(short) {
0019   static const std::string s_v("TLeafS");
0020   return s_v;
0021 }
0022 inline const std::string& leaf_store_class(int) {
0023   static const std::string s_v("TLeafI");
0024   return s_v;
0025 }
0026 inline const std::string& leaf_store_class(float) {
0027   static const std::string s_v("TLeafF");
0028   return s_v;
0029 }
0030 inline const std::string& leaf_store_class(double) {
0031   static const std::string s_v("TLeafD");
0032   return s_v;
0033 }
0034 inline const std::string& leaf_string_store_class() {
0035   static const std::string s_v("TLeafC");
0036   return s_v;
0037 }
0038 inline const std::string& leaf_element_store_class() {
0039   static const std::string s_v("TLeafElement");
0040   return s_v;
0041 }
0042 inline const std::string& leaf_object_store_class() {
0043   static const std::string s_v("TLeafObject");
0044   return s_v;
0045 }
0046 
0047 template <class T>
0048 class leaf_ref : public base_leaf {
0049   typedef base_leaf parent;
0050 public:
0051   static cid id_class() {return base_leaf_cid()+_cid(T())+10000;} //10000 same as in ntuple::column_ref.
0052   virtual void* cast(cid a_class) const {
0053     if(void* p = cmp_cast<leaf_ref>(this,a_class)) {return p;}
0054     return parent::cast(a_class);
0055   }
0056   virtual cid id_cls() const {return id_class();}
0057 public: //ibo
0058   virtual const std::string& store_cls() const {return leaf_store_class(T());}
0059   virtual bool stream(buffer& a_buffer) const {
0060     unsigned int c;
0061     if(!a_buffer.write_version(1,c)) return false;
0062     if(!parent::stream(a_buffer)) return false;
0063     if(!a_buffer.write(m_min)) return false;
0064     if(!a_buffer.write(m_max)) return false;
0065     if(!a_buffer.set_byte_count(c)) return false;
0066     return true;
0067   }
0068 public: //base_leaf
0069   virtual bool fill_buffer(buffer& a_buffer) const {
0070     if(m_is_range) {
0071       if(m_ref>=m_max) {
0072         leaf_ref& self = const_cast<leaf_ref&>(*this);
0073         self.m_max = m_ref;
0074       }
0075     }
0076     return a_buffer.write<T>(m_ref);
0077   }
0078 public:
0079   leaf_ref(std::ostream& a_out,const std::string& a_name,const T& a_ref)
0080   :parent(a_out,a_name,a_name)
0081   ,m_min(T()),m_max(T())
0082   ,m_ref(a_ref)
0083   {
0084     m_length = 1;
0085     m_length_type = sizeof(T);
0086   }
0087   virtual ~leaf_ref(){}
0088 protected:
0089   leaf_ref(const leaf_ref& a_from):ibo(a_from),parent(a_from),m_ref(a_from.m_ref){}
0090   leaf_ref& operator=(const leaf_ref&){return *this;}
0091 public:
0092   T get_max() const {return m_max;}
0093   void set_max(const T& a_value) {m_max = a_value;}
0094 public:
0095   const T& variable() const {return m_ref;}
0096   T& variable() {return const_cast<T&>(m_ref);}
0097 protected:
0098   T m_min;    //Minimum value if leaf range is specified
0099   T m_max;    //Maximum value if leaf range is specified
0100   const T& m_ref;
0101 };
0102 
0103 template <class T>
0104 class leaf : public leaf_ref<T> {
0105   typedef leaf_ref<T> parent;
0106 public:
0107   static cid id_class() {return base_leaf_cid()+_cid(T());}
0108   virtual void* cast(cid a_class) const {
0109     if(void* p = cmp_cast<leaf>(this,a_class)) {return p;}
0110     return parent::cast(a_class);
0111   }
0112   virtual cid id_cls() const {return id_class();}
0113 public:
0114   leaf(std::ostream& a_out,const std::string& a_name)
0115   :parent(a_out,a_name,m_value)
0116   ,m_value(T())
0117   {}
0118   virtual ~leaf(){}
0119 protected:
0120   leaf(const leaf& a_from):ibo(a_from),parent(a_from){}
0121   leaf& operator=(const leaf&){return *this;}
0122 public:
0123   void fill(const T& a_value) {m_value = a_value;}
0124 protected:
0125   T m_value;
0126 };
0127 
0128 class leaf_string_ref : public base_leaf {
0129   typedef base_leaf parent;
0130 public:
0131   static cid id_class() {return leaf_string_cid()+10000;}
0132   virtual void* cast(cid a_class) const {
0133     if(void* p = cmp_cast<leaf_string_ref>(this,a_class)) {return p;}
0134     return parent::cast(a_class);
0135   }
0136   virtual cid id_cls() const {return id_class();}
0137 public: //ibo
0138   virtual const std::string& store_cls() const {return leaf_string_store_class();}
0139   virtual bool stream(buffer& a_buffer) const {
0140     unsigned int c;
0141     if(!a_buffer.write_version(1,c)) return false;
0142     if(!parent::stream(a_buffer)) return false;
0143     if(!a_buffer.write(m_min)) return false;
0144     if(!a_buffer.write(m_max)) return false;
0145     if(!a_buffer.set_byte_count(c)) return false;
0146     return true;
0147   }
0148 public: //base_leaf
0149   virtual bool fill_buffer(buffer& a_buffer) const {
0150     leaf_string_ref& self = const_cast<leaf_string_ref&>(*this);
0151     size_t len = ::strlen(m_ref.c_str());
0152     if(len >= (size_t)m_max) self.m_max = int(len)+1;
0153     if(len >= m_length) self.m_length = uint32(len)+1;
0154     if(len < 255) {
0155       if(!a_buffer.write<unsigned char>((unsigned char)len)) return false;
0156     } else {
0157       if(!a_buffer.write<unsigned char>(255)) return false;
0158       if(!a_buffer.write<uint32>(uint32(len))) return false;
0159     }
0160     if(len) if(!a_buffer.write_fast_array(m_ref.c_str(),uint32(len))) return false;
0161     return true;
0162   }
0163 public:
0164   leaf_string_ref(std::ostream& a_out,const std::string& a_name,const std::string& a_ref)
0165   :parent(a_out,a_name,a_name)
0166   ,m_min(0),m_max(0)
0167   ,m_ref(a_ref)
0168   {
0169     m_length_type = 1;
0170   }
0171   virtual ~leaf_string_ref(){}
0172 protected:
0173   leaf_string_ref(const leaf_string_ref& a_from):ibo(a_from),parent(a_from),m_ref(a_from.m_ref){}
0174   leaf_string_ref& operator=(const leaf_string_ref&){return *this;}
0175 public:
0176   int get_max() const {return m_max;}
0177   void set_max(int a_value) {m_max = a_value;}
0178 public:
0179   const std::string& variable() const {return m_ref;}
0180   std::string& variable() {return const_cast<std::string&>(m_ref);}
0181 protected:
0182   int m_min; //Minimum value if leaf range is specified
0183   int m_max; //Maximum value if leaf range is specified
0184   const std::string& m_ref;
0185 };
0186 
0187 class leaf_string : public leaf_string_ref {
0188   typedef leaf_string_ref parent;
0189 public:
0190   static cid id_class() {return leaf_string_cid();}
0191   virtual void* cast(cid a_class) const {
0192     if(void* p = cmp_cast<leaf_string>(this,a_class)) {return p;}
0193     return parent::cast(a_class);
0194   }
0195   virtual cid id_cls() const {return id_class();}
0196 public:
0197   leaf_string(std::ostream& a_out,const std::string& a_name)
0198   :parent(a_out,a_name,m_value)
0199   {}
0200   virtual ~leaf_string(){}
0201 protected:
0202   leaf_string(const leaf_string& a_from):ibo(a_from),parent(a_from){}
0203   leaf_string& operator=(const leaf_string&){return *this;}
0204 public:
0205   void fill(const std::string& a_value) {m_value = a_value;}
0206 protected:
0207   std::string m_value;
0208 };
0209 
0210 // to store vector columns of a row_wise ntuple :
0211 template <class T>
0212 class leaf_std_vector_ref : public base_leaf {
0213   typedef base_leaf parent;
0214 public:
0215   static cid id_class() {return base_leaf_cid()+_cid(std::vector<T>())+10000;} //10000 same as in ntuple::column_ref.
0216   virtual void* cast(cid a_class) const {
0217     if(void* p = cmp_cast<leaf_std_vector_ref>(this,a_class)) {return p;}
0218     return parent::cast(a_class);
0219   }
0220   virtual cid id_cls() const {return id_class();}
0221 public: //ibo
0222   virtual const std::string& store_cls() const {return leaf_store_class(T());}
0223   virtual bool stream(buffer& a_buffer) const {
0224     unsigned int c;
0225     if(!a_buffer.write_version(1,c)) return false;
0226     if(!parent::stream(a_buffer)) return false;
0227     if(!a_buffer.write(m_min)) return false;
0228     if(!a_buffer.write(m_max)) return false;
0229     if(!a_buffer.set_byte_count(c)) return false;
0230     return true;
0231   }
0232 public: //base_leaf
0233   virtual bool fill_buffer(buffer& a_buffer) const {
0234     if(!a_buffer.write_fast_array(m_ref.data(),uint32(m_ref.size()))) return false;
0235     return true;
0236   }
0237 public:
0238   leaf_std_vector_ref(std::ostream& a_out,const std::string& a_name,
0239                       base_leaf& a_leaf_count,const std::vector<T>& a_ref)
0240   :parent(a_out,a_name,a_name)
0241   ,m_min(T()),m_max(T())
0242   ,m_ref(a_ref)
0243   {
0244     parent::m_leaf_count = &a_leaf_count;
0245     a_leaf_count.set_is_range(true);
0246     m_length = 1;
0247     m_length_type = sizeof(T);
0248   }
0249   virtual ~leaf_std_vector_ref(){}
0250 protected:
0251   leaf_std_vector_ref(const leaf_std_vector_ref& a_from):ibo(a_from),parent(a_from),m_ref(a_from.m_ref){}
0252   leaf_std_vector_ref& operator=(const leaf_std_vector_ref&){return *this;}
0253 public:
0254   T get_max() const {return m_max;}
0255   void set_max(const T& a_value) {m_max = a_value;}
0256 public:
0257   const std::vector<T>& variable() const {return m_ref;}
0258   std::vector<T>& variable() {return const_cast< std::vector<T>& >(m_ref);}
0259 protected:
0260   T m_min;    //Minimum value if leaf range is specified
0261   T m_max;    //Maximum value if leaf range is specified
0262   const std::vector<T>& m_ref;
0263 };
0264 
0265 class leaf_element : public base_leaf {
0266   typedef base_leaf parent;
0267 public:
0268   static cid id_class() {return leaf_element_cid();}
0269   virtual void* cast(cid a_class) const {
0270     if(void* p = cmp_cast<leaf_element>(this,a_class)) {return p;}
0271     return base_leaf::cast(a_class);
0272   }
0273   virtual cid id_cls() const {return id_class();}
0274 public:
0275   virtual const std::string& store_cls() const {return leaf_element_store_class();}
0276   virtual bool stream(buffer& a_buffer) const {
0277     unsigned int c;
0278     if(!a_buffer.write_version(1,c)) return false;
0279     if(!parent::stream(a_buffer)) return false;
0280 
0281     if(!a_buffer.write(fID)) return false;
0282     if(!a_buffer.write(fType)) return false;
0283 
0284     if(!a_buffer.set_byte_count(c)) return false;
0285     return true;
0286   }
0287 public: //base_leaf
0288   virtual bool fill_buffer(buffer&) const {
0289     m_out << "tools::wroot::leaf_element::fill_buffer : dummy." << std::endl;
0290     return false;
0291   }
0292 public:
0293   leaf_element(std::ostream& a_out,const std::string& a_name,int a_id,int a_type)
0294   :parent(a_out,a_name,a_name)
0295   ,fID(a_id)
0296   ,fType(a_type)
0297   {}
0298   virtual ~leaf_element(){}
0299 protected:
0300   leaf_element(const leaf_element& a_from):ibo(a_from),parent(a_from){}
0301   leaf_element& operator=(const leaf_element& a_from){
0302     parent::operator=(a_from);
0303     return *this;
0304   }
0305 protected:
0306   int fID;   //element serial number in fInfo
0307   int fType; //leaf type
0308 };
0309 
0310 }}
0311 
0312 #include "iobject"
0313 
0314 namespace tools {
0315 namespace wroot {
0316 
0317 class leaf_object : public base_leaf {
0318   typedef base_leaf parent;
0319 public:
0320   static cid id_class() {return leaf_object_cid();}
0321   virtual void* cast(cid a_class) const {
0322     if(void* p = cmp_cast<leaf_object>(this,a_class)) {return p;}
0323     return base_leaf::cast(a_class);
0324   }
0325   virtual cid id_cls() const {return id_class();}
0326 public:
0327   virtual const std::string& store_cls() const {return leaf_object_store_class();}
0328   virtual bool stream(buffer& a_buffer) const {
0329     unsigned int c;
0330     if(!a_buffer.write_version(4,c)) return false;
0331     if(!parent::stream(a_buffer)) return false;
0332     if(!a_buffer.write(fVirtual)) return false;
0333     if(!a_buffer.set_byte_count(c)) return false;
0334     return true;
0335   }
0336 public: //base_leaf
0337   virtual bool fill_buffer(buffer& a_buffer) const {
0338     if(fVirtual) {
0339       const std::string& _class = m_obj.store_class_name();
0340       if(_class.size()>255) return false;
0341       unsigned char n = (unsigned char)_class.size();
0342       if(!a_buffer.write(n)) return false;
0343       if(!a_buffer.write_fast_array(_class.c_str(),n+1)) return false;
0344     }
0345     return m_obj.stream(a_buffer);
0346   }
0347 public:
0348   leaf_object(std::ostream& a_out,const std::string& a_name,const iobject& a_obj)
0349   :parent(a_out,a_name,a_obj.store_class_name()) //CERN-ROOT/TLeafObject::Streamer() wants store class name on m_title.
0350   ,m_obj(a_obj)
0351   ,fVirtual(true)
0352   {}
0353   virtual ~leaf_object(){}
0354 protected:
0355   leaf_object(const leaf_object& a_from):ibo(a_from),parent(a_from),m_obj(a_from.m_obj),fVirtual(true){}
0356   leaf_object& operator=(const leaf_object& a_from){
0357     parent::operator=(a_from);
0358     return *this;
0359   }
0360 protected:
0361   const iobject& m_obj;
0362   bool fVirtual; // Support for Virtuality
0363 };
0364 
0365 }}
0366 
0367 #endif