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