Geant4  10.02.p01
ExG4HbookP1Manager.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id$
27 //
30 
31 // Author: Ivana Hrivnacova, 03/11/2014 (ivana@ipno.in2p3.fr)
32 
33 #ifdef G4_USE_HBOOK
34 
35 #include "ExG4HbookP1Manager.hh"
36 #include "ExG4HbookFileManager.hh"
37 #include "G4HnManager.hh"
39 #include "G4AnalysisUtilities.hh"
40 #include "G4BinScheme.hh"
41 
42 #include <fstream>
43 
44 using namespace G4Analysis;
45 
46 const G4int ExG4HbookP1Manager::fgkDefaultP1HbookIdOffset = 201;
47 
48 //_____________________________________________________________________________
49 ExG4HbookP1Manager::ExG4HbookP1Manager(const G4AnalysisManagerState& state)
50  : G4VP1Manager(),
51  G4THnManager<tools::hbook::p1>(state, "P1"),
52  fBaseToolsManager("P1"),
53  fFileManager(0),
54  fP1HbookIdOffset(-1),
55  fP1Vector(),
56  fP1BookingVector(),
57  fP1NameIdMap()
58 {
59 }
60 
61 //_____________________________________________________________________________
62 ExG4HbookP1Manager::~ExG4HbookP1Manager()
63 {
64  // Delete p1
65  // Reset();
66 
67  // Delete p1 booking
68  std::vector<p1_booking*>::iterator it;
69  for ( it = fP1BookingVector.begin(); it != fP1BookingVector.end(); it++ ) {
70  delete *it;
71  }
72 }
73 
74 //
75 // utility functions
76 //
77 
78 namespace {
79 
80 //_____________________________________________________________________________
81 void ConvertToFloat(const std::vector<G4double>& doubleVector,
82  std::vector<float>& floatVector)
83 {
84  for (G4int i=0; i<G4int(doubleVector.size()); ++i)
85  floatVector.push_back((float)doubleVector[i]);
86 }
87 
88 //_____________________________________________________________________________
89 void UpdateP1Information(G4HnInformation* hnInformation,
90  const G4String& xunitName,
91  const G4String& yunitName,
92  const G4String& xfcnName,
93  const G4String& yfcnName,
94  G4BinScheme xbinScheme)
95 {
96  hnInformation->SetDimension(kX, xunitName, xfcnName, xbinScheme);
97  hnInformation->SetDimension(kY, yunitName, yfcnName, G4BinScheme::kLinear);
98 }
99 
100 //_____________________________________________________________________________
101 p1_booking* CreateP1Booking(const G4String& title,
102  G4int nbins, G4double xmin, G4double xmax,
103  G4double ymin, G4double ymax,
104  const G4String& xunitName,
105  const G4String& yunitName,
106  const G4String& xfcnName,
107  const G4String& yfcnName,
108  G4BinScheme xbinScheme)
109 {
110  G4double xunit = GetUnitValue(xunitName);
111  G4double yunit = GetUnitValue(yunitName);
112  G4Fcn xfcn = GetFunction(xfcnName);
113  G4Fcn yfcn = GetFunction(yfcnName);
114 
115  p1_booking* p1Booking = 0;
116  if ( xbinScheme != G4BinScheme::kLog ) {
117  if ( xbinScheme == G4BinScheme::kUser ) {
118  // This should never happen, but let's make sure about it
119  // by issuing a warning
120  G4ExceptionDescription description;
121  description
122  << " User binning scheme setting was ignored." << G4endl
123  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
124  G4Exception("ExG4HbookP1Manager::CreateP1",
125  "Analysis_W013", JustWarning, description);
126  }
127  p1Booking = new p1_booking(nbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
128  yfcn(ymin/yunit), yfcn(ymax/yunit));
129  // p1_booking object is deleted in destructor
130  }
131  else {
132  // Compute edges
133  std::vector<G4double> edges;
134  ComputeEdges(nbins, xmin, xmax, xunit, xfcn, xbinScheme, edges);
135  p1Booking = new p1_booking(edges, yfcn(ymin/yunit), yfcn(ymax/yunit));
136  // p1_booking object is deleted in destructor
137  }
138 
139  p1Booking->fTitle = title;
140  UpdateTitle(p1Booking->fTitle, xunitName, xfcnName);
141 
142  return p1Booking;
143 }
144 
145 //_____________________________________________________________________________
146 p1_booking* CreateP1Booking(const G4String& title,
147  const std::vector<G4double>& edges,
148  G4double ymin, G4double ymax,
149  const G4String& xunitName,
150  const G4String& yunitName,
151  const G4String& xfcnName,
152  const G4String& yfcnName)
153 {
154  G4double xunit = GetUnitValue(xunitName);
155  G4double yunit = GetUnitValue(yunitName);
156  G4Fcn xfcn = GetFunction(xfcnName);
157  G4Fcn yfcn = GetFunction(yfcnName);
158 
159  // Apply function
160  std::vector<G4double> newEdges;
161  ComputeEdges(edges, xunit, xfcn, newEdges);
162 
163  p1_booking* p1Booking = new p1_booking(newEdges, yfcn(ymin/yunit), yfcn(ymax/yunit));
164  // p1_booking object is deleted in destructor
165 
166  p1Booking->fTitle = title;
167  UpdateTitle(p1Booking->fTitle, xunitName, xfcnName);
168 
169  return p1Booking;
170 }
171 
172 //_____________________________________________________________________________
173 void UpdateP1Booking(p1_booking* p1Booking,
174  G4int nbins, G4double xmin, G4double xmax,
175  G4double ymin, G4double ymax,
176  const G4String& xunitName,
177  const G4String& yunitName,
178  const G4String& xfcnName,
179  const G4String& yfcnName,
180  G4BinScheme xbinScheme)
181 {
182  G4double xunit = GetUnitValue(xunitName);
183  G4double yunit = GetUnitValue(yunitName);
184  G4Fcn xfcn = GetFunction(xfcnName);
185  G4Fcn yfcn = GetFunction(yfcnName);
186 
187  if ( xbinScheme != G4BinScheme::kLog ) {
188  if ( xbinScheme == G4BinScheme::kUser ) {
189  // This should never happen, but let's make sure about it
190  // by issuing a warning
191  G4ExceptionDescription description;
192  description
193  << " User binning scheme setting was ignored." << G4endl
194  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
195  G4Exception("ExG4HbookP1Manager::SetP1",
196  "Analysis_W013", JustWarning, description);
197  }
198  p1Booking->fNbins = nbins;
199  p1Booking->fXmin = xfcn(xmin/xunit);
200  p1Booking->fXmax = xfcn(xmax/xunit);
201  p1Booking->fYmin = yfcn(ymin/yunit);
202  p1Booking->fYmax = yfcn(ymax/yunit);
203  }
204  else {
205  // Compute edges
206  ComputeEdges(nbins, xmin, xmax, xunit, xfcn, xbinScheme, p1Booking->fEdges);
207  p1Booking->fYmin = yfcn(ymin/yunit);
208  p1Booking->fYmax = yfcn(ymax/yunit);
209  }
210 
211  UpdateTitle(p1Booking->fTitle, xunitName, xfcnName);
212 }
213 
214 //_____________________________________________________________________________
215 void UpdateP1Booking(p1_booking* p1Booking,
216  const std::vector<G4double>& edges,
217  G4double ymin, G4double ymax,
218  const G4String& xunitName,
219  const G4String& yunitName,
220  const G4String& xfcnName,
221  const G4String& yfcnName)
222 {
223  G4double xunit = GetUnitValue(xunitName);
224  G4double yunit = GetUnitValue(yunitName);
225  G4Fcn xfcn = GetFunction(xfcnName);
226  G4Fcn yfcn = GetFunction(yfcnName);
227 
228  // Apply function
229  ComputeEdges(edges, xunit, xfcn, p1Booking->fEdges);
230  p1Booking->fYmin = yfcn(ymin/yunit);
231  p1Booking->fYmax = yfcn(ymax/yunit);
232 
233  UpdateTitle(p1Booking->fTitle, xunitName, xfcnName);
234 }
235 
236 //_____________________________________________________________________________
237 void ConfigureHbookP1(tools::hbook::p1* p1,
238  G4int nbins, G4double xmin, G4double xmax,
239  G4double ymin, G4double ymax,
240  const G4String& xunitName,
241  const G4String& yunitName,
242  const G4String& xfcnName,
243  const G4String& yfcnName,
244  G4BinScheme xbinScheme)
245 {
246  G4double xunit = GetUnitValue(xunitName);
247  G4double yunit = GetUnitValue(yunitName);
248  G4Fcn xfcn = GetFunction(xfcnName);
249  G4Fcn yfcn = GetFunction(yfcnName);
250 
251  if ( xbinScheme != G4BinScheme::kLog ) {
252  if ( xbinScheme == G4BinScheme::kUser ) {
253  // This should never happen, but let's make sure about it
254  // by issuing a warning
255  G4ExceptionDescription description;
256  description
257  << " User binning scheme setting was ignored." << G4endl
258  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
259  G4Exception("ExG4HbookP1Manager::SetP1",
260  "Analysis_W013", JustWarning, description);
261  }
262  // not available !!
263  p1->configure(nbins, xfcn(xmin/xunit), xfcn(xmax/xunit),
264  yfcn(ymin/yunit), yfcn(ymax/yunit));
265  }
266  else {
267  // Compute bins
268  std::vector<G4double> edges;
269  ComputeEdges(nbins, xmin, xmax, xunit, xfcn, xbinScheme, edges);
270  // Convert to float
271  std::vector<float> fedges;
272  ConvertToFloat(edges, fedges);
273 
274  // not available !!
275  //p1->configure(edges, yfcn(ymin/yunit), yfcn(ymax/yunit));
276  }
277 }
278 
279 //_____________________________________________________________________________
280 void ConfigureHbookP1(tools::hbook::p1* /*p1*/,
281  const std::vector<G4double>& edges,
282  G4double /*ymin*/, G4double /*ymax*/,
283  const G4String& xunitName,
284  const G4String& /*yunitName*/,
285  const G4String& xfcnName,
286  const G4String& /*yfcnName*/)
287 {
288  // Apply function to edges
289  G4double xunit = GetUnitValue(xunitName);
290  //G4double yunit = GetUnitValue(yunitName);
291  G4Fcn xfcn = GetFunction(xfcnName);
292  //G4Fcn yfcn = GetFunction(yfcnName);
293  std::vector<G4double> newEdges;
294  ComputeEdges(edges, xunit, xfcn, newEdges);
295 
296  // Convert to float
297  std::vector<float> newFEdges;
298  ConvertToFloat(newEdges, newFEdges);
299 
300  // not available !!
301  //p1->configure(newFEdges);
302 }
303 
304 }
305 
306 //
307 // private methods
308 //
309 
310 //_____________________________________________________________________________
311 void ExG4HbookP1Manager::SetP1HbookIdOffset()
312 {
313 // Set fP1HbookIdOffset if needed
314 
315  if ( fP1HbookIdOffset == -1 ) {
316  if ( fHnManager->GetFirstId() > 0 )
317  fP1HbookIdOffset = fgkDefaultP1HbookIdOffset - 1;
318  else
319  fP1HbookIdOffset = fgkDefaultP1HbookIdOffset;
320 
321  //G4ExceptionDescription description;
322  // description << "P1 will be defined in HBOOK with ID = G4_firstProfileId + 201";
323  // G4Exception("ExG4HbookP1Manager::SetP1HbookIdOffset",
324  // "Analysis_W013", JustWarning, description);
325  }
326 }
327 
328 //_____________________________________________________________________________
329 void ExG4HbookP1Manager::AddP1Information(const G4String& name,
330  const G4String& xunitName,
331  const G4String& yunitName,
332  const G4String& xfcnName,
333  const G4String& yfcnName,
334  G4BinScheme xbinScheme) const
335 {
336  auto hnInformation = fHnManager->AddHnInformation(name, 2);
337  hnInformation->AddDimension(xunitName, xfcnName, xbinScheme);
338  hnInformation->AddDimension(yunitName, yfcnName, G4BinScheme::kLinear);
339 }
340 
341 //_____________________________________________________________________________
342 G4int ExG4HbookP1Manager::CreateP1FromBooking(p1_booking* p1Booking,
343  G4bool chDir)
344 {
345 // Create p1 from p1_booking.
346 
347  if ( chDir ) {
348  // Go to histograms directory if defined
349  if ( fFileManager->GetHistoDirectoryName() != "" ) {
350  G4String histoPath = "//PAWC/LUN1/";
351  histoPath.append(fFileManager->GetHistoDirectoryName().data());
352  tools::hbook::CHCDIR(histoPath.data()," ");
353  }
354  }
355 
356  G4int index = fP1Vector.size();
357  G4int id = index + fHnManager->GetFirstId();
359  info = fHnManager->GetHnInformation(id, "CreateP1FromBooking");
360  // Hbook index
361  G4int hbookIndex = fP1HbookIdOffset + index + fHnManager->GetFirstId();
362 
363 #ifdef G4VERBOSE
364  if ( fState.GetVerboseL4() )
365  fState.GetVerboseL4()->Message("create from booking", "p1", info->GetName());
366 #endif
367 
368  // Create p1
369  tools::hbook::p1* p1 = 0;
370  if ( ! p1Booking->fEdges.size() ) {
371  p1 = new tools::hbook::p1(
372  hbookIndex, p1Booking->fTitle,
373  p1Booking->fNbins, p1Booking->fXmin, p1Booking->fXmax,
374  p1Booking->fYmin, p1Booking->fYmax);
375  }
376  else {
377  // Convert to float
378  std::vector<float> newEdges;
379  ConvertToFloat(p1Booking->fEdges, newEdges);
380 
381  // not supported
382  //p1 = new tools::hbook::p1(hbookIndex, p1Booking->fTitle, newEdges,
383  // p1Booking->fYmin, p1Booking->fYmax);
384  }
385 
386  fP1Vector.push_back(p1);
387 
388  // Update axis titles
389  fBaseToolsManager.SetAxisTitle(*p1, ExG4HbookBaseHnManager::kX, p1Booking->fXAxisTitle);
390  fBaseToolsManager.SetAxisTitle(*p1, ExG4HbookBaseHnManager::kY, p1Booking->fYAxisTitle);
391 
392  if ( chDir ) {
393  if ( fFileManager->GetHistoDirectoryName() != "" ) {
394  // Return to //PAWC/LUN1 :
395  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
396  }
397  }
398 
399 #ifdef G4VERBOSE
400  if ( fState.GetVerboseL3() ) {
401  G4ExceptionDescription description;
402  description << " name : " << info->GetName() << " hbook index : " << hbookIndex;
403  fState.GetVerboseL3()->Message("create from booking", "p1", description);
404  }
405 #endif
406 
407  return id;
408 }
409 
410 //_____________________________________________________________________________
411 G4int ExG4HbookP1Manager::RegisterP1Booking(const G4String& name,
412  p1_booking* p1Booking)
413 {
414  // Register p1
415  G4int index = fP1BookingVector.size();
416  fP1BookingVector.push_back(p1Booking);
417  fP1NameIdMap[name] = index + fHnManager->GetFirstId();
418 
419  return index + fHnManager->GetFirstId();
420 }
421 
422 //_____________________________________________________________________________
423 void ExG4HbookP1Manager::BeginCreateP1(const G4String& name)
424 {
425 #ifdef G4VERBOSE
426  if ( fState.GetVerboseL4() )
427  fState.GetVerboseL4()->Message("create", "P1", name);
428 #endif
429 
430  // Set fP1HbookIdOffset if needed
431  SetP1HbookIdOffset();
432 }
433 
434 //_____________________________________________________________________________
435 G4int ExG4HbookP1Manager::FinishCreateP1(
436  const G4String& name, p1_booking* p1Booking,
437  const G4String& xunitName,
438  const G4String& yunitName,
439  const G4String& xfcnName,
440  const G4String& yfcnName,
441  G4BinScheme xbinScheme)
442 {
443  // Register p1 booking
444  G4int id = RegisterP1Booking(name, p1Booking);
445 
446  // Save P1 information
447  AddP1Information(name, xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
448 
449  // Create p1 if the file is open
450  if ( fFileManager->IsFile() ) {
451  CreateP1FromBooking(p1Booking);
452  }
453 
454 #ifdef G4VERBOSE
455  if ( fState.GetVerboseL2() ) {
456  G4int hbookIndex = fP1HbookIdOffset + id;
457  G4ExceptionDescription description;
458  description << " name : " << name << " hbook index : " << hbookIndex;
459  fState.GetVerboseL2()->Message("create", "P1", description);
460  }
461 #endif
462 
463  return id;
464 }
465 
466 //_____________________________________________________________________________
467 G4bool ExG4HbookP1Manager::FinishSetP1(
468  G4int id,
469  G4HnInformation* info,
470  const G4String& xunitName,
471  const G4String& yunitName,
472  const G4String& xfcnName,
473  const G4String& yfcnName,
474  G4BinScheme xbinScheme)
475 {
476  // Update information
477  UpdateP1Information(info, xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
478 
479  // Set activation
480  fHnManager->SetActivation(id, true);
481 
482  return true;
483 }
484 
485 
486 //_____________________________________________________________________________
487 void ExG4HbookP1Manager::CreateP1sFromBooking()
488 {
489 // Create all p1 from p1_booking.
490 
491  // Do nothing if any p1 profile already exists
492  // or no p1 profiles are booked
493  if ( fP1Vector.size() || ( fP1BookingVector.size() == 0 ) ) return;
494 
495  // Go to histograms directory if defined
496  if ( fFileManager->GetHistoDirectoryName() != "" ) {
497  G4String histoPath = "//PAWC/LUN1/";
498  histoPath.append(fFileManager->GetHistoDirectoryName().data());
499  tools::hbook::CHCDIR(histoPath.data()," ");
500  }
501 
502  // Create profiles
503  std::vector<p1_booking*>::const_iterator it;
504  for ( it = fP1BookingVector.begin(); it != fP1BookingVector.end(); ++it) {
505  CreateP1FromBooking(*it, false);
506  }
507 
508  // Return back from histograms directory if defined
509  if ( fFileManager->GetHistoDirectoryName() != "" ) {
510  // Return to //PAWC/LUN1 :
511  tools::hbook::CHCDIR("//PAWC/LUN1"," ");
512  }
513 }
514 
515 //_____________________________________________________________________________
516 void ExG4HbookP1Manager::Reset()
517 {
518 // Reset profiles and ntuple
519 
520  // Delete histograms (not needed as done by Hbook when closing file)
521  //std::vector<tools::hbook::p1*>::iterator it;
522  //for (it = fP1Vector.begin(); it != fP1Vector.end(); it++ ) {
523  // delete *it;
524  //}
525 
526  // Clear vectors
527  fP1Vector.clear();
528 }
529 
530 //_____________________________________________________________________________
531 p1_booking* ExG4HbookP1Manager::GetP1Booking(G4int id, G4bool warn) const
532 {
533  G4int index = id - fHnManager->GetFirstId();
534  if ( index < 0 || index >= G4int(fP1BookingVector.size()) ) {
535  if ( warn) {
536  G4ExceptionDescription description;
537  description << " " << "profile " << id << " does not exist.";
538  G4Exception("G4HbookP1Manager::GetP1Booking()",
539  "Analysis_W011", JustWarning, description);
540  }
541  return 0;
542  }
543 
544  return fP1BookingVector[index];
545 }
546 
547 //
548 // protected methods
549 //
550 /*
551 //_____________________________________________________________________________
552 G4bool ExG4HbookP1Manager::WriteOnAscii(std::ofstream& output)
553 {
554 // Write selected objects on ASCII file
555 // (Only P1 implemented by now)
556 // According to the implementation by Michel Maire, originally in
557 // extended examples.
558 
559  // p1 profiles
560  for ( G4int i=0; i<G4int(fP1Vector.size()); ++i ) {
561  G4int id = i + fHnManager->GetFirstId();
562  G4HnInformation* info
563  = fHnManager->GetHnInformation(id, "WriteOnAscii");
564  // skip writing if activation is enabled and P1 is inactivated
565  if ( ! info->fAscii ) continue;
566  tools::hbook::p1* p1 = fP1Vector[i];
567 
568 #ifdef G4VERBOSE
569  if ( fState.GetVerboseL3() )
570  fState.GetVerboseL3()->Message("write on ascii", "p1", info->GetName());
571 #endif
572 
573  output << "\n 1D profile " << id << ": " << p1->title()
574  << "\n \n \t X \t\t Y" << G4endl;
575 
576  for (G4int j=0; j< G4int(p1->axis().bins()); ++j) {
577  output << " " << j << "\t"
578  << p1->axis().bin_center(j) << "\t"
579  << p1->bin_height(j) << G4endl;
580  }
581  }
582 
583  return true;
584 }
585 */
586 //_____________________________________________________________________________
587 tools::hbook::p1* ExG4HbookP1Manager::GetP1InFunction(G4int id,
588  G4String functionName, G4bool warn,
589  G4bool onlyIfActive) const
590 {
591  G4int index = id - fHnManager->GetFirstId();
592  if ( index < 0 || index >= G4int(fP1Vector.size()) ) {
593  if ( warn) {
594  G4String inFunction = "ExG4HbookP1Manager::";
595  inFunction += functionName;
596  G4ExceptionDescription description;
597  description << " " << "profile " << id << " does not exist.";
598  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
599  }
600  return 0;
601  }
602 
603  // Do not return profile if inactive
604  if ( fState.GetIsActivation() && onlyIfActive && ( ! fHnManager->GetActivation(id) ) ) {
605  return 0;
606  }
607 
608  return fP1Vector[index];
609 }
610 
611 //
612 // public methods
613 //
614 
615 //_____________________________________________________________________________
616 G4int ExG4HbookP1Manager::CreateP1(
617  const G4String& name, const G4String& title,
618  G4int nbins, G4double xmin, G4double xmax,
619  G4double ymin, G4double ymax,
620  const G4String& xunitName,
621  const G4String& yunitName,
622  const G4String& xfcnName,
623  const G4String& yfcnName,
624  const G4String& xbinSchemeName)
625 {
626  BeginCreateP1(name);
627 
628  G4BinScheme xbinScheme = GetBinScheme(xbinSchemeName);
629 
630  // Create p1 booking
631  p1_booking* p1Booking
632  = CreateP1Booking(title, nbins, xmin, xmax, ymin, ymax,
633  xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
634 
635  return FinishCreateP1(name, p1Booking,
636  xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
637 }
638 
639 //_____________________________________________________________________________
640 G4int ExG4HbookP1Manager::CreateP1(
641  const G4String& name, const G4String& title,
642  const std::vector<G4double>& edges,
643  G4double ymin, G4double ymax,
644  const G4String& xunitName,
645  const G4String& yunitName,
646  const G4String& xfcnName,
647  const G4String& yfcnName)
648 {
649  BeginCreateP1(name);
650 
651  // Create p1 booking
652  p1_booking* p1Booking
653  = CreateP1Booking(title, edges, ymin, ymax,
654  xunitName, yunitName, xfcnName, yfcnName);
655 
656  return FinishCreateP1(name, p1Booking,
657  xunitName, yunitName, xfcnName, yfcnName, G4BinScheme::kUser);
658 }
659 
660 
661 //_____________________________________________________________________________
662 G4bool ExG4HbookP1Manager::SetP1(G4int id,
663  G4int nbins, G4double xmin, G4double xmax,
664  G4double ymin, G4double ymax,
665  const G4String& xunitName, const G4String& yunitName,
666  const G4String& xfcnName, const G4String& yfcnName,
667  const G4String& xbinSchemeName)
668 {
669  p1_booking*p1Booking = GetP1Booking(id, false);
670  if ( ! p1Booking ) {
671  G4ExceptionDescription description;
672  description << " " << "profile " << id << " does not exist.";
673  G4Exception("G4HbookP1Manager::SetP1()",
674  "Analysis_W011", JustWarning, description);
675  return false;
676  }
677 
678  G4HnInformation* info = fHnManager->GetHnInformation(id,"SetP1");
679 #ifdef G4VERBOSE
680  if ( fState.GetVerboseL4() )
681  fState.GetVerboseL4()->Message("configure", "P1", info->GetName());
682 #endif
683 
684  G4BinScheme xbinScheme = GetBinScheme(xbinSchemeName);
685 
686  // Update P1 booking
687  UpdateP1Booking(p1Booking, nbins, xmin, xmax, ymin, ymax,
688  xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
689 
690  // Re-configure profile if it was already defined
691  if ( fP1Vector.size() ) {
692  tools::hbook::p1* p1 = GetP1(id);
693  ConfigureHbookP1(p1, nbins, xmin, xmax, ymin, ymax,
694  xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
695  }
696 
697  return FinishSetP1(id, info,
698  xunitName, yunitName, xfcnName, yfcnName, xbinScheme);
699 }
700 
701 //_____________________________________________________________________________
702 G4bool ExG4HbookP1Manager::SetP1(G4int id,
703  const std::vector<G4double>& edges,
704  G4double ymin, G4double ymax,
705  const G4String& xunitName, const G4String& yunitName,
706  const G4String& xfcnName, const G4String& yfcnName)
707 {
708  p1_booking*p1Booking = GetP1Booking(id, false);
709  if ( ! p1Booking ) {
710  G4ExceptionDescription description;
711  description << " " << "profile " << id << " does not exist.";
712  G4Exception("G4HbookP1Manager::SetP1()",
713  "Analysis_W011", JustWarning, description);
714  return false;
715  }
716 
717  G4HnInformation* info = fHnManager->GetHnInformation(id,"SetP1");
718 #ifdef G4VERBOSE
719  if ( fState.GetVerboseL4() )
720  fState.GetVerboseL4()->Message("configure", "P1", info->GetName());
721 #endif
722 
723  // Update P1 booking
724  UpdateP1Booking(p1Booking, edges, ymin, ymax,
725  xunitName, yunitName, xfcnName, yfcnName);
726 
727  // Re-configure profile if it was already defined
728  if ( fP1Vector.size() ) {
729  tools::hbook::p1* p1 = GetP1(id);
730  ConfigureHbookP1(p1, edges, ymin, ymax,
731  xunitName, yunitName, xfcnName, yfcnName);
732  }
733 
734  return
735  FinishSetP1(id, info,
736  xunitName, yunitName, xfcnName, yfcnName, G4BinScheme::kUser);
737 }
738 
739 //_____________________________________________________________________________
740 G4bool ExG4HbookP1Manager::ScaleP1(G4int id, G4double factor)
741 {
742  tools::hbook::p1* p1 = GetP1InFunction(id, "ScaleP1", false, false);
743  if ( ! p1 ) return false;
744 
745  return p1->scale(factor);
746 }
747 
748 //_____________________________________________________________________________
749 G4bool ExG4HbookP1Manager::FillP1(G4int id, G4double xvalue, G4double yvalue,
750  G4double weight)
751 {
752  tools::hbook::p1* p1 = GetP1InFunction(id, "FillP1", true, false);
753  if ( ! p1 ) return false;
754 
755  if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
756  //G4cout << "Skipping FillP1 for " << id << G4endl;
757  return false;
758  }
759 
761  = fHnManager->GetHnDimensionInformation(id, kX, "FillP1");
763  = fHnManager->GetHnDimensionInformation(id, kY, "FillP1");
764 
765  p1->fill(xInfo->fFcn(xvalue/xInfo->fUnit),
766  yInfo->fFcn(yvalue/yInfo->fUnit), weight);
767 #ifdef G4VERBOSE
768  if ( fState.GetVerboseL4() ) {
769  G4ExceptionDescription description;
770  description << " id " << id
771  << " xvalue " << xvalue
772  << " xfcn(xvalue/xunit) " << xInfo->fFcn(xvalue/xInfo->fUnit)
773  << " yvalue " << yvalue
774  << " yfcn(yvalue/yunit) " << yInfo->fFcn(yvalue/yInfo->fUnit)
775  << " weight " << weight;
776  fState.GetVerboseL4()->Message("fill", "P1", description);
777  }
778 #endif
779  return true;
780 }
781 
782 //_____________________________________________________________________________
783 tools::hbook::p1* ExG4HbookP1Manager::GetP1(G4int id, G4bool warn,
784  G4bool onlyIfActive) const
785 {
786  return GetP1InFunction(id, "GetP1", warn, onlyIfActive);
787 }
788 
789 //_____________________________________________________________________________
790 G4int ExG4HbookP1Manager::GetP1Id(const G4String& name, G4bool warn) const
791 {
792  std::map<G4String, G4int>::const_iterator it = fP1NameIdMap.find(name);
793  if ( it == fP1NameIdMap.end() ) {
794  if ( warn) {
795  G4String inFunction = "ExG4HbookP1Manager::GetP1Id";
796  G4ExceptionDescription description;
797  description << " " << "profile " << name << " does not exist.";
798  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
799  }
800  return -1;
801  }
802  return it->second;
803 }
804 
805 //_____________________________________________________________________________
806 G4int ExG4HbookP1Manager::GetP1Nbins(G4int id) const
807 {
808  tools::hbook::p1* p1 = GetP1InFunction(id, "GetP1Nbins");
809  if ( ! p1 ) return 0;
810 
811  return fBaseToolsManager.GetNbins(p1->axis());
812 }
813 
814 //_____________________________________________________________________________
815 G4double ExG4HbookP1Manager::GetP1Xmin(G4int id) const
816 {
817 // Returns xmin value with applied unit and profile function
818 
819  tools::hbook::p1* p1 = GetP1InFunction(id, "GetP1Xmin");
820  if ( ! p1 ) return 0;
821 
822  return fBaseToolsManager.GetMin(p1->axis());
823 }
824 
825 //_____________________________________________________________________________
826 G4double ExG4HbookP1Manager::GetP1Xmax(G4int id) const
827 {
828  tools::hbook::p1* p1 = GetP1InFunction(id, "GetP1Xmax");
829  if ( ! p1 ) return 0;
830 
831  return fBaseToolsManager.GetMax(p1->axis());
832 }
833 
834 //_____________________________________________________________________________
835 G4double ExG4HbookP1Manager::GetP1XWidth(G4int id) const
836 {
837  tools::hbook::p1* p1 = GetP1InFunction(id, "GetP1XWidth", true, false);
838  if ( ! p1 ) return 0;
839 
840  G4int nbins = p1->axis().bins();
841  if ( ! nbins ) {
842  G4ExceptionDescription description;
843  description << " nbins = 0 (for p1 id = " << id << ").";
844  G4Exception("ExG4HbookP1Manager::GetP1Width",
845  "Analysis_W014", JustWarning, description);
846  return 0;
847  }
848 
849  return fBaseToolsManager.GetWidth(p1->axis());
850 }
851 
852 //_____________________________________________________________________________
853 G4double ExG4HbookP1Manager::GetP1Ymin(G4int id) const
854 {
855 // Returns xmin value with applied unit and profile function
856 
857  tools::hbook::p1* p1 = GetP1InFunction(id, "GetP1Ymin", true, false);
858  if ( ! p1 ) return 0;
859 
860  // not available
861  //return p1->min_v();
862 
863  G4String inFunction = "ExG4HbookP1Manager::GetP1Ymin";
864  G4ExceptionDescription description;
865  description << "Get function not available.";
866  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
867  return 0;
868 }
869 
870 //_____________________________________________________________________________
871 G4double ExG4HbookP1Manager::GetP1Ymax(G4int id) const
872 {
873  tools::hbook::p1* p1 = GetP1InFunction(id, "GetP1Ymax", true, false);
874  if ( ! p1 ) return 0;
875 
876  // not available
877  //return p1->max_v();
878 
879  G4String inFunction = "ExG4HbookP1Manager::GetP1Ymax";
880  G4ExceptionDescription description;
881  description << "Get function not available.";
882  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
883  return 0;
884 }
885 
886 //_____________________________________________________________________________
887 G4bool ExG4HbookP1Manager::SetP1Title(G4int id, const G4String& title)
888 {
889  p1_booking* p1Booking = GetP1Booking(id, false);
890  if ( ! p1Booking ) {
891  G4ExceptionDescription description;
892  description << " " << "profile " << id << " does not exist.";
893  G4Exception("G4HbookP1Manager::SetP1Title()",
894  "Analysis_W011", JustWarning, description);
895  return false;
896  }
897 
898  p1Booking->fTitle = title;
899  return true;
900 }
901 
902 //_____________________________________________________________________________
903 G4bool ExG4HbookP1Manager::SetP1XAxisTitle(G4int id, const G4String& title)
904 {
905  p1_booking* p1Booking = GetP1Booking(id, false);
906  if ( ! p1Booking ) {
907  G4ExceptionDescription description;
908  description << " " << "histogram " << id << " does not exist.";
909  G4Exception("G4HbookP1Manager::SetP1XAxisTitle()",
910  "Analysis_W011", JustWarning, description);
911  return false;
912  }
913 
914  // set value to booking
915  p1Booking->fXAxisTitle = title;
916 
917  // update value in the histogram if it exists
918  tools::hbook::p1* p1 = GetP1InFunction(id, "SetP1XAxisTitle", false, false);
919  if ( ! p1 ) return true;
920 
921  return fBaseToolsManager.SetAxisTitle(*p1, ExG4HbookBaseHnManager::kX, title);
922 }
923 
924 //_____________________________________________________________________________
925 G4bool ExG4HbookP1Manager::SetP1YAxisTitle(G4int id, const G4String& title)
926 {
927  p1_booking* p1Booking = GetP1Booking(id, false);
928  if ( ! p1Booking ) {
929  G4ExceptionDescription description;
930  description << " " << "histogram " << id << " does not exist.";
931  G4Exception("G4Hbookp1Manager::SetP1YAxisTitle()",
932  "Analysis_W011", JustWarning, description);
933  return false;
934  }
935 
936  // set value to booking
937  p1Booking->fYAxisTitle = title;
938 
939  // update value in the histogram if it exists
940  tools::hbook::p1* p1 = GetP1InFunction(id, "SetP1YAxisTitle", false, false);
941  if ( ! p1 ) return true;
942 
943  return fBaseToolsManager.SetAxisTitle(*p1, ExG4HbookBaseHnManager::kY, title);
944 }
945 
946 //_____________________________________________________________________________
947 G4String ExG4HbookP1Manager::GetP1Title(G4int id) const
948 {
949  p1_booking* p1Booking = GetP1Booking(id, false);
950  if ( ! p1Booking ) {
951  G4ExceptionDescription description;
952  description << " " << "profile " << id << " does not exist.";
953  G4Exception("G4HbookP1Manager::GetP1Title()",
954  "Analysis_W011", JustWarning, description);
955  return "";
956  }
957 
958  return p1Booking->fTitle;
959 }
960 
961 
962 //_____________________________________________________________________________
963 G4String ExG4HbookP1Manager::GetP1XAxisTitle(G4int id) const
964 {
965  p1_booking* p1Booking = GetP1Booking(id, false);
966  if ( ! p1Booking ) {
967  G4ExceptionDescription description;
968  description << " " << "histogram " << id << " does not exist.";
969  G4Exception("G4HbookP1Manager::GetP1XAxisTitle()",
970  "Analysis_W011", JustWarning, description);
971  return "";
972  }
973 
974  return p1Booking->fXAxisTitle;
975 }
976 
977 //_____________________________________________________________________________
978 G4String ExG4HbookP1Manager::GetP1YAxisTitle(G4int id) const
979 {
980  p1_booking* p1Booking = GetP1Booking(id, false);
981  if ( ! p1Booking ) {
982  G4ExceptionDescription description;
983  description << " " << "histogram " << id << " does not exist.";
984  G4Exception("G4HbookP1Manager::GetP1YAxisTitle()",
985  "Analysis_W011", JustWarning, description);
986  return "";
987  }
988 
989  return p1Booking->fYAxisTitle;
990 }
991 
992 //_____________________________________________________________________________
993 G4bool ExG4HbookP1Manager::SetP1HbookIdOffset(G4int offset)
994 {
995  if ( fP1Vector.size() ) {
996  G4ExceptionDescription description;
997  description
998  << "Cannot set P1HbookIdOffset as some P1 profiles already exist.";
999  G4Exception("G4HbookP1Manager::SetP1HbookIdOffset()",
1000  "Analysis_W013", JustWarning, description);
1001  return false;
1002  }
1003 
1004  if ( fHnManager->GetFirstId() + offset < 1 ) {
1005  G4ExceptionDescription description;
1006  description << "The first profile HBOOK id must be >= 1.";
1007  G4Exception("G4HbookP1Manager::SetP1HbookIdOffset()",
1008  "Analysis_W013", JustWarning, description);
1009  return false;
1010  }
1011 
1012  fP1HbookIdOffset = offset;
1013  return true;
1014 }
1015 
1016 #endif
G4double(*)(G4double) G4Fcn
Definition: G4Fcn.hh:36
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
void ComputeEdges(G4int nbins, G4double xmin, G4double xmax, G4double unit, G4Fcn fcn, G4BinScheme, std::vector< G4double > &edges)
Definition: G4BinScheme.cc:56
G4String name
Definition: TRTMaterials.hh:40
int G4int
Definition: G4Types.hh:78
void UpdateTitle(G4String &title, const G4String &unitName, const G4String &fcnName)
bool G4bool
Definition: G4Types.hh:79
const G4int kX
G4double GetUnitValue(const G4String &unit)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
const char * data() const
static const G4double factor
G4Fcn GetFunction(const G4String &fcnName)
Definition: G4Fcn.cc:36
G4String & append(const G4String &)
G4BinScheme GetBinScheme(const G4String &binSchemeName)
Definition: G4BinScheme.cc:36
#define G4endl
Definition: G4ios.hh:61
G4String GetName() const
G4BinScheme
Definition: G4BinScheme.hh:40
double G4double
Definition: G4Types.hh:76
void AddDimension(const G4String &unitName, const G4String &fcnName, G4BinScheme binScheme)
const G4int kY
Definition of the ExG4HbookFileManager class.
void SetDimension(G4int dimension, const G4String &unitName, const G4String &fcnName, G4BinScheme binScheme)
Definition of the ExG4HbookP1Manager class.