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[ 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_wroot_streamers
  5 #define tools_wroot_streamers
  6 
  7 #include "named"
  8 #include "directory"
  9 #include "file"
 10 #include "../vmanip" //convert
 11 
 12 #include "../histo/h1d"
 13 #include "../histo/h2d"
 14 #include "../histo/h3d"
 15 #include "../histo/p1d"
 16 #include "../histo/p2d"
 17 
 18 #include "../histo/h1df"
 19 #include "../histo/h2df"
 20 #include "../histo/h3df"
 21 
 22 namespace tools {
 23 namespace wroot {
 24 
 25 typedef histo::histo_data<double,unsigned int,unsigned int,double> hd_data;
 26 typedef histo::histo_data<double,unsigned int,unsigned int,float> hf_data;
 27 typedef histo::profile_data<double,unsigned int,unsigned int,double,double> pd_data;
 28 
 29 inline bool AttAxis_stream(buffer& a_buffer) {
 30   int fNdivisions = 510;   //Number of divisions(10000*n3 + 100*n2 + n1)
 31   short fAxisColor = 1;    //color of the line axis
 32   short fLabelColor = 1;   //color of labels
 33   short fLabelFont = 62;    //font for labels
 34   float fLabelOffset = 0.005F;  //offset of labels
 35   float fLabelSize = 0.04F;    //size of labels
 36   float fTickLength = 0.03F;   //length of tick marks
 37   float fTitleOffset = 1;  //offset of axis title
 38   float fTitleSize = 0.04F;    //size of axis title
 39   short fTitleColor = 1;   //color of axis title
 40   short fTitleFont = 62;    //font for axis title
 41 
 42   // Version 4 streaming (ROOT/v3-00-6).
 43   unsigned int beg;
 44   if(!a_buffer.write_version(4,beg)) return false;
 45 
 46   if(!a_buffer.write(fNdivisions)) return false;
 47   if(!a_buffer.write(fAxisColor)) return false;
 48   if(!a_buffer.write(fLabelColor)) return false;
 49   if(!a_buffer.write(fLabelFont)) return false;
 50   if(!a_buffer.write(fLabelOffset)) return false;
 51   if(!a_buffer.write(fLabelSize)) return false;
 52   if(!a_buffer.write(fTickLength)) return false;
 53   if(!a_buffer.write(fTitleOffset)) return false;
 54   if(!a_buffer.write(fTitleSize)) return false;
 55   if(!a_buffer.write(fTitleColor)) return false;
 56   if(!a_buffer.write(fTitleFont)) return false;
 57 
 58   if(!a_buffer.set_byte_count(beg)) return false;
 59   return true;
 60 }
 61 
 62 inline bool axis_stream(buffer& a_buffer,
 63                         const histo::axis<double,unsigned int>& a_axis,
 64                         const std::string& a_name,
 65                         const std::string& a_title) {
 66     // Version 6 streaming (ROOT/v3-00-6).
 67 
 68     unsigned int beg;
 69     if(!a_buffer.write_version(6,beg)) return false;
 70 
 71     if(!Named_stream(a_buffer,a_name,a_title)) return false;
 72 
 73     if(!AttAxis_stream(a_buffer)) return false;
 74 
 75     if(!a_buffer.write(a_axis.bins())) return false;
 76     if(!a_buffer.write(a_axis.lower_edge())) return false;
 77     if(!a_buffer.write(a_axis.upper_edge())) return false;
 78 
 79     // fXbins
 80     //if(a_axis.m_fixed) {
 81     //  std::vector<double> v;
 82     //  ArrayT<double> dummy(v);
 83     //  if(!dummy.stream(a_buffer)) return false; //TArrayD
 84     //} else {
 85       if(!a_buffer.write_array(a_axis.edges())) return false; //TArrayD
 86     //}
 87 
 88     if(!a_buffer.write((int)0)) return false; //fFirst
 89     if(!a_buffer.write((int)0)) return false; //fLast
 90 
 91     //Bool_t
 92     if(!a_buffer.write((unsigned char)0)) return false;  //TimeDisplay
 93 
 94     //TString
 95     if(!a_buffer.write(std::string())) return false; //TimeFormat
 96 
 97     if(!a_buffer.set_byte_count(beg)) return false;
 98 
 99     return true;
100 }
101 
102 inline bool List_empty_stream(buffer& a_buffer) {
103   unsigned int beg;
104   if(!a_buffer.write_version(4,beg)) return false;
105   if(!Object_stream(a_buffer)) return false;
106   std::string name;
107   if(!a_buffer.write(name)) return false;
108   int nobjects = 0;
109   if(!a_buffer.write(nobjects)) return false;
110   if(!a_buffer.set_byte_count(beg)) return false;
111   return true;
112 }
113 
114 template <class HIST>
115 inline std::string axis_title(const HIST& a_h,const std::string& a_key) {
116   typedef std::map<std::string,std::string> annotations_t;
117   annotations_t::const_iterator it = a_h.annotations().find(a_key);
118   if(it==a_h.annotations().end()) return std::string();
119   return (*it).second;
120 }
121 
122 template <class HIST>
123 inline bool TH_write_1D(buffer& a_buffer,
124                         const HIST& a_h,
125                         const std::string& a_name,
126                         const std::vector<double>& a_bin_Sw2) {
127 
128     if(!a_buffer.write_version(3)) return false;
129 
130     if(!Named_stream(a_buffer,a_name,a_h.title())) return false;
131 
132     if(!AttLine_stream(a_buffer)) return false;
133     if(!AttFill_stream(a_buffer)) return false;
134     if(!AttMarker_stream(a_buffer)) return false;
135 
136     if(!a_buffer.write((int)a_h.get_bins())) return false;
137 
138     //fXAxis,fYAxis,fZAxis
139     if(a_h.dimension()==3) {
140 
141      {histo::axis<double,unsigned int> haxis(a_h.get_axis(0));
142       if(!axis_stream(a_buffer,haxis,"xaxis",axis_title(a_h,histo::key_axis_x_title()))) return false;}
143 
144      {histo::axis<double,unsigned int> haxis(a_h.get_axis(1));
145       if(!axis_stream(a_buffer,haxis,"yaxis",axis_title(a_h,histo::key_axis_y_title()))) return false;}
146 
147      {histo::axis<double,unsigned int> haxis(a_h.get_axis(2));
148       if(!axis_stream(a_buffer,haxis,"zaxis",axis_title(a_h,histo::key_axis_z_title()))) return false;}
149 
150     } else if(a_h.dimension()==2) {
151 
152      {histo::axis<double,unsigned int> haxis(a_h.get_axis(0));
153       if(!axis_stream(a_buffer,haxis,"xaxis",axis_title(a_h,histo::key_axis_x_title()))) return false;}
154 
155      {histo::axis<double,unsigned int> haxis(a_h.get_axis(1));
156       if(!axis_stream(a_buffer,haxis,"yaxis",axis_title(a_h,histo::key_axis_y_title()))) return false;}
157 
158      {histo::axis<double,unsigned int> dummy;
159       dummy.configure(1,0,1);
160       if(!axis_stream(a_buffer,dummy,"zaxis",axis_title(a_h,histo::key_axis_z_title()))) return false;}
161 
162     } else if(a_h.dimension()==1) {
163 
164      {histo::axis<double,unsigned int> haxis(a_h.get_axis(0));
165       if(!axis_stream(a_buffer,haxis,"xaxis",axis_title(a_h,histo::key_axis_x_title()))) return false;}
166 
167      {histo::axis<double,unsigned int> dummy;
168       dummy.configure(1,0,1);
169       if(!axis_stream(a_buffer,dummy,"yaxis",axis_title(a_h,histo::key_axis_y_title()))) return false;}
170 
171      {histo::axis<double,unsigned int> dummy;
172       dummy.configure(1,0,1);
173       if(!axis_stream(a_buffer,dummy,"zaxis",axis_title(a_h,histo::key_axis_z_title()))) return false;}
174 
175     } else {
176       return false;
177     }
178 
179     if(!a_buffer.write((short)(1000 * 0.25))) return false; //fBarOffset
180     if(!a_buffer.write((short)(1000 * 0.5))) return false; //fBarWidth
181 
182     if(!a_buffer.write((double)a_h.all_entries())) return false;
183     if(!a_buffer.write((double)a_h.get_in_range_Sw())) return false;  //enforce double in case h1df
184     if(!a_buffer.write((double)a_h.get_in_range_Sw2())) return false; //idem
185 
186    {double value;
187     a_h.get_ith_axis_Sxw(0,value);
188     if(!a_buffer.write(value)) return false;}
189 
190    {double value;
191     a_h.get_ith_axis_Sx2w(0,value);
192     if(!a_buffer.write(value)) return false;}
193 
194     if(!a_buffer.write((double)-1111)) return false; //fMaximum
195     if(!a_buffer.write((double)-1111)) return false; //fMinimum
196     if(!a_buffer.write((double)0)) return false; //NormFactor
197 
198     if(!a_buffer.write_array(std::vector<double>())) return false; //fContour TArrayD
199 
200     if(!a_buffer.write_array(a_bin_Sw2)) return false; //fSumw2 TArrayD
201 
202     // store annotation on fOption
203     // but try to fool CERN-ROOT in order that it does not
204     // understand fOption as.. CERN-ROOT options !
205    //{std::string opt = " "+fAnnotation;
206    // opt[0] = 0; //awfull trick
207    // if(!a_buffer.write(opt)) return false;} //TString fOption
208    {std::string opt;
209     if(!a_buffer.write(opt)) return false;} //TString fOption
210 
211     if(!List_empty_stream(a_buffer)) return false; //*TList fFunctions
212 
213     return true;
214 }
215 
216 template <class HIST>
217 inline bool TH_write_2D(buffer& a_buffer,
218                         const HIST& a_h,
219                         const std::string& a_name,
220                         const std::vector<double>& a_bin_Sw2) {
221   if(!a_buffer.write_version(3)) return false;
222   if(!TH_write_1D(a_buffer,a_h,a_name,a_bin_Sw2)) return false;
223   if(!a_buffer.write((double)1)) return false; //ScaleFactor
224 
225  {double value;
226   a_h.get_ith_axis_Sxw(1,value);
227   if(!a_buffer.write(value)) return false;}
228 
229  {double value;
230   a_h.get_ith_axis_Sx2w(1,value);
231   if(!a_buffer.write(value)) return false;}
232 
233   if(!a_buffer.write((double)a_h.Sxyw())) return false; //Tsumwxy
234 
235   return true;
236 }
237 
238 template <class HIST>
239 inline bool TH_write_3D(buffer& a_buffer,
240                         const HIST& a_h,
241                         const std::string& a_name,
242                         const std::vector<double>& a_bin_Sw2) {
243   if(!a_buffer.write_version(4)) return false;
244   if(!TH_write_1D(a_buffer,a_h,a_name,a_bin_Sw2)) return false;
245   if(!Att3D_stream(a_buffer)) return false;
246 
247  {double value;
248   a_h.get_ith_axis_Sxw(1,value);
249   if(!a_buffer.write(value)) return false;}    //Tsumwy : Total Sum of weight*Y
250  {double value;
251   a_h.get_ith_axis_Sx2w(1,value);
252   if(!a_buffer.write(value)) return false;}    //Tsumwy2 : Total Sum of weight*Y*Y
253   if(!a_buffer.write((double)a_h.Sxyw())) return false; //Tsumwxy : Total Sum of weight*X*Y
254 
255  {double value;
256   a_h.get_ith_axis_Sxw(2,value);
257   if(!a_buffer.write(value)) return false;}    //Tsumwz : Total Sum of weight*Z
258  {double value;
259   a_h.get_ith_axis_Sx2w(2,value);
260   if(!a_buffer.write(value)) return false;}    //Tsumwz2 : Total Sum of weight*Z*Z
261   if(!a_buffer.write((double)a_h.Szxw())) return false; //Tsumwxz : Total Sum of weight*X*Z
262   if(!a_buffer.write((double)a_h.Syzw())) return false; //Tsumwyz : Total Sum of weight*Y*Z
263 
264   return true;
265 }
266 
267 inline bool TH1F_stream(buffer& a_buffer,const histo::h1df& a_h,const std::string& a_name) {
268   if(!a_buffer.write_version(1)) return false;
269   std::vector<double> bins_sum_w2d;
270   convert<float,double>(a_h.bins_sum_w2(),bins_sum_w2d);
271   if(!TH_write_1D(a_buffer,a_h,a_name,bins_sum_w2d)) return false;
272   if(!a_buffer.write_array(a_h.bins_sum_w())) return false;
273   return true;
274 }
275 
276 inline bool TH1F_stream(buffer& a_buffer,const histo::h1d& a_h,const std::string& a_name) {
277   if(!a_buffer.write_version(1)) return false;
278   if(!TH_write_1D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
279   std::vector<float> bins_sum_wf;
280   convert<double,float>(a_h.bins_sum_w(),bins_sum_wf);
281   if(!a_buffer.write_array(bins_sum_wf)) return false;
282   return true;
283 }
284 
285 inline bool TH1D_stream(buffer& a_buffer,const histo::h1d& a_h,const std::string& a_name) {
286   if(!a_buffer.write_version(1)) return false;
287   if(!TH_write_1D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
288   if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
289   return true;
290 }
291 
292 inline bool TH2F_stream(buffer& a_buffer,const histo::h2d& a_h,const std::string& a_name){
293   if(!a_buffer.write_version(3)) return false;
294   if(!TH_write_2D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
295   std::vector<float> bins_sum_wf;
296   convert<double,float>(a_h.bins_sum_w(),bins_sum_wf);
297   if(!a_buffer.write_array(bins_sum_wf)) return false; //fArray TArrayF
298   return true;
299 }
300 
301 inline bool TH2F_stream(buffer& a_buffer,const histo::h2df& a_h,const std::string& a_name){
302   if(!a_buffer.write_version(3)) return false;
303   std::vector<double> bins_sum_w2d;
304   convert<float,double>(a_h.bins_sum_w2(),bins_sum_w2d);
305   if(!TH_write_2D(a_buffer,a_h,a_name,bins_sum_w2d)) return false;
306   if(!a_buffer.write_array(a_h.bins_sum_w())) return false;
307   return true;
308 }
309 
310 inline bool TH2D_stream(buffer& a_buffer,const histo::h2d& a_h,const std::string& a_name){
311   if(!a_buffer.write_version(3)) return false;
312   if(!TH_write_2D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
313   if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
314   return true;
315 }
316 
317 inline bool TH3F_stream(buffer& a_buffer,const histo::h3d& a_h,const std::string& a_name){
318   if(!a_buffer.write_version(3)) return false;
319   if(!TH_write_3D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
320   std::vector<float> bins_sum_wf;
321   convert<double,float>(a_h.bins_sum_w(),bins_sum_wf);
322   if(!a_buffer.write_array(bins_sum_wf)) return false; //fArray TArrayF
323   return true;
324 }
325 
326 inline bool TH3F_stream(buffer& a_buffer,const histo::h3df& a_h,const std::string& a_name){
327   if(!a_buffer.write_version(3)) return false;
328   std::vector<double> bins_sum_w2d;
329   convert<float,double>(a_h.bins_sum_w2(),bins_sum_w2d);
330   if(!TH_write_3D(a_buffer,a_h,a_name,bins_sum_w2d)) return false;
331   if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayF
332   return true;
333 }
334 
335 inline bool TH3D_stream(buffer& a_buffer,const histo::h3d& a_h,const std::string& a_name){
336   if(!a_buffer.write_version(3)) return false;
337   if(!TH_write_3D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
338   if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
339   return true;
340 }
341 
342 inline bool TProfile_stream(buffer& a_buffer,const histo::p1d& a_p,const std::string& a_name){
343   if(!a_buffer.write_version(4)) return false;
344 
345   //WARNING : the mapping histo::p1d / TProfile is not obvious.
346   //p1d::m_bin_Svw  <---> TProfile::fArray
347   //p1d::m_bin_Sv2w <---> TProfile::fSumw2
348   //p1d::m_bin_Sw   <---> TProfile::fBinEntries
349 
350   // TH1D_stream(a_buffer,h,a_name) :
351   //if(!a_buffer.write_version(1)) return false;
352   //if(!TH_write_1D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false; //fSumw2 TArrayD
353   //if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
354   // but for profile :
355   if(!a_buffer.write_version(1)) return false;
356   if(!TH_write_1D(a_buffer,a_p,a_name,a_p.bins_sum_v2w())) return false; //fSumw2 TArrayD
357   if(!a_buffer.write_array(a_p.bins_sum_vw())) return false; //fArray TArrayD
358 
359   //TProfile specific :
360   if(!a_buffer.write_array(a_p.bins_sum_w())) return false; //fBinEntries TArrayD
361 
362   int errorMode = 0;
363   if(!a_buffer.write(errorMode)) return false;
364   if(!a_buffer.write(a_p.min_v())) return false;
365   if(!a_buffer.write(a_p.max_v())) return false;
366 
367   // version 4 :
368   if(!a_buffer.write(a_p.get_Svw())) return false;              //Double_t fTsumwy;  //Total Sum of weight*Y
369   if(!a_buffer.write(a_p.get_Sv2w())) return false;             //Double_t fTsumwy2; //Total Sum of weight*Y*Y
370 
371   return true;
372 }
373 
374 inline bool TProfile2D_stream(buffer& a_buffer,const histo::p2d& a_p,const std::string& a_name){
375   if(!a_buffer.write_version(5)) return false;
376 
377   //WARNING : the mapping histo::p2d / TProfile2D is not obvious.
378   //p2d::m_bin_Svw  <---> TProfile2D::fArray
379   //p2d::m_bin_Sv2w <---> TProfile2D::fSumw2
380   //p2d::m_bin_Sw   <---> TProfile2D::fBinEntries
381 
382   // TH2D_stream(a_buffer,h,a_name) :
383   //if(!a_buffer.write_version(3)) return false;
384   //if(!TH_write_2D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false; //fSumw2 TArrayD
385   //if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
386   // for profile :
387   if(!a_buffer.write_version(3)) return false;
388   if(!TH_write_2D(a_buffer,a_p,a_name,a_p.bins_sum_v2w())) return false; //fSumw2 TArrayD
389   if(!a_buffer.write_array(a_p.bins_sum_vw())) return false; //fArray TArrayD
390 
391   //TProfile2D specific :
392   if(!a_buffer.write_array(a_p.bins_sum_w())) return false; //fBinEntries TArrayD
393 
394   int errorMode = 0;
395   if(!a_buffer.write(errorMode)) return false;    //fErrorMode
396   if(!a_buffer.write(a_p.min_v())) return false; //fZmin
397   if(!a_buffer.write(a_p.max_v())) return false; //fZmax
398 
399   // version 5 :
400   if(!a_buffer.write(a_p.get_Svw())) return false;              //Double_t fTsumwz;  //Total Sum of weight*Z
401   if(!a_buffer.write(a_p.get_Sv2w())) return false;             //Double_t fTsumwz2; //Total Sum of weight*Z*Z
402 
403   return true;
404 }
405 
406 }}
407 
408 #endif