38 #include "ExG4HbookAnalysisManager.hh"
46 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::fgInstance = 0;
47 const G4int ExG4HbookAnalysisManager::fgkDefaultH2HbookIdOffset = 100;
48 const G4int ExG4HbookAnalysisManager::fgkDefaultNtupleHbookId = 1;
49 const G4String ExG4HbookAnalysisManager::fgkDefaultNtupleDirectoryName =
"ntuple";
52 ExG4HbookAnalysisManager* ExG4HbookAnalysisManager::Instance()
54 if ( fgInstance == 0 ) {
55 fgInstance =
new ExG4HbookAnalysisManager();
62 ExG4HbookAnalysisManager::ExG4HbookAnalysisManager()
82 <<
"G4HbookAnalysisManager already exists."
83 <<
"Cannot create another instance.";
84 G4Exception(
"G4HbookAnalysisManager::G4HbookAnalysisManager()",
91 tools::hbook::CHLIMIT(
setpawc());
96 ExG4HbookAnalysisManager::~ExG4HbookAnalysisManager()
98 std::vector<tools::hbook::h1*>::iterator it;
99 for ( it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
102 std::vector<tools::hbook::h2*>::iterator it2;
103 for ( it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
117 tools::hbook::wntuple::column<int>*
118 ExG4HbookAnalysisManager::GetNtupleIColumn(
G4int id)
const
120 std::map<G4int, tools::hbook::wntuple::column<int>* >::const_iterator it
121 = fNtupleIColumnMap.find(
id);
122 if ( it == fNtupleIColumnMap.end() ) {
124 description <<
" " <<
"column " <<
id <<
" does not exist.";
125 G4Exception(
"G4HbookAnalysisManager::GetNtupleIColumn()",
134 tools::hbook::wntuple::column<float>*
135 ExG4HbookAnalysisManager::GetNtupleFColumn(
G4int id)
const
137 std::map<G4int, tools::hbook::wntuple::column<float>* >::const_iterator it
138 = fNtupleFColumnMap.find(
id);
139 if ( it == fNtupleFColumnMap.end() ) {
141 description <<
" " <<
"column " <<
id <<
" does not exist.";
142 G4Exception(
"G4HbookAnalysisManager::GetNtupleFColumn()",
152 tools::hbook::wntuple::column<double>*
153 ExG4HbookAnalysisManager::GetNtupleDColumn(
G4int id)
const
155 std::map<G4int, tools::hbook::wntuple::column<double>* >::const_iterator it
156 = fNtupleDColumnMap.find(
id);
157 if ( it == fNtupleDColumnMap.end() ) {
159 description <<
" " <<
"column " <<
id <<
" does not exist.";
160 G4Exception(
"G4HbookAnalysisManager::GetNtupleDColumn()",
173 G4bool ExG4HbookAnalysisManager::OpenFile(
const G4String& fileName)
176 if (
name.find(
".") == std::string::npos ) {
178 name.append(GetFileType());
183 fpVerboseL3->Message(
"open",
"analysis file",
name);
186 tools::hbook::CHCDIR(
"//PAWC",
" ");
188 unsigned int unit = 1;
189 fFile =
new tools::hbook::wfile(std::cout,
name, unit);
190 if ( ! fFile->is_valid() ) {
192 description <<
" " <<
"Cannot open file " << fileName;
204 if ( fHistoDirectoryName !=
"" ) {
205 tools::hbook::CHCDIR(
"//PAWC/LUN1",
" ");
206 tools::hbook::CHMDIR(fHistoDirectoryName.data(),
" ");
207 tools::hbook::CHCDIR(
"//LUN1",
" ");
208 tools::hbook::CHMDIR(fHistoDirectoryName.data(),
" ");
211 fLockHistoDirectoryName =
true;
219 fpVerboseL1->Message(
"open",
"analysis file",
name);
226 G4bool ExG4HbookAnalysisManager::Write()
230 fpVerboseL3->Message(
"write",
"file",
"");
236 G4bool result = fFile->write();
240 fpVerboseL1->Message(
"write",
"file",
"", result);
247 G4bool ExG4HbookAnalysisManager::CloseFile()
251 fpVerboseL3->Message(
"close",
"file",
"");
258 G4bool result = fFile->close();
262 fpVerboseL1->Message(
"close",
"file",
"", result);
274 fpVerboseL3->Message(
"create",
"H1", name);
278 if ( fHistoDirectoryName !=
"" ) {
279 G4String histoPath =
"//PAWC/LUN1/";
280 histoPath.
append(fHistoDirectoryName.data());
281 tools::hbook::CHCDIR(histoPath.
data(),
" ");
285 if ( fH1Vector.size() == 0 ) {
286 if ( fH1HbookIdOffset == -1 ) {
287 if ( fFirstHistoId > 0 )
288 fH1HbookIdOffset = 0;
290 fH1HbookIdOffset = 1;
292 if ( fH1HbookIdOffset > 0 ) {
294 description <<
"H1 will be defined in HBOOK with ID = G4_firstHistoId + 1";
295 G4Exception(
"ExG4HbookAnalysisManager::CreateH1()",
303 G4int hbookIndex = fH1HbookIdOffset + fH1Vector.size() + fFirstHistoId;
305 fH1Vector.push_back(h1);
308 if ( fHistoDirectoryName !=
"" ) {
310 tools::hbook::CHCDIR(
"//PAWC/LUN1",
" ");
313 fLockFirstHistoId =
true;
318 description <<
" name : " << name <<
" hbook index : " << hbookIndex;
319 fpVerboseL1->Message(
"create",
"H1", description);
323 return index + fFirstHistoId;
333 fpVerboseL3->Message(
"create",
"H2", name);
337 if ( fHistoDirectoryName !=
"" ) {
338 G4String histoPath =
"//PAWC/LUN1/";
339 histoPath.
append(fHistoDirectoryName.data());
340 tools::hbook::CHCDIR(histoPath.
data(),
" ");
344 if ( fH2Vector.size() == 0 ) {
345 if ( fH2HbookIdOffset == -1 ) {
346 if ( fFirstHistoId > 0 )
347 fH2HbookIdOffset = fgkDefaultH2HbookIdOffset;
349 fH2HbookIdOffset = fgkDefaultH2HbookIdOffset + 1;
351 if ( fH2HbookIdOffset != fgkDefaultH2HbookIdOffset ) {
354 <<
"H2 will be defined in HBOOK with ID = "
355 << fgkDefaultH2HbookIdOffset <<
" + G4_firstHistoId + 1";
356 G4Exception(
"ExG4HbookAnalysisManager::CreateH2()",
362 G4int index = fH2Vector.size();
363 G4int hbookIndex = fH2HbookIdOffset + fH2Vector.size() + fFirstHistoId;
365 =
new tools::hbook::h2(hbookIndex, title, nxbins, xmin, xmax, nybins, ymin, ymax);
366 fH2Vector.push_back(h2);
370 if ( fHistoDirectoryName !=
"" ) {
371 tools::hbook::CHCDIR(
"//PAWC/LUN1",
" ");
374 fLockFirstHistoId =
true;
379 description <<
" name : " << name <<
" hbook index : " << hbookIndex;
380 fpVerboseL1->Message(
"create",
"H2", description);
384 return index + fFirstHistoId;
388 void ExG4HbookAnalysisManager::CreateNtuple(
const G4String& name,
393 fpVerboseL3->Message(
"create",
"ntuple", name);
399 <<
"Ntuple already exists. "
400 <<
"(Only one ntuple is currently supported.)";
401 G4Exception(
"G4HbookAnalysisManager::CreateNtuple()",
408 if ( fNtupleDirectoryName ==
"" )
409 fFile->mkcd(fgkDefaultNtupleDirectoryName.data());
411 fFile->mkcd(fNtupleDirectoryName.data());
414 if ( fNtupleHbookId == -1 ) fNtupleHbookId = fgkDefaultNtupleHbookId;
417 fNtuple =
new tools::hbook::wntuple(fNtupleHbookId, name);
419 fNtupleTitle =
title;
424 description <<
" name : " << name <<
" hbook index : " << fNtupleHbookId;
425 fpVerboseL1->Message(
"create",
"ntuple", description);
431 G4int ExG4HbookAnalysisManager::CreateNtupleIColumn(
const G4String& name)
435 fpVerboseL3->Message(
"create",
"ntuple I column", name);
438 G4int index = fNtuple->columns().size();
439 tools::hbook::wntuple::column<int>* column = fNtuple->create_column<
int>(
name);
440 fNtupleIColumnMap[
index] = column;
441 fLockFirstNtupleColumnId =
true;
445 fpVerboseL1->Message(
"create",
"ntuple I column", name);
448 return index + fFirstNtupleColumnId;
452 G4int ExG4HbookAnalysisManager::CreateNtupleFColumn(
const G4String& name)
456 fpVerboseL3->Message(
"create",
"ntuple F column", name);
459 G4int index = fNtuple->columns().size();
460 tools::hbook::wntuple::column<float>* column = fNtuple->create_column<
float>(
name);
461 fNtupleFColumnMap[
index] = column;
462 fLockFirstNtupleColumnId =
true;
466 fpVerboseL1->Message(
"create",
"ntuple F column", name);
469 return index + fFirstNtupleColumnId;
474 G4int ExG4HbookAnalysisManager::CreateNtupleDColumn(
const G4String& name)
478 fpVerboseL3->Message(
"create",
"ntuple D column", name);
481 G4int index = fNtuple->columns().size();
482 tools::hbook::wntuple::column<double>* column = fNtuple->create_column<
double>(
name);
483 fNtupleDColumnMap[
index] = column;
484 fLockFirstNtupleColumnId =
true;
488 fpVerboseL1->Message(
"create",
"ntuple D column", name);
491 return index + fFirstNtupleColumnId;
495 void ExG4HbookAnalysisManager::FinishNtuple()
499 fpVerboseL3->Message(
"finish",
"ntuple", fNtupleName);
503 tools::hbook::CHCDIR(
"//PAWC/LUN1",
" ");
508 fpVerboseL1->Message(
"finish",
"ntuple", fNtupleName);
518 description <<
" id " <<
id <<
" value " <<
value;
519 fpVerboseL3->Message(
"fill",
"H1", description);
526 description <<
" " <<
"histogram " <<
id <<
" does not exist.";
532 h1->fill(value, weight);
536 description <<
" id " <<
id <<
" value " <<
value;
537 fpVerboseL2->Message(
"fill",
"H1", description);
551 description <<
" id " <<
id
552 <<
" xvalue " << xvalue <<
" yvalue " << yvalue;
553 fpVerboseL3->Message(
"fill",
"H2", description);
560 description <<
" " <<
"histogram " <<
id <<
" does not exist.";
566 h2->fill(xvalue, yvalue, weight);
570 description <<
" id " <<
id
571 <<
" xvalue " << xvalue <<
" yvalue " << yvalue;
572 fpVerboseL2->Message(
"fill",
"H2", description);
584 description <<
" id " <<
id <<
" value " <<
value;
585 fpVerboseL3->Message(
"fill",
"ntuple I column", description);
589 tools::hbook::wntuple::column<int>* column = GetNtupleIColumn(
id);
592 description <<
" " <<
"column " <<
id <<
" does not exist.";
593 G4Exception(
"G4HbookAnalysisManager::FillNtupleIColumn()",
602 description <<
" id " <<
id <<
" value " <<
value;
603 fpVerboseL2->Message(
"fill",
"ntuple I column", description);
614 description <<
" id " <<
id <<
" value " <<
value;
615 fpVerboseL3->Message(
"fill",
"ntuple F column", description);
619 tools::hbook::wntuple::column<float>* column = GetNtupleFColumn(
id);
622 description <<
" " <<
"column " <<
id <<
" does not exist.";
623 G4Exception(
"G4HbookAnalysisManager::FillNtupleFColumn()",
632 description <<
" id " <<
id <<
" value " <<
value;
633 fpVerboseL2->Message(
"fill",
"ntuple F column", description);
644 description <<
" id " <<
id <<
" value " <<
value;
645 fpVerboseL3->Message(
"fill",
"ntuple D column", description);
649 tools::hbook::wntuple::column<double>* column = GetNtupleDColumn(
id);
652 description <<
" " <<
"column " <<
id <<
" does not exist.";
653 G4Exception(
"G4HbookAnalysisManager::FillNtupleDColumn()",
662 description <<
" id " <<
id <<
" value " <<
value;
663 fpVerboseL2->Message(
"fill",
"ntuple D column", description);
670 G4bool ExG4HbookAnalysisManager::AddNtupleRow()
674 fpVerboseL3->Message(
"add",
"ntuple row",
"");
680 description <<
" " <<
"ntuple does not exist. ";
681 G4Exception(
"G4HbookAnalysisManager::AddNtupleRow()",
690 fpVerboseL2->Message(
"add",
"ntuple row",
"");
698 G4int index =
id - fFirstHistoId;
699 if ( index < 0 || index >=
G4int(fH1Vector.size()) ) {
702 description <<
" " <<
"histo " <<
id <<
" does not exist.";
708 return fH1Vector[
index];
714 G4int index =
id - fFirstHistoId;
715 if ( index < 0 || index >=
G4int(fH2Vector.size()) ) {
718 description <<
" " <<
"histo " <<
id <<
" does not exist.";
724 return fH2Vector[
index];
728 tools::hbook::wntuple* ExG4HbookAnalysisManager::GetNtuple()
const
734 G4bool ExG4HbookAnalysisManager::SetH1HbookIdOffset(
G4int offset)
736 if ( fH1Vector.size() ) {
739 <<
"Cannot set H1HbookIdOffset as some H1 histogramms already exist.";
740 G4Exception(
"G4HbookAnalysisManager::SetH1HbookIdOffset()",
745 if ( fFirstHistoId + offset < 1 ) {
747 description <<
"The first histogram HBOOK id must be >= 1.";
748 G4Exception(
"G4HbookAnalysisManager::SetH1HbookIdOffset()",
753 fH1HbookIdOffset = offset;
758 G4bool ExG4HbookAnalysisManager::SetH2HbookIdOffset(
G4int offset)
760 if ( fH2Vector.size() ) {
763 <<
"Cannot set H2HbookIdOffset as some H2 histogramms already exist.";
764 G4Exception(
"G4HbookAnalysisManager::SetH2HbookIdOffset()",
769 if ( fFirstHistoId + offset < 1 ) {
771 description <<
"The first histogram HBOOK id must be >= 1.";
772 G4Exception(
"G4HbookAnalysisManager::SetH1HbookIdOffset()",
777 fH2HbookIdOffset = offset;
782 G4bool ExG4HbookAnalysisManager::SetNtupleHbookId(
G4int ntupleId)
787 <<
"Cannot set NtupleHbookId as an ntuple already exists.";
788 G4Exception(
"G4HbookAnalysisManager::SetNtupleHbookId()",
793 if ( ntupleId < 1 ) {
795 description <<
"The ntuple HBOOK id must be >= 1.";
796 G4Exception(
"G4HbookAnalysisManager::SetNtupleHbookId()",
801 fNtupleHbookId = ntupleId;