00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "G4XmlAnalysisManager.hh"
00031 #include "G4UnitsTable.hh"
00032
00033 #include "tools/waxml/begend"
00034 #include "tools/waxml/histos"
00035
00036 #include <iostream>
00037
00038 G4XmlAnalysisManager* G4XmlAnalysisManager::fgInstance = 0;
00039
00040
00041 G4XmlAnalysisManager* G4XmlAnalysisManager::Instance()
00042 {
00043 if ( fgInstance == 0 ) {
00044 fgInstance = new G4XmlAnalysisManager();
00045 }
00046
00047 return fgInstance;
00048 }
00049
00050
00051 G4XmlAnalysisManager::G4XmlAnalysisManager()
00052 : G4VAnalysisManager("Xml"),
00053 fFile(0),
00054 fH1Vector(),
00055 fH2Vector(),
00056 fH1NameIdMap(),
00057 fH2NameIdMap(),
00058 fNtuple(0),
00059 fNtupleBooking(0),
00060 fNtupleIColumnMap(),
00061 fNtupleFColumnMap(),
00062 fNtupleDColumnMap()
00063 {
00064 if ( fgInstance ) {
00065 G4ExceptionDescription description;
00066 description << "G4XmlAnalysisManager already exists."
00067 << "Cannot create another instance.";
00068 G4Exception("G4XmlAnalysisManager::G4XmlAnalysisManager",
00069 "Analysis_F001", FatalException, description);
00070 }
00071
00072 fgInstance = this;
00073 }
00074
00075
00076 G4XmlAnalysisManager::~G4XmlAnalysisManager()
00077 {
00078 std::vector<tools::histo::h1d*>::iterator it;
00079 for ( it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
00080 delete *it;
00081 }
00082
00083 std::vector<tools::histo::h2d*>::iterator it2;
00084 for ( it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
00085 delete *it2;
00086 }
00087
00088 delete fNtuple;
00089 delete fNtupleBooking;
00090 delete fFile;
00091
00092 fgInstance = 0;
00093 }
00094
00095
00096
00097
00098
00099
00100 void G4XmlAnalysisManager::CreateNtupleFromBooking()
00101 {
00102
00103 #ifdef G4VERBOSE
00104 if ( fpVerboseL4 )
00105 fpVerboseL4->Message("create from booking", "ntuple", fNtupleBooking->m_name);
00106 #endif
00107
00108 fNtuple = new tools::waxml::ntuple(*fFile, G4cerr, *fNtupleBooking);
00109 if ( fNtupleBooking->m_columns.size() ) {
00110
00111 const std::vector<tools::ntuple_booking::col_t>& columns
00112 = fNtupleBooking->m_columns;
00113 std::vector<tools::ntuple_booking::col_t>::const_iterator it;
00114 G4int index = 0;
00115 for ( it = columns.begin(); it!=columns.end(); ++it) {
00116 if ( (*it).second == tools::_cid(int(0) ) ) {
00117 G4cout << "adding int " << fNtuple->find_column<int>((*it).first) << G4endl;
00118 fNtupleIColumnMap[index++] = fNtuple->find_column<int>((*it).first);
00119 }
00120 else if( (*it).second == tools::_cid(float(0) ) ) {
00121 fNtupleFColumnMap[index++] = fNtuple->find_column<float>((*it).first);
00122 }
00123 else if((*it).second== tools::_cid(double(0))) {
00124 fNtupleDColumnMap[index++] = fNtuple->find_column<double>((*it).first);
00125 }
00126 else {
00127 G4ExceptionDescription description;
00128 description << " "
00129 << "Unsupported column type " << (*it).first;
00130 G4Exception("G4XmlAnalysisManager::OpenFile()",
00131 "Analysis_W004", JustWarning, description);
00132 }
00133 }
00134 }
00135 FinishNtuple();
00136 }
00137
00138
00139 tools::waxml::ntuple::column<int>*
00140 G4XmlAnalysisManager::GetNtupleIColumn(G4int id) const
00141 {
00142 std::map<G4int, tools::waxml::ntuple::column<int>* >::const_iterator it
00143 = fNtupleIColumnMap.find(id);
00144 if ( it == fNtupleIColumnMap.end() ) {
00145 G4ExceptionDescription description;
00146 description << " " << "column " << id << " does not exist.";
00147 G4Exception("G4XmlAnalysisManager::GetNtupleIColumn()",
00148 "Analysis_W009", JustWarning, description);
00149 return 0;
00150 }
00151
00152 return it->second;
00153 }
00154
00155
00156 tools::waxml::ntuple::column<float>*
00157 G4XmlAnalysisManager::GetNtupleFColumn(G4int id) const
00158 {
00159 std::map<G4int, tools::waxml::ntuple::column<float>* >::const_iterator it
00160 = fNtupleFColumnMap.find(id);
00161 if ( it == fNtupleFColumnMap.end() ) {
00162 G4ExceptionDescription description;
00163 description << " " << "column " << id << " does not exist.";
00164 G4Exception("G4XmlAnalysisManager::GetNtupleFColumn()",
00165 "Analysis_W009", JustWarning, description);
00166 return 0;
00167 }
00168
00169 return it->second;
00170 }
00171
00172
00173 G4bool G4XmlAnalysisManager::Reset()
00174 {
00175
00176
00177 G4bool finalResult = true;
00178
00179 std::vector<tools::histo::h1d*>::iterator it;
00180 for (it = fH1Vector.begin(); it != fH1Vector.end(); it++ ) {
00181 G4bool result = (*it)->reset();
00182 if ( ! result ) finalResult = false;
00183 }
00184
00185 std::vector<tools::histo::h2d*>::iterator it2;
00186 for (it2 = fH2Vector.begin(); it2 != fH2Vector.end(); it2++ ) {
00187 G4bool result = (*it2)->reset();
00188 if ( ! result ) finalResult = false;
00189 }
00190
00191 delete fNtuple;
00192 fNtuple = 0;
00193
00194 return finalResult;
00195 }
00196
00197
00198
00199 tools::waxml::ntuple::column<double>*
00200 G4XmlAnalysisManager::GetNtupleDColumn(G4int id) const
00201 {
00202 std::map<G4int, tools::waxml::ntuple::column<double>* >::const_iterator it
00203 = fNtupleDColumnMap.find(id);
00204 if ( it == fNtupleDColumnMap.end() ) {
00205 G4ExceptionDescription description;
00206 description << " " << "column " << id << " does not exist.";
00207 G4Exception("G4XmlAnalysisManager::GetNtupleDColumn()",
00208 "Analysis_W009", JustWarning, description);
00209 return 0;
00210 }
00211
00212 return it->second;
00213 }
00214
00215
00216 tools::histo::h1d* G4XmlAnalysisManager::GetH1InFunction(G4int id,
00217 G4String functionName, G4bool warn,
00218 G4bool onlyIfActive) const
00219 {
00220 G4int index = id - fFirstHistoId;
00221 if ( index < 0 || index >= G4int(fH1Vector.size()) ) {
00222 if ( warn) {
00223 G4String inFunction = "G4XmlAnalysisManager::";
00224 inFunction += functionName;
00225 G4ExceptionDescription description;
00226 description << " " << "histogram " << id << " does not exist.";
00227 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
00228 }
00229 return 0;
00230 }
00231
00232
00233 if ( fActivation && onlyIfActive && ( ! GetActivation(kH1, id) ) ) {
00234 return 0;
00235 }
00236
00237 return fH1Vector[index];
00238 }
00239
00240
00241 tools::histo::h2d* G4XmlAnalysisManager::GetH2InFunction(G4int id,
00242 G4String functionName, G4bool warn,
00243 G4bool onlyIfActive) const
00244 {
00245 G4int index = id - fFirstHistoId;
00246 if ( index < 0 || index >= G4int(fH2Vector.size()) ) {
00247 if ( warn) {
00248 G4String inFunction = "G4XmlAnalysisManager::";
00249 inFunction += functionName;
00250 G4ExceptionDescription description;
00251 description << " " << "histogram " << id << " does not exist.";
00252 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
00253 }
00254 return 0;
00255 }
00256
00257
00258 if ( fActivation && onlyIfActive && ( ! GetActivation(kH2, id) ) ) {
00259 return 0;
00260 }
00261
00262 return fH2Vector[index];
00263 }
00264
00265
00266 void G4XmlAnalysisManager::UpdateTitle(G4String& title,
00267 const G4String& unitName,
00268 const G4String& fcnName) const
00269 {
00270 if ( fcnName != "none" ) { title += " "; title += fcnName; title += "("; }
00271 if ( unitName != "none" ) { title += " ["; title += unitName; title += "]";}
00272 if ( fcnName != "none" ) { title += ")"; }
00273 }
00274
00275
00276
00277
00278
00279
00280 G4bool G4XmlAnalysisManager::WriteOnAscii(std::ofstream& output)
00281 {
00282
00283
00284
00285
00286
00287
00288 for ( G4int i=0; i<G4int(fH1Vector.size()); ++i ) {
00289 G4int id = i + fFirstHistoId;
00290 G4HnInformation* info = GetH1Information(id);
00291
00292 if ( ! info->fAscii ) continue;
00293 tools::histo::h1d* h1 = fH1Vector[i];
00294
00295 #ifdef G4VERBOSE
00296 if ( fpVerboseL3 )
00297 fpVerboseL3->Message("write on ascii", "h1d", info->fName);
00298 #endif
00299
00300 output << "\n 1D histogram " << id << ": " << h1->title()
00301 << "\n \n \t X \t\t Y" << G4endl;
00302
00303 for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
00304 output << " " << j << "\t"
00305 << h1->axis().bin_center(j) << "\t"
00306 << h1->bin_height(j) << G4endl;
00307 }
00308 }
00309
00310 return true;
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 G4bool G4XmlAnalysisManager::OpenFile(const G4String& fileName)
00320 {
00321
00322 G4String name(fileName);
00323 if ( name.find(".") == std::string::npos ) {
00324 name.append(".");
00325 name.append(GetFileType());
00326 }
00327
00328 #ifdef G4VERBOSE
00329 if ( fpVerboseL4 )
00330 fpVerboseL4->Message("open", "analysis file", name);
00331 #endif
00332
00333
00334 if ( fFile ) delete fFile;
00335
00336 fFile = new std::ofstream(name);
00337 if ( fFile->fail() ) {
00338 G4ExceptionDescription description;
00339 description << " " << "Cannot open file " << name;
00340 G4Exception("G4XmlAnalysisManager::OpenFile()",
00341 "Analysis_W001", JustWarning, description);
00342 return false;
00343 }
00344
00345 tools::waxml::begin(*fFile);
00346 #ifdef G4VERBOSE
00347 if ( fpVerboseL1 )
00348 fpVerboseL1->Message("open", "analysis file", name);
00349 #endif
00350
00351
00352 if ( fNtupleBooking && ( ! fNtuple ) )
00353 CreateNtupleFromBooking();
00354
00355 fLockFileName = true;
00356 return true;
00357 }
00358
00359
00360 G4bool G4XmlAnalysisManager::Write()
00361 {
00362
00363 if ( fNtuple ) fNtuple->write_trailer();
00364
00365
00366 for ( G4int i=0; i<G4int(fH1Vector.size()); ++i ) {
00367 G4int id = i + fFirstHistoId;
00368 G4HnInformation* info = GetH1Information(id);
00369
00370 if ( fActivation && ( ! info->fActivation ) ) continue;
00371 tools::histo::h1d* h1 = fH1Vector[i];
00372 #ifdef G4VERBOSE
00373 if ( fpVerboseL3 )
00374 fpVerboseL3->Message("write", "h1d", info->fName);
00375 #endif
00376 G4String path = "/";
00377 path.append(fHistoDirectoryName);
00378 G4bool result
00379 = tools::waxml::write(*fFile, *h1, path, info->fName);
00380 if ( ! result ) {
00381 G4ExceptionDescription description;
00382 description << " " << "saving histogram " << info->fName << " failed";
00383 G4Exception("G4XmlAnalysisManager::Write()",
00384 "Analysis_W003", JustWarning, description);
00385 return false;
00386 }
00387 fLockHistoDirectoryName = true;
00388 }
00389
00390
00391 for ( G4int i=0; i<G4int(fH2Vector.size()); ++i ) {
00392 G4int id = i + fFirstHistoId;
00393 G4HnInformation* info = GetH2Information(id);
00394
00395 if ( fActivation && ( ! info->fActivation ) ) continue;
00396 tools::histo::h2d* h2 = fH2Vector[i];
00397 #ifdef G4VERBOSE
00398 if ( fpVerboseL3 )
00399 fpVerboseL3->Message("write", "h2d", info->fName);
00400 #endif
00401 G4String path = "/";
00402 path.append(fHistoDirectoryName);
00403 G4bool result
00404 = tools::waxml::write(*fFile, *h2, path, info->fName);
00405 if ( ! result ) {
00406 G4ExceptionDescription description;
00407 description << " " << "saving histogram " << info->fName << " failed";
00408 G4Exception("G4XmlAnalysisManager::Write()",
00409 "Analysis_W003", JustWarning, description);
00410 return false;
00411 }
00412 fLockHistoDirectoryName = true;
00413 }
00414 G4bool result = true;
00415
00416 #ifdef G4VERBOSE
00417 if ( fpVerboseL1 )
00418 fpVerboseL1->Message("write", "file", GetFullFileName(), result);
00419 #endif
00420
00421
00422 if ( IsAscii() ) {
00423 result = WriteAscii();
00424 }
00425
00426 return result;
00427 }
00428
00429
00430 G4bool G4XmlAnalysisManager::CloseFile()
00431 {
00432 G4bool result = true;
00433
00434 #ifdef G4VERBOSE
00435 if ( fpVerboseL4 )
00436 fpVerboseL4->Message("close", "file", GetFullFileName());
00437 #endif
00438
00439
00440 result = Reset();
00441 if ( ! result ) {
00442 G4ExceptionDescription description;
00443 description << " " << "Resetting data failed";
00444 G4Exception("G4XmlAnalysisManager::CloseFile()",
00445 "Analysis_W002", JustWarning, description);
00446 result = false;
00447 }
00448
00449
00450 tools::waxml::end(*fFile);
00451 fFile->close();
00452 fLockFileName = false;
00453
00454 #ifdef G4VERBOSE
00455 if ( fpVerboseL1 )
00456 fpVerboseL1->Message("close", "file", GetFullFileName());
00457 #endif
00458
00459 return result;
00460 }
00461
00462
00463 G4int G4XmlAnalysisManager::CreateH1(const G4String& name, const G4String& title,
00464 G4int nbins, G4double xmin, G4double xmax,
00465 const G4String& unitName, const G4String& fcnName)
00466 {
00467 #ifdef G4VERBOSE
00468 if ( fpVerboseL4 )
00469 fpVerboseL4->Message("create", "H1", name);
00470 #endif
00471 G4int index = fH1Vector.size();
00472 G4double unit = GetUnitValue(unitName);
00473 G4Fcn fcn = GetFunction(fcnName);
00474 tools::histo::h1d* h1
00475 = new tools::histo::h1d(title, nbins, fcn(xmin), fcn(xmax));
00476
00477
00478
00479 G4String axisTitle;
00480 UpdateTitle(axisTitle,unitName, fcnName);
00481 h1->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
00482
00483 fH1Vector.push_back(h1);
00484 AddH1Information(name, unitName, fcnName, unit, fcn);
00485
00486 fLockFirstHistoId = true;
00487 #ifdef G4VERBOSE
00488 if ( fpVerboseL2 )
00489 fpVerboseL2->Message("create", "H1", name);
00490 #endif
00491 fH1NameIdMap[name] = index + fFirstHistoId;
00492 return index + fFirstHistoId;
00493 }
00494
00495
00496 G4int G4XmlAnalysisManager::CreateH2(const G4String& name, const G4String& title,
00497 G4int nxbins, G4double xmin, G4double xmax,
00498 G4int nybins, G4double ymin, G4double ymax,
00499 const G4String& xunitName, const G4String& yunitName,
00500 const G4String& xfcnName, const G4String& yfcnName)
00501 {
00502 #ifdef G4VERBOSE
00503 if ( fpVerboseL4 )
00504 fpVerboseL4->Message("create", "H2", name);
00505 #endif
00506 G4int index = fH2Vector.size();
00507 G4double xunit = GetUnitValue(xunitName);
00508 G4double yunit = GetUnitValue(yunitName);
00509 G4Fcn xfcn = GetFunction(xfcnName);
00510 G4Fcn yfcn = GetFunction(yfcnName);
00511 tools::histo::h2d* h2
00512 = new tools::histo::h2d(title,
00513 nxbins, xfcn(xmin), xfcn(xmax),
00514 nybins, yfcn(ymin), yfcn(ymax));
00515
00516
00517
00518 G4String xaxisTitle;
00519 G4String yaxisTitle;
00520 UpdateTitle(xaxisTitle, xunitName, xfcnName);
00521 UpdateTitle(yaxisTitle, yunitName, yfcnName);
00522 h2->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
00523 h2->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
00524
00525 fH2Vector.push_back(h2);
00526 AddH2Information(name, xunitName, yunitName, xfcnName, yfcnName,
00527 xunit, yunit, xfcn, yfcn);
00528
00529 fLockFirstHistoId = true;
00530 #ifdef G4VERBOSE
00531 if ( fpVerboseL2 )
00532 fpVerboseL2->Message("create", "H2", name);
00533 #endif
00534 fH2NameIdMap[name] = index + fFirstHistoId;
00535 return index + fFirstHistoId;
00536 }
00537
00538
00539 G4bool G4XmlAnalysisManager::SetH1(G4int id,
00540 G4int nbins, G4double xmin, G4double xmax,
00541 const G4String& unitName, const G4String& fcnName)
00542 {
00543
00544 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1", false, false);
00545 if ( ! h1d ) return false;
00546
00547 G4HnInformation* info = GetH1Information(id);
00548 #ifdef G4VERBOSE
00549 if ( fpVerboseL4 )
00550 fpVerboseL4->Message("configure", "H1", info->fName);
00551 #endif
00552
00553 G4double unit = GetUnitValue(unitName);
00554 G4Fcn fcn = GetFunction(fcnName);
00555 h1d->configure(nbins, fcn(xmin), fcn(xmax));
00556 info->fXUnitName = unitName;
00557 info->fYUnitName = unitName;
00558 info->fXFcnName = fcnName;
00559 info->fYFcnName = fcnName;
00560 info->fXUnit = unit;
00561 info->fYUnit = unit;
00562 info->fXFcn = fcn;
00563 info->fYFcn = fcn;
00564 SetActivation(kH1, id, true);
00565
00566 G4String axisTitle;
00567 UpdateTitle(axisTitle,unitName, fcnName);
00568 h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
00569
00570 return true;
00571 }
00572
00573
00574 G4bool G4XmlAnalysisManager::SetH2(G4int id,
00575 G4int nxbins, G4double xmin, G4double xmax,
00576 G4int nybins, G4double ymin, G4double ymax,
00577 const G4String& xunitName, const G4String& yunitName,
00578 const G4String& xfcnName, const G4String& yfcnName)
00579 {
00580 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2", false, false);
00581 if ( ! h2d ) return false;
00582
00583 G4HnInformation* info = GetH2Information(id);
00584 #ifdef G4VERBOSE
00585 if ( fpVerboseL4 )
00586 fpVerboseL4->Message("configure", "H2", info->fName);
00587 #endif
00588
00589 G4double xunit = GetUnitValue(xunitName);
00590 G4double yunit = GetUnitValue(yunitName);
00591 G4Fcn xfcn = GetFunction(xfcnName);
00592 G4Fcn yfcn = GetFunction(yfcnName);
00593 h2d->configure(nxbins, xfcn(xmin), xfcn(xmax),
00594 nybins, yfcn(ymin), yfcn(ymax));
00595 info->fXUnitName = xunitName;
00596 info->fYUnitName = yunitName;
00597 info->fXFcnName = xfcnName;
00598 info->fYFcnName = yfcnName;
00599 info->fXUnit = xunit;
00600 info->fYUnit = yunit;
00601 info->fXFcn = xfcn;
00602 info->fYFcn = yfcn;
00603 SetActivation(kH2, id, true);
00604
00605 G4String xaxisTitle;
00606 G4String yaxisTitle;
00607 UpdateTitle(xaxisTitle, xunitName, xfcnName);
00608 UpdateTitle(yaxisTitle, yunitName, yfcnName);
00609 h2d->add_annotation(tools::histo::key_axis_x_title(), xaxisTitle);
00610 h2d->add_annotation(tools::histo::key_axis_y_title(), yaxisTitle);
00611
00612 return true;
00613 }
00614
00615
00616 G4bool G4XmlAnalysisManager::ScaleH1(G4int id, G4double factor)
00617 {
00618 tools::histo::h1d* h1d = GetH1InFunction(id, "ScaleH1", false, false);
00619 if ( ! h1d ) return false;
00620
00621 return h1d->scale(factor);
00622 }
00623
00624
00625 G4bool G4XmlAnalysisManager::ScaleH2(G4int id, G4double factor)
00626 {
00627 tools::histo::h2d* h2d = GetH2InFunction(id, "ScaleH2", false, false);
00628 if ( ! h2d ) return false;
00629
00630 return h2d->scale(factor);
00631 }
00632
00633
00634 void G4XmlAnalysisManager::CreateNtuple(const G4String& name,
00635 const G4String& title)
00636 {
00637 if ( fNtupleBooking ) {
00638 G4ExceptionDescription description;
00639 description << " "
00640 << "Ntuple already exists. "
00641 << "(Only one ntuple is currently supported.)";
00642 G4Exception("G4XmlAnalysisManager::CreateNtuple()",
00643 "Analysis_W006", JustWarning, description);
00644 return;
00645 }
00646
00647 #ifdef G4VERBOSE
00648 if ( fpVerboseL4 )
00649 fpVerboseL4->Message("create", "ntuple", name);
00650 #endif
00651
00652
00653 fNtupleBooking = new tools::ntuple_booking();
00654 fNtupleBooking->m_name = name;
00655 fNtupleBooking->m_title = title;
00656
00657
00658 if ( fFile ) {
00659 fNtuple = new tools::waxml::ntuple(*fFile);
00660
00661 }
00662
00663 #ifdef G4VERBOSE
00664 if ( fpVerboseL2 )
00665 fpVerboseL2->Message("create", "ntuple", name);
00666 #endif
00667 }
00668
00669
00670 G4int G4XmlAnalysisManager::CreateNtupleIColumn(const G4String& name)
00671 {
00672 #ifdef G4VERBOSE
00673 if ( fpVerboseL4 )
00674 fpVerboseL4->Message("create", "ntuple I column", name);
00675 #endif
00676
00677 if ( ! fNtupleBooking ) {
00678 G4ExceptionDescription description;
00679 description << " "
00680 << "Ntuple has to be created first. ";
00681 G4Exception("G4XmlAnalysisManager::CreateNtupleIColumn()",
00682 "Analysis_W005", JustWarning, description);
00683 return -1;
00684 }
00685
00686
00687 G4int index = fNtupleBooking->m_columns.size();
00688 fNtupleBooking->add_column<int>(name);
00689
00690
00691 if ( fNtuple ) {
00692 tools::waxml::ntuple::column<int>* column
00693 = fNtuple->create_column<int>(name);
00694 fNtupleIColumnMap[index] = column;
00695 }
00696
00697 fLockFirstNtupleColumnId = true;
00698
00699 #ifdef G4VERBOSE
00700 if ( fpVerboseL2 )
00701 fpVerboseL2->Message("create", "ntuple I column", name);
00702 #endif
00703
00704 return index + fFirstNtupleColumnId;
00705 }
00706
00707
00708 G4int G4XmlAnalysisManager::CreateNtupleFColumn(const G4String& name)
00709 {
00710 #ifdef G4VERBOSE
00711 if ( fpVerboseL4 )
00712 fpVerboseL4->Message("create", "ntuple F column", name);
00713 #endif
00714
00715 if ( ! fNtupleBooking ) {
00716 G4ExceptionDescription description;
00717 description << " "
00718 << "Ntuple has to be created first. ";
00719 G4Exception("G4XmlAnalysisManager::CreateNtupleFColumn()",
00720 "Analysis_W005", JustWarning, description);
00721 return -1;
00722 }
00723
00724
00725 G4int index = fNtupleBooking->m_columns.size();
00726 fNtupleBooking->add_column<float>(name);
00727
00728
00729 if ( fNtuple ) {
00730 tools::waxml::ntuple::column<float>* column
00731 = fNtuple->create_column<float>(name);
00732 fNtupleFColumnMap[index] = column;
00733 }
00734
00735 fLockFirstNtupleColumnId = true;
00736
00737 #ifdef G4VERBOSE
00738 if ( fpVerboseL2 )
00739 fpVerboseL2->Message("create", "ntuple F column", name);
00740 #endif
00741
00742 return index + fFirstNtupleColumnId;
00743 }
00744
00745
00746 G4int G4XmlAnalysisManager::CreateNtupleDColumn(const G4String& name)
00747 {
00748 #ifdef G4VERBOSE
00749 if ( fpVerboseL4 )
00750 fpVerboseL4->Message("create", "ntuple D column", name);
00751 #endif
00752
00753 if ( ! fNtupleBooking ) {
00754 G4ExceptionDescription description;
00755 description << " "
00756 << "Ntuple has to be created first. ";
00757 G4Exception("G4XmlAnalysisManager::CreateNtupleDColumn()",
00758 "Analysis_W005", JustWarning, description);
00759 return -1;
00760 }
00761
00762
00763 G4int index = fNtupleBooking->m_columns.size();
00764 fNtupleBooking->add_column<double>(name);
00765
00766
00767 if ( fNtuple ) {
00768 tools::waxml::ntuple::column<double>* column
00769 = fNtuple->create_column<double>(name);
00770 fNtupleDColumnMap[index] = column;
00771 }
00772
00773 fLockFirstNtupleColumnId = true;
00774
00775 #ifdef G4VERBOSE
00776 if ( fpVerboseL2 )
00777 fpVerboseL2->Message("create", "ntuple D column", name);
00778 #endif
00779
00780 return index + fFirstNtupleColumnId;
00781 }
00782
00783
00784 void G4XmlAnalysisManager::FinishNtuple()
00785 {
00786 if ( ! fNtuple ) return;
00787
00788 #ifdef G4VERBOSE
00789 if ( fpVerboseL4 )
00790 fpVerboseL4->Message("finish", "ntuple", fNtupleBooking->m_name);
00791 #endif
00792
00793 G4String path = "/";
00794 path.append(fNtupleDirectoryName);
00795 fNtuple->write_header(path, fNtupleBooking->m_name, fNtupleBooking->m_title);
00796
00797 fLockNtupleDirectoryName = true;
00798
00799 #ifdef G4VERBOSE
00800 if ( fpVerboseL2 )
00801 fpVerboseL2->Message("finish", "ntuple", fNtupleBooking->m_name);
00802 #endif
00803 }
00804
00805
00806
00807 G4bool G4XmlAnalysisManager::FillH1(G4int id, G4double value, G4double weight)
00808 {
00809 tools::histo::h1d* h1d = GetH1InFunction(id, "FillH1", true, false);
00810 if ( ! h1d ) return false;
00811
00812 if ( fActivation && ( ! GetActivation(kH1, id) ) ) {
00813
00814 return false;
00815 }
00816
00817 G4HnInformation* info = GetInformation(kH1, id);
00818 h1d->fill(info->fXFcn(value/info->fXUnit), weight);
00819 #ifdef G4VERBOSE
00820 if ( fpVerboseL4 ) {
00821 G4ExceptionDescription description;
00822 description << " id " << id << " value " << value/GetXUnit(kH1, id);
00823 fpVerboseL4->Message("fill", "H1", description);
00824 }
00825 #endif
00826 return true;
00827 }
00828
00829
00830 G4bool G4XmlAnalysisManager::FillH2(G4int id,
00831 G4double xvalue, G4double yvalue,
00832 G4double weight)
00833 {
00834 tools::histo::h2d* h2d = GetH2InFunction(id, "FillH2", true, false);
00835 if ( ! h2d ) return false;
00836
00837 if ( fActivation && ( ! GetActivation(kH2, id) ) ) return false;
00838
00839 G4HnInformation* info = GetInformation(kH2, id);
00840 h2d->fill(info->fXFcn(xvalue/info->fXUnit),
00841 info->fYFcn(yvalue/info->fYUnit), weight);
00842 #ifdef G4VERBOSE
00843 if ( fpVerboseL4 ) {
00844 G4ExceptionDescription description;
00845 description << " id " << id
00846 << " xvalue " << xvalue/GetXUnit(kH2, id)
00847 << " yvalue " << yvalue/GetYUnit(kH2, id);
00848 fpVerboseL4->Message("fill", "H2", description);
00849 }
00850 #endif
00851 return true;
00852 }
00853
00854
00855 G4bool G4XmlAnalysisManager::FillNtupleIColumn(G4int id, G4int value)
00856 {
00857 tools::waxml::ntuple::column<int>* column = GetNtupleIColumn(id);
00858 if ( ! column ) {
00859 G4ExceptionDescription description;
00860 description << " " << "column " << id << " does not exist.";
00861 G4Exception("G4XmlAnalysisManager::FillNtupleIColumn()",
00862 "Analysis_W009", JustWarning, description);
00863 return false;
00864 }
00865
00866 column->fill(value);
00867 #ifdef G4VERBOSE
00868 if ( fpVerboseL4 ) {
00869 G4ExceptionDescription description;
00870 description << " id " << id << " value " << value;
00871 fpVerboseL4->Message("fill", "ntuple I column", description);
00872 }
00873 #endif
00874 return true;
00875 }
00876
00877 G4bool G4XmlAnalysisManager::FillNtupleFColumn(G4int id, G4float value)
00878 {
00879 tools::waxml::ntuple::column<float>* column = GetNtupleFColumn(id);
00880 if ( ! column ) {
00881 G4ExceptionDescription description;
00882 description << " " << "column " << id << " does not exist.";
00883 G4Exception("G4XmlAnalysisManager::FillNtupleFColumn()",
00884 "Analysis_W009", JustWarning, description);
00885 return false;
00886 }
00887
00888 column->fill(value);
00889 #ifdef G4VERBOSE
00890 if ( fpVerboseL4 ) {
00891 G4ExceptionDescription description;
00892 description << " id " << id << " value " << value;
00893 fpVerboseL4->Message("fill", "ntuple F column", description);
00894 }
00895 #endif
00896 return true;
00897 }
00898
00899
00900 G4bool G4XmlAnalysisManager::FillNtupleDColumn(G4int id, G4double value)
00901 {
00902 tools::waxml::ntuple::column<double>* column = GetNtupleDColumn(id);
00903 if ( ! column ) {
00904 G4ExceptionDescription description;
00905 description << " " << "column " << id << " does not exist.";
00906 G4Exception("G4XmlAnalysisManager::FillNtupleDColumn()",
00907 "Analysis_W009", JustWarning, description);
00908 return false;
00909 }
00910
00911 column->fill(value);
00912 #ifdef G4VERBOSE
00913 if ( fpVerboseL4 ) {
00914 G4ExceptionDescription description;
00915 description << " id " << id << " value " << value;
00916 fpVerboseL4->Message("fill", "ntuple D column", description);
00917 }
00918 #endif
00919 return true;
00920 }
00921
00922
00923 G4bool G4XmlAnalysisManager::AddNtupleRow()
00924 {
00925 #ifdef G4VERBOSE
00926 if ( fpVerboseL4 )
00927 fpVerboseL4->Message("add", "ntuple row", "");
00928 #endif
00929
00930 if ( ! fNtuple ) {
00931 G4ExceptionDescription description;
00932 description << " " << "ntuple does not exist. ";
00933 G4Exception("G4XmlAnalysisManager::AddNtupleRow()",
00934 "Analysis_W008", JustWarning, description);
00935 return false;
00936 }
00937
00938 fNtuple->add_row();
00939 #ifdef G4VERBOSE
00940 if ( fpVerboseL4 )
00941 fpVerboseL4->Message("add", "ntuple row", "");
00942 #endif
00943
00944 return true;
00945 }
00946
00947
00948 tools::histo::h1d* G4XmlAnalysisManager::GetH1(G4int id, G4bool warn,
00949 G4bool onlyIfActive) const
00950 {
00951 return GetH1InFunction(id, "GetH1", warn, onlyIfActive);
00952 }
00953
00954
00955 tools::histo::h2d* G4XmlAnalysisManager::GetH2(G4int id, G4bool warn,
00956 G4bool onlyIfActive) const
00957 {
00958 return GetH2InFunction(id, "GetH2", warn, onlyIfActive);
00959 }
00960
00961
00962 G4int G4XmlAnalysisManager::GetH1Id(const G4String& name, G4bool warn) const
00963 {
00964 std::map<G4String, G4int>::const_iterator it = fH1NameIdMap.find(name);
00965 if ( it == fH1NameIdMap.end() ) {
00966 if ( warn) {
00967 G4String inFunction = "G4RootAnalysisManager::GetH1Id";
00968 G4ExceptionDescription description;
00969 description << " " << "histogram " << name << " does not exist.";
00970 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
00971 }
00972 return -1;
00973 }
00974 return it->second;
00975 }
00976
00977
00978 G4int G4XmlAnalysisManager::GetH2Id(const G4String& name, G4bool warn) const
00979 {
00980 std::map<G4String, G4int>::const_iterator it = fH2NameIdMap.find(name);
00981 if ( it == fH2NameIdMap.end() ) {
00982 if ( warn) {
00983 G4String inFunction = "G4RootAnalysisManager::GetH2Id";
00984 G4ExceptionDescription description;
00985 description << " " << "histogram " << name << " does not exist.";
00986 G4Exception(inFunction, "Analysis_W007", JustWarning, description);
00987 }
00988 return -1;
00989 }
00990 return it->second;
00991 }
00992
00993
00994 tools::waxml::ntuple* G4XmlAnalysisManager::GetNtuple() const
00995 {
00996 return fNtuple;
00997 }
00998
00999
01000 G4int G4XmlAnalysisManager::GetH1Nbins(G4int id) const
01001 {
01002 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Nbins");
01003 if ( ! h1d ) return 0;
01004
01005 return h1d->axis().bins();
01006 }
01007
01008
01009 G4double G4XmlAnalysisManager::GetH1Xmin(G4int id) const
01010 {
01011
01012
01013 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Xmin");
01014 if ( ! h1d ) return 0;
01015
01016 G4HnInformation* info = GetInformation(kH1, id);
01017 return info->fXFcn(h1d->axis().lower_edge()*info->fXUnit);
01018 }
01019
01020
01021 G4double G4XmlAnalysisManager::GetH1Xmax(G4int id) const
01022 {
01023 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Xmax");
01024 if ( ! h1d ) return 0;
01025
01026 G4HnInformation* info = GetInformation(kH1, id);
01027 return info->fXFcn(h1d->axis().upper_edge()*info->fXUnit);
01028 }
01029
01030
01031 G4double G4XmlAnalysisManager::GetH1Width(G4int id) const
01032 {
01033 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1XWidth", true, false);
01034 if ( ! h1d ) return 0;
01035
01036 G4int nbins = h1d->axis().bins();
01037 if ( ! nbins ) {
01038 G4ExceptionDescription description;
01039 description << " nbins = 0 (for h1 id = " << id << ").";
01040 G4Exception("G4XmlAnalysisManager::GetH1Width",
01041 "Analysis_W014", JustWarning, description);
01042 return 0;
01043 }
01044
01045 G4HnInformation* info = GetInformation(kH1, id);
01046 return ( info->fXFcn(h1d->axis().upper_edge())
01047 - info->fXFcn(h1d->axis().lower_edge()))*info->fXUnit/nbins;
01048 }
01049
01050
01051 G4int G4XmlAnalysisManager::GetH2Nxbins(G4int id) const
01052 {
01053 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2NXbins");
01054 if ( ! h2d ) return 0;
01055
01056 return h2d->axis_x().bins();
01057 }
01058
01059
01060 G4double G4XmlAnalysisManager::GetH2Xmin(G4int id) const
01061 {
01062
01063
01064 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Xmin");
01065 if ( ! h2d ) return 0;
01066
01067 G4HnInformation* info = GetInformation(kH2, id);
01068 return info->fXFcn(h2d->axis_x().lower_edge()*info->fXUnit);
01069 }
01070
01071
01072 G4double G4XmlAnalysisManager::GetH2Xmax(G4int id) const
01073 {
01074 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Xmax");
01075 if ( ! h2d ) return 0;
01076
01077 G4HnInformation* info = GetInformation(kH2, id);
01078 return info->fXFcn(h2d->axis_x().upper_edge()*info->fXUnit);
01079 }
01080
01081
01082 G4double G4XmlAnalysisManager::GetH2XWidth(G4int id) const
01083 {
01084 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2XWidth", true, false);
01085 if ( ! h2d ) return 0;
01086
01087 G4int nbins = h2d->axis_x().bins();
01088 if ( ! nbins ) {
01089 G4ExceptionDescription description;
01090 description << " nbins = 0 (for h1 id = " << id << ").";
01091 G4Exception("G4XmlAnalysisManager::GetH2Width",
01092 "Analysis_W014", JustWarning, description);
01093 return 0;
01094 }
01095
01096 G4HnInformation* info = GetInformation(kH2, id);
01097 return ( info->fXFcn(h2d->axis_x().upper_edge())
01098 - info->fXFcn(h2d->axis_x().lower_edge()))*info->fXUnit/nbins;
01099 }
01100
01101
01102 G4int G4XmlAnalysisManager::GetH2Nybins(G4int id) const
01103 {
01104 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2NYbins");
01105 if ( ! h2d ) return 0;
01106
01107 return h2d->axis_y().bins();
01108 }
01109
01110
01111 G4double G4XmlAnalysisManager::GetH2Ymin(G4int id) const
01112 {
01113
01114
01115 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Ymin");
01116 if ( ! h2d ) return 0;
01117
01118 G4HnInformation* info = GetInformation(kH2, id);
01119 return info->fYFcn(h2d->axis_y().lower_edge()*info->fYUnit);
01120 }
01121
01122
01123 G4double G4XmlAnalysisManager::GetH2Ymax(G4int id) const
01124 {
01125 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Ymax");
01126 if ( ! h2d ) return 0;
01127
01128 G4HnInformation* info = GetInformation(kH2, id);
01129 return info->fYFcn(h2d->axis_y().upper_edge()*info->fYUnit);
01130 }
01131
01132
01133 G4double G4XmlAnalysisManager::GetH2YWidth(G4int id) const
01134 {
01135 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2YWidth", true, false);
01136 if ( ! h2d ) return 0;
01137
01138 G4int nbins = h2d->axis_y().bins();
01139 if ( ! nbins ) {
01140 G4ExceptionDescription description;
01141 description << " nbins = 0 (for h1 id = " << id << ").";
01142 G4Exception("G4XmlAnalysisManager::GetH2Width",
01143 "Analysis_W014", JustWarning, description);
01144 return 0;
01145 }
01146
01147 G4HnInformation* info = GetInformation(kH2, id);
01148 return ( info->fYFcn(h2d->axis_y().upper_edge())
01149 - info->fYFcn(h2d->axis_y().lower_edge()))*info->fYUnit/nbins;
01150 }
01151
01152
01153 G4bool G4XmlAnalysisManager::SetH1Title(G4int id, const G4String& title)
01154 {
01155 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1Title");
01156 if ( ! h1d ) return false;
01157
01158 return h1d->set_title(title);
01159 }
01160
01161
01162 G4bool G4XmlAnalysisManager::SetH1XAxisTitle(G4int id, const G4String& title)
01163 {
01164 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1XAxisTitle");
01165 if ( ! h1d ) return false;
01166
01167 h1d->add_annotation(tools::histo::key_axis_x_title(), title);
01168 return true;
01169 }
01170
01171
01172 G4bool G4XmlAnalysisManager::SetH1YAxisTitle(G4int id, const G4String& title)
01173 {
01174 tools::histo::h1d* h1d = GetH1InFunction(id, "SetH1YAxisTitle");
01175 if ( ! h1d ) return false;
01176
01177 h1d->add_annotation(tools::histo::key_axis_y_title(), title);
01178 return true;
01179 }
01180
01181
01182 G4bool G4XmlAnalysisManager::SetH2Title(G4int id, const G4String& title)
01183 {
01184 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2Title");
01185 if ( ! h2d ) return false;
01186
01187 return h2d->set_title(title);
01188 }
01189
01190
01191 G4bool G4XmlAnalysisManager::SetH2XAxisTitle(G4int id, const G4String& title)
01192 {
01193 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2XAxisTitle");
01194 if ( ! h2d ) return false;
01195
01196 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
01197 return true;
01198 }
01199
01200
01201 G4bool G4XmlAnalysisManager::SetH2YAxisTitle(G4int id, const G4String& title)
01202 {
01203 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2YAxisTitle");
01204 if ( ! h2d ) return false;
01205
01206 h2d->add_annotation(tools::histo::key_axis_x_title(), title);
01207 return true;
01208 }
01209
01210
01211 G4bool G4XmlAnalysisManager::SetH2ZAxisTitle(G4int id, const G4String& title)
01212 {
01213 tools::histo::h2d* h2d = GetH2InFunction(id, "SetH2ZAxisTitle");
01214 if ( ! h2d ) return false;
01215
01216 h2d->add_annotation(tools::histo::key_axis_z_title(), title);
01217 return true;
01218 }
01219
01220
01221 G4String G4XmlAnalysisManager::GetH1Title(G4int id) const
01222 {
01223 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1Title");
01224 if ( ! h1d ) return "";
01225
01226 return h1d->title();
01227 }
01228
01229
01230 G4String G4XmlAnalysisManager::GetH1XAxisTitle(G4int id) const
01231 {
01232 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1XAxisTitle");
01233 if ( ! h1d ) return "";
01234
01235 G4String title;
01236 G4bool result = h1d->annotation(tools::histo::key_axis_x_title(), title);
01237 if ( ! result ) {
01238 G4ExceptionDescription description;
01239 description << " Failed to get x_axis title for h1 id = " << id << ").";
01240 G4Exception("G4XmlAnalysisManager::GetH1XAxisTitle",
01241 "Analysis_W014", JustWarning, description);
01242 return "";
01243 }
01244
01245 return title;
01246 }
01247
01248
01249 G4String G4XmlAnalysisManager::GetH1YAxisTitle(G4int id) const
01250 {
01251 tools::histo::h1d* h1d = GetH1InFunction(id, "GetH1YAxisTitle");
01252 if ( ! h1d ) return "";
01253
01254 G4String title;
01255 G4bool result = h1d->annotation(tools::histo::key_axis_y_title(), title);
01256 if ( ! result ) {
01257 G4ExceptionDescription description;
01258 description << " Failed to get y_axis title for h1 id = " << id << ").";
01259 G4Exception("G4XmlAnalysisManager::GetH1YAxisTitle",
01260 "Analysis_W014", JustWarning, description);
01261 return "";
01262 }
01263
01264 return title;
01265 }
01266
01267
01268 G4String G4XmlAnalysisManager::GetH2Title(G4int id) const
01269 {
01270 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2Title");
01271 if ( ! h2d ) return "";
01272
01273 return h2d->title();
01274 }
01275
01276
01277
01278 G4String G4XmlAnalysisManager::GetH2XAxisTitle(G4int id) const
01279 {
01280 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2XAxisTitle");
01281 if ( ! h2d ) return "";
01282
01283 G4String title;
01284 G4bool result = h2d->annotation(tools::histo::key_axis_x_title(), title);
01285 if ( ! result ) {
01286 G4ExceptionDescription description;
01287 description << " Failed to get x_axis title for h2 id = " << id << ").";
01288 G4Exception("G4XmlAnalysisManager::GetH2XAxisTitle",
01289 "Analysis_W014", JustWarning, description);
01290 return "";
01291 }
01292
01293 return title;
01294 }
01295
01296
01297 G4String G4XmlAnalysisManager::GetH2YAxisTitle(G4int id) const
01298 {
01299 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2YAxisTitle");
01300 if ( ! h2d ) return "";
01301
01302 G4String title;
01303 G4bool result = h2d->annotation(tools::histo::key_axis_y_title(), title);
01304 if ( ! result ) {
01305 G4ExceptionDescription description;
01306 description << " Failed to get y_axis title for h2 id = " << id << ").";
01307 G4Exception("G4XmlAnalysisManager::GetH2YAxisTitle",
01308 "Analysis_W014", JustWarning, description);
01309 return "";
01310 }
01311
01312 return title;
01313 }
01314
01315
01316 G4String G4XmlAnalysisManager::GetH2ZAxisTitle(G4int id) const
01317 {
01318 tools::histo::h2d* h2d = GetH2InFunction(id, "GetH2ZAxisTitle");
01319 if ( ! h2d ) return "";
01320
01321 G4String title;
01322 G4bool result = h2d->annotation(tools::histo::key_axis_z_title(), title);
01323 if ( ! result ) {
01324 G4ExceptionDescription description;
01325 description << " Failed to get z_axis title for h2 id = " << id << ").";
01326 G4Exception("G4XmlAnalysisManager::GetH2ZAxisTitle",
01327 "Analysis_W014", JustWarning, description);
01328 return "";
01329 }
01330
01331 return title;
01332 }