ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4H1ToolsManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4H1ToolsManager.cc
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 
27 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
28 
29 #include "G4H1ToolsManager.hh"
31 #include "G4BaseHistoUtilities.hh"
32 #include "G4AnalysisUtilities.hh"
33 
34 #include "tools/histo/h1d"
35 
36 #include <fstream>
37 
38 using namespace G4Analysis;
39 
40 // static definitions
42 
43 //
44 // Constructors, destructor
45 //
46 
47 //_____________________________________________________________________________
49  : G4VH1Manager(),
50  G4THnManager<tools::histo::h1d>(state, "H1")
51 {}
52 
53 //_____________________________________________________________________________
55 {}
56 
57 //
58 // Utility functions
59 //
60 
61 
62 namespace {
63 
64 //_____________________________________________________________________________
65 void UpdateH1Information(G4HnInformation* hnInformation,
66  const G4String& unitName,
67  const G4String& fcnName,
68  G4BinScheme binScheme)
69 {
70  hnInformation->SetDimension(kX, unitName, fcnName, binScheme);
71 }
72 
73 //_____________________________________________________________________________
74 void AddH1Annotation(tools::histo::h1d* h1d,
75  const G4String& unitName,
76  const G4String& fcnName)
77 {
78  G4String axisTitle;
79  UpdateTitle(axisTitle, unitName, fcnName);
80  h1d->add_annotation(tools::histo::key_axis_x_title(), axisTitle);
81 }
82 
83 //_____________________________________________________________________________
84 tools::histo::h1d* CreateToolsH1(const G4String& title,
85  G4int nbins, G4double xmin, G4double xmax,
86  const G4String& unitName,
87  const G4String& fcnName,
88  const G4String& binSchemeName)
89 {
90  auto unit = GetUnitValue(unitName);
91  auto fcn = GetFunction(fcnName);
92  auto binScheme = GetBinScheme(binSchemeName);
93 
94  if ( binScheme != G4BinScheme::kLog ) {
95  if ( binScheme == G4BinScheme::kUser ) {
96  // This should never happen, but let's make sure about it
97  // by issuing a warning
98  G4ExceptionDescription description;
99  description
100  << " User binning scheme setting was ignored." << G4endl
101  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
102  G4Exception("G4H1ToolsManager::CreateH1",
103  "Analysis_W013", JustWarning, description);
104  }
105  return new tools::histo::h1d(title, nbins, fcn(xmin/unit), fcn(xmax/unit));
106  }
107  else {
108  // Compute edges
109  std::vector<G4double> edges;
110  ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
111  return new tools::histo::h1d(title, edges);
112  }
113 }
114 
115 //_____________________________________________________________________________
116 tools::histo::h1d* CreateToolsH1(const G4String& title,
117  const std::vector<G4double>& edges,
118  const G4String& unitName,
119  const G4String& fcnName)
120 {
121  auto unit = GetUnitValue(unitName);
122  auto fcn = GetFunction(fcnName);
123 
124  // Apply function
125  std::vector<G4double> newEdges;
126  ComputeEdges(edges, unit, fcn, newEdges);
127 
128  return new tools::histo::h1d(title, newEdges);
129 }
130 
131 //_____________________________________________________________________________
132 void ConfigureToolsH1(tools::histo::h1d* h1d,
133  G4int nbins, G4double xmin, G4double xmax,
134  const G4String& unitName,
135  const G4String& fcnName,
136  const G4String& binSchemeName)
137 {
138  auto unit = GetUnitValue(unitName);
139  auto fcn = GetFunction(fcnName);
140  auto binScheme = GetBinScheme(binSchemeName);
141 
142  if ( binScheme != G4BinScheme::kLog ) {
143  if ( binScheme == G4BinScheme::kUser ) {
144  // This should never happen, but let's make sure about it
145  // by issuing a warning
146  G4ExceptionDescription description;
147  description
148  << " User binning scheme setting was ignored." << G4endl
149  << " Linear binning will be applied with given (nbins, xmin, xmax) values";
150  G4Exception("G4H1ToolsManager::SetH1",
151  "Analysis_W013", JustWarning, description);
152  }
153  h1d->configure(nbins, fcn(xmin/unit), fcn(xmax/unit));
154  }
155  else {
156  // Compute bins
157  std::vector<G4double> edges;
158  ComputeEdges(nbins, xmin, xmax, unit, fcn, binScheme, edges);
159  h1d->configure(edges);
160  }
161 }
162 
163 //_____________________________________________________________________________
164 void ConfigureToolsH1(tools::histo::h1d* h1d,
165  const std::vector<G4double>& edges,
166  const G4String& unitName,
167  const G4String& fcnName)
168 {
169  // Apply function to edges
170  auto unit = GetUnitValue(unitName);
171  auto fcn = GetFunction(fcnName);
172  std::vector<G4double> newEdges;
173  ComputeEdges(edges, unit, fcn, newEdges);
174 
175  h1d->configure(newEdges);
176 }
177 
178 }
179 
180 //
181 // private methods
182 //
183 
184 //_____________________________________________________________________________
186  const G4String& unitName,
187  const G4String& fcnName,
188  G4BinScheme binScheme) const
189 {
190  auto hnInformation = fHnManager->AddHnInformation(name, kDimension);
191  hnInformation->AddDimension(unitName, fcnName, binScheme);
192 }
193 
194 //
195 // protected methods
196 //
197 
198 //_____________________________________________________________________________
200  G4int nbins, G4double xmin, G4double xmax,
201  const G4String& unitName, const G4String& fcnName,
202  const G4String& binSchemeName)
203 {
204 #ifdef G4VERBOSE
205  if ( fState.GetVerboseL4() )
206  fState.GetVerboseL4()->Message("create", "H1", name);
207 #endif
208 
209  // Create H1
210  auto h1d
211  = CreateToolsH1(title, nbins, xmin, xmax, unitName, fcnName, binSchemeName);
212 
213  // Add annotation
214  AddH1Annotation(h1d, unitName, fcnName);
215 
216  // Save H1 information
217  auto binScheme = GetBinScheme(binSchemeName);
218  AddH1Information(name, unitName, fcnName, binScheme);
219 
220  // Register histogram
221  auto id = RegisterT(h1d, name);
222 
223 #ifdef G4VERBOSE
224  if ( fState.GetVerboseL2() )
225  fState.GetVerboseL2()->Message("create", "H1", name);
226 #endif
227  return id;
228 }
229 
230 //_____________________________________________________________________________
232  const std::vector<G4double>& edges,
233  const G4String& unitName, const G4String& fcnName)
234 {
235 #ifdef G4VERBOSE
236  if ( fState.GetVerboseL4() )
237  fState.GetVerboseL4()->Message("create", "H1", name);
238 #endif
239  auto h1d
240  = CreateToolsH1(title, edges, unitName, fcnName);
241 
242  // Add annotation
243  AddH1Annotation(h1d, unitName, fcnName);
244 
245  // Save H1 information
246  AddH1Information(name, unitName, fcnName, G4BinScheme::kUser);
247 
248  // Register histogram
249  auto id = RegisterT(h1d, name);
250 
251 #ifdef G4VERBOSE
252  if ( fState.GetVerboseL2() )
253  fState.GetVerboseL2()->Message("create", "H1", name);
254 #endif
255  return id;
256 }
257 
258 //_____________________________________________________________________________
260  G4int nbins, G4double xmin, G4double xmax,
261  const G4String& unitName, const G4String& fcnName,
262  const G4String& binSchemeName)
263 {
264  auto h1d = GetTInFunction(id, "SetH1", false, false);
265  if ( ! h1d ) return false;
266 
267  auto info = fHnManager->GetHnInformation(id,"SetH1");
268 #ifdef G4VERBOSE
269  if ( fState.GetVerboseL4() )
270  fState.GetVerboseL4()->Message("configure", "H1", info->GetName());
271 #endif
272 
273  // Configure tools h1
274  ConfigureToolsH1(h1d, nbins, xmin, xmax, unitName, fcnName, binSchemeName);
275 
276  // Add annotation
277  AddH1Annotation(h1d, unitName, fcnName);
278 
279  // Update information
280  auto binScheme = GetBinScheme(binSchemeName);
281  UpdateH1Information(info, unitName, fcnName, binScheme);
282 
283  // Set activation
284  fHnManager->SetActivation(id, true);
285 
286  return true;
287 }
288 
289 //_____________________________________________________________________________
291  const std::vector<G4double>& edges,
292  const G4String& unitName,
293  const G4String& fcnName)
294 {
295  auto h1d = GetTInFunction(id, "SetH1", false, false);
296  if ( ! h1d ) return false;
297 
298  auto info = fHnManager->GetHnInformation(id,"SetH1");
299 #ifdef G4VERBOSE
300  if ( fState.GetVerboseL4() )
301  fState.GetVerboseL4()->Message("configure", "H1", info->GetName());
302 #endif
303 
304  // Configure tools h1
305  ConfigureToolsH1(h1d, edges, unitName, fcnName);
306 
307  // Add annotation
308  AddH1Annotation(h1d, unitName, fcnName);
309 
310  // Update information
311  UpdateH1Information(info, unitName, fcnName, G4BinScheme::kUser);
312 
313  // Set activation
314  fHnManager->SetActivation(id, true);
315 
316  return true;
317 }
318 
319 
320 //_____________________________________________________________________________
322 {
323  auto h1d = GetTInFunction(id, "ScaleH1", false, false);
324  if ( ! h1d ) return false;
325 
326  return h1d->scale(factor);
327 }
328 
329 //_____________________________________________________________________________
331 {
332  auto h1d = GetTInFunction(id, "FillH1", true, false);
333  if ( ! h1d ) return false;
334 
335  if ( fState.GetIsActivation() && ( ! fHnManager->GetActivation(id) ) ) {
336  //G4cout << "Skipping FillH1 for " << id << G4endl;
337  return false;
338  }
339 
340  auto info
341  = fHnManager->GetHnDimensionInformation(id, kX, "FillH1");
342  h1d->fill(info->fFcn(value/info->fUnit), weight);
343 #ifdef G4VERBOSE
344  if ( fState.GetVerboseL4() ) {
345  G4ExceptionDescription description;
346  description << " id " << id << " value " << value
347  << " fcn(value/unit) " << info->fFcn(value/info->fUnit)
348  << " weight " << weight;
349  fState.GetVerboseL4()->Message("fill", "H1", description);
350  }
351 #endif
352  return true;
353 }
354 
355 //_____________________________________________________________________________
357 {
358  return GetTId(name, warn);
359 }
360 
361 //_____________________________________________________________________________
363 {
364  auto h1d = GetTInFunction(id, "GetH1Nbins");
365  if ( ! h1d ) return 0;
366 
367  return GetNbins(*h1d, kX);
368 }
369 
370 //_____________________________________________________________________________
372 {
373 // Returns xmin value with applied unit and histogram function
374 
375  auto h1d = GetTInFunction(id, "GetH1Xmin");
376  if ( ! h1d ) return 0.;
377 
378  return GetMin(*h1d, kX);
379 }
380 
381 //_____________________________________________________________________________
383 {
384  auto h1d = GetTInFunction(id, "GetH1Xmax");
385  if ( ! h1d ) return 0.;
386 
387  return GetMax(*h1d, kX);
388 }
389 
390 //_____________________________________________________________________________
392 {
393  auto h1d = GetTInFunction(id, "GetH1XWidth", true, false);
394  if ( ! h1d ) return 0.;
395 
396  return GetWidth(*h1d, kX, fHnManager->GetHnType());
397 }
398 
399 //_____________________________________________________________________________
401 {
402  auto h1d = GetTInFunction(id, "SetH1Title");
403  if ( ! h1d ) return false;
404 
405  return SetTitle(*h1d, title);
406 }
407 
408 //_____________________________________________________________________________
410 {
411  auto h1d = GetTInFunction(id, "SetH1XAxisTitle");
412  if ( ! h1d ) return false;
413 
414  return SetAxisTitle(*h1d, kX, title);
415 }
416 
417 //_____________________________________________________________________________
419 {
420  auto h1d = GetTInFunction(id, "SetH1YAxisTitle");
421  if ( ! h1d ) return false;
422 
423  return SetAxisTitle(*h1d, kY, title);
424 }
425 
426 //_____________________________________________________________________________
428 {
429  auto h1d = GetTInFunction(id, "GetH1Title");
430  if ( ! h1d ) return "";
431 
432  return GetTitle(*h1d);
433 }
434 
435 
436 //_____________________________________________________________________________
438 {
439  auto h1d = GetTInFunction(id, "GetH1XAxisTitle");
440  if ( ! h1d ) return "";
441 
442  return GetAxisTitle(*h1d, kX, fHnManager->GetHnType());
443 }
444 
445 //_____________________________________________________________________________
447 {
448  auto h1d = GetTInFunction(id, "GetH1YAxisTitle");
449  if ( ! h1d ) return "";
450 
451  return GetAxisTitle(*h1d, kY, fHnManager->GetHnType());
452 }
453 
454 //_____________________________________________________________________________
456 {
457 // Write selected objects on ASCII file
458 // According to the implementation by Michel Maire, originally in
459 // extended examples.
460 
461  // h1 histograms
462  for ( G4int i=0; i<G4int(fTVector.size()); ++i ) {
463  auto id = i + fHnManager->GetFirstId();
464  auto info = fHnManager->GetHnInformation(id,"WriteOnAscii");
465  // skip writing if activation is enabled and H1 is inactivated
466  if ( ! info->GetAscii() ) continue;
467  auto h1 = fTVector[i];
468 
469 #ifdef G4VERBOSE
470  if ( fState.GetVerboseL3() )
471  fState.GetVerboseL3()->Message("write on ascii", "h1d", info->GetName());
472 #endif
473 
474  output << "\n 1D histogram " << id << ": " << h1->title()
475  << "\n \n \t X \t\t Y" << G4endl;
476 
477  for (G4int j=0; j< G4int(h1->axis().bins()); ++j) {
478  output << " " << j << "\t"
479  << h1->axis().bin_center(j) << "\t"
480  << h1->bin_height(j) << G4endl;
481  }
482  }
483 
484  return true;
485 }
486 
487 //
488 // public methods
489 //
490 
491 //_____________________________________________________________________________
493 {
494 #ifdef G4VERBOSE
495  if ( fState.GetVerboseL4() )
496  fState.GetVerboseL4()->Message("add", "H1", name);
497 #endif
498 
499  // Add annotation
500  AddH1Annotation(h1d, "none", "none");
501  // Add information
502  AddH1Information(name, "none", "none", G4BinScheme::kLinear);
503 
504  // Register histogram
505  auto id = RegisterT(h1d, name);
506 
507 #ifdef G4VERBOSE
508  if ( fState.GetVerboseL2() )
509  fState.GetVerboseL2()->Message("add", "H1", name);
510 #endif
511  return id;
512 }
513 
514 
515 //_____________________________________________________________________________
517  const std::vector<tools::histo::h1d*>& h1Vector)
518 {
519  AddTVector(h1Vector);
520 }
521 
522 //_____________________________________________________________________________
524  G4bool onlyIfActive) const
525 {
526  return GetTInFunction(id, "GetH1", warn, onlyIfActive);
527 }
528