g4tools  5.4.0
to
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_wroot_to
5 #define tools_wroot_to
6 
7 #include "directory"
8 #include "streamers"
9 #include "bufobj"
10 
11 namespace tools {
12 namespace wroot {
13 
14 //typedef std::map<std::string,std::string> annotations_t;
15 inline bool to(directory& a_dir,const std::map<std::string,std::string>& a_annotations,const std::string& a_histo_name) {
16  //if(a_annotations.empty()) return true;
17  std::string sas;
18  tools_mforcit(std::string,std::string,a_annotations,it) {
19  if(it!=a_annotations.begin()) sas += "\n";
20  sas += (*it).first;
21  sas += "\n";
22  sas += (*it).second;
23  }
24  std::string key_name = "annotations_"+a_histo_name;
25  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,key_name,"annotations","TNamed");
26  if(!Named_stream(*bo,a_histo_name,sas)) {
27  a_dir.file().out() << "tools::wroot::to : Named_stream failed." << std::endl;
28  delete bo;
29  return false;
30  }
31  a_dir.append_object(bo); //a_dir takes ownership of bo.
32  return true;
33 }
34 
35 inline bool to(directory& a_dir,const histo::h1d& a_histo,const std::string& a_name) {
36  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,a_name,a_histo.title(),"TH1D");
37  if(!TH1D_stream(*bo,a_histo,a_name)) {
38  a_dir.file().out() << "tools::wroot::to : TH1D_stream failed." << std::endl;
39  delete bo;
40  return false;
41  }
42  a_dir.append_object(bo); //a_dir takes ownership of bo.
43  return true;
44 }
45 
46 inline bool to(directory& a_dir,const histo::h1df& a_histo,const std::string& a_name) {
47  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,a_name,a_histo.title(),"TH1F");
48  if(!TH1F_stream(*bo,a_histo,a_name)) {
49  a_dir.file().out() << "tools::wroot::to : TH1F_stream failed." << std::endl;
50  delete bo;
51  return false;
52  }
53  a_dir.append_object(bo); //a_dir takes ownership of bo.
54  return true;
55 }
56 
57 inline bool to(directory& a_dir,const histo::h2d& a_histo,const std::string& a_name) {
58  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,a_name,a_histo.title(),"TH2D");
59  if(!TH2D_stream(*bo,a_histo,a_name)) {
60  a_dir.file().out() << "tools::wroot::to : TH2D_stream failed." << std::endl;
61  delete bo;
62  return false;
63  }
64  a_dir.append_object(bo); //a_dir takes ownership of bo.
65  return true;
66 }
67 
68 inline bool to(directory& a_dir,const histo::h2df& a_histo,const std::string& a_name) {
69  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,a_name,a_histo.title(),"TH2F");
70  if(!TH2F_stream(*bo,a_histo,a_name)) {
71  a_dir.file().out() << "tools::wroot::to :"
72  << " TH2F_stream failed."
73  << std::endl;
74  delete bo;
75  return false;
76  }
77  a_dir.append_object(bo); //a_dir takes ownership of bo.
78  return true;
79 }
80 
81 inline bool to(directory& a_dir,const histo::h3d& a_histo,const std::string& a_name) {
82  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
83  a_name,a_histo.title(),"TH3D");
84  if(!TH3D_stream(*bo,a_histo,a_name)) {
85  a_dir.file().out() << "tools::wroot::to :"
86  << " TH3D_stream failed."
87  << std::endl;
88  delete bo;
89  return false;
90  }
91  a_dir.append_object(bo); //a_dir takes ownership of bo.
92  return true;
93 }
94 
95 inline bool to(directory& a_dir,const histo::h3df& a_histo,const std::string& a_name) {
96  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,a_name,a_histo.title(),"TH3F");
97  if(!TH3F_stream(*bo,a_histo,a_name)) {
98  a_dir.file().out() << "tools::wroot::to :"
99  << " TH3F_stream failed."
100  << std::endl;
101  delete bo;
102  return false;
103  }
104  a_dir.append_object(bo); //a_dir takes ownership of bo.
105  return true;
106 }
107 
108 inline bool to(directory& a_dir,const histo::p1d& a_histo,const std::string& a_name) {
109  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
110  a_name,a_histo.title(),"TProfile");
111  if(!TProfile_stream(*bo,a_histo,a_name)) {
112  a_dir.file().out() << "tools::wroot::to :"
113  << " TProfile_stream failed."
114  << std::endl;
115  delete bo;
116  return false;
117  }
118  a_dir.append_object(bo); //a_dir takes ownership of bo.
119  return true;
120 }
121 
122 inline bool to(directory& a_dir,const histo::p2d& a_histo,const std::string& a_name) {
123  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
124  a_name,a_histo.title(),"TProfile2D");
125  if(!TProfile2D_stream(*bo,a_histo,a_name)) {
126  a_dir.file().out() << "tools::wroot::to :"
127  << " TProfile2D_stream failed."
128  << std::endl;
129  delete bo;
130  return false;
131  }
132  a_dir.append_object(bo); //a_dir takes ownership of bo.
133  return true;
134 }
135 
136 inline bool write_histos(directory& a_dir,const std::vector< std::pair<std::string,void*> >& a_hists) {
137  typedef std::pair<std::string,void*> class_pointer;
138 
139  tools_vforcit(class_pointer,a_hists,it) {
140  const std::string& scls = (*it).first;
141  void* p = (*it).second;
142  if(scls==histo::h1d::s_class()) {
143  histo::h1d& h = *((histo::h1d*)p);
144  if(!to(a_dir,h,h.title())) return false;
145 
146  } else if(scls==histo::h2d::s_class()) {
147  histo::h2d& h = *((histo::h2d*)p);
148  if(!to(a_dir,h,h.title())) return false;
149 
150  } else if(scls==histo::h3d::s_class()) {
151  histo::h3d& h = *((histo::h3d*)p);
152  if(!to(a_dir,h,h.title())) return false;
153 
154  } else if(scls==histo::p1d::s_class()) {
155  histo::p1d& h = *((histo::p1d*)p);
156  if(!to(a_dir,h,h.title())) return false;
157 
158  } else if(scls==histo::p2d::s_class()) {
159  histo::p2d& h = *((histo::p2d*)p);
160  if(!to(a_dir,h,h.title())) return false;
161 
162  } else {
163  a_dir.file().out() << "tools::wroot::write_histos :"
164  << " WARNING : class " << scls << " not handled."
165  << std::endl;
166  }
167 
168  }
169  return true;
170 }
171 
172 }}
173 
174 #include "member_writer"
175 #include "../store/osc_streamers"
176 
177 namespace tools {
178 namespace wroot {
179 
180 inline bool to_osc(directory& a_dir,const histo::h1d& a_histo,const std::string& a_name) {
181  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
182  a_name,a_histo.title(),osc::s_h1d());
183 
184  //Stream as in a BatchLab/Rio/Data.h :
185  if(!bo->write_version(1)) return false;
186  if(!Named_stream(*bo,a_name,a_histo.title())) return false;
187 
188  member_writer mw(*bo);
189  if(!osc::visit(mw,a_histo)) {
190  a_dir.file().out() << "tools::wroot::to_osc :"
191  << " Histogram1D_stream failed."
192  << std::endl;
193  delete bo;
194  return false;
195  }
196  a_dir.append_object(bo); //a_dir takes ownership of bo.
197  return true;
198 }
199 
200 inline bool to_osc(directory& a_dir,const histo::h2d& a_histo,const std::string& a_name) {
201  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
202  a_name,a_histo.title(),osc::s_h2d());
203 
204  //Stream as in a BatchLab/Rio/Data.h :
205  if(!bo->write_version(1)) return false;
206  if(!Named_stream(*bo,a_name,a_histo.title())) return false;
207 
208  member_writer mw(*bo);
209  if(!osc::visit(mw,a_histo)) {
210  a_dir.file().out() << "tools::wroot::to_osc :"
211  << " Histogram2D_stream failed."
212  << std::endl;
213  delete bo;
214  return false;
215  }
216  a_dir.append_object(bo); //a_dir takes ownership of bo.
217  return true;
218 }
219 
220 inline bool to_osc(directory& a_dir,const histo::h3d& a_histo,const std::string& a_name) {
221  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
222  a_name,a_histo.title(),osc::s_h3d());
223 
224  //Stream as in a BatchLab/Rio/Data.h :
225  if(!bo->write_version(1)) return false;
226  if(!Named_stream(*bo,a_name,a_histo.title())) return false;
227 
228  member_writer mw(*bo);
229  if(!osc::visit(mw,a_histo)) {
230  a_dir.file().out() << "tools::wroot::to_osc :"
231  << " Histogram3D_stream failed."
232  << std::endl;
233  delete bo;
234  return false;
235  }
236  a_dir.append_object(bo); //a_dir takes ownership of bo.
237  return true;
238 }
239 
240 inline bool to_osc(directory& a_dir,const histo::p1d& a_histo,const std::string& a_name) {
241  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
242  a_name,a_histo.title(),osc::s_p1d());
243 
244  //Stream as in a BatchLab/Rio/Data.h :
245  if(!bo->write_version(1)) return false;
246  if(!Named_stream(*bo,a_name,a_histo.title())) return false;
247 
248  member_writer mw(*bo);
249  if(!osc::visit(mw,a_histo)) {
250  a_dir.file().out() << "tools::wroot::to_osc :"
251  << " Profile1D_stream failed."
252  << std::endl;
253  delete bo;
254  return false;
255  }
256  a_dir.append_object(bo); //a_dir takes ownership of bo.
257  return true;
258 }
259 
260 inline bool to_osc(directory& a_dir,const histo::p2d& a_histo,const std::string& a_name) {
261  bufobj* bo = new bufobj(a_dir.file().out(),a_dir.file().byte_swap(),256,
262  a_name,a_histo.title(),osc::s_p2d());
263 
264  //Stream as in a BatchLab/Rio/Data.h :
265  if(!bo->write_version(1)) return false;
266  if(!Named_stream(*bo,a_name,a_histo.title())) return false;
267 
268  member_writer mw(*bo);
269  if(!osc::visit(mw,a_histo)) {
270  a_dir.file().out() << "tools::wroot::to_osc :"
271  << " Profile2D_stream failed."
272  << std::endl;
273  delete bo;
274  return false;
275  }
276  a_dir.append_object(bo); //a_dir takes ownership of bo.
277  return true;
278 }
279 
280 }}
281 
282 #endif
tools::wroot::to
bool to(directory &a_dir, const std::map< std::string, std::string > &a_annotations, const std::string &a_histo_name)
Definition: to:15
tools::histo::p1d
Definition: p1d:12
tools::histo::h3d::s_class
static const std::string & s_class()
Definition: h3d:15
tools::osc::s_p2d
const std::string & s_p2d()
Definition: osc_streamers:61
tools::wroot::TH1F_stream
bool TH1F_stream(buffer &a_buffer, const histo::h1df &a_h, const std::string &a_name)
Definition: streamers:267
tools::osc::visit
bool visit(iobj_const_visitor &a_v, const histo::h1d &a_histo)
Definition: osc_streamers:522
tools::histo::h1d
Definition: h1d:14
tools::histo::p1d::s_class
static const std::string & s_class()
Definition: p1d:15
tools::histo::h2d::s_class
static const std::string & s_class()
Definition: h2d:15
bufobj
tools::osc::s_h2d
const std::string & s_h2d()
Definition: osc_streamers:46
tools::osc::s_h3d
const std::string & s_h3d()
Definition: osc_streamers:51
tools::wroot::TH3F_stream
bool TH3F_stream(buffer &a_buffer, const histo::h3d &a_h, const std::string &a_name)
Definition: streamers:317
tools::histo::p2d
Definition: p2d:12
tools::wroot::directory::file
virtual ifile & file()
Definition: directory:34
tools::histo::h2d
Definition: h2d:12
member_writer
tools::wroot::TH3D_stream
bool TH3D_stream(buffer &a_buffer, const histo::h3d &a_h, const std::string &a_name)
Definition: streamers:335
tools::wroot::Named_stream
bool Named_stream(buffer &a_buffer, const std::string &a_name, const std::string &a_title)
Definition: named:23
tools::wroot::TH2D_stream
bool TH2D_stream(buffer &a_buffer, const histo::h2d &a_h, const std::string &a_name)
Definition: streamers:310
tools::histo::p2d::s_class
static const std::string & s_class()
Definition: p2d:15
tools::osc::s_h1d
const std::string & s_h1d()
Definition: osc_streamers:41
tools::wroot::buffer::write_version
bool write_version(short a_version)
Definition: buffer:169
tools::wroot::TProfile2D_stream
bool TProfile2D_stream(buffer &a_buffer, const histo::p2d &a_p, const std::string &a_name)
Definition: streamers:374
tools::histo::h1df
Definition: h1df:15
tools::histo::h1d::s_class
static const std::string & s_class()
Definition: h1d:17
tools::histo::h2df
Definition: h2df:15
tools::histo::h3df
Definition: h3df:15
tools::wroot::TProfile_stream
bool TProfile_stream(buffer &a_buffer, const histo::p1d &a_p, const std::string &a_name)
Definition: streamers:342
tools::wroot::TH2F_stream
bool TH2F_stream(buffer &a_buffer, const histo::h2d &a_h, const std::string &a_name)
Definition: streamers:292
tools::wroot::bufobj
Definition: bufobj:13
tools::histo::base_histo::title
const std::string & title() const
Definition: base_histo:87
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::wroot::member_writer
Definition: member_writer:14
tools_mforcit
#define tools_mforcit(a__K, a__V, a__m, a__it)
Definition: forit:49
tools::wroot::ifile::byte_swap
virtual bool byte_swap() const =0
tools::wroot::TH1D_stream
bool TH1D_stream(buffer &a_buffer, const histo::h1d &a_h, const std::string &a_name)
Definition: streamers:285
tools::wroot::to_osc
bool to_osc(directory &a_dir, const histo::h1d &a_histo, const std::string &a_name)
Definition: to:180
tools::wroot::write_histos
bool write_histos(directory &a_dir, const std::vector< std::pair< std::string, void * > > &a_hists)
Definition: to:136
tools::wroot::ifile::out
virtual std::ostream & out() const =0
tools::histo::h3d
Definition: h3d:12
directory
tools::osc::s_p1d
const std::string & s_p1d()
Definition: osc_streamers:56
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::wroot::directory
Definition: directory:26
tools::wroot::directory::append_object
virtual void append_object(iobject *a_object)
Definition: directory:36
streamers