33 #include "tools/waxml/begend"
34 #include "tools/waxml/histos"
43 if ( fgInstance == 0 ) {
66 description <<
"G4XmlAnalysisManager already exists."
67 <<
"Cannot create another instance.";
68 G4Exception(
"G4XmlAnalysisManager::G4XmlAnalysisManager",
78 std::vector<tools::histo::h1d*>::iterator it;
79 for ( it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
83 std::vector<tools::histo::h2d*>::iterator it2;
84 for ( it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
89 delete fNtupleBooking;
100 void G4XmlAnalysisManager::CreateNtupleFromBooking()
109 if ( fNtupleBooking->m_columns.size() ) {
111 const std::vector<tools::ntuple_booking::col_t>& columns
112 = fNtupleBooking->m_columns;
113 std::vector<tools::ntuple_booking::col_t>::const_iterator it;
115 for ( it = columns.begin(); it!=columns.end(); ++it) {
116 if ( (*it).second == tools::_cid(
int(0) ) ) {
117 G4cout <<
"adding int " << fNtuple->find_column<
int>((*it).first) <<
G4endl;
118 fNtupleIColumnMap[index++] = fNtuple->find_column<
int>((*it).first);
120 else if( (*it).second == tools::_cid(
float(0) ) ) {
121 fNtupleFColumnMap[index++] = fNtuple->find_column<
float>((*it).first);
123 else if((*it).second== tools::_cid(
double(0))) {
124 fNtupleDColumnMap[index++] = fNtuple->find_column<
double>((*it).first);
129 <<
"Unsupported column type " << (*it).first;
139 tools::waxml::ntuple::column<int>*
140 G4XmlAnalysisManager::GetNtupleIColumn(
G4int id)
const
142 std::map<G4int, tools::waxml::ntuple::column<int>* >::const_iterator it
143 = fNtupleIColumnMap.find(
id);
144 if ( it == fNtupleIColumnMap.end() ) {
146 description <<
" " <<
"column " <<
id <<
" does not exist.";
147 G4Exception(
"G4XmlAnalysisManager::GetNtupleIColumn()",
156 tools::waxml::ntuple::column<float>*
157 G4XmlAnalysisManager::GetNtupleFColumn(
G4int id)
const
159 std::map<G4int, tools::waxml::ntuple::column<float>* >::const_iterator it
160 = fNtupleFColumnMap.find(
id);
161 if ( it == fNtupleFColumnMap.end() ) {
163 description <<
" " <<
"column " <<
id <<
" does not exist.";
164 G4Exception(
"G4XmlAnalysisManager::GetNtupleFColumn()",
173 G4bool G4XmlAnalysisManager::Reset()
177 G4bool finalResult =
true;
179 std::vector<tools::histo::h1d*>::iterator it;
180 for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
181 G4bool result = (*it)->reset();
182 if ( ! result ) finalResult =
false;
185 std::vector<tools::histo::h2d*>::iterator it2;
186 for (it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
187 G4bool result = (*it2)->reset();
188 if ( ! result ) finalResult =
false;
199 tools::waxml::ntuple::column<double>*
200 G4XmlAnalysisManager::GetNtupleDColumn(
G4int id)
const
202 std::map<G4int, tools::waxml::ntuple::column<double>* >::const_iterator it
203 = fNtupleDColumnMap.find(
id);
204 if ( it == fNtupleDColumnMap.end() ) {
206 description <<
" " <<
"column " <<
id <<
" does not exist.";
207 G4Exception(
"G4XmlAnalysisManager::GetNtupleDColumn()",
218 G4bool onlyIfActive)
const
221 if ( index < 0 || index >=
G4int(fH1Vector.size()) ) {
223 G4String inFunction =
"G4XmlAnalysisManager::";
224 inFunction += functionName;
226 description <<
" " <<
"histogram " <<
id <<
" does not exist.";
237 return fH1Vector[
index];
241 tools::histo::h2d* G4XmlAnalysisManager::GetH2InFunction(
G4int id,
243 G4bool onlyIfActive)
const
246 if ( index < 0 || index >=
G4int(fH2Vector.size()) ) {
248 G4String inFunction =
"G4XmlAnalysisManager::";
249 inFunction += functionName;
251 description <<
" " <<
"histogram " <<
id <<
" does not exist.";
262 return fH2Vector[
index];
270 if ( fcnName !=
"none" ) { title +=
" "; title += fcnName; title +=
"("; }
271 if ( unitName !=
"none" ) { title +=
" ["; title += unitName; title +=
"]";}
272 if ( fcnName !=
"none" ) { title +=
")"; }
288 for (
G4int i=0; i<
G4int(fH1Vector.size()); ++i ) {
292 if ( ! info->
fAscii )
continue;
300 output <<
"\n 1D histogram " <<
id <<
": " << h1->title()
301 <<
"\n \n \t X \t\t Y" <<
G4endl;
303 for (
G4int j=0; j<
G4int(h1->axis().bins()); ++j) {
304 output <<
" " << j <<
"\t"
305 << h1->axis().bin_center(j) <<
"\t"
306 << h1->bin_height(j) <<
G4endl;
323 if ( name.find(
".") == std::string::npos ) {
334 if ( fFile )
delete fFile;
336 fFile =
new std::ofstream(name);
337 if ( fFile->fail() ) {
339 description <<
" " <<
"Cannot open file " <<
name;
345 tools::waxml::begin(*fFile);
352 if ( fNtupleBooking && ( ! fNtuple ) )
353 CreateNtupleFromBooking();
363 if ( fNtuple ) fNtuple->write_trailer();
366 for (
G4int i=0; i<
G4int(fH1Vector.size()); ++i ) {
379 = tools::waxml::write(*fFile, *h1, path, info->
fName);
382 description <<
" " <<
"saving histogram " << info->
fName <<
" failed";
391 for (
G4int i=0; i<
G4int(fH2Vector.size()); ++i ) {
396 tools::histo::h2d*
h2 = fH2Vector[i];
404 = tools::waxml::write(*fFile, *h2, path, info->
fName);
407 description <<
" " <<
"saving histogram " << info->
fName <<
" failed";
443 description <<
" " <<
"Resetting data failed";
450 tools::waxml::end(*fFile);
471 G4int index = fH1Vector.size();
480 UpdateTitle(axisTitle,unitName, fcnName);
481 h1->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
483 fH1Vector.push_back(h1);
506 G4int index = fH2Vector.size();
511 tools::histo::h2d*
h2
512 =
new tools::histo::h2d(title,
513 nxbins, xfcn(xmin), xfcn(xmax),
514 nybins, yfcn(ymin), yfcn(ymax));
520 UpdateTitle(xaxisTitle, xunitName, xfcnName);
521 UpdateTitle(yaxisTitle, yunitName, yfcnName);
522 h2->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
523 h2->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
525 fH2Vector.push_back(h2);
527 xunit, yunit, xfcn, yfcn);
545 if ( ! h1d )
return false;
555 h1d->configure(nbins, fcn(xmin), fcn(xmax));
567 UpdateTitle(axisTitle,unitName, fcnName);
568 h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
580 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2",
false,
false);
581 if ( ! h2d )
return false;
593 h2d->configure(nxbins, xfcn(xmin), xfcn(xmax),
594 nybins, yfcn(ymin), yfcn(ymax));
607 UpdateTitle(xaxisTitle, xunitName, xfcnName);
608 UpdateTitle(yaxisTitle, yunitName, yfcnName);
609 h2d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
610 h2d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
619 if ( ! h1d )
return false;
621 return h1d->scale(factor);
627 tools::histo::h2d* h2d = GetH2InFunction(
id,
"ScaleH2",
false,
false);
628 if ( ! h2d )
return false;
630 return h2d->scale(factor);
637 if ( fNtupleBooking ) {
640 <<
"Ntuple already exists. "
641 <<
"(Only one ntuple is currently supported.)";
642 G4Exception(
"G4XmlAnalysisManager::CreateNtuple()",
653 fNtupleBooking =
new tools::ntuple_booking();
654 fNtupleBooking->m_name =
name;
655 fNtupleBooking->m_title =
title;
677 if ( ! fNtupleBooking ) {
680 <<
"Ntuple has to be created first. ";
681 G4Exception(
"G4XmlAnalysisManager::CreateNtupleIColumn()",
687 G4int index = fNtupleBooking->m_columns.size();
688 fNtupleBooking->add_column<
int>(
name);
692 tools::waxml::ntuple::column<int>* column
693 = fNtuple->create_column<
int>(
name);
694 fNtupleIColumnMap[
index] = column;
715 if ( ! fNtupleBooking ) {
718 <<
"Ntuple has to be created first. ";
719 G4Exception(
"G4XmlAnalysisManager::CreateNtupleFColumn()",
725 G4int index = fNtupleBooking->m_columns.size();
726 fNtupleBooking->add_column<
float>(
name);
730 tools::waxml::ntuple::column<float>* column
731 = fNtuple->create_column<
float>(
name);
732 fNtupleFColumnMap[
index] = column;
753 if ( ! fNtupleBooking ) {
756 <<
"Ntuple has to be created first. ";
757 G4Exception(
"G4XmlAnalysisManager::CreateNtupleDColumn()",
763 G4int index = fNtupleBooking->m_columns.size();
764 fNtupleBooking->add_column<
double>(
name);
768 tools::waxml::ntuple::column<double>* column
769 = fNtuple->create_column<
double>(
name);
770 fNtupleDColumnMap[
index] = column;
786 if ( ! fNtuple )
return;
795 fNtuple->write_header(path, fNtupleBooking->m_name, fNtupleBooking->m_title);
810 if ( ! h1d )
return false;
822 description <<
" id " <<
id <<
" value " << value/
GetXUnit(
kH1,
id);
834 tools::histo::h2d* h2d = GetH2InFunction(
id,
"FillH2",
true,
false);
835 if ( ! h2d )
return false;
845 description <<
" id " <<
id
857 tools::waxml::ntuple::column<int>* column = GetNtupleIColumn(
id);
860 description <<
" " <<
"column " <<
id <<
" does not exist.";
861 G4Exception(
"G4XmlAnalysisManager::FillNtupleIColumn()",
870 description <<
" id " <<
id <<
" value " <<
value;
879 tools::waxml::ntuple::column<float>* column = GetNtupleFColumn(
id);
882 description <<
" " <<
"column " <<
id <<
" does not exist.";
883 G4Exception(
"G4XmlAnalysisManager::FillNtupleFColumn()",
892 description <<
" id " <<
id <<
" value " <<
value;
902 tools::waxml::ntuple::column<double>* column = GetNtupleDColumn(
id);
905 description <<
" " <<
"column " <<
id <<
" does not exist.";
906 G4Exception(
"G4XmlAnalysisManager::FillNtupleDColumn()",
915 description <<
" id " <<
id <<
" value " <<
value;
932 description <<
" " <<
"ntuple does not exist. ";
933 G4Exception(
"G4XmlAnalysisManager::AddNtupleRow()",
949 G4bool onlyIfActive)
const
951 return GetH1InFunction(
id,
"GetH1", warn, onlyIfActive);
956 G4bool onlyIfActive)
const
958 return GetH2InFunction(
id,
"GetH2", warn, onlyIfActive);
964 std::map<G4String, G4int>::const_iterator it = fH1NameIdMap.find(name);
965 if ( it == fH1NameIdMap.end() ) {
967 G4String inFunction =
"G4RootAnalysisManager::GetH1Id";
969 description <<
" " <<
"histogram " << name <<
" does not exist.";
980 std::map<G4String, G4int>::const_iterator it = fH2NameIdMap.find(name);
981 if ( it == fH2NameIdMap.end() ) {
983 G4String inFunction =
"G4RootAnalysisManager::GetH2Id";
985 description <<
" " <<
"histogram " << name <<
" does not exist.";
1003 if ( ! h1d )
return 0;
1005 return h1d->axis().bins();
1014 if ( ! h1d )
return 0;
1017 return info->
fXFcn(h1d->axis().lower_edge()*info->
fXUnit);
1024 if ( ! h1d )
return 0;
1027 return info->
fXFcn(h1d->axis().upper_edge()*info->
fXUnit);
1034 if ( ! h1d )
return 0;
1036 G4int nbins = h1d->axis().bins();
1039 description <<
" nbins = 0 (for h1 id = " <<
id <<
").";
1046 return ( info->
fXFcn(h1d->axis().upper_edge())
1047 - info->
fXFcn(h1d->axis().lower_edge()))*info->
fXUnit/nbins;
1053 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2NXbins");
1054 if ( ! h2d )
return 0;
1056 return h2d->axis_x().bins();
1064 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Xmin");
1065 if ( ! h2d )
return 0;
1068 return info->
fXFcn(h2d->axis_x().lower_edge()*info->
fXUnit);
1074 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Xmax");
1075 if ( ! h2d )
return 0;
1078 return info->
fXFcn(h2d->axis_x().upper_edge()*info->
fXUnit);
1084 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2XWidth",
true,
false);
1085 if ( ! h2d )
return 0;
1087 G4int nbins = h2d->axis_x().bins();
1090 description <<
" nbins = 0 (for h1 id = " <<
id <<
").";
1097 return ( info->
fXFcn(h2d->axis_x().upper_edge())
1098 - info->
fXFcn(h2d->axis_x().lower_edge()))*info->
fXUnit/nbins;
1104 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2NYbins");
1105 if ( ! h2d )
return 0;
1107 return h2d->axis_y().bins();
1115 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Ymin");
1116 if ( ! h2d )
return 0;
1119 return info->
fYFcn(h2d->axis_y().lower_edge()*info->
fYUnit);
1125 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Ymax");
1126 if ( ! h2d )
return 0;
1129 return info->
fYFcn(h2d->axis_y().upper_edge()*info->
fYUnit);
1135 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2YWidth",
true,
false);
1136 if ( ! h2d )
return 0;
1138 G4int nbins = h2d->axis_y().bins();
1141 description <<
" nbins = 0 (for h1 id = " <<
id <<
").";
1148 return ( info->
fYFcn(h2d->axis_y().upper_edge())
1149 - info->
fYFcn(h2d->axis_y().lower_edge()))*info->
fYUnit/nbins;
1156 if ( ! h1d )
return false;
1158 return h1d->set_title(title);
1165 if ( ! h1d )
return false;
1167 h1d->add_annotation(tools::histo::key_axis_x_title(), title);
1175 if ( ! h1d )
return false;
1177 h1d->add_annotation(tools::histo::key_axis_y_title(), title);
1184 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2Title");
1185 if ( ! h2d )
return false;
1187 return h2d->set_title(title);
1193 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2XAxisTitle");
1194 if ( ! h2d )
return false;
1196 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
1203 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2YAxisTitle");
1204 if ( ! h2d )
return false;
1206 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
1213 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2ZAxisTitle");
1214 if ( ! h2d )
return false;
1216 h2d->add_annotation(tools::histo::key_axis_z_title(), title);
1224 if ( ! h1d )
return "";
1226 return h1d->title();
1233 if ( ! h1d )
return "";
1236 G4bool result = h1d->annotation(tools::histo::key_axis_x_title(), title);
1239 description <<
" Failed to get x_axis title for h1 id = " <<
id <<
").";
1240 G4Exception(
"G4XmlAnalysisManager::GetH1XAxisTitle",
1252 if ( ! h1d )
return "";
1255 G4bool result = h1d->annotation(tools::histo::key_axis_y_title(), title);
1258 description <<
" Failed to get y_axis title for h1 id = " <<
id <<
").";
1259 G4Exception(
"G4XmlAnalysisManager::GetH1YAxisTitle",
1270 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Title");
1271 if ( ! h2d )
return "";
1273 return h2d->title();
1280 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2XAxisTitle");
1281 if ( ! h2d )
return "";
1284 G4bool result = h2d->annotation(tools::histo::key_axis_x_title(), title);
1287 description <<
" Failed to get x_axis title for h2 id = " <<
id <<
").";
1288 G4Exception(
"G4XmlAnalysisManager::GetH2XAxisTitle",
1299 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2YAxisTitle");
1300 if ( ! h2d )
return "";
1303 G4bool result = h2d->annotation(tools::histo::key_axis_y_title(), title);
1306 description <<
" Failed to get y_axis title for h2 id = " <<
id <<
").";
1307 G4Exception(
"G4XmlAnalysisManager::GetH2YAxisTitle",
1318 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2ZAxisTitle");
1319 if ( ! h2d )
return "";
1322 G4bool result = h2d->annotation(tools::histo::key_axis_z_title(), title);
1325 description <<
" Failed to get z_axis title for h2 id = " <<
id <<
").";
1326 G4Exception(
"G4XmlAnalysisManager::GetH2ZAxisTitle",