Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/externals/g4tools/include/tools/store/osc_streamers

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.
  3 
  4 #ifndef tools_store_osc_streamers
  5 #define tools_store_osc_streamers
  6 
  7 #include "iobj_const_visitor"
  8 #include "iobj_visitor"
  9 
 10 #include "../histo/h1d"
 11 #include "../histo/h2d"
 12 #include "../histo/h3d"
 13 #include "../histo/p1d"
 14 #include "../histo/p2d"
 15 #include "../vmanip"
 16 #include "../sto"
 17 #include "../num2s"
 18 #include "../S_STRING"
 19 #include "../scast"
 20 
 21 namespace tools {
 22 namespace osc {
 23 
 24 inline const std::string& s_axis() {
 25   static const std::string s_v("BatchLab::Axis");
 26   return s_v;
 27 }
 28 inline const std::string& s_annotation() {
 29   static const std::string s_v("BatchLab::Annotation");
 30   return s_v;
 31 }
 32 inline const std::string& s_base_histogram() {
 33   static const std::string s_v("BatchLab::BaseHistogram");
 34   return s_v;
 35 }
 36 inline const std::string& s_item() {
 37   static const std::string s_v("BatchLab::Item");
 38   return s_v;
 39 }
 40 
 41 inline const std::string& s_h1d() {
 42   static const std::string s_v("BatchLab::Histogram1D");
 43   return s_v;
 44 }
 45 
 46 inline const std::string& s_h2d() {
 47   static const std::string s_v("BatchLab::Histogram2D");
 48   return s_v;
 49 }
 50 
 51 inline const std::string& s_h3d() {
 52   static const std::string s_v("BatchLab::Histogram3D");
 53   return s_v;
 54 }
 55 
 56 inline const std::string& s_p1d() {
 57   static const std::string s_v("BatchLab::Profile1D");
 58   return s_v;
 59 }
 60 
 61 inline const std::string& s_p2d() {
 62   static const std::string s_v("BatchLab::Profile2D");
 63   return s_v;
 64 }
 65 
 66 class Axis : public virtual istorable {
 67 public:
 68   TOOLS_SCLASS(tools::osc::Axis)
 69 protected:
 70   virtual void* cast(const std::string& a_class) const {
 71     if(void* p = cmp_cast<Axis>(this,a_class)) return p;
 72     return 0;
 73   }
 74   virtual const std::string& store_cls() const {return s_axis();}
 75   virtual bool visit(iobj_const_visitor& a_v) const {
 76     if(!a_v.begin(*this,s_axis(),Axis::s_visit)) return false;
 77 
 78     int version = 1;
 79     if(!a_v.visit("fVersion",version)) return false;
 80 
 81     if(!a_v.visit("fOffset",m_axis.m_offset)) return false;
 82     if(!a_v.visit("fNumberOfBins",(int)m_axis.m_number_of_bins)) return false;
 83     if(!a_v.visit("fMinimumValue",m_axis.m_minimum_value)) return false;
 84     if(!a_v.visit("fMaximumValue",m_axis.m_maximum_value)) return false;
 85     if(!a_v.visit("fFixed",m_axis.m_fixed)) return false;
 86     if(!a_v.visit("fBinWidth",m_axis.m_bin_width)) return false;
 87     if(!a_v.visit("fEdges",m_axis.m_edges)) return false;
 88 
 89     if(!a_v.end(*this)) return false;
 90     return true;
 91   }
 92   static bool s_visit(const istorable& a_o,iobj_const_visitor& a_v){
 93     const Axis* local = safe_cast<istorable,Axis>(a_o);
 94     if(!local) return false;
 95     return local->Axis::visit(a_v); //IMPORTANT : have Axis::
 96   }
 97 public:
 98   Axis(const histo::axis<double,unsigned int>& a_axis):m_axis(a_axis){}
 99   virtual ~Axis(){}
100 private:
101   Axis(const Axis& a_from):istorable(a_from),m_axis(a_from.m_axis){}
102   Axis& operator=(const Axis&){return *this;}
103 protected:
104   const histo::axis<double,unsigned int>& m_axis;
105 };
106 
107 
108 inline bool Axis_read(iobj_visitor& a_visitor,histo::axis<double,unsigned int>& a_axis){
109   //if(!a_visitor.begin(*this)) return false;
110 
111   int version;
112   if(!a_visitor.visit(version)) return false;
113 
114   if(!a_visitor.visit(a_axis.m_offset)) return false;
115 
116  {int nbin;
117   if(!a_visitor.visit(nbin)) return false;
118   a_axis.m_number_of_bins = nbin;}
119 
120   if(!a_visitor.visit(a_axis.m_minimum_value)) return false;
121   if(!a_visitor.visit(a_axis.m_maximum_value)) return false;
122   if(!a_visitor.visit(a_axis.m_fixed)) return false;
123   if(!a_visitor.visit(a_axis.m_bin_width)) return false;
124   if(!a_visitor.visit(a_axis.m_edges)) return false;
125 
126   //if(!a_visitor.end(*this)) return false;
127   return true;
128 }
129 
130 class Item : public virtual istorable {
131 public:
132   TOOLS_SCLASS(tools::osc::Item)
133 protected:
134   virtual void* cast(const std::string& a_class) const {
135     if(void* p = cmp_cast<Item>(this,a_class)) return p;
136     return 0;
137   }
138 public:
139   virtual const std::string& store_cls() const {return s_item();}
140   virtual bool visit(iobj_const_visitor& a_visitor) const {
141     if(!a_visitor.begin(*this,s_item(),Item::s_visit)) return false;
142 
143     int version = 1;
144     if(!a_visitor.visit("fVersion",version)) return false;
145 
146     if(!a_visitor.visit("fKey",fKey)) return false;
147     if(!a_visitor.visit("fValue",fValue)) return false;
148     if(!a_visitor.visit("fSticky",fSticky)) return false;
149 
150     if(!a_visitor.end(*this)) return false;
151     return true;
152   }
153   static bool s_visit(const istorable& a_o,iobj_const_visitor& a_v){
154     const Item* local = safe_cast<istorable,Item>(a_o);
155     if(!local) return false;
156     return local->Item::visit(a_v); //IMPORTANT : have Item::
157   }
158 public:
159   virtual bool read(iobj_visitor& a_visitor) {
160     //if(!a_visitor.begin(*this)) return false;
161 
162     int version;
163     if(!a_visitor.visit(version)) return false;
164 
165     if(!a_visitor.visit(fKey)) return false;
166     if(!a_visitor.visit(fValue)) return false;
167     if(!a_visitor.visit(fSticky)) return false;
168 
169     //if(!a_visitor.end(*this)) return false;
170     return true;
171   }
172 public:
173   Item(){}
174   Item(const std::string& aKey,const std::string& aValue,bool aSticky)
175   :fKey(aKey),fValue(aValue),fSticky(aSticky){}
176   virtual ~Item(){}
177 public:
178   Item(const Item& a_from)
179   :istorable(a_from)
180   ,fKey(a_from.fKey)
181   ,fValue(a_from.fValue)
182   ,fSticky(a_from.fSticky)
183   {}
184   Item& operator=(const Item& a_from) {
185     if(&a_from==this) return *this;
186     fKey = a_from.fKey;
187     fValue = a_from.fValue;
188     fSticky = a_from.fSticky;
189     return *this;
190   }
191 public:
192   std::string fKey;
193   std::string fValue;
194   bool fSticky;
195 };
196 
197 
198 template <class T>
199 class Vector : public virtual istorable {
200 public:
201   TOOLS_T_SCLASS(T,tools::osc::Vector)
202 protected:
203   virtual void* cast(const std::string& a_class) const {
204     if(void* p = cmp_cast<Vector>(this,a_class)) return p;
205     return 0;
206   }
207   virtual const std::string& store_cls() const {
208     static const std::string s_v("BatchLab::Vector<"+m_T+">");
209     return s_v;
210   }
211   virtual bool visit(iobj_const_visitor& a_v) const {
212     if(!a_v.begin(*this,Vector<T>::store_cls(),Vector<T>::s_visit)) return false;
213 
214     int version = 1;
215     if(!a_v.visit("fVersion",version)) return false;
216 
217     unsigned int number = (unsigned int)m_vec.size();
218     if(!a_v.visit("fSize",number)) return false;
219 
220     std::string stmp;
221     for(unsigned int index=0;index<number;index++) {
222       const T& elem = m_vec[index];
223       if(!num2s(index,stmp)) return false;
224       if(!a_v.visit(stmp,elem)) return false;
225     }
226 
227     if(!a_v.end(*this)) return false;
228     return true;
229   }
230   static bool s_visit(const istorable& a_o,iobj_const_visitor& a_v){
231     const Vector* local = safe_cast<istorable,Vector>(a_o);
232     if(!local) return false;
233     return local->Vector<T>::visit(a_v); //IMPORTANT : have Vector::
234   }
235 public:
236   Vector(const std::vector<T>& a_vec,const std::string& a_T)
237   :m_vec(a_vec),m_T(a_T){}
238   virtual ~Vector(){}
239 private:
240   Vector(const Vector& a_from)
241   :istorable(a_from),m_vec(a_from.m_vec),m_T(a_from.m_T){}
242   Vector& operator=(const Vector&){return *this;}
243 protected:
244   const std::vector<T>& m_vec;
245   std::string m_T;
246 };
247 
248 
249 template <class T>
250 inline bool std_vector_read(iobj_visitor& a_visitor,std::vector<T>& a_vec) {
251   a_vec.clear();
252 
253   //if(!a_visitor.begin(*this)) return false;
254 
255   int version;
256   if(!a_visitor.visit(version)) return false;
257 
258   unsigned int number;
259   if(!a_visitor.visit(number)) return false;
260 
261   a_vec.resize(number);
262   for(unsigned int index=0;index<number;index++) {
263     T& elem = a_vec[index];
264     if(!elem.read(a_visitor)) return false;
265   }
266 
267   //if(!a_visitor.end(*this)) return false;
268   return true;
269 }
270 
271 class Annotation : public virtual istorable {
272 public:
273   TOOLS_SCLASS(tools::osc::Annotation)
274 protected:
275   virtual void* cast(const std::string& a_class) const {
276     if(void* p = cmp_cast<Annotation>(this,a_class)) return p;
277     return 0;
278   }
279   virtual const std::string& store_cls() const {return s_annotation();}
280   virtual bool visit(iobj_const_visitor& a_v) const {
281     if(!a_v.begin(*this,s_annotation(),Annotation::s_visit)) return false;
282 
283     int version = 1;
284     if(!a_v.visit("fVersion",version)) return false;
285 
286     Vector<Item> v(m_items,s_item());
287     if(!a_v.visit("fItems",v)) return false;
288 
289     if(!a_v.end(*this)) return false;
290     return true;
291   }
292   static bool s_visit(const istorable& a_o,iobj_const_visitor& a_v){
293     const Annotation* local = safe_cast<istorable,Annotation>(a_o);
294     if(!local) return false;
295     return local->Annotation::visit(a_v); //IMPORTANT : have Annotation::
296   }
297 public:
298   Annotation(){}
299   virtual ~Annotation(){}
300 private:
301   Annotation(const Annotation& a_from):istorable(a_from){}
302   Annotation& operator=(const Annotation&){return *this;}
303 public:
304   std::vector<Item> m_items;
305 };
306 
307 inline bool Annotation_read(iobj_visitor& a_visitor) {
308   //if(!a_visitor.begin(*this)) return false;
309   int version;
310   if(!a_visitor.visit(version)) return false;
311   std::vector<Item> fItems;
312   if(!std_vector_read<Item>(a_visitor,fItems)) return false;
313   //if(!a_visitor.end(*this)) return false;
314   return true;
315 }
316 
317 inline void map2vec(const std::map<std::string,std::string>& a_in,std::vector<Item>& a_out) {
318   a_out.clear();
319   std::map<std::string,std::string>::const_iterator it;
320   for(it=a_in.begin();it!=a_in.end();++it) {
321     a_out.push_back(Item((*it).first,(*it).second,false));
322   }
323 }
324 
325 typedef histo::histo_data<double,unsigned int,unsigned int,double> hd_data;
326 
327 template <class HIST>
328 class BaseHistogram : public virtual istorable {
329 public:
330   TOOLS_T_SCLASS(HIST,tools::osc::BaseHistogram)
331 protected:
332   virtual void* cast(const std::string& a_class) const {
333     if(void* p = cmp_cast<BaseHistogram>(this,a_class)) return p;
334     return 0;
335   }
336 public:
337   virtual const std::string& store_cls() const {return s_base_histogram();}
338   virtual bool visit(iobj_const_visitor& a_v) const {
339     if(!a_v.begin(*this,s_base_histogram(),BaseHistogram::s_visit)) return false;
340 
341     int version = 1;
342     if(!a_v.visit("fVersion",version)) return false;
343 
344     Annotation ano;
345     map2vec(m_hist.annotations(),ano.m_items);
346     if(!a_v.visit("fAnnotation",ano)) return false;
347 
348     if(!a_v.end(*this)) return false;
349     return true;
350   }
351 protected:
352   static bool s_visit(const istorable& a_o,iobj_const_visitor& a_v){
353     const BaseHistogram* local =
354       safe_cast<istorable,BaseHistogram>(a_o);
355     if(!local) return false;
356     return local->BaseHistogram::visit(a_v); //IMPORTANT : have BaseHistogram::
357   }
358 public:
359   BaseHistogram(const HIST& a_hist):m_hist(a_hist){}
360   virtual ~BaseHistogram(){}
361 private:
362   BaseHistogram(const BaseHistogram& a_from)
363   :istorable(a_from),m_hist(a_from.m_hist)
364   {}
365   BaseHistogram& operator=(const BaseHistogram&){return *this;}
366 protected:
367   const HIST& m_hist;
368 };
369 
370 inline bool BaseHistogram_read(iobj_visitor& a_visitor){
371   //if(!a_visitor.begin(*this)) return false;
372 
373   int version;
374   if(!a_visitor.visit(version)) return false;
375 
376   if(!Annotation_read(a_visitor)) return false;
377 
378   //if(!a_visitor.end(*this)) return false;
379   return true;
380 }
381 
382 template <class HIST>
383 inline bool visitHistogram(const HIST& a_hist,iobj_const_visitor& a_visitor){
384   if(!a_visitor.visit("fTitle",a_hist.title())) return false;
385   if(!a_visitor.visit("fDimension",(int)a_hist.dimension())) return false;
386   if(!a_visitor.visit("fBinNumber",(int)a_hist.get_bins())) return false;
387 
388   std::vector<int> bins_entries;
389   convert<unsigned int,int>(a_hist.bins_entries(),bins_entries);
390   if(!a_visitor.visit("fBinEntries",bins_entries)) return false;
391 
392   if(!a_visitor.visit("fBinSw",a_hist.bins_sum_w())) return false;
393   if(!a_visitor.visit("fBinSw2",a_hist.bins_sum_w2())) return false;
394   if(!a_visitor.visit("fBinSxw",a_hist.bins_sum_xw())) return false;
395   if(!a_visitor.visit("fBinSx2w",a_hist.bins_sum_x2w())) return false;
396  {std::string name;
397   for(unsigned int iaxis=0;iaxis<a_hist.dimension();iaxis++) {
398     if(!num2s(iaxis,name)) return false;
399     name = "fAxes_"+name;
400     Axis axis(a_hist.get_axis(iaxis));
401     if(!a_visitor.visit(name,axis)) return false;
402   }}
403  {int dummy = 0;
404   if(!a_visitor.visit("fMode",dummy)) return false;} //m_mode
405   if(!a_visitor.visit("fProfile",false)) return false;
406  {std::vector<double> dummy;
407   if(!a_visitor.visit("fBinSvw",dummy)) return false;
408   if(!a_visitor.visit("fBinSv2w",dummy)) return false;}
409   if(!a_visitor.visit("fCutV",false)) return false;
410  {double dummy = 0;
411   if(!a_visitor.visit("fMinV",dummy)) return false;
412   if(!a_visitor.visit("fMaxV",dummy)) return false;}
413   // Not written :
414   //a_hist.fDoubles
415   //a_hist.fInts
416   return true;
417 }
418 
419 inline bool readHistogram(hd_data& aData,iobj_visitor& a_visitor){
420   if(!a_visitor.visit(aData.m_title)) return false;
421  {int dim;
422   if(!a_visitor.visit(dim)) return false;
423   aData.m_dimension = dim;}
424 
425  {int nbin;
426   if(!a_visitor.visit(nbin)) return false;
427   aData.m_bin_number = nbin;}
428 
429  {std::vector<int> vec;
430   if(!a_visitor.visit(vec)) return false;
431   convert<int,unsigned int>(vec,aData.m_bin_entries);}
432 
433   if(!a_visitor.visit(aData.m_bin_Sw)) return false;
434   if(!a_visitor.visit(aData.m_bin_Sw2)) return false;
435   if(!a_visitor.visit(aData.m_bin_Sxw)) return false;
436   if(!a_visitor.visit(aData.m_bin_Sx2w)) return false;
437   aData.m_axes.clear();
438   for(unsigned int iaxis=0;iaxis<aData.m_dimension;iaxis++) {
439     histo::axis<double,unsigned int> baxis;
440     if(!Axis_read(a_visitor,baxis)) return false;
441     aData.m_axes.push_back(baxis);
442   }
443  {int dummy;
444   if(!a_visitor.visit(dummy)) return false;} //m_mode
445 
446  {bool dummy;
447   if(!a_visitor.visit(dummy)) return false;} //m_is_profile
448 
449  {std::vector<double> dummy;
450   if(!a_visitor.visit(dummy)) return false;} //m_bin_Svw
451 
452  {std::vector<double> dummy;
453   if(!a_visitor.visit(dummy)) return false;} //m_bin_Sv2w
454 
455  {bool dummy;
456   if(!a_visitor.visit(dummy)) return false;} //m_cut_v
457 
458  {double dummy;
459   if(!a_visitor.visit(dummy)) return false;} //aData.m_min_v
460 
461  {double dummy;
462   if(!a_visitor.visit(dummy)) return false;} //aData.m_max_v
463 
464   //aData.fDoubles
465   //aData.fInts
466   //aData.m_coords.resize(aData.m_dimension,0);
467   //aData.m_ints.resize(aData.m_dimension,0);
468 
469   return true;
470 }
471 
472 template <class HIST>
473 class Histogram : public virtual istorable {
474 public:
475   TOOLS_T_SCLASS(HIST,tools::osc::Histogram)
476 protected:
477   virtual void* cast(const std::string& a_class) const {
478     if(void* p = cmp_cast<Histogram>(this,a_class)) return p;
479     return 0;
480   }
481 public:
482   virtual const std::string& store_cls() const {return m_cls;}
483   virtual bool visit(iobj_const_visitor& a_v) const {
484     if(!a_v.begin(*this,m_cls,Histogram::s_visit)) return false;
485 
486     int version = 1;
487     if(!a_v.visit("fVersion",version)) return false;
488 
489     BaseHistogram<HIST> bh(m_hist);
490     if(!bh.visit(a_v)) return false;
491 
492     if(!visitHistogram(m_hist,a_v)) return false;
493 
494     if(!a_v.end(*this)) return false;
495     return true;
496   }
497 protected:
498   static bool s_visit(const istorable& a_o,iobj_const_visitor& a_v){
499     const Histogram* local = safe_cast<istorable,Histogram>(a_o);
500     if(!local) return false;
501     return local->Histogram::visit(a_v); //IMPORTANT : have Histogram::
502   }
503 public:
504   Histogram(const HIST& a_hist,const std::string& a_cls)
505   :m_hist(a_hist),m_cls(a_cls){}
506   virtual ~Histogram(){}
507 public:
508   Histogram(const Histogram& a_from)
509   :istorable(a_from)
510   ,m_hist(a_from.m_hist)
511   ,m_cls(a_from.m_cls)
512   {}
513   Histogram& operator=(const Histogram& a_from){
514     m_cls = a_from.m_cls;
515     return *this;
516   }
517 protected:
518   const HIST& m_hist;
519   std::string m_cls;
520 };
521 
522 inline bool visit(iobj_const_visitor& a_v,const histo::h1d& a_histo) {
523   Histogram<histo::h1d> h(a_histo,s_h1d());
524   return h.visit(a_v);
525 }
526 
527 inline bool read(iobj_visitor& a_visitor,histo::h1d& a_histo){
528   //if(!a_visitor.begin(*this)) return false;
529 
530   int version;
531   if(!a_visitor.visit(version)) return false;
532 
533   if(version!=1) {
534     //this may come from an unexpected byteswap.
535     a_visitor.out() << "tools::osc::read :"
536                     << " unexpected version " << version
537                     << std::endl;
538     return false;
539   }
540 
541   if(!BaseHistogram_read(a_visitor)) return false;
542 
543   hd_data hdata;
544   if(!readHistogram(hdata,a_visitor)) return false;
545 
546   hdata.update_fast_getters();
547   a_histo.copy_from_data(hdata);
548 
549   //fAxis.copy(fHistogram.get_axis(0));
550 
551   //if(!a_visitor.end(*this)) return false;
552 
553   return true;
554 }
555 
556 inline bool visit(iobj_const_visitor& a_v,const histo::h2d& a_histo) {
557   Histogram<histo::h2d> h(a_histo,s_h2d());
558   return h.visit(a_v);
559 }
560 
561 inline bool read(iobj_visitor& a_visitor,histo::h2d& a_histo){
562   //if(!a_visitor.begin(*this)) return false;
563 
564   int version;
565   if(!a_visitor.visit(version)) return false;
566 
567   if(version!=1) {
568     //this may come from an unexpected byteswap.
569     a_visitor.out() << "tools::osc::read :"
570                     << " unexpected version " << version
571                     << std::endl;
572     return false;
573   }
574 
575   if(!BaseHistogram_read(a_visitor)) return false;
576 
577   hd_data hdata;
578   if(!readHistogram(hdata,a_visitor)) return false;
579 
580   hdata.update_fast_getters();
581   a_histo.copy_from_data(hdata);
582 
583   //fAxisX.copy(fHistogram.get_axis(0));
584   //fAxisY.copy(fHistogram.get_axis(1));
585 
586   //if(!a_visitor.end(*this)) return false;
587 
588   return true;
589 }
590 
591 inline bool visit(iobj_const_visitor& a_v,const histo::h3d& a_histo) {
592   Histogram<histo::h3d> h(a_histo,s_h3d());
593   return h.visit(a_v);
594 }
595 
596 inline bool read(iobj_visitor& a_visitor,histo::h3d& a_histo){
597   //if(!a_visitor.begin(*this)) return false;
598 
599   int version;
600   if(!a_visitor.visit(version)) return false;
601 
602   if(version!=1) {
603     //this may come from an unexpected byteswap.
604     a_visitor.out() << "tools::osc::read :"
605                     << " unexpected version " << version
606                     << std::endl;
607     return false;
608   }
609 
610   if(!BaseHistogram_read(a_visitor)) return false;
611 
612   hd_data hdata;
613   if(!readHistogram(hdata,a_visitor)) return false;
614   hdata.update_fast_getters();
615   a_histo.copy_from_data(hdata);
616 
617   //fAxisX.copy(fHistogram.get_axis(0));
618   //fAxisY.copy(fHistogram.get_axis(1));
619   //fAxisZ.copy(fHistogram.get_axis(2));
620 
621   //if(!a_visitor.end(*this)) return false;
622 
623   return true;
624 }
625 
626 typedef histo::profile_data<double,unsigned int,unsigned int,double,double> pd_data;
627 
628 template <class PROF>
629 inline bool visitProfile(const PROF& a_prof,iobj_const_visitor& a_visitor){
630   if(!a_visitor.visit("fTitle",a_prof.title())) return false;
631   if(!a_visitor.visit("fDimension",(int)a_prof.dimension())) return false;
632   if(!a_visitor.visit("fBinNumber",(int)a_prof.get_bins())) return false;
633 
634   std::vector<int> bins_entries;
635   convert<unsigned int,int>(a_prof.bins_entries(),bins_entries);
636   if(!a_visitor.visit("fBinEntries",bins_entries)) return false;
637 
638   if(!a_visitor.visit("fBinSw",a_prof.bins_sum_w())) return false;
639   if(!a_visitor.visit("fBinSw2",a_prof.bins_sum_w2())) return false;
640   if(!a_visitor.visit("fBinSxw",a_prof.bins_sum_xw())) return false;
641   if(!a_visitor.visit("fBinSx2w",a_prof.bins_sum_x2w())) return false;
642   std::string name;
643   for(unsigned int iaxis=0;iaxis<a_prof.dimension();iaxis++) {
644     if(!num2s(iaxis,name)) return false;
645     name = "fAxes_"+name;
646     Axis axis(a_prof.get_axis(iaxis));
647     if(!a_visitor.visit(name,axis)) return false;
648   }
649  {int dummy = 0;
650   if(!a_visitor.visit("fMode",dummy)) return false;} //m_mode
651   if(!a_visitor.visit("fProfile",true)) return false;
652   if(!a_visitor.visit("fBinSvw",a_prof.bins_sum_vw())) return false;
653   if(!a_visitor.visit("fBinSv2w",a_prof.bins_sum_v2w())) return false;
654   if(!a_visitor.visit("fCutV",a_prof.cut_v())) return false;
655   if(!a_visitor.visit("fMinV",a_prof.min_v())) return false;
656   if(!a_visitor.visit("fMaxV",a_prof.max_v())) return false;
657   // Not written :
658   //a_prof.fDoubles
659   //a_prof.fInts
660   return true;
661 }
662 
663 template <class PROF>
664 class Profile : public virtual istorable {
665 public:
666   TOOLS_T_SCLASS(PROF,tools::osc::Profile)
667 protected:
668   virtual void* cast(const std::string& a_class) const {
669     if(void* p = cmp_cast<Profile>(this,a_class)) return p;
670     return 0;
671   }
672 public:
673   virtual const std::string& store_cls() const {return m_cls;}
674   virtual bool visit(iobj_const_visitor& a_v) const {
675     if(!a_v.begin(*this,m_cls,Profile::s_visit)) return false;
676 
677     int version = 1;
678     if(!a_v.visit("fVersion",version)) return false;
679 
680     BaseHistogram<PROF> bh(m_hist);
681     if(!bh.visit(a_v)) return false;
682 
683     if(!visitProfile(m_hist,a_v)) return false;
684 
685     if(!a_v.end(*this)) return false;
686     return true;
687   }
688 protected:
689   static bool s_visit(const istorable& a_o,iobj_const_visitor& a_v){
690     const Profile* local = safe_cast<istorable,Profile>(a_o);
691     if(!local) return false;
692     return local->Profile::visit(a_v); //IMPORTANT : have Profile::
693   }
694 public:
695   Profile(const PROF& a_hist,const std::string& a_cls)
696   :m_hist(a_hist),m_cls(a_cls){}
697   virtual ~Profile(){}
698 public:
699   Profile(const Profile& a_from)
700   :istorable(a_from)
701   ,m_hist(a_from.m_hist)
702   ,m_cls(a_from.m_cls)
703   {}
704   Profile& operator=(const Profile& a_from){
705     m_cls = a_from.m_cls;
706     return *this;
707   }
708 protected:
709   const PROF& m_hist;
710   std::string m_cls;
711 };
712 
713 inline bool readProfile(pd_data& aData,iobj_visitor& a_visitor){
714   if(!a_visitor.visit(aData.m_title)) return false;
715 
716  {int dim;
717   if(!a_visitor.visit(dim)) return false;
718   aData.m_dimension = dim;}
719 
720  {int nbin;
721   if(!a_visitor.visit(nbin)) return false;
722   aData.m_bin_number = nbin;}
723 
724  {std::vector<int> vec;
725   if(!a_visitor.visit(vec)) return false;
726   convert<int,unsigned int>(vec,aData.m_bin_entries);}
727 
728   if(!a_visitor.visit(aData.m_bin_Sw)) return false;
729   if(!a_visitor.visit(aData.m_bin_Sw2)) return false;
730   if(!a_visitor.visit(aData.m_bin_Sxw)) return false;
731   if(!a_visitor.visit(aData.m_bin_Sx2w)) return false;
732   aData.m_axes.clear();
733   for(unsigned int iaxis=0;iaxis<aData.m_dimension;iaxis++) {
734     histo::axis<double,unsigned int> baxis;
735     if(!Axis_read(a_visitor,baxis)) return false;
736     aData.m_axes.push_back(baxis);
737   }
738  {int dummy;
739   if(!a_visitor.visit(dummy)) return false;} //m_mode
740   if(!a_visitor.visit(aData.m_is_profile)) return false;
741   if(!a_visitor.visit(aData.m_bin_Svw)) return false;
742   if(!a_visitor.visit(aData.m_bin_Sv2w)) return false;
743   if(!a_visitor.visit(aData.m_cut_v)) return false;
744   if(!a_visitor.visit(aData.m_min_v)) return false;
745   if(!a_visitor.visit(aData.m_max_v)) return false;
746 
747   // Not written :
748   //aData.fDoubles
749   //aData.fInts
750   //aData.m_coords.resize(aData.m_dimension,0);
751   //aData.m_ints.resize(aData.m_dimension,0);
752 
753   return true;
754 }
755 
756 inline bool visit(iobj_const_visitor& a_v,const histo::p1d& a_histo) {
757   Profile<histo::p1d> h(a_histo,s_p1d());
758   return h.visit(a_v);
759 }
760 
761 inline bool read(iobj_visitor& a_visitor,histo::p1d& a_histo){
762   //if(!a_visitor.begin(*this)) return false;
763 
764   int version;
765   if(!a_visitor.visit(version)) return false;
766 
767   if(version!=1) {
768     //this may come from an unexpected byteswap.
769     a_visitor.out() << "tools::osc::read :"
770                     << " unexpected version " << version
771                     << std::endl;
772     return false;
773   }
774 
775 
776   if(!BaseHistogram_read(a_visitor)) return false;
777 
778   pd_data hdata;
779   if(!readProfile(hdata,a_visitor)) return false;
780   hdata.update_fast_getters();
781   a_histo.copy_from_data(hdata);
782 
783   //fAxis.copy(fHistogram.get_axis(0));
784 
785   //if(!a_visitor.end(*this)) return false;
786 
787   return true;
788 }
789 
790 inline bool visit(iobj_const_visitor& a_v,const histo::p2d& a_histo) {
791   Profile<histo::p2d> h(a_histo,s_p2d());
792   return h.visit(a_v);
793 }
794 
795 inline bool read(iobj_visitor& a_visitor,histo::p2d& a_histo){
796   //if(!a_visitor.begin(*this)) return false;
797 
798   int version;
799   if(!a_visitor.visit(version)) return false;
800 
801   if(version!=1) {
802     //this may come from an unexpected byteswap.
803     a_visitor.out() << "tools::osc::read :"
804                     << " unexpected version " << version
805                     << std::endl;
806     return false;
807   }
808 
809 
810   if(!BaseHistogram_read(a_visitor)) return false;
811 
812   pd_data hdata;
813   if(!readProfile(hdata,a_visitor)) return false;
814   hdata.update_fast_getters();
815   a_histo.copy_from_data(hdata);
816 
817   //fAxisX.copy(a_histo.get_axis(0));
818   //fAxisY.copy(a_histo.get_axis(1));
819 
820   //if(!a_visitor.end(*this)) return false;
821 
822   return true;
823 }
824 
825 template <class HIST>
826 class Histogram_cp : public Histogram<HIST> {
827   typedef Histogram<HIST> parent;
828 public:
829   Histogram_cp(const HIST& a_h,const std::string& a_cls)
830   :parent(m_cp,a_cls) //give ref of m_cp to Histogram.
831   ,m_cp(a_h)   //do a local copy.
832   //WARNING : the upper is ok as long as Histogram constructor does nothing
833   //          else than keeping the ref to m_cp. Else it would do
834   //          something on an empty histo (and not on a copy of the
835   //          passed a_data).
836   {}
837   virtual ~Histogram_cp(){}
838 public:
839   Histogram_cp(const Histogram_cp& a_from)
840   :istorable(a_from)
841   ,parent(m_cp,a_from.m_cls)
842   ,m_cp(a_from.m_cp)
843   {}
844   Histogram_cp& operator=(const Histogram_cp& a_from){
845     parent::operator=(a_from);
846     m_cp = a_from.m_cp;
847     return *this;
848   }
849 protected:
850   HIST m_cp;
851 };
852 
853 template <class PROF>
854 class Profile_cp : public Profile<PROF> {
855   typedef Profile<PROF> parent;
856 public:
857   Profile_cp(const PROF& a_p,const std::string& a_cls)
858   :parent(m_cp,a_cls) //give ref of m_cp to Profile.
859   ,m_cp(a_p)   //do a local copy.
860   //WARNING : the upper is ok as long as Profile constructor does nothing
861   //          else than keeping the ref to m_cp. Else it would do
862   //          something on an empty histo (and not on a copy of the
863   //          passed a_data).
864   {}
865   virtual ~Profile_cp(){}
866 public:
867   Profile_cp(const Profile_cp& a_from)
868   :istorable(a_from)
869   ,parent(m_cp,a_from.m_cls)
870   ,m_cp(a_from.m_cp)
871   {}
872   Profile_cp& operator=(const Profile_cp& a_from){
873     parent::operator=(a_from);
874     m_cp = a_from.m_cp;
875     return *this;
876   }
877 protected:
878   PROF m_cp;
879 };
880 
881 }}
882 
883 #endif