40 if ( fgInstance == 0 ) {
66 <<
"G4RootAnalysisManager already exists."
67 <<
"Cannot create another instance.";
68 G4Exception(
"G4RootAnalysisManager::G4RootAnalysisManager()",
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 G4bool G4RootAnalysisManager::CreateHistoDirectory()
104 fHistoDirectory = &(fFile->dir());
114 if ( ! fHistoDirectory ) {
118 G4Exception(
"G4RootAnalysisManager::CreateHistoDirectory()",
132 G4bool G4RootAnalysisManager::CreateNtupleDirectory()
136 fNtupleDirectory = &(fFile->dir());
147 if ( ! fNtupleDirectory ) {
151 G4Exception(
"G4RootAnalysisManager::CreateNtupleDirectory()",
166 void G4RootAnalysisManager::CreateNtupleFromBooking()
170 if ( fNtuple || (! fNtupleBooking) )
return;
178 if ( fNtupleBooking->m_columns.size() ) {
180 const std::vector<tools::ntuple_booking::col_t>& columns
181 = fNtupleBooking->m_columns;
182 std::vector<tools::ntuple_booking::col_t>::const_iterator it;
184 for ( it = columns.begin(); it!=columns.end(); ++it) {
185 if ( (*it).second == tools::_cid(
int(0) ) ) {
186 G4cout <<
"adding int " << fNtuple->find_column<
int>((*it).first) <<
G4endl;
187 fNtupleIColumnMap[index++] = fNtuple->find_column<
int>((*it).first);
189 else if( (*it).second == tools::_cid(
float(0) ) ) {
190 fNtupleFColumnMap[index++] = fNtuple->find_column<
float>((*it).first);
192 else if((*it).second== tools::_cid(
double(0))) {
193 fNtupleDColumnMap[index++] = fNtuple->find_column<
double>((*it).first);
198 <<
"Unsupported column type " << (*it).first;
199 G4Exception(
"G4RootAnalysisManager::CreateNtupleFromBooking()",
207 tools::wroot::ntuple::column<int>*
208 G4RootAnalysisManager::GetNtupleIColumn(
G4int id)
const
210 std::map<G4int, tools::wroot::ntuple::column<int>* >::const_iterator it
211 = fNtupleIColumnMap.find(
id);
212 if ( it == fNtupleIColumnMap.end() ) {
214 description <<
" " <<
"column " <<
id <<
" does not exist.";
215 G4Exception(
"G4RootAnalysisManager::GetNtupleIColumn()",
224 tools::wroot::ntuple::column<float>*
225 G4RootAnalysisManager::GetNtupleFColumn(
G4int id)
const
227 std::map<G4int, tools::wroot::ntuple::column<float>* >::const_iterator it
228 = fNtupleFColumnMap.find(
id);
229 if ( it == fNtupleFColumnMap.end() ) {
231 description <<
" " <<
"column " <<
id <<
" does not exist.";
232 G4Exception(
"G4RootAnalysisManager::GetNtupleFColumn()",
242 tools::wroot::ntuple::column<double>*
243 G4RootAnalysisManager::GetNtupleDColumn(
G4int id)
const
245 std::map<G4int, tools::wroot::ntuple::column<double>* >::const_iterator it
246 = fNtupleDColumnMap.find(
id);
247 if ( it == fNtupleDColumnMap.end() ) {
249 description <<
" " <<
"column " <<
id <<
" does not exist.";
250 G4Exception(
"G4RootAnalysisManager::GetNtupleDColumn()",
259 G4bool G4RootAnalysisManager::Reset()
263 G4bool finalResult =
true;
265 std::vector<tools::histo::h1d*>::iterator it;
266 for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
267 G4bool result = (*it)->reset();
268 if ( ! result ) finalResult =
false;
271 std::vector<tools::histo::h2d*>::iterator it2;
272 for (it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
273 G4bool result = (*it2)->reset();
274 if ( ! result ) finalResult =
false;
289 if ( fcnName !=
"none" ) { title +=
" "; title += fcnName; title +=
"("; }
290 if ( unitName !=
"none" ) { title +=
" ["; title += unitName; title +=
"]";}
291 if ( fcnName !=
"none" ) { title +=
")"; }
307 for (
G4int i=0; i<
G4int(fH1Vector.size()); ++i ) {
311 if ( ! info->
fAscii )
continue;
319 output <<
"\n 1D histogram " <<
id <<
": " << h1->title()
320 <<
"\n \n \t X \t\t Y" <<
G4endl;
322 for (
G4int j=0; j<
G4int(h1->axis().bins()); ++j) {
323 output <<
" " << j <<
"\t"
324 << h1->axis().bin_center(j) <<
"\t"
325 << h1->bin_height(i) <<
G4endl;
335 G4bool onlyIfActive)
const
338 if ( index < 0 || index >=
G4int(fH1Vector.size()) ) {
340 G4String inFunction =
"G4RootAnalysisManager::";
341 inFunction += functionName;
343 description <<
" " <<
"histogram " <<
id <<
" does not exist.";
354 return fH1Vector[
index];
358 tools::histo::h2d* G4RootAnalysisManager::GetH2InFunction(
G4int id,
360 G4bool onlyIfActive)
const
363 if ( index < 0 || index >=
G4int(fH2Vector.size()) ) {
365 G4String inFunction =
"G4RootAnalysisManager::";
366 inFunction += functionName;
368 description <<
" " <<
"histogram " <<
id <<
" does not exist.";
379 return fH2Vector[
index];
394 if ( name.find(
".") == std::string::npos ) {
405 if ( fFile )
delete fFile;
408 if ( ! fFile->is_open() ) {
410 description <<
" " <<
"Cannot open file " << fileName;
417 if ( ! CreateHistoDirectory() )
return false;
418 if ( ! CreateNtupleDirectory() )
return false;
421 if ( fNtupleBooking && ( ! fNtuple ) )
422 CreateNtupleFromBooking();
440 for (
G4int i=0; i<
G4int(fH1Vector.size()); ++i ) {
451 = to(*fHistoDirectory,*h1,info->
fName);
454 description <<
" " <<
"saving histogram " << info->
fName <<
" failed";
462 for (
G4int i=0; i<
G4int(fH2Vector.size()); ++i ) {
467 tools::histo::h2d*
h2 = fH2Vector[i];
473 = to(*fHistoDirectory,*h2,info->
fName);
476 description <<
" " <<
"saving histogram " << info->
fName <<
" failed";
489 G4bool result = fFile->write(n);
499 result = result && result2;
519 description <<
" " <<
"Resetting data failed";
546 G4int index = fH1Vector.size();
555 UpdateTitle(axisTitle,unitName, fcnName);
556 h1->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
558 fH1Vector.push_back(h1);
582 G4int index = fH2Vector.size();
587 tools::histo::h2d*
h2
588 =
new tools::histo::h2d(title,
589 nxbins, xfcn(xmin), xfcn(xmax),
590 nybins, yfcn(ymin), yfcn(ymax));
596 UpdateTitle(xaxisTitle, xunitName, xfcnName);
597 UpdateTitle(yaxisTitle, yunitName, yfcnName);
598 h2->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
599 h2->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
601 fH2Vector.push_back(h2);
603 xunit, yunit, xfcn, yfcn);
620 if ( ! h1d )
return false;
630 h1d->configure(nbins, fcn(xmin), fcn(xmax));
642 UpdateTitle(axisTitle,unitName, fcnName);
643 h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
655 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2",
false,
false);
656 if ( ! h2d )
return false;
668 h2d->configure(nxbins, xfcn(xmin), xfcn(xmax),
669 nybins, yfcn(ymin), yfcn(ymax));
683 UpdateTitle(xaxisTitle, xunitName, xfcnName);
684 UpdateTitle(yaxisTitle, yunitName, yfcnName);
685 h2d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
686 h2d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
695 if ( ! h1d )
return false;
697 return h1d->scale(factor);
703 tools::histo::h2d* h2d = GetH2InFunction(
id,
"ScaleH2",
false,
false);
704 if ( ! h2d )
return false;
706 return h2d->scale(factor);
713 if ( fNtupleBooking ) {
716 <<
"Ntuple already exists. "
717 <<
"(Only one ntuple is currently supported.)";
718 G4Exception(
"G4RootAnalysisManager::CreateNtuple()",
724 if ( fFile && ( ! fNtupleDirectory ) ) {
725 if ( ! CreateNtupleDirectory() )
return;
734 fNtupleBooking =
new tools::ntuple_booking();
735 fNtupleBooking->m_name =
name;
736 fNtupleBooking->m_title =
title;
759 if ( ! fNtupleBooking ) {
762 <<
"Ntuple has to be created first. ";
763 G4Exception(
"G4RootAnalysisManager::CreateNtupleIColumn()",
769 G4int index = fNtupleBooking->m_columns.size();
770 fNtupleBooking->add_column<
int>(
name);
774 tools::wroot::ntuple::column<int>* column
775 = fNtuple->create_column<
int>(
name);
776 fNtupleIColumnMap[
index] = column;
797 if ( ! fNtupleBooking ) {
800 <<
"Ntuple has to be created first. ";
801 G4Exception(
"G4RootAnalysisManager::CreateNtupleFColumn()",
807 G4int index = fNtupleBooking->m_columns.size();
808 fNtupleBooking->add_column<
float>(
name);
812 tools::wroot::ntuple::column<float>* column
813 = fNtuple->create_column<
float>(
name);
814 fNtupleFColumnMap[
index] = column;
836 if ( ! fNtupleBooking ) {
839 <<
"Ntuple has to be created first. ";
840 G4Exception(
"G4RootAnalysisManager::CreateNtupleDColumn()",
846 G4int index = fNtupleBooking->m_columns.size();
847 fNtupleBooking->add_column<
double>(
name);
851 tools::wroot::ntuple::column<double>* column
852 = fNtuple->create_column<
double>(
name);
853 fNtupleDColumnMap[
index] = column;
876 if ( ! h1d )
return false;
888 description <<
" id " <<
id <<
" value " <<
value;
900 tools::histo::h2d* h2d = GetH2InFunction(
id,
"FillH2",
true,
false);
901 if ( ! h2d )
return false;
911 description <<
" id " <<
id
912 <<
" xvalue " << xvalue <<
" yvalue " << yvalue;
922 tools::wroot::ntuple::column<int>* column = GetNtupleIColumn(
id);
925 description <<
" " <<
"column " <<
id <<
" does not exist.";
926 G4Exception(
"G4RootAnalysisManager::FillNtupleIColumn()",
935 description <<
" id " <<
id <<
" value " <<
value;
944 tools::wroot::ntuple::column<float>* column = GetNtupleFColumn(
id);
947 description <<
" " <<
"column " <<
id <<
" does not exist.";
948 G4Exception(
"G4RootAnalysisManager::FillNtupleFColumn()",
957 description <<
" id " <<
id <<
" value " <<
value;
966 tools::wroot::ntuple::column<double>* column = GetNtupleDColumn(
id);
969 description <<
" " <<
"column " <<
id <<
" does not exist.";
970 G4Exception(
"G4RootAnalysisManager::FillNtupleDColumn()",
979 description <<
" id " <<
id <<
" value " <<
value;
996 description <<
" " <<
"ntuple does not exist. ";
997 G4Exception(
"G4RootAnalysisManager::AddNtupleRow()",
1002 G4bool result =fNtuple->add_row();
1005 description <<
" " <<
"adding row has failed.";
1006 G4Exception(
"G4RootAnalysisManager::AddNtupleRow()",
1019 G4bool onlyIfActive)
const
1021 return GetH1InFunction(
id,
"GetH1", warn, onlyIfActive);
1026 G4bool onlyIfActive)
const
1028 return GetH2InFunction(
id,
"GetH2", warn, onlyIfActive);
1034 std::map<G4String, G4int>::const_iterator it = fH1NameIdMap.find(name);
1035 if ( it == fH1NameIdMap.end() ) {
1037 G4String inFunction =
"G4RootAnalysisManager::GetH1Id";
1039 description <<
" " <<
"histogram " << name <<
" does not exist.";
1050 std::map<G4String, G4int>::const_iterator it = fH2NameIdMap.find(name);
1051 if ( it == fH2NameIdMap.end() ) {
1053 G4String inFunction =
"G4RootAnalysisManager::GetH2Id";
1055 description <<
" " <<
"histogram " << name <<
" does not exist.";
1073 if ( ! h1d )
return 0;
1075 return h1d->axis().bins();
1084 if ( ! h1d )
return 0;
1087 return info->
fXFcn(h1d->axis().lower_edge()*info->
fXUnit);
1094 if ( ! h1d )
return 0;
1097 return info->
fXFcn(h1d->axis().upper_edge()*info->
fXUnit);
1104 if ( ! h1d )
return 0;
1106 G4int nbins = h1d->axis().bins();
1109 description <<
" nbins = 0 (for h1 id = " <<
id <<
").";
1116 return ( info->
fXFcn(h1d->axis().upper_edge())
1117 - info->
fXFcn(h1d->axis().lower_edge()))*info->
fXUnit/nbins;
1123 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2NXbins");
1124 if ( ! h2d )
return 0;
1126 return h2d->axis_x().bins();
1134 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Xmin");
1135 if ( ! h2d )
return 0;
1138 return info->
fXFcn(h2d->axis_x().lower_edge()*info->
fXUnit);
1144 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Xmax");
1145 if ( ! h2d )
return 0;
1148 return info->
fXFcn(h2d->axis_x().upper_edge()*info->
fXUnit);
1154 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2XWidth",
true,
false);
1155 if ( ! h2d )
return 0;
1157 G4int nbins = h2d->axis_x().bins();
1160 description <<
" nbins = 0 (for h1 id = " <<
id <<
").";
1167 return ( info->
fXFcn(h2d->axis_x().upper_edge())
1168 - info->
fXFcn(h2d->axis_x().lower_edge()))*info->
fXUnit/nbins;
1174 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2NYbins");
1175 if ( ! h2d )
return 0;
1177 return h2d->axis_y().bins();
1185 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Ymin");
1186 if ( ! h2d )
return 0;
1189 return info->
fYFcn(h2d->axis_y().lower_edge()*info->
fYUnit);
1195 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Ymax");
1196 if ( ! h2d )
return 0;
1199 return info->
fYFcn(h2d->axis_y().upper_edge()*info->
fYUnit);
1205 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2YWidth",
true,
false);
1206 if ( ! h2d )
return 0;
1208 G4int nbins = h2d->axis_y().bins();
1211 description <<
" nbins = 0 (for h1 id = " <<
id <<
").";
1218 return ( info->
fYFcn(h2d->axis_y().upper_edge())
1219 - info->
fYFcn(h2d->axis_y().lower_edge()))*info->
fYUnit/nbins;
1226 if ( ! h1d )
return false;
1228 return h1d->set_title(title);
1235 if ( ! h1d )
return false;
1237 h1d->add_annotation(tools::histo::key_axis_x_title(), title);
1245 if ( ! h1d )
return false;
1247 h1d->add_annotation(tools::histo::key_axis_y_title(), title);
1254 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2Title");
1255 if ( ! h2d )
return false;
1257 return h2d->set_title(title);
1263 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2XAxisTitle");
1264 if ( ! h2d )
return false;
1266 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
1273 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2YAxisTitle");
1274 if ( ! h2d )
return false;
1276 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
1283 tools::histo::h2d* h2d = GetH2InFunction(
id,
"SetH2ZAxisTitle");
1284 if ( ! h2d )
return false;
1286 h2d->add_annotation(tools::histo::key_axis_z_title(), title);
1294 if ( ! h1d )
return "";
1296 return h1d->title();
1304 if ( ! h1d )
return "";
1307 G4bool result = h1d->annotation(tools::histo::key_axis_x_title(), title);
1310 description <<
" Failed to get x_axis title for h1 id = " <<
id <<
").";
1311 G4Exception(
"G4RootAnalysisManager::GetH1XAxisTitle",
1323 if ( ! h1d )
return "";
1326 G4bool result = h1d->annotation(tools::histo::key_axis_y_title(), title);
1329 description <<
" Failed to get y_axis title for h1 id = " <<
id <<
").";
1330 G4Exception(
"G4RootAnalysisManager::GetH1YAxisTitle",
1341 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2Title");
1342 if ( ! h2d )
return "";
1344 return h2d->title();
1350 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2XAxisTitle");
1351 if ( ! h2d )
return "";
1354 G4bool result = h2d->annotation(tools::histo::key_axis_x_title(), title);
1357 description <<
" Failed to get x_axis title for h2 id = " <<
id <<
").";
1358 G4Exception(
"G4RootAnalysisManager::GetH2XAxisTitle",
1369 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2YAxisTitle");
1370 if ( ! h2d )
return "";
1373 G4bool result = h2d->annotation(tools::histo::key_axis_y_title(), title);
1376 description <<
" Failed to get y_axis title for h2 id = " <<
id <<
").";
1377 G4Exception(
"G4RootAnalysisManager::GetH2YAxisTitle",
1388 tools::histo::h2d* h2d = GetH2InFunction(
id,
"GetH2ZAxisTitle");
1389 if ( ! h2d )
return "";
1392 G4bool result = h2d->annotation(tools::histo::key_axis_z_title(), title);
1395 description <<
" Failed to get z_axis title for h2 id = " <<
id <<
").";
1396 G4Exception(
"G4RootAnalysisManager::GetH2ZAxisTitle",