source: branches/fitshead-branch/Utils/plottingUtilities.cc @ 1441

Last change on this file since 1441 was 56, checked in by Matthew Whiting, 18 years ago

Changes mostly motivated by additional plotting routines (not really used for
Duchamp, but for analysis programs):
Utils/cpgcumul.c -- A pgplot program to plot the cumulative distribution.
Utils/plottingUtilities.cc -- Various functions to draw lines and contour plots.
Utils/utils.hh -- Prototypes for the above, plus the removal of the isDetection function.
Detection/thresholding_functions.cc -- Removal of the #include call for utils.hh
Detection/detection.hh -- Added the prototype for isDetection
Utils/menus.cc -- Changed the menu() function to return "" instead of calling

the cubeMenu when a cube is requested. This enables the
calling program to use the Duchamp input file instead.

File size: 3.5 KB
Line 
1#include <iostream>
2#include <math.h>
3#include <cpgplot.h>
4#include <Utils/utils.hh>
5
6void plotLine(const float slope, const float intercept)
7{
8  float x1, x2, y1, y2;
9  cpgqwin(&x1,&x2,&y1,&y2);
10  cpgmove(x1,slope*x1+intercept);
11  cpgdraw(x2,slope*x2+intercept);
12}
13
14void lineOfEquality()
15{
16  plotLine(1.,0.);
17}
18
19void lineOfBestFit(int size, float *x, float *y)
20{
21  float a,b,r,erra,errb;
22  linear_regression(size,x,y,0,size-1,a,erra,b,errb,r);
23  plotLine(a,b);
24}
25
26void plotVertLine(const float xval, const int colour, const int style)
27{
28  float x1, x2, y1, y2;
29  cpgqwin(&x1,&x2,&y1,&y2);
30  int currentColour,currentStyle;
31  cpgqci(&currentColour);
32  cpgqls(&currentStyle);
33  if(currentColour!=colour) cpgsci(colour);
34  if(currentStyle !=style)  cpgsls(style);
35  cpgmove(xval,y1);
36  cpgdraw(xval,y2);
37  if(currentColour!=colour) cpgsci(currentColour);
38  if(currentStyle !=style)  cpgsls(currentStyle);
39}
40
41void plotVertLine(const float xval)
42{
43  int colour,style;
44  cpgqci(&colour);
45  cpgqls(&style);
46  plotVertLine(xval,colour,style);
47}
48
49void plotVertLine(const float xval, const int colour)
50{
51  int style;
52  cpgqls(&style);
53  plotVertLine(xval,colour,style);
54}
55
56void plotHorizLine(const float yval, const int colour, const int style)
57{
58  float x1, x2, y1, y2;
59  cpgqwin(&x1,&x2,&y1,&y2);
60  int currentColour,currentStyle;
61  cpgqci(&currentColour);
62  cpgqls(&currentStyle);
63  if(currentColour!=colour) cpgsci(colour);
64  if(currentStyle !=style)  cpgsls(style);
65  cpgmove(x1,yval);
66  cpgdraw(x2,yval);
67  if(currentColour!=colour) cpgsci(currentColour);
68  if(currentStyle !=style)  cpgsls(currentStyle);
69}
70
71void plotHorizLine(const float yval)
72{
73  int colour,style;
74  cpgqci(&colour);
75  cpgqls(&style);
76  plotHorizLine(yval,colour,style);
77}
78
79void plotHorizLine(const float yval, const int colour)
80{
81  int style;
82  cpgqls(&style);
83  plotHorizLine(yval,colour,style);
84}
85
86void lineOfBestFitPB(const int size, const float *x, const float *y)
87{
88  int numSim = int(size * log(float(size)*log(float(size))) );
89  float slope=0, intercept=0;
90  float *xboot = new float[size];
91  float *yboot = new float[size];
92  for(int sim=0;sim<numSim;sim++){
93    for(int i=0;i<size;i++){
94      int point = int((1.*rand())/(RAND_MAX+1.0) * size);
95      xboot[i] = x[point];
96      yboot[i] = y[point];
97    }
98    float a,b,r,erra,errb;
99    linear_regression(size,xboot,yboot,0,size-1,a,erra,b,errb,r);
100    slope += a;
101    intercept += b;
102  }
103  delete [] xboot,yboot;
104  slope /= float(numSim);
105  intercept /= float(numSim);
106 
107  plotLine(slope,intercept);
108}
109
110void drawContours(const int size, const float *x, const float *y)
111{
112  float x1, x2, y1, y2;
113  cpgqwin(&x1,&x2,&y1,&y2);
114  const int nbin = 30;
115  // widths of bins in x and y directions
116  float xbin = (x2 - x1) / float(nbin-2); // have one bin either side of extrema
117  float ybin = (y2 - y1) / float(nbin-2);
118  float *binnedarray = new float[nbin*nbin];
119  for(int i=0;i<nbin*nbin;i++) binnedarray[i] = 0.;
120  for(int i=0;i<size;i++){
121    int xpos = int( (x[i] - (x1-xbin)) / xbin );
122    int ypos = int( (y[i] - (y1-ybin)) / ybin );
123    if((ypos*nbin+xpos) >= (nbin*nbin)) std::cerr << "AAARRRRGGGHHHHH!!!!\n";
124    binnedarray[ypos*nbin+xpos] += 1.;
125  } 
126  float maxct = binnedarray[0];
127  for(int i=1;i<nbin*nbin;i++) if(binnedarray[i]>maxct) maxct=binnedarray[i];
128  float tr[6]={x1-3*xbin/2,xbin,0.,y1-3*ybin/2,0.,ybin};
129  const int ncont = 10;
130  float *cont = new float[ncont];
131  for(int i=0;i<ncont;i++) cont[i] = maxct / pow(2,float(i)/2.);
132  cpgcont(binnedarray,nbin,nbin,1,nbin,1,nbin,cont,ncont,tr);
133  delete [] binnedarray;
134  delete [] cont;
135}
Note: See TracBrowser for help on using the repository browser.