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