Warning, /include/Geant4/tools/wroot/streamers 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_streamers
0005 #define tools_wroot_streamers
0006
0007 #include "named"
0008 #include "directory"
0009 #include "file"
0010 #include "../vmanip" //convert
0011
0012 #include "../histo/h1d"
0013 #include "../histo/h2d"
0014 #include "../histo/h3d"
0015 #include "../histo/p1d"
0016 #include "../histo/p2d"
0017
0018 #include "../histo/h1df"
0019 #include "../histo/h2df"
0020 #include "../histo/h3df"
0021
0022 namespace tools {
0023 namespace wroot {
0024
0025 typedef histo::histo_data<double,unsigned int,unsigned int,double> hd_data;
0026 typedef histo::histo_data<double,unsigned int,unsigned int,float> hf_data;
0027 typedef histo::profile_data<double,unsigned int,unsigned int,double,double> pd_data;
0028
0029 inline bool AttAxis_stream(buffer& a_buffer) {
0030 int fNdivisions = 510; //Number of divisions(10000*n3 + 100*n2 + n1)
0031 short fAxisColor = 1; //color of the line axis
0032 short fLabelColor = 1; //color of labels
0033 short fLabelFont = 62; //font for labels
0034 float fLabelOffset = 0.005F; //offset of labels
0035 float fLabelSize = 0.04F; //size of labels
0036 float fTickLength = 0.03F; //length of tick marks
0037 float fTitleOffset = 1; //offset of axis title
0038 float fTitleSize = 0.04F; //size of axis title
0039 short fTitleColor = 1; //color of axis title
0040 short fTitleFont = 62; //font for axis title
0041
0042 // Version 4 streaming (ROOT/v3-00-6).
0043 unsigned int beg;
0044 if(!a_buffer.write_version(4,beg)) return false;
0045
0046 if(!a_buffer.write(fNdivisions)) return false;
0047 if(!a_buffer.write(fAxisColor)) return false;
0048 if(!a_buffer.write(fLabelColor)) return false;
0049 if(!a_buffer.write(fLabelFont)) return false;
0050 if(!a_buffer.write(fLabelOffset)) return false;
0051 if(!a_buffer.write(fLabelSize)) return false;
0052 if(!a_buffer.write(fTickLength)) return false;
0053 if(!a_buffer.write(fTitleOffset)) return false;
0054 if(!a_buffer.write(fTitleSize)) return false;
0055 if(!a_buffer.write(fTitleColor)) return false;
0056 if(!a_buffer.write(fTitleFont)) return false;
0057
0058 if(!a_buffer.set_byte_count(beg)) return false;
0059 return true;
0060 }
0061
0062 inline bool axis_stream(buffer& a_buffer,
0063 const histo::axis<double,unsigned int>& a_axis,
0064 const std::string& a_name,
0065 const std::string& a_title) {
0066 // Version 6 streaming (ROOT/v3-00-6).
0067
0068 unsigned int beg;
0069 if(!a_buffer.write_version(6,beg)) return false;
0070
0071 if(!Named_stream(a_buffer,a_name,a_title)) return false;
0072
0073 if(!AttAxis_stream(a_buffer)) return false;
0074
0075 if(!a_buffer.write(a_axis.bins())) return false;
0076 if(!a_buffer.write(a_axis.lower_edge())) return false;
0077 if(!a_buffer.write(a_axis.upper_edge())) return false;
0078
0079 // fXbins
0080 //if(a_axis.m_fixed) {
0081 // std::vector<double> v;
0082 // ArrayT<double> dummy(v);
0083 // if(!dummy.stream(a_buffer)) return false; //TArrayD
0084 //} else {
0085 if(!a_buffer.write_array(a_axis.edges())) return false; //TArrayD
0086 //}
0087
0088 if(!a_buffer.write((int)0)) return false; //fFirst
0089 if(!a_buffer.write((int)0)) return false; //fLast
0090
0091 //Bool_t
0092 if(!a_buffer.write((unsigned char)0)) return false; //TimeDisplay
0093
0094 //TString
0095 if(!a_buffer.write(std::string())) return false; //TimeFormat
0096
0097 if(!a_buffer.set_byte_count(beg)) return false;
0098
0099 return true;
0100 }
0101
0102 inline bool List_empty_stream(buffer& a_buffer) {
0103 unsigned int beg;
0104 if(!a_buffer.write_version(4,beg)) return false;
0105 if(!Object_stream(a_buffer)) return false;
0106 std::string name;
0107 if(!a_buffer.write(name)) return false;
0108 int nobjects = 0;
0109 if(!a_buffer.write(nobjects)) return false;
0110 if(!a_buffer.set_byte_count(beg)) return false;
0111 return true;
0112 }
0113
0114 template <class HIST>
0115 inline std::string axis_title(const HIST& a_h,const std::string& a_key) {
0116 typedef std::map<std::string,std::string> annotations_t;
0117 annotations_t::const_iterator it = a_h.annotations().find(a_key);
0118 if(it==a_h.annotations().end()) return std::string();
0119 return (*it).second;
0120 }
0121
0122 template <class HIST>
0123 inline bool TH_write_1D(buffer& a_buffer,
0124 const HIST& a_h,
0125 const std::string& a_name,
0126 const std::vector<double>& a_bin_Sw2) {
0127
0128 if(!a_buffer.write_version(3)) return false;
0129
0130 if(!Named_stream(a_buffer,a_name,a_h.title())) return false;
0131
0132 if(!AttLine_stream(a_buffer)) return false;
0133 if(!AttFill_stream(a_buffer)) return false;
0134 if(!AttMarker_stream(a_buffer)) return false;
0135
0136 if(!a_buffer.write((int)a_h.get_bins())) return false;
0137
0138 //fXAxis,fYAxis,fZAxis
0139 if(a_h.dimension()==3) {
0140
0141 {histo::axis<double,unsigned int> haxis(a_h.get_axis(0));
0142 if(!axis_stream(a_buffer,haxis,"xaxis",axis_title(a_h,histo::key_axis_x_title()))) return false;}
0143
0144 {histo::axis<double,unsigned int> haxis(a_h.get_axis(1));
0145 if(!axis_stream(a_buffer,haxis,"yaxis",axis_title(a_h,histo::key_axis_y_title()))) return false;}
0146
0147 {histo::axis<double,unsigned int> haxis(a_h.get_axis(2));
0148 if(!axis_stream(a_buffer,haxis,"zaxis",axis_title(a_h,histo::key_axis_z_title()))) return false;}
0149
0150 } else if(a_h.dimension()==2) {
0151
0152 {histo::axis<double,unsigned int> haxis(a_h.get_axis(0));
0153 if(!axis_stream(a_buffer,haxis,"xaxis",axis_title(a_h,histo::key_axis_x_title()))) return false;}
0154
0155 {histo::axis<double,unsigned int> haxis(a_h.get_axis(1));
0156 if(!axis_stream(a_buffer,haxis,"yaxis",axis_title(a_h,histo::key_axis_y_title()))) return false;}
0157
0158 {histo::axis<double,unsigned int> dummy;
0159 dummy.configure(1,0,1);
0160 if(!axis_stream(a_buffer,dummy,"zaxis",axis_title(a_h,histo::key_axis_z_title()))) return false;}
0161
0162 } else if(a_h.dimension()==1) {
0163
0164 {histo::axis<double,unsigned int> haxis(a_h.get_axis(0));
0165 if(!axis_stream(a_buffer,haxis,"xaxis",axis_title(a_h,histo::key_axis_x_title()))) return false;}
0166
0167 {histo::axis<double,unsigned int> dummy;
0168 dummy.configure(1,0,1);
0169 if(!axis_stream(a_buffer,dummy,"yaxis",axis_title(a_h,histo::key_axis_y_title()))) return false;}
0170
0171 {histo::axis<double,unsigned int> dummy;
0172 dummy.configure(1,0,1);
0173 if(!axis_stream(a_buffer,dummy,"zaxis",axis_title(a_h,histo::key_axis_z_title()))) return false;}
0174
0175 } else {
0176 return false;
0177 }
0178
0179 if(!a_buffer.write((short)(1000 * 0.25))) return false; //fBarOffset
0180 if(!a_buffer.write((short)(1000 * 0.5))) return false; //fBarWidth
0181
0182 if(!a_buffer.write((double)a_h.all_entries())) return false;
0183 if(!a_buffer.write((double)a_h.get_in_range_Sw())) return false; //enforce double in case h1df
0184 if(!a_buffer.write((double)a_h.get_in_range_Sw2())) return false; //idem
0185
0186 {double value;
0187 a_h.get_ith_axis_Sxw(0,value);
0188 if(!a_buffer.write(value)) return false;}
0189
0190 {double value;
0191 a_h.get_ith_axis_Sx2w(0,value);
0192 if(!a_buffer.write(value)) return false;}
0193
0194 if(!a_buffer.write((double)-1111)) return false; //fMaximum
0195 if(!a_buffer.write((double)-1111)) return false; //fMinimum
0196 if(!a_buffer.write((double)0)) return false; //NormFactor
0197
0198 if(!a_buffer.write_array(std::vector<double>())) return false; //fContour TArrayD
0199
0200 if(!a_buffer.write_array(a_bin_Sw2)) return false; //fSumw2 TArrayD
0201
0202 // store annotation on fOption
0203 // but try to fool CERN-ROOT in order that it does not
0204 // understand fOption as.. CERN-ROOT options !
0205 //{std::string opt = " "+fAnnotation;
0206 // opt[0] = 0; //awfull trick
0207 // if(!a_buffer.write(opt)) return false;} //TString fOption
0208 {std::string opt;
0209 if(!a_buffer.write(opt)) return false;} //TString fOption
0210
0211 if(!List_empty_stream(a_buffer)) return false; //*TList fFunctions
0212
0213 return true;
0214 }
0215
0216 template <class HIST>
0217 inline bool TH_write_2D(buffer& a_buffer,
0218 const HIST& a_h,
0219 const std::string& a_name,
0220 const std::vector<double>& a_bin_Sw2) {
0221 if(!a_buffer.write_version(3)) return false;
0222 if(!TH_write_1D(a_buffer,a_h,a_name,a_bin_Sw2)) return false;
0223 if(!a_buffer.write((double)1)) return false; //ScaleFactor
0224
0225 {double value;
0226 a_h.get_ith_axis_Sxw(1,value);
0227 if(!a_buffer.write(value)) return false;}
0228
0229 {double value;
0230 a_h.get_ith_axis_Sx2w(1,value);
0231 if(!a_buffer.write(value)) return false;}
0232
0233 if(!a_buffer.write((double)a_h.Sxyw())) return false; //Tsumwxy
0234
0235 return true;
0236 }
0237
0238 template <class HIST>
0239 inline bool TH_write_3D(buffer& a_buffer,
0240 const HIST& a_h,
0241 const std::string& a_name,
0242 const std::vector<double>& a_bin_Sw2) {
0243 if(!a_buffer.write_version(4)) return false;
0244 if(!TH_write_1D(a_buffer,a_h,a_name,a_bin_Sw2)) return false;
0245 if(!Att3D_stream(a_buffer)) return false;
0246
0247 {double value;
0248 a_h.get_ith_axis_Sxw(1,value);
0249 if(!a_buffer.write(value)) return false;} //Tsumwy : Total Sum of weight*Y
0250 {double value;
0251 a_h.get_ith_axis_Sx2w(1,value);
0252 if(!a_buffer.write(value)) return false;} //Tsumwy2 : Total Sum of weight*Y*Y
0253 if(!a_buffer.write((double)a_h.Sxyw())) return false; //Tsumwxy : Total Sum of weight*X*Y
0254
0255 {double value;
0256 a_h.get_ith_axis_Sxw(2,value);
0257 if(!a_buffer.write(value)) return false;} //Tsumwz : Total Sum of weight*Z
0258 {double value;
0259 a_h.get_ith_axis_Sx2w(2,value);
0260 if(!a_buffer.write(value)) return false;} //Tsumwz2 : Total Sum of weight*Z*Z
0261 if(!a_buffer.write((double)a_h.Szxw())) return false; //Tsumwxz : Total Sum of weight*X*Z
0262 if(!a_buffer.write((double)a_h.Syzw())) return false; //Tsumwyz : Total Sum of weight*Y*Z
0263
0264 return true;
0265 }
0266
0267 inline bool TH1F_stream(buffer& a_buffer,const histo::h1df& a_h,const std::string& a_name) {
0268 if(!a_buffer.write_version(1)) return false;
0269 std::vector<double> bins_sum_w2d;
0270 convert<float,double>(a_h.bins_sum_w2(),bins_sum_w2d);
0271 if(!TH_write_1D(a_buffer,a_h,a_name,bins_sum_w2d)) return false;
0272 if(!a_buffer.write_array(a_h.bins_sum_w())) return false;
0273 return true;
0274 }
0275
0276 inline bool TH1F_stream(buffer& a_buffer,const histo::h1d& a_h,const std::string& a_name) {
0277 if(!a_buffer.write_version(1)) return false;
0278 if(!TH_write_1D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
0279 std::vector<float> bins_sum_wf;
0280 convert<double,float>(a_h.bins_sum_w(),bins_sum_wf);
0281 if(!a_buffer.write_array(bins_sum_wf)) return false;
0282 return true;
0283 }
0284
0285 inline bool TH1D_stream(buffer& a_buffer,const histo::h1d& a_h,const std::string& a_name) {
0286 if(!a_buffer.write_version(1)) return false;
0287 if(!TH_write_1D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
0288 if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
0289 return true;
0290 }
0291
0292 inline bool TH2F_stream(buffer& a_buffer,const histo::h2d& a_h,const std::string& a_name){
0293 if(!a_buffer.write_version(3)) return false;
0294 if(!TH_write_2D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
0295 std::vector<float> bins_sum_wf;
0296 convert<double,float>(a_h.bins_sum_w(),bins_sum_wf);
0297 if(!a_buffer.write_array(bins_sum_wf)) return false; //fArray TArrayF
0298 return true;
0299 }
0300
0301 inline bool TH2F_stream(buffer& a_buffer,const histo::h2df& a_h,const std::string& a_name){
0302 if(!a_buffer.write_version(3)) return false;
0303 std::vector<double> bins_sum_w2d;
0304 convert<float,double>(a_h.bins_sum_w2(),bins_sum_w2d);
0305 if(!TH_write_2D(a_buffer,a_h,a_name,bins_sum_w2d)) return false;
0306 if(!a_buffer.write_array(a_h.bins_sum_w())) return false;
0307 return true;
0308 }
0309
0310 inline bool TH2D_stream(buffer& a_buffer,const histo::h2d& a_h,const std::string& a_name){
0311 if(!a_buffer.write_version(3)) return false;
0312 if(!TH_write_2D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
0313 if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
0314 return true;
0315 }
0316
0317 inline bool TH3F_stream(buffer& a_buffer,const histo::h3d& a_h,const std::string& a_name){
0318 if(!a_buffer.write_version(3)) return false;
0319 if(!TH_write_3D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
0320 std::vector<float> bins_sum_wf;
0321 convert<double,float>(a_h.bins_sum_w(),bins_sum_wf);
0322 if(!a_buffer.write_array(bins_sum_wf)) return false; //fArray TArrayF
0323 return true;
0324 }
0325
0326 inline bool TH3F_stream(buffer& a_buffer,const histo::h3df& a_h,const std::string& a_name){
0327 if(!a_buffer.write_version(3)) return false;
0328 std::vector<double> bins_sum_w2d;
0329 convert<float,double>(a_h.bins_sum_w2(),bins_sum_w2d);
0330 if(!TH_write_3D(a_buffer,a_h,a_name,bins_sum_w2d)) return false;
0331 if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayF
0332 return true;
0333 }
0334
0335 inline bool TH3D_stream(buffer& a_buffer,const histo::h3d& a_h,const std::string& a_name){
0336 if(!a_buffer.write_version(3)) return false;
0337 if(!TH_write_3D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false;
0338 if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
0339 return true;
0340 }
0341
0342 inline bool TProfile_stream(buffer& a_buffer,const histo::p1d& a_p,const std::string& a_name){
0343 if(!a_buffer.write_version(4)) return false;
0344
0345 //WARNING : the mapping histo::p1d / TProfile is not obvious.
0346 //p1d::m_bin_Svw <---> TProfile::fArray
0347 //p1d::m_bin_Sv2w <---> TProfile::fSumw2
0348 //p1d::m_bin_Sw <---> TProfile::fBinEntries
0349
0350 // TH1D_stream(a_buffer,h,a_name) :
0351 //if(!a_buffer.write_version(1)) return false;
0352 //if(!TH_write_1D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false; //fSumw2 TArrayD
0353 //if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
0354 // but for profile :
0355 if(!a_buffer.write_version(1)) return false;
0356 if(!TH_write_1D(a_buffer,a_p,a_name,a_p.bins_sum_v2w())) return false; //fSumw2 TArrayD
0357 if(!a_buffer.write_array(a_p.bins_sum_vw())) return false; //fArray TArrayD
0358
0359 //TProfile specific :
0360 if(!a_buffer.write_array(a_p.bins_sum_w())) return false; //fBinEntries TArrayD
0361
0362 int errorMode = 0;
0363 if(!a_buffer.write(errorMode)) return false;
0364 if(!a_buffer.write(a_p.min_v())) return false;
0365 if(!a_buffer.write(a_p.max_v())) return false;
0366
0367 // version 4 :
0368 if(!a_buffer.write(a_p.get_Svw())) return false; //Double_t fTsumwy; //Total Sum of weight*Y
0369 if(!a_buffer.write(a_p.get_Sv2w())) return false; //Double_t fTsumwy2; //Total Sum of weight*Y*Y
0370
0371 return true;
0372 }
0373
0374 inline bool TProfile2D_stream(buffer& a_buffer,const histo::p2d& a_p,const std::string& a_name){
0375 if(!a_buffer.write_version(5)) return false;
0376
0377 //WARNING : the mapping histo::p2d / TProfile2D is not obvious.
0378 //p2d::m_bin_Svw <---> TProfile2D::fArray
0379 //p2d::m_bin_Sv2w <---> TProfile2D::fSumw2
0380 //p2d::m_bin_Sw <---> TProfile2D::fBinEntries
0381
0382 // TH2D_stream(a_buffer,h,a_name) :
0383 //if(!a_buffer.write_version(3)) return false;
0384 //if(!TH_write_2D(a_buffer,a_h,a_name,a_h.bins_sum_w2())) return false; //fSumw2 TArrayD
0385 //if(!a_buffer.write_array(a_h.bins_sum_w())) return false; //fArray TArrayD
0386 // for profile :
0387 if(!a_buffer.write_version(3)) return false;
0388 if(!TH_write_2D(a_buffer,a_p,a_name,a_p.bins_sum_v2w())) return false; //fSumw2 TArrayD
0389 if(!a_buffer.write_array(a_p.bins_sum_vw())) return false; //fArray TArrayD
0390
0391 //TProfile2D specific :
0392 if(!a_buffer.write_array(a_p.bins_sum_w())) return false; //fBinEntries TArrayD
0393
0394 int errorMode = 0;
0395 if(!a_buffer.write(errorMode)) return false; //fErrorMode
0396 if(!a_buffer.write(a_p.min_v())) return false; //fZmin
0397 if(!a_buffer.write(a_p.max_v())) return false; //fZmax
0398
0399 // version 5 :
0400 if(!a_buffer.write(a_p.get_Svw())) return false; //Double_t fTsumwz; //Total Sum of weight*Z
0401 if(!a_buffer.write(a_p.get_Sv2w())) return false; //Double_t fTsumwz2; //Total Sum of weight*Z*Z
0402
0403 return true;
0404 }
0405
0406 }}
0407
0408 #endif