00001 package cedar.jetweb.html; 00002 00003 import java.io.PrintWriter; 00004 import java.io.BufferedWriter; 00005 import java.io.BufferedReader; 00006 import java.io.FileInputStream; 00007 import java.io.InputStreamReader; 00008 import java.io.File; 00009 import java.io.FileWriter; 00010 import java.io.IOException; 00011 import java.text.DateFormat; 00012 import java.text.SimpleDateFormat; 00013 import java.text.NumberFormat; 00014 import java.text.ParseException; 00015 00016 import java.util.Locale; 00017 import java.util.Date; 00018 import java.util.Enumeration; 00019 import java.util.Vector; 00020 import java.util.List; 00021 import java.util.Iterator; 00022 import java.util.Map; 00023 import java.util.Map.Entry; 00024 import java.util.TreeMap; 00025 00026 import cedar.jetweb.model.*; 00027 import cedar.jetweb.model.paper.*; 00028 import cedar.jetweb.model.plots.*; 00029 import cedar.jetweb.model.fit.*; 00030 import cedar.jetweb.job.*; 00031 import cedar.jetweb.batch.SubmitScriptHandler; 00032 import cedar.jetweb.JetWebException; 00033 import cedar.jetweb.JetWebConfig; 00034 import cedar.jetweb.generator.Generator; 00035 00044 public class HTMLPlotWriter extends HTMLWriter { 00045 00046 private String imagePath_; 00047 00057 public HTMLPlotWriter(PrintWriter out, String newServletPath ){ 00058 super(out,newServletPath); 00059 } 00060 00061 public HTMLPlotWriter(BufferedWriter out, String newServletPath ){ 00062 00063 super(out,newServletPath); 00064 00065 } 00066 00077 public HTMLPlotWriter(String filename, String newServletPath ){ 00078 super(filename,newServletPath); 00079 } 00080 00081 00088 public void displaySelection(PlotSelection plotsel) throws JetWebException { 00089 00090 try { 00091 00092 ofile.write("<div class='plotselection'>"); 00093 00094 if (!verbose) { 00095 00096 if (modifiable) { 00097 ofile.write(HTMLUtils.writeSubmitButton("SELECTPLOTS", 00098 "Select the plots to be included in the fit", 00099 "Select plots")); 00100 } 00101 00102 00103 } 00104 if ((plotsel==null) || (plotsel!=null && plotsel.isDefault())) { 00105 ofile.write(" Default dataset"); 00106 } else { 00107 ofile.write(" Non-default dataset"); 00108 } 00109 00110 if (verbose) { 00111 00112 if (plotsel==null) { 00113 plotsel = new PlotSelection(); 00114 } 00115 00116 ofile.newLine(); 00117 00118 ofile.write("<p>Data used in this fit.</p>"); 00119 00120 if (modifiable){ 00121 ofile.write(HTMLUtils.writeSubmitButton("RESET", 00122 "Clear the selection", 00123 "Clear")); 00124 } 00125 ofile.write("<hr/>"); 00126 ofile.newLine(); 00127 ofile.write("<table>"); 00128 00129 for (Paper paper : PaperBank.getPapers()){ 00130 00131 ofile.write("<tr><td colspan='2'> "); 00132 ofile.write(HTMLPaperWriter.getLink(paper)); 00133 ofile.write("</td>"); 00134 ofile.write("<td colspan='2'> "); 00135 ofile.write("<em> "+paper.getReference()+"</em><br/>"); 00136 ofile.write("</td></tr>"); 00137 ofile.newLine(); 00138 00139 ofile.write("<tr>"); 00140 ofile.write("<td colspan='2'>Plot title</td>"); 00141 if (modifiable) { 00142 ofile.write("<td>Use in Chi2 sum</td>"); 00143 ofile.write("<td>Use in Fit</td>"); 00144 } else { 00145 ofile.write("<td>Used in Chi2 sum</td>"); 00146 ofile.write("<td>Used in Fit</td>"); 00147 } 00148 ofile.write("</tr>"); 00149 ofile.newLine(); 00150 00151 for (DataPlot plot : paper.getPlots()){ 00152 if (modifiable || plotsel.getSumPlots().contains(plot)) { 00153 00154 ofile.write("<tr><td>-</td><td><A href='" 00155 +JetWebConfig.getHTMLRoot() 00156 +"/"+JetWebConfig.plotCacheDirName 00157 +"/"+paper.getDirName() 00158 +"/plot"+plot.getNumber()+"."+JetWebConfig.graphicsType 00159 +"'>"+plot.getTitle()+ 00160 "</a></td>"); 00161 ofile.newLine(); 00162 if (modifiable){ 00163 ofile.write("<td align='left'><input type='checkbox' "); 00164 if (plotsel.getSumPlots().contains(plot)){ 00165 ofile.write("checked "); 00166 } 00167 ofile.write("name='SUMPLOT' value='"+plot.getId()+"'>"); 00168 if (plot.isDefaultSum()) { 00169 ofile.write(" <font color=red>(Recommended)</font></td>"); 00170 } 00171 00172 ofile.newLine(); 00173 ofile.write("<td align='left'><input type='checkbox' "); 00174 if (plotsel.getFitPlots().contains(plot)){ 00175 ofile.write("checked "); 00176 } 00177 ofile.write("name='FITPLOT' value='"+plot.getId()+"'>"); 00178 if (plot.isDefaultFit()) { 00179 ofile.write(" <font color=red>(Recommended)</font></td></tr>"); 00180 } 00181 } else { 00182 ofile.write("<td align='left'>"); 00183 if (plotsel.getSumPlots().contains(plot)){ 00184 ofile.write("YES "); 00185 } else { 00186 ofile.write("NO "); 00187 } 00188 ofile.write("</td>"); 00189 ofile.newLine(); 00190 ofile.write("<td align='left'> "); 00191 if (plotsel.getFitPlots().contains(plot)){ 00192 ofile.write("YES "); 00193 } else { 00194 ofile.write("NO "); 00195 } 00196 ofile.write("</td></tr>"); 00197 00198 } 00199 00200 } 00201 00202 00203 ofile.newLine(); 00204 } 00205 ofile.write("<tr><td colspan='6'><hr/></td></tr>"); 00206 ofile.newLine(); 00207 } 00208 ofile.write("</table>"); 00209 00210 } 00211 00212 ofile.write("</div>"); 00213 00214 00215 } catch ( IOException e ) { 00216 System.out.println("HTMLPlotWriter: problem writing form"); 00217 e.printStackTrace(); 00218 return; 00219 } 00220 } 00221 00228 public static void writePlotComparison(BufferedWriter ofile, DataPlot plot1,double chi21,double chi22){ 00229 00230 NumberFormat chi2format = JetWebConfig.getFormat(); 00231 00232 try { 00233 ofile.write("<TR><TD>"+plot1.getTitle()+"</TD><TD>"+chi2format.format(chi21)+"</TD><TD>"+chi2format.format(chi22)+"</TD></TR>"); 00234 } catch (IOException e){ 00235 System.out.println("problem writing form"); 00236 } 00237 } 00238 00242 public void display(DataPlot plot) throws JetWebException { 00243 display(plot,null); 00244 } 00245 00249 public void display(Object object) throws JetWebException { 00250 try { 00251 display((DataPlot)object,null); 00252 } catch (ClassCastException e) { 00253 throw new JetWebException(e); 00254 } 00255 } 00256 00261 public void display(DataPlot plot, Fit fit) throws JetWebException { 00262 00263 //System.out.println("AAAAAAAAAAAAAAAAAAAAAA"); 00264 00265 00266 //System.out.println("HTMLPlotWriter: plot class is: "+ 00267 // plot.getClass().toString()); 00268 00269 00270 00271 if (imagePath_==null) { 00272 StringBuffer imagePath = new StringBuffer(JetWebConfig.plotCacheDirName); 00273 if (fit!=null) { 00274 imagePath.append("/"+fit.getId()); 00275 } 00276 Paper paper = PaperBank.getPaper(plot.getPaperId()); 00277 imagePath.append("/"+paper.getDirName()); 00278 imagePath_=imagePath.toString(); 00279 } 00280 00281 HTMLProcessWriter procWriter = new HTMLProcessWriter(ofile,servletPath); 00282 String plotLink = JetWebConfig.getHTMLRoot()+"/" 00283 +imagePath_+"/plot"+Integer.toString(plot.getNumber()) 00284 +"."+JetWebConfig.graphicsType; 00285 00286 PlotSelection selection; 00287 if (fit==null) { 00288 selection = new PlotSelection(); 00289 } else { 00290 selection = fit.getPlotSelection(); 00291 } 00292 00293 DataPlot compPlot = null; 00294 00295 if (plot instanceof PredictedPlot){ 00296 compPlot = ((PredictedPlot)plot).getCompPlot(); 00297 } else if (plot instanceof FittedPlot){ 00298 compPlot = ((FittedPlot)plot).getCompPlot(); 00299 } else if (plot instanceof RealPlot && fit!=null) { 00300 FittedPlot newPlot = new FittedPlot((RealPlot)plot,fit); 00301 compPlot = newPlot.getCompPlot(); 00302 plot = newPlot; 00303 System.out.println("new fitted plot made"); 00304 } 00305 00306 try { 00307 00308 ofile.write("<div class='jetweb-plot'>"); 00309 ofile.newLine(); 00310 00311 // Start table row & entry 00312 ofile.write("<h4>"+plot.getTitle()+"</h4>"); 00313 ofile.newLine(); 00314 00315 // include the image 00316 if (verbose) { 00317 00318 if (plot.getChi2()[1]>0 || compPlot==null) { 00319 // Link to the graphics file, unless this is a fitted plot with no data. 00320 ofile.write("<A href='"+plotLink+"'> <img src='" 00321 +plotLink+"'></a>"); 00322 ofile.newLine(); 00323 } 00324 00325 00326 if (fit!=null){ 00327 ofile.write("<p>Chi2 Contribution: (chi2 / DoF):"); 00328 ofile.newLine(); 00329 00330 ofile.write(dform.format(plot.getChi2()[0])+" / " 00331 +dform.format(plot.getChi2()[1])); 00332 ofile.write("</p>"); 00333 ofile.newLine(); 00334 ofile.write("<p>Data (black) was scaled by: " 00335 +dform.format(compPlot.getSysScale())); 00336 ofile.write("<br/>The model was scaled by " 00337 +dform.format(plot.getSysScale())+"</p>"); 00338 } else { 00339 if (verbose) { 00340 ofile.write("<P>Data was scaled by: " 00341 +dform.format(plot.getSysScale())+"</p>"); 00342 } 00343 } 00344 ofile.newLine(); 00345 00346 ofile.write("<p><form method='post' target='_blank' action='" 00347 +JetWebConfig.searchServletPath+"'>"); 00348 ofile.write("<input type='hidden' name='PLOTID' value='"+plot.getId()+"'/>"); 00349 if (fit!=null){ 00350 ofile.write("<input type='hidden' name='FITID' value='"+fit.getId()+"'/>"); 00351 } 00352 ofile.write("<input type='submit' name='PLOTTEXT' value='Vector output of plotted data'/>"); 00353 ofile.write("</form></p>"); 00354 00355 if (compPlot != null) { 00356 if (plot.getChi2()[1]>0){ 00357 ofile.write("<p>For this fit, the simulated data was generated as "); 00358 procWriter.display(plot.getMCProcessType()); 00359 } else if (fit!=null) { 00360 ofile.write("<p>There is no suitable simulated data for this plot."); 00361 ofile.write("For this fit, the required process type is "); 00362 //procWriter.display(new MCProcessType(plot.getProcIdWanted())); 00363 procWriter.display(plot.getMCProcessType()); 00364 } else if (fit==null) { 00365 ofile.write("<p>For this fit, the required process type is "); 00366 procWriter.display(plot.getMCProcessType()); 00367 } 00368 } else { 00369 ofile.write("<p>The default process type for this data is "); 00370 procWriter.display(plot.getMCProcessType()); 00371 } 00372 00373 ofile.write("</p>"); 00374 00375 00376 ofile.newLine(); 00377 if (compPlot!=null){ 00378 ofile.write("<p><b>Pull for each point:</b><br/>"); 00379 for (DataPoint point : plot.getDataPoints()){ 00380 ofile.write("<em>{"+dform.format(point.getChi2())+"}</em>"); 00381 ofile.newLine(); 00382 } 00383 if (!selection.getSumPlots().contains(compPlot)){ 00384 ofile.write("<br/>(this plot not included in the fit)"); 00385 } 00386 } 00387 ofile.write("</p>"); 00388 00389 } else { 00390 00391 openForm("post",servletPath); 00392 00393 ofile.write("<input type='hidden' name='PLOTID' value='"+plot.getId()+"'>"); 00394 ofile.write(HTMLUtils.writeSubmitButton("REALDATA", 00395 "More details on this plot.", 00396 "More")); 00397 closeForm(); 00398 00399 // Link to the graphics file, unless this is a fitted plot with no data. 00400 //ofile.write("<A href='"+plotLink+"'>Graphic</a>"); 00401 ofile.newLine(); 00402 } 00403 00404 00405 ofile.write("</div>"); 00406 00407 00408 } catch ( IOException e ) { 00409 System.out.println("problem writing plot tag"); 00410 return; 00411 } 00412 } 00413 00417 public void setImagePath(String path){ 00418 imagePath_ = path; 00419 } 00420 00421 00431 public static void writePlotText(String fitId, String plotId, 00432 PrintWriter out ) throws JetWebException { 00433 // Start new page. 00434 int fid=-1, plid=-1; 00435 try { 00436 fid= Integer.parseInt(fitId); 00437 } catch (Exception e){ 00438 out.println("Error parsing fitId "+fitId); 00439 out.println("Contact jetweb@hep.ucl.ac.uk"); 00440 } 00441 try { 00442 plid= Integer.parseInt(plotId); 00443 } catch (Exception e){ 00444 out.println("Error parsing plotId "+plotId); 00445 out.println("Contact jetweb@hep.ucl.ac.uk"); 00446 } 00447 FittedPlot plot = null; 00448 RealPlot realPlot = (RealPlot)PaperBank.getPlot(plid); 00449 00450 if (fid>=0){ 00451 plot = new FittedPlot(plid, new Fit(fid)); 00452 String title = "Text for fit "+fitId+", plot "+plid; 00453 writeDataPointText(plot.getDataPoints(),title,out); 00454 } else { 00455 String title = "Text for data plot "+plid; 00456 writeDataPointText(realPlot.getDataPoints(),title,out); 00457 } 00458 } 00459 00467 public static void writeDataPointText(Vector points, String title, PrintWriter out ){ 00468 00469 Enumeration plist = points.elements(); 00470 00471 out.println("<plaintext>"); 00472 out.println(title); 00473 out.println("xmin xmax y error"); 00474 while (plist.hasMoreElements()){ 00475 DataPoint p = (DataPoint)plist.nextElement(); 00476 out.println(p.getXMin()+" " 00477 +p.getXMax()+" "+p.getY()+" "+p.getYUp()); 00478 } 00479 00480 00481 } 00482 }
Generated Wed Jan 17 09:14:27 GMT 2007