-
Notifications
You must be signed in to change notification settings - Fork 0
/
Histogram.java
124 lines (113 loc) · 5.54 KB
/
Histogram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.io.*;
class Histogram
{
static PrintWriter screen = new PrintWriter( System.out, true);// Have to define it, since WriteToDisk uses it for one command
// these variables have class scope. see Hubbardpage 197 for use of 'protected'
protected double binsize, binlow, binhigh;
protected String title;
protected int SIZE, underflow, overflow,Nsteps;
int[] hist; // define an integer array to store the histogram
double[] error; //define a double array to store the error on histogram values
double[] perror; //define a double array to store the percentage error on histogram values
// contructor method for the class Histogram
public Histogram(String t, int S, double binlo, double binhi)
{
// store the parameters in local variables to be used later
title = t;
SIZE = S;
binlow = binlo;
binhigh = binhi;
//calculate any variables that might be useful later.
binsize = ( binhigh - binlow)/(double) SIZE;
hist = new int[SIZE];
underflow = 0;
overflow = 0;
error = new double[SIZE];
perror = new double[SIZE];
}
// ---------------------------------------------------------
// instance methods start here
// ---------------------------------------------------------
public int getSize() { return SIZE;}
// ---------------------------------------------------------
public double getBinSize() { return binsize;}
// ---------------------------------------------------------
public void fillh( double x)
{
if ( x > binlow && x < binhigh)
{
// update the correct bin
int bin = (int)((x - binlow)/binsize);
hist[bin]++; // add 1 to the bin
error[bin] =Math.sqrt(hist[bin]);
perror[bin] = (error[bin]/hist[bin])*100;
}
else
{
if (x <= binlow) underflow++;
if( x>= binhigh) overflow++;
}
}
// -----------------------------------------------------------
public double getError(int nbin)
{
return error[nbin];
}
public double getPerror(int nbin)
{
return perror[nbin];
}
// -----------------------------------------------------------
public int getUnderflow() {return underflow;}
public int getOverflow() {return overflow;}
// -----------------------------------------------------------
public String getTitle()
{
// return the title of the histogram to the user
return title;
}
// -----------------------------------------------------------
public int getContent( int nbin)
{
// returns the contnets on bin 'nbin' to the user
return hist[nbin];
}
//-------------------Write to Disk-------------------------
//Example of a class method to save the histogram data in a file on disk to import
//into Excel to make presentation quality graphs and charts
public void WriteToDisk( double trials) throws IOException
//public static void WriteToDisk(String t, int s, int[] hist, double[] error, double[] perror, double binlow, double binhigh, double binsize, int underflow, int overflow, double trials, double sumN) throws IOException
{ //
//This method handles the writing to disk
String filename="..\\"+title+".csv"; //Creates a file with given name in directory one above the class directory
FileWriter file1 = new FileWriter(filename); //this crates the file
PrintWriter outputFile = new PrintWriter (file1); // this sends the output to file1
// we chose to write the file as a comma separated file (.csv) so you can read it into EXCEL
screen.println("Writing to disk, please wait....");
outputFile.println("Title of Histogram: , "+title);
outputFile.println("Number of trials: , " +trials);
outputFile.println("Binlow , " +binlow); // note the comma in the text here
outputFile.println("Binhigh , " +binhigh);
outputFile.println("Binint , " +binsize); //ditto the previous comment
outputFile.println("nbins , " +SIZE); //ditto the previous comment
outputFile.println("Underflows , "+underflow);
outputFile.println("Overflows , "+overflow);
outputFile.println("Bin number , Centre , N , Error , %Error");
outputFile.println(" , Underflow , "+underflow+" , "+(Math.sqrt(underflow))+" , "+((100*Math.sqrt(underflow))/underflow));
// now make a loop to write the content to each bin to disk, one number at a time
//together with the x -coordinate of the centre of each bin
for (int n = 0; n <= SIZE-1; n++)
{
//calculate the x coordinates of the centre of each bin
double binCentre = binlow + binsize/2 + n*binsize;
outputFile.println(n+" , " + binCentre+" , " +hist[n]+","+error[n]+","+perror[n]);
//note in the above line we specifically write the comma into the file
}
outputFile.println(" , Overflow , "+overflow+" , "+(Math.sqrt(overflow))+" , "+((100*Math.sqrt(overflow))/overflow));
//outputFile.println(" , Sum: ,"+sumN); //Display the sum of all counts in bins
//outputFile.println(" , Total: ,"+(sumN+underflow+overflow));//Checking that no points were lost, should be equal to a number of trials
outputFile.close(); // close the output file. THIS IS AN IMPORTANT LINE
screen.println(" Data written to disk in file " +filename);
}
//----------------------Write to disk finished---------------------------
}