Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Histo1DVar.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 //
28 //
29 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 //
31 // MODULE: Histo1DVar.cc
32 //
33 // Version: 1.0
34 // Date: 09/03/00
35 // Author: P R Truscott
36 // Organisation: DERA UK
37 // Customer: ESA/ESTEC, NOORDWIJK
38 // Contract: 12115/96/NL/JG Work Order No. 3
39 //
40 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 //
42 // CHANGE HISTORY
43 // --------------
44 //
45 // 30 June 1999, P R Truscott, DERA UK
46 // Version number update 0.b.2 -> 0.b.3, but no functional change.
47 //
48 // 28 August 1999, F Lei & P R Truscott, DERA UK
49 // Version number update 0.b.3 -> 0.b.4, but no functional change.
50 //
51 // 17 September 1999, P R Truscott, DERA UK
52 // Version number update 0.b.4 -> 0.b.5, but no functional change.
53 //
54 // 09 March 2000, P R Truscott, DERA UK
55 // Update 0.b.3 -> 1.0, for compliance with ISO ANSI C++ (no functional change).
56 //
57 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 //
60 #include "Histo1DVar.hh"
62 //
63 Histo1DVar::Histo1DVar (G4String name, double *ep_list, size_t ep_list_len,
64  side conv = LEFT)
65  : theName(name)
66 {
67  //
68  // Set the name of the histogram, define the VariableLengthPartition and
69  // reset the contents of the histogram.
70  //
71  side conv_list[1] = {conv};
73  (ep_list, ep_list_len, conv_list, 1);
74  length = part.total_bins();
75  reset();
76  return;
77 }
79 //
81 {
82  //
83  //
84  // Set default name and partition, and reset the contents of the histogram.
85  //
86  theName = "Blank Array";
88  length= 0;
89  reset();
90  return;
91 }
93 //
95 {
96  totalWeight.clear();
97  meanPosition.clear();
98  totalWeightSquared.clear();
99  nEvents.clear();
100  for (size_t i=0; i< length; i++) {
101  totalWeight.push_back(0.);
102  meanPosition.push_back(0.);
103  totalWeightSquared.push_back(0.);
104  nEvents.push_back(0);
105  }
106  underflowTotalWeight = 0.;
107  overflowTotalWeight = 0.;
108 
109  meanUnderflowPosition = 0.;
110  meanOverflowPosition = 0.;
111 
112  underflowTotalWeightSquared = 0.;
113  overflowTotalWeightSquared = 0.;
114 
115  underflownEvents = 0;
116  overflownEvents = 0;
117 
118  nAllEvents = 0;
119 }
121 //
122 void Histo1DVar::fill (double data_point, double weight = 1.0)
123 {
124  //
125  //
126  // Determine the bin numbers for the point data_point.
127  //
128  int i = (part.get_elem_bin(&data_point));
129 
130  switch (i) {
131 
132  //
133  //
134  // If an underflow or overflow condition is present, then modify the overflow
135  // or underflow variables, otherwise modify the conventional histogram
136  // variables.
137  //
138  case BIN_OVERFLOW :
139  overflowTotalWeight += weight;
140  overflowTotalWeightSquared += weight*weight;
141  meanOverflowPosition += data_point*weight; // saved as total for effiency
142  overflownEvents++;
143  break;
144  case BIN_UNDERFLOW :
145  underflowTotalWeight += weight;
146  underflowTotalWeightSquared += weight*weight;
147  meanUnderflowPosition += data_point*weight; // saved as total for effiency
148  underflownEvents++;
149  break;
150  default:
151  totalWeight[i] += weight;
152  totalWeightSquared[i] += weight*weight;
153  meanPosition[i] += data_point*weight; // saved as total for effiency
154  nEvents[i]++;
155  }
156  nAllEvents++;
157 }
159 //
161 {
162  double value(0.);
163  switch (specialBin) {
164 
165  //
166  //
167  // Output the contents of the overflow, underflow or inrange bin depending
168  // upon the value of specialBin.
169  //
170  case overflow_bin :
171  value = overflowTotalWeight;
172  break;
173  case underflow_bin :
174  value = underflowTotalWeight;
175  break;
176  case inrange :
177  value = get_all_bins();
178  break;
179  }
180  return value;
181 }
183 //
185 {
186  double error(0.);
187  switch (specialBin) {
188 
189  //
190  //
191  // Output the error of the overflow, underflow or inrange bin depending
192  // upon the value of specialBin.
193  //
194  case overflow_bin :
195  if (overflownEvents>0) {
196  error = overflowTotalWeight/std::sqrt((G4double) overflownEvents);}
197  else {
198  error = 0.;}
199  break;
200  case underflow_bin :
201  if (underflownEvents>0) {
202  error = underflowTotalWeight/std::sqrt((G4double) underflownEvents);}
203  else {
204  error = 0.;}
205  break;
206  case inrange :
207  error = 0.;
208  if (nAllEvents-overflownEvents-underflownEvents>0) {
209  for (size_t i=0; i<(part.total_bins()); i++) {
210  error+=totalWeightSquared[i];}
211  error = error/std::sqrt((G4double) nAllEvents-overflownEvents-underflownEvents);
212  }
213  break;
214  }
215  return error;
216 }
218 //
220 {
221  //
222  //
223  // If i is within range, output the conventional histogram variables.
224  // Otherwise output overflow or underflow variables.
225  //
226  double value(0.);
227  if (i > int(part.total_bins()-1))
228  {value = overflowTotalWeight;}
229  else if (i < 0)
230  {value = underflowTotalWeight;}
231  else
232  {value = totalWeight[i];}
233  // cout << i << " " << value << " " << totalWeight[i] << endl;
234  return value;
235 }
237 //
239 {
240  //
241  //
242  // If i is within range, output the conventional histogram variables.
243  // Otherwise output overflow or underflow variables.
244  //
245  double error(0.);
246  if (i > int(part.total_bins())-1) {
247  if (overflownEvents>0) {
248  error = overflowTotalWeight/std::sqrt((G4double) overflownEvents);}
249  else {
250  error = 0.;}
251  }
252  else if (i < 0) {
253  if (underflownEvents>0) {
254  error = underflowTotalWeight/std::sqrt((G4double) underflownEvents);}
255  else {
256  error = 0.;}
257  }
258  else {
259  if (nEvents[i]>0) {
260  error = totalWeight[i]/std::sqrt((G4double) nEvents[i]);}
261  else {
262  error = 0.;}
263  }
264  return error;
265 }
266 
268 //
270 {
271  //
272  //
273  // If i is within range, output the conventional histogram variables.
274  // Otherwise output overflow or underflow variables.
275  //
276  double value = 0.;
277  if (i > int((part.total_bins()-1))) {
278  if (overflownEvents > 0 ) value = meanOverflowPosition/overflowTotalWeight;
279  }
280  else if (i < 0) {
281  if (underflownEvents > 0 ) value = meanUnderflowPosition/underflowTotalWeight;
282  }
283  else if (nEvents[i] > 0 ) value = meanPosition[i]/totalWeight[i];
284 
285  return value;
286 }
287 
289 //
291 {
292  //
293  //
294  // Sum up all bins, including overflow and underflow.
295  //
296  double sum = underflowTotalWeight + overflowTotalWeight;
297  for (size_t i=0; i<(part.total_bins()); i++) {sum += totalWeight[i];}
298 
299  return sum;
300 }
302 //
303 void Histo1DVar::div (double r)
304 {
305  //
306  //
307  // Divide all histogram (including underflow and overflow) variables by r.
308  //
309  overflowTotalWeight = overflowTotalWeight / r;
310  overflowTotalWeightSquared = overflowTotalWeightSquared / r;
311  meanOverflowPosition = meanOverflowPosition / r;
312  underflowTotalWeight = underflowTotalWeight / r;
313  underflowTotalWeightSquared = underflowTotalWeightSquared / r;
314  meanUnderflowPosition = meanUnderflowPosition / r;
315  for (size_t i = 0; i<(part.total_bins()); i++)
316  {
317  totalWeight[i] = totalWeight[i] / r;
318  totalWeightSquared[i] = totalWeightSquared[i] / r;
319  meanPosition[i] = meanPosition[i]/r;
320  }
321 }
323 
324 
325 
326 
327 
328 
329 
330 
331 
332 
333