UltraScan III
us_eqhistogram.cpp
Go to the documentation of this file.
1 
3 #include "us_eqhistogram.h"
4 #include "us_gui_settings.h"
5 #include "us_settings.h"
6 #include "us_math2.h"
7 
8 #ifdef ARRAY_SIZE
9 #undef ARRAY_SIZE
10 #endif
11 #define ARRAY_SIZE 50
12 
13 // Main constructor
15  QVector< EqScanFit >& scanfits,
16  QWidget* parent,
17  Qt::WindowFlags f )
18  :US_WidgetsDialog( parent, f ),
19  od_limit ( od_limit ),
20  scanfits ( scanfits )
21 {
22  setAttribute ( Qt::WA_DeleteOnClose );
23  setPalette ( US_GuiSettings::frameColor() );
24 
25  // Main layout
26  QVBoxLayout* main = new QVBoxLayout( this );
27  main->setContentsMargins( 2, 2, 2, 2 );
28  main->setSpacing ( 2 );
29 
30  // Analyze data to determine type of histogram
31  double min_extinc = 1.0e28;
32  double max_extinc = -1.0;
33  bool first_scan = true;
34  bool multi_lamd = false;
35  bool ext_define = false;
36  bool plot_conc = false;
37  int first_lamd = 0;
38 
39  for ( int ii = 0; ii < scanfits.size(); ii++ )
40  { // Accumulate information about wavelengths and extinctions
41  if ( ! scanfits[ ii ].scanFit ) continue;
42 
43  int lambda = scanfits[ ii ].wavelen;
44  double extincv = scanfits[ ii ].extincts[ 0 ];
45 
46  if ( first_scan )
47  {
48  first_lamd = lambda;
49  first_scan = false;
50  }
51 
52  else if ( first_lamd != lambda )
53  multi_lamd = true;
54 
55  if ( extincv != 1.0 )
56  {
57  ext_define = true;
58  min_extinc = min( min_extinc, extincv );
59  max_extinc = max( max_extinc, extincv );
60  }
61  }
62 
63  // If extinctions exist, allow the option to plot concentrations
64  if ( ext_define )
65  { // Extinctions are defined
66  QString msgstr;
67 
68  if ( multi_lamd )
69  { // There are multiple wavelengths
70  msgstr = tr( "You appear to be fitting multiple wavelengths\n"
71  "for which different extinction coefficients are\n"
72  "available. Would you like to correct the optical\n"
73  "density profile for extinction, and display real\n"
74  "concentations?\n\n" );
75  }
76 
77  else
78  { // There is a single wavelength
79  msgstr = tr( "You have extinction coefficients defined for\n"
80  "your scans. Would you like to correct the optical\n"
81  "density profile for extinction, and display real\n"
82  "concentations?\n\n" );
83  }
84 
85  msgstr += tr( "NOTE: This program only corrects for the 1st component." );
86 
87  QMessageBox msgBox;
88  msgBox.setWindowTitle( "Display Concentrations?" );
89  msgBox.setText( msgstr );
90  msgBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No );
91 
92  if ( msgBox.exec() == QMessageBox::Yes )
93  plot_conc = true; // Plot-Molar-Concentration selected
94 
95  else
96  plot_conc = false; // Plot-Optical-Density selected
97  }
98 
99  double xplot[ ARRAY_SIZE ];
100  double yplot[ ARRAY_SIZE ];
101 
102  double tmp_od_lim = ( od_limit == 0.0 ) ? 1.0 : od_limit;
103  double bin_low = 0.01 / ( 1.2 * max_extinc );
104  double bin_high = tmp_od_lim / ( 1.2 * min_extinc );
105  double x_incr = 1.0 / (double)ARRAY_SIZE;
106  double x_valu = tmp_od_lim * x_incr;
107 
108  if ( plot_conc )
109  { // initial X value and X increment for Concentration
110  x_valu = bin_low;
111  x_incr *= ( bin_high - bin_low );
112  }
113 
114  else
115  { // initial X value (as above) and X increment for Density
116  x_incr = x_valu;
117  }
118 
119  for ( int jj = 0; jj < ARRAY_SIZE; jj++ )
120  { // Build X-values ramp and initialize Y's to zero
121  xplot[ jj ] = x_valu;
122  x_valu += x_incr;
123  yplot[ jj ] = 0.0;
124  }
125 
126  for ( int ii = 0; ii < scanfits.size(); ii++ )
127  { // Build histogram data for each scan
128  EqScanFit* scnf = &scanfits[ ii ];
129 
130  if ( ! scnf->scanFit ) continue;
131 
132  int strtx = scnf->start_ndx;
133  int stopx = scnf->stop_ndx;
134  int stopn = stopx + 1;
135  double scl_ext = plot_conc ?
136  ( 1.0 / ( scnf->pathlen * scnf->extincts[ 0 ] ) ) : 1.0;
137  double xpres = xplot[ 0 ] - x_incr;
138  double xlast;
139 int nx=scnf->xvs.size();
140 qDebug() << "UH: ii" << ii << " strt,stop,nx" << strtx << stopx << nx
141  << " t_odlim scex" << tmp_od_lim << scl_ext << " yvs,e"
142  << scnf->yvs[strtx] << scnf->yvs[stopx];
143 
144  for ( int jj = 0; jj < ARRAY_SIZE; jj++ )
145  { // Update histogram points for this scan
146  xlast = xpres; // Lower limit for histogram bar
147  xpres = xplot[ jj ]; // Upper limit for histogram bar
148  xpres = min( xpres, tmp_od_lim );
149 
150  for ( int kk = strtx; kk < stopn; kk++ ) // test in radius range
151  { // Add to histogram point where Y's are within OD range
152  double xtest = scnf->yvs[ kk ] * scl_ext;
153 
154  if ( xtest > xlast && xtest <= xpres ) // Y value in bar range,
155  yplot[ jj ] += 1.0; // so bump bar
156  }
157 if ( jj < 3 || (jj+4) > ARRAY_SIZE )
158 qDebug() << "UH: jj" << jj << " xp" << xpres << " yplot[jj]" << yplot[jj];
159  }
160  }
161 
162  // Create the histogram plot titles
163  QString odtype = tr( "Optical Density" );
164  QString mctype = tr( "Molar Concentration" );
165  QString htitle = odtype + tr( " Histogram" );
166  QString hxaxis = odtype + tr( " Bin" );
167  QString hyaxis = tr( "Frequency" );
168  QString wtitle = tr( "GlobalEquil " ) + htitle;
169  int points = ARRAY_SIZE;
170 
171  if ( plot_conc )
172  {
173  htitle.replace( odtype, mctype );
174  hxaxis.replace( odtype, mctype );
175  wtitle.replace( odtype, mctype );
176  }
177 
178 qDebug() << "UH: wtitle" << wtitle;
179  setWindowTitle( wtitle );
180 
181  // Create the plot
182  hplot = new US_Plot( hist_plot, htitle, hxaxis, hyaxis );
183  QwtPlotGrid* grid = us_grid( hist_plot );
184  grid->enableYMin( true );
185  grid->enableY ( true );
186 
187  hist_plot->setMinimumSize( 600, 400 );
188  hist_plot->setAxisScale( QwtPlot::xBottom, 0.0, 1.0 );
189  hist_plot->setAxisAutoScale( QwtPlot::yLeft );
190 
191  // Build a "curve" consisting of bars
192  QwtPlotCurve* hcurve = us_curve( hist_plot, "Histogram Bar" );
193  hcurve->setPen( QPen( QBrush( Qt::red ), 6.0 ) );
194  hcurve->setStyle( QwtPlotCurve::Sticks );
195 qDebug() << "UH: x0 x1 xm xn" << xplot[0] << xplot[1] << xplot[48] << xplot[49];
196 qDebug() << "UH: y0 y1 ym yn" << yplot[0] << yplot[1] << yplot[48] << yplot[49];
197  hcurve->setData( xplot, yplot, points );
198 
199  // Add a "curve" of circles at the max point of each bar
200  QwtPlotCurve* pcurve = us_curve( hist_plot, "Histogram MaxPoints" );
201  pcurve->setStyle( QwtPlotCurve::NoCurve );
202  QwtSymbol sym;
203  sym.setStyle( QwtSymbol::Ellipse );
204  sym.setPen ( QPen( Qt::blue ) );
205  sym.setBrush( QBrush( Qt::yellow ) );
206  sym.setSize ( 12 );
207  pcurve->setSymbol( sym );
208  pcurve->setData( xplot, yplot, points );
209 
210  // Display the plot
211  hist_plot->replot();
212 
213  main->addLayout( hplot );
214 
215  adjustSize();
216 }
217