Java source code of 'jhplot.io.HBook'

// * This code is licensed under:
// * JHPlot License, Version 1.0
// * - for license details see http://hepforge.cedar.ac.uk/jhepwork/ 
// *
// * Copyright (c) 2005 by S.Chekanov (chekanov@mail.desy.de). 
// * All rights reserved.

package jhplot.io;

import java.io.*;
import java.net.*;
import java.util.*;
import jhplot.*;
import jhplot.gui.HelpBrowser;
import jplot.XMLRead;
import java.text.DecimalFormat;
import hep.aida.*;

/**
 * 
 * This class is used to book histograms for writing and reading. It can read
 * any input with histograms defined by an external XML file, 1D and 2D arrays.
 * In particular, it can read histogram files generated by FORTRAN or C++
 * external programs. Unlike the Java serialisation, we keep only data using
 * very clear human-readable syntax. Use integer keys to insert or retrieve
 * objects from XML files. The output size is substantially smaller than for the
 * standard serialisation. You can read data from URL as well.
 * 

* The file extension of HBook is "jdat". * * @see CFBook web page. Look at * examples in examples/hbook.py and hbook2d.py. *

* Generally, however, use HFile or Serialized class to keep objects to * read and write objects in a serialized form. * * @author S.Chekanov * */ public class HBook { private BufferedReader reader = null; private Map map; private String description = "JDAT file format"; private String createdBy = "(jWork) @S.Chekanov"; private int version = 2; private String time = ""; final private String sep = " "; private String file; private String option="r"; private static int count=0; private DecimalFormat dfb = new DecimalFormat("##.#####E00"); /** * Initialize HBook and tell what to do. * If the option is "w", the file will be written. * If the option is "r", we read the file. * If the file name starts with * "http" or "ftp", the file will be read from URL. * You should call "close" to write the objects in case "w" option. * * @param file * file for reading or writing * @param option * can be "r" (read) or "w" (write). */ public HBook(String file,String option) { this.file=file; this.option=option; map = new HashMap(); if (option.equalsIgnoreCase("r")) { read(file); }; } /** * Set format for writting double values. The default format is * "##.#####E00. * @param dfb writing format. */ public void setFormat(DecimalFormat dfb) { this.dfb = dfb; } /** * Set a desciption which will be attached to the header of the output file. * @param description description. **/ public void setDescription(String description) { this.description = description; } /** * Initialize HBook class for reading a file. * The file can have URL (should start from http or ftp.) * @param file input file for reading (can be URL). */ public HBook(String file) { this(file,"r"); } /** * write an external XML file with all HBook objects (H1D, H2D, P1D). * @param file * output file name */ private void write(String file) { Date dat = new Date(); String today = String.valueOf(dat); try { FileOutputStream f1 = new FileOutputStream(new File(file)); PrintStream tx = new PrintStream(f1); tx.println(""); tx.println(""); setString("created-by", createdBy, tx); setString("created-on", today, tx); setString("description", description, tx); setInt("version", version, tx); tx.println(""); Iterator> entries = map.entrySet() .iterator(); while (entries.hasNext()) { Map.Entry entry = entries.next(); String key = entry.getKey(); Object ob = entry.getValue(); if (ob instanceof jhplot.H1D) { H1D h1 = (jhplot.H1D) ob; writeH1D(tx, key, h1); } else if (ob instanceof jhplot.H2D) { H2D h2 = (jhplot.H2D) ob; writeH2D(tx, key, h2); } else if (ob instanceof jhplot.P1D) { P1D p1 = (jhplot.P1D) ob; writeP1D(tx, key, p1); } else if (ob instanceof jhplot.P0D) { P0D p1 = (jhplot.P0D) ob; writeP0D(tx, key, p1); } else if (ob instanceof jhplot.P0I) { P0I p1 = (jhplot.P0I) ob; writeP0I(tx, key, p1); } else if (ob instanceof jhplot.PND) { PND p1 = (jhplot.PND) ob; writePND(tx, key, p1); } else if (ob instanceof jhplot.PNI) { PNI p1 = (jhplot.PNI) ob; writePNI(tx, key, p1); } else if (ob instanceof jhplot.F1D) { F1D p1 = (jhplot.F1D) ob; writeF1D(tx, key, p1); } else if (ob instanceof jhplot.F2D) { F2D p1 = (jhplot.F2D) ob; writeF2D(tx, key, p1); } else if (ob instanceof jhplot.FPR) { FPR p1 = (jhplot.FPR) ob; writeFPR(tx, key, p1); } else if (ob instanceof jhplot.FND) { FND p1 = (jhplot.FND) ob; writeFND(tx, key, p1); } else if (ob instanceof jhplot.P2D) { P2D p1 = (jhplot.P2D) ob; writeP2D(tx, key, p1); } else if (ob instanceof double[]) { double[] p1 = (double[]) ob; writeArrayD(tx, key, p1); } else if (ob instanceof int[]) { int[] p1 = (int[]) ob; writeArrayI(tx, key, p1); } else if (ob instanceof double[][]) { double[][] p1 = (double[][]) ob; writeArrayDD(tx, key, p1); } else if (ob instanceof int[][]) { int[][] p1 = (int[][]) ob; writeArrayII(tx, key, p1); } } // end loop over objects tx.println(""); tx.close(); f1.close(); } catch (IOException e) { jhplot.utils.Util.ErrorMessage("Error in the output file"); e.printStackTrace(); } } /** * Get version of the file. * * @return */ public int getVersion() { return version; } /** * Read external XML file with all objects. If the file name starts with * "hhtp" or "ftp", the file will be read from URL. * * @param file * input file name or URL */ private void read(String file) { if (file.startsWith("http") || file.startsWith("ftp")) { URL url = null; try { url = new URL(file); reader = new BufferedReader(new InputStreamReader( url.openStream())); } catch (IOException e) { jhplot.utils.Util.ErrorMessage(e.toString()); } } else { // this is normal file on a file system try { reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { jhplot.utils.Util.ErrorMessage(e.toString()); } } try { XMLRead xr = new XMLRead(); if (!xr.parse(reader, "jhepwork")) { jhplot.utils.Util .ErrorMessage("This is not valid jhepwork XML file"); return; } createdBy = xr.getString("created-by", "NOT SET"); time = xr.getString("created-on", "NOT SET"); description = xr.getString("description", "NOT SET"); version = xr.getInt("version", -1); // now read H1D histograms double min, max; double underflow, overflow; String id = "0"; int bins = 0; int k1 = 0; while (xr.open("h1d")) { id = xr.getString("id", Integer.toString(k1)); String stitle = xr.getString("title", ""); String labelx = xr.getString("labelx", ""); String labely = xr.getString("labely", ""); int isWeighted = xr.getInt("weighted", 0); // open axis xr.open("x-axis"); min = xr.getDouble("min", 0.0); max = xr.getDouble("max", 0.0); bins = xr.getInt("bins", 0); // System.out.println(bins); underflow = xr.getDouble("underflow", 0.0); overflow = xr.getDouble("overflow", 0.0); // check if bins are variable String sline = xr.getString("variable-bins"," "); H1D h1 =null; double[] edges = getDoubles(sline); if (edges != null && edges.length>0) { h1 = new H1D(stitle,edges ); } else { h1 = new H1D(stitle, bins, min, max); } if (labelx.length() > 0) h1.setLabelX(labelx); if (labely.length() > 0) h1.setLabelY(labely); xr.close(); // close x-axis xr.hide("x-axis"); // open statistics xr.open("stat"); h1.setNEntries((int) xr.getDouble("all-entries", 0.0)); h1.setValidEntries((int) xr.getDouble("in-range-entries", 0.0)); double out_of_range = xr.getDouble("out-of-range-entries", 0.0); // h1.setMean(xr.getDouble("mean", 0.0)); // h1.setRms(xr.getDouble("rms", 0.0)); h1.setMeanAndRms(xr.getDouble("mean", 0.0), xr.getDouble("rms", 0.0)); // System.out.println(mean); xr.close(); // statistics xr.hide("stat"); xr.open("data"); Vector data = xr.getData(); int nn = data.size(); if (nn != bins) { System.out .println("Not valid H1D histogram definition in XML file"); System.out.println("data block has the size=" + nn); System.out.println("but the number of expected bins=" + bins); // return; } // System.out.println("Check="); // System.out.println(nn); // System.out.println(bins); double[] hight = new double[nn + 2]; double[] errors = new double[nn + 2]; hight[0] = underflow; // underflow hight[nn + 1] = overflow; // overflow errors[0] = underflow; // underflow errors[nn + 1] = overflow; // overflow if (isWeighted == 0) { for (int i = 0; i < nn; i++) { String line = (String) data.elementAt(i); double d[] = getDoubles(line); // System.out.println("h1d data="+line);; hight[i + 1] = d[0]; errors[i + 1] = d[1]; } h1.setContents(hight, errors); } if (isWeighted == 1) { double[] means = new double[nn + 2]; double[] rms = new double[nn + 2]; int[] entries = new int[nn + 2]; entries[0] = 0; // underflow entries[nn + 1] = 0; // overflow for (int i = 0; i < nn; i++) { String line = (String) data.elementAt(i); double d[] = getDoubles(line); // System.out.println("h1d data="+line);; hight[i + 1] = d[0]; errors[i + 1] = d[1]; entries[i + 1] = (int)d[2]; } h1.setContents(hight, errors,entries,means,rms); } xr.close(); // close data xr.hide("data"); map.put(id, (Object) h1); // System.out.println(" new histo="+Integer.toString(k1)); xr.close(); // close histogram1d xr.hide("h1d"); } double minx, miny, maxx, maxy; int binsx, binsy; while (xr.open("h2d")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); String labelx = xr.getString("labelx", ""); String labely = xr.getString("labely", ""); String labelz = xr.getString("labely", ""); // open x axis xr.open("x-axis"); binsx = xr.getInt("bins", 0); minx = xr.getDouble("min", 0.0); maxx = xr.getDouble("max", 0.0); String sline = xr.getString("variable-bins"," "); double[] edgesX = null; if ( sline != null) { edgesX = getDoubles(sline); } xr.hide("x-axis"); xr.close(); // close y-axis xr.open("y-axis"); binsy = xr.getInt("bins", 0); miny = xr.getDouble("min", 0.0); maxy = xr.getDouble("max", 0.0); sline = xr.getString("variable-bins"," "); double[] edgesY = null; if ( sline != null) { edgesY = getDoubles(sline); } xr.hide("y-axis"); xr.close(); // close y-axis // System.out.println( "x-bins="+Integer.toString(binsx)); // System.out.println( "y-bins="+Integer.toString(binsy)); H2D h2 = null; if ( edgesX != null && edgesX.length>0 && edgesY != null && edgesY.length>0 ) h2=new H2D(stitle, edgesX, edgesY ); else h2 = new H2D(stitle, binsx, minx, maxx, binsy, miny, maxy); if (labelx.length() > 0) h2.setLabelX(labelx); if (labely.length() > 0) h2.setLabelY(labely); if (labelz.length() > 0) h2.setLabelY(labelz); // open statistics xr.open("stat"); h2.setNEntries((int) xr.getDouble("all-entries", 0.0)); h2.setValidEntries((int) xr.getDouble("in-range-entries", 0.0)); h2.setMeanX(xr.getDouble("x-mean", 0.0)); h2.setRmsX(xr.getDouble("x-rms", 0.0)); h2.setMeanY(xr.getDouble("y-mean", 0.0)); h2.setRmsY(xr.getDouble("y-rms", 0.0)); // System.out.println(mean); xr.close(); // statistics xr.hide("stat"); // Y // out of range // 6 | 7 | 8 // ----------- // 3 | 4 | 5 // ----------- // 0 | 1 | 2 // X double[][] hight = new double[binsx + 2][binsy + 2]; double[][] errors = new double[binsx + 2][binsy + 2]; xr.open("out-of-range-data"); Vector outR = xr.getData(); double[] outr = new double[outR.size()]; for (int i = 0; i < outR.size(); i++) { String line = (String) outR.elementAt(i); outr[i] = 0; try { outr[i] = Double.parseDouble(line); } catch (NumberFormatException e) { } // System.out.println(outr[i]); } xr.close(); // close variableWidthBins xr.hide("out-of-range-data"); hight[0][0] = outr[0]; hight[binsx + 1][0] = outr[2]; hight[0][binsy + 1] = outr[6]; hight[binsx + 1][binsy + 1] = outr[8]; // open data xr.open("data"); Vector data = xr.getData(); for (int i = 0; i < data.size(); i++) { String line = (String) data.elementAt(i); double d[] = getDoubles(line); // System.out.println("h2d data="+line); // System.out.println( Integer.toString(i) +" " + // Integer.toString( (int)d[0] )+ " "+ // Integer.toString( (int)d[1]) ); // System.out.println( d[1] ); // System.out.println( d.length ); int j1 = (int) d[0] + 1; int j2 = (int) d[1] + 1; hight[j1][j2] = d[2]; errors[j1][j2] = d[3]; } xr.hide("data"); xr.close(); // close data h2.setContents(hight, errors); map.put(id, h2); // System.out.println(k2); xr.close(); xr.hide("h2d"); k1++; } // ------------------- P1D container // ---------------------------------- while (xr.open("p1d")) { String stitle = xr.getString("title", "NONE"); id = xr.getString("id", Integer.toString(k1)); String labelx = xr.getString("labelx", ""); String labely = xr.getString("labely", ""); int dim = xr.getInt("dimen", 10); // sufficient to build it P1D p1 = new P1D(stitle, dim); if (labelx.length() > 0) p1.setLabelX(labelx); if (labely.length() > 0) p1.setLabelY(labely); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); // System.out.println("p1d data="+line); double snum[] = getDoubles(line); int ncount = snum.length; if (ncount == 2) p1.add(snum[0], snum[1]); if (ncount == 3) p1.add(snum[0], snum[1], snum[2]); if (ncount == 4) p1.add(snum[0], snum[1], snum[2], snum[3]); if (ncount == 6) p1.add(snum[0], snum[1], snum[2], snum[3], snum[4], snum[5]); if (ncount == 10) p1.add(snum[0], snum[1], snum[2], snum[3], snum[4], snum[5], snum[6], snum[7], snum[8], snum[9]); } xr.close(); // close data xr.hide("data"); // now set it map.put(id, p1); xr.close(); // close p1d xr.hide("p1d"); k1++; } // P0D while (xr.open("p0d")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); int dim = xr.getInt("size", 10); // sufficient to build it P0D p0d = new P0D(stitle); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); // System.out.println("p1d data="+line); double snum[] = getDoubles(line); p0d.setArray(snum); } xr.close(); // close data xr.hide("data"); // now set it map.put(id, p0d); xr.close(); // close p1d xr.hide("p0d"); k1++; } // P0I while (xr.open("p0i")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); int dim = xr.getInt("size", 10); // sufficient to build it P0I p0i = new P0I(stitle); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); // System.out.println("p1d data="+line); int snum[] = getIntegers(line); p0i.setArray(snum); } xr.close(); // close data xr.hide("data"); // now set it map.put(id, p0i); xr.close(); // close p1d xr.hide("p0i"); k1++; } // PND while (xr.open("pnd")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); int size = xr.getInt("size", 10); int dim = xr.getInt("dimen", 10); // sufficient to build it PND pnd = new PND(stitle); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); double snum[] = getDoubles(line); pnd.add(snum); } xr.close(); // close data xr.hide("data"); // now set it map.put(id, pnd); xr.close(); // close p1d xr.hide("pnd"); k1++; } while (xr.open("p2d")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); int size = xr.getInt("size", 10); int dim = xr.getInt("dimen", 10); String labelx = xr.getString("labelx", ""); String labely = xr.getString("labely", ""); String labelz = xr.getString("labelz", ""); P2D pnd = new P2D(stitle); if (labelx.length() > 0) pnd.setLabelX(labelx); if (labely.length() > 0) pnd.setLabelY(labely); if (labelz.length() > 0) pnd.setLabelZ(labelz); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); double snum[] = getDoubles(line); pnd.add(snum[0], snum[1], snum[2]); } xr.close(); // close data xr.hide("data"); // now set it map.put(id, pnd); xr.close(); // close p1d xr.hide("p2d"); k1++; } // PID while (xr.open("pni")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); int size = xr.getInt("size", 10); int dim = xr.getInt("dimen", 10); // sufficient to build it PNI pni = new PNI(stitle); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); // System.out.println("p1d data="+line); int snum[] = getIntegers(line); pni.add(snum); } xr.close(); // close data xr.hide("data"); // now set it map.put(id, pni); xr.close(); // close p1d xr.hide("pni"); k1++; } while (xr.open("array1D")) { id = xr.getString("id", Integer.toString(k1)); xr.open("data"); Vector pdata = xr.getData(); double[] d = new double[pdata.size()]; for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); d[i] = Double.parseDouble(line); } xr.close(); // close data xr.hide("data"); // now set it map.put(id, d); xr.close(); // close p1d xr.hide("array1D"); k1++; } while (xr.open("array1I")) { id = xr.getString("id", Integer.toString(k1)); xr.open("data"); Vector pdata = xr.getData(); int[] d = new int[pdata.size()]; for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); d[i] = Integer.parseInt(line); } xr.close(); // close data xr.hide("data"); map.put(id, d); xr.close(); // close p1d xr.hide("array1I"); k1++; } while (xr.open("array2D")) { id = xr.getString("id", Integer.toString(k1)); PND pnd = new PND("array"); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); double snum[] = getDoubles(line); pnd.add(snum); } xr.close(); // close data xr.hide("data"); map.put(id, pnd.getArray()); xr.close(); // close p1d xr.hide("array2D"); k1++; } while (xr.open("array2I")) { id = xr.getString("id", Integer.toString(k1)); PNI pnd = new PNI("array"); xr.open("data"); Vector pdata = xr.getData(); for (int i = 0; i < pdata.size(); i++) { String line = (String) pdata.elementAt(i); int snum[] = getIntegers(line); pnd.add(snum); } xr.close(); // close data xr.hide("data"); map.put(id, pnd.getArray()); xr.close(); // close p1d xr.hide("array2I"); k1++; } while (xr.open("f1d")) { id = xr.getString("id", Integer.toString(k1)); String stitle = xr.getString("title", "none"); String name = xr.getString("name", "none"); double Xmin = xr.getDouble("min", 0); double Xmax = xr.getDouble("max", 1); map.put(id, new F1D(stitle, name, Xmin, Xmax)); xr.close(); // close f1d xr.hide("f1d"); k1++; } while (xr.open("f2d")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); String name = xr.getString("name", "none"); double minX = xr.getDouble("Xmin", 0); double maxX = xr.getDouble("Xmax", 1); double minY = xr.getDouble("Ymin", 0); double maxY = xr.getDouble("Ymax", 1); map.put(id, new F2D(stitle, name, minX, maxX, minY, maxY)); xr.close(); // close f1d xr.hide("f2d"); k1++; } while (xr.open("fnd")) { String stitle = xr.getString("title", "NOT SET"); id = xr.getString("id", Integer.toString(k1)); String options = xr.getString("vars", "x"); map.put(id, new FND(stitle, options)); xr.close(); // close f1d xr.hide("fnd"); k1++; } while (xr.open("fpr")) { id = xr.getString("id", Integer.toString(k1)); String stitle = xr.getString("title", "NOT SET"); String name = xr.getString("name", "x"); int n1 = xr.getInt("divX", 40); int n2 = xr.getInt("divY", 40); map.put(id, new FPR(stitle, name, n1, n2)); xr.close(); // close f1d xr.hide("fpr"); k1++; } xr.close(); // close jhepwork } finally { try { if (reader != null) { // flush and close both "input" and its underlying // FileReader reader.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } // end of HBook class /** * Get an object using the key * * @param id * key for object access * * * @return H1D histogram */ public Object get(String id) { if (map.containsKey(id) == false) { jhplot.utils.Util.ErrorMessage("The key = " + id + " was not found!"); return null; } return (Object) map.get(id); } /** * Return array of all know keys * * @return keys */ public String[] getKeys() { Iterator> entries = map.entrySet().iterator(); List a = new ArrayList(); while (entries.hasNext()) { Map.Entry entry = entries.next(); String key = entry.getKey(); a.add(key); } String[] tmp = a.toArray(new String[a.size()]); return tmp; } /** * write H1D histogram to HBook * * @param id * key to be assigned * @param h1d * H1D histogram to be added * */ public void write(String id, H1D h1d) { map.put(id, h1d); } /** * write H2D histogram to HBook * * @param id * key to be assigned * @param h2d * H2D histogram to be added * */ public void write(String id, H2D h2d) { map.put(id, h2d); } /** * add P1D histogram to HBook * * @param id * key to be assigned * @param p1d * P1D histogram to be added * */ public void write(String id, P1D p1d) { map.put(id, p1d); } /** * add PND data object. * * @param id * key to be assigned * @param pnd * PND histogram to be added * */ public void write(String id, PND pnd) { map.put(id, pnd); } /** * add PID data object. * * @param id * key to be assigned * @param pnd * PID histogram to be added * */ public void write(String id, PNI pnd) { map.put(id, pnd); } /** * add P0D data object. * * @param id * key to be assigned * @param pnd * P0D histogram to be added * */ public void write(String id, P0D pnd) { map.put(id, pnd); } /** * add P0I data object. * * @param id * key to be assigned * @param pnd * P0D histogram to be added * */ public void write(String id, P0I pnd) { map.put(id, pnd); } /** * add P2D data object. * * @param id * key to be assigned * @param pnd * P2D histogram to be added * */ public void write(String id, P2D pnd) { map.put(id, pnd); } /** * Add any object. The key will be assigned automatically (integer number incremented by 1). * object. All objects should belong to jhplot package. * @param obj object for writing. */ public void write( Object ob) { count++; write(count, ob); } /** * Write jhplot data container. * Integer key will be transformed to string. Only predefined * objects are supported. * * @param id * integer key * @param obj * object */ public void write(int id, Object ob) { String key = Integer.toString(id); if (ob instanceof jhplot.H1D) { write(key, (jhplot.H1D) ob); } else if (ob instanceof jhplot.H2D) { write(key, (jhplot.H2D) ob); } else if (ob instanceof jhplot.P1D) { write(key, (jhplot.P1D) ob); } else if (ob instanceof jhplot.P0D) { write(key, (jhplot.P0D) ob); } else if (ob instanceof jhplot.P0I) { write(key, (jhplot.P0I) ob); } else if (ob instanceof jhplot.PND) { write(key, (jhplot.PND) ob); } else if (ob instanceof jhplot.P2D) { write(key, (jhplot.P2D) ob); } else if (ob instanceof jhplot.PNI) { write(key, (jhplot.PNI) ob); } else if (ob instanceof jhplot.F1D) { write(key, (jhplot.F1D) ob); } else if (ob instanceof jhplot.FPR) { write(key, (jhplot.FPR) ob); } else if (ob instanceof jhplot.F2D) { write(key, (jhplot.F2D) ob); } else if (ob instanceof jhplot.FND) { write(key, (jhplot.FND) ob); } else if (ob instanceof double[]) { write(key, (double[]) ob); } else if (ob instanceof int[]) { write(key, (int[]) ob); } else if (ob instanceof double[][]) { write(key, (double[][]) ob); } else if (ob instanceof int[][]) { write(key, (int[][]) ob); } else { jhplot.utils.Util.ErrorMessage("Not supported object!"); } } /** * write F1D function * * @param id * key to be assigned * @param f1 * function * * */ public void write(String id, F1D f1) { map.put(id, f1); } /** * write FPR parametric function * * @param id * key to be assigned * @param f1 * function * * */ public void write(String id, FPR f1) { map.put(id, f1); } /** * write F2D function * * @param id * key to be assigned * @param f2 * function * * */ public void write(String id, F2D f2) { map.put(id, f2); } /** * add 1D array * * @param id * key to be assigned * @param a * array * * */ public void write(String id, double[] a) { map.put(id, a); } /** * add 1D array * * @param id * key to be assigned * @param a * array * * */ public void write(String id, int[] a) { map.put(id, a); } /** * add 2D array * * @param id * key to be assigned * @param a * array * * */ public void write(String id, double[][] a) { map.put(id, a); } /** * add 2D array * * @param id * key to be assigned * @param a * array * * */ public void write(String id, int[][] a) { map.put(id, a); } /** * add 2D array * * @param id * key to be assigned * @param a * array * * */ public void write(String id, FND a) { map.put(id, a); } /** * Clear all objects and exit. * If yu write a file, all objects will be writted to the disk. */ public void close() { if (option.equalsIgnoreCase("w")) write(file); map.clear(); map = null; reader = null; } /** * Get array of double numbers from the line * * @param a * input string * @param tok * input token */ private double[] getDoubles(String a) { a = a.trim(); StringTokenizer st = new StringTokenizer(a, sep); int ncount = st.countTokens(); // number of words double[] d = new double[ncount]; int m = 0; while (st.hasMoreTokens()) { // make sure there is stuff String s = st.nextToken(); try { d[m] = Double.parseDouble(s); } catch (NumberFormatException e) { } m++; } return d; } /** * Get array of double numbers from the line * * @param a * input string * @param tok * input token */ private int[] getIntegers(String a) { a = a.trim(); StringTokenizer st = new StringTokenizer(a, sep); int ncount = st.countTokens(); // number of words int[] d = new int[ncount]; int m = 0; while (st.hasMoreTokens()) { // make sure there is stuff String s = st.nextToken(); try { d[m] = Integer.parseInt(s); } catch (NumberFormatException e) { } m++; } return d; } /** * Show online documentation. */ public void doc() { String a = this.getClass().getName(); a = a.replace(".", "/") + ".html"; new HelpBrowser(HelpBrowser.JHPLOT_HTTP + a); } /** * Write H1D * * @param tx * @param me * @param h1 */ private void writeH1D(PrintStream tx, String key, H1D h1) { IAxis axis = h1.getAxis(); tx.println(""); setString("id", key, tx); setString("title", h1.getTitle(), tx); if (h1.getLabelX().length()>0) setString("labelx", h1.getLabelX(), tx); if (h1.getLabelY().length()>0) setString("labely", h1.getLabelY(), tx); if (h1.getLabelZ().length()>0) setString("labelz", h1.getLabelZ(), tx); double w1=h1.sumAllBinHeights(); double w2=h1.allEntries(); boolean isWeighted=false; if (w1 != w2) isWeighted=true; setBoolean("weighted", isWeighted, tx); tx.println(""); String tmp=sep; if (!h1.isFixedBinning()) { for (int j = 0; j < h1.getBins(); j++) { tmp=tmp+sep+DoubleS(axis.binLowerEdge(j)); } tmp=tmp+sep+axis.binUpperEdge(h1.getBins()-1); setString("variable-bins", tmp, tx); } setInt("bins", h1.getBins(), tx); setDouble("min", h1.getMin(), tx); setDouble("max", h1.getMax(), tx); setInt("underflow", h1.getUnderflow(), tx); setInt("overflow", h1.getOverflow(), tx); tx.println(""); // statistics tx.println(""); setInt("all-entries", h1.allEntries(), tx); setInt("in-range-entries", h1.entries(), tx); setInt("out-of-range-entries", h1.extraEntries(), tx); setDouble("mean", h1.mean(), tx); setDouble("rms", h1.rms(), tx); tx.println(""); // bin content tx.println(""); if (isWeighted) { for (int i = 0; i < h1.getBins(); i++) { tx.println(sep+DoubleS(h1.binHeight(i)) + sep + DoubleS(h1.binError(i)) + sep + IntS(h1.binEntries(i))); } } else { for (int i = 0; i < h1.getBins(); i++) { tx.println(sep+DoubleS(h1.binHeight(i)) + sep + DoubleS(h1.binError(i))); } } tx.println(""); tx.println(""); tx.println(""); } /** * Write H2D * * @param tx * @param me * @param h1 */ private void writeH2D(PrintStream tx, String key, H2D h2) { tx.println(""); setString("id", key, tx); setString("title", h2.getTitle(), tx); if (h2.getLabelX().length() > 0) setString("labelx", h2.getLabelX(), tx); if (h2.getLabelY().length() > 0) setString("labely", h2.getLabelY(), tx); if (h2.getLabelZ().length() > 0) setString("labelz", h2.getLabelZ(), tx); // X-axis tx.println(""); setInt("bins", h2.getBinsX(), tx); setDouble("min", h2.getMinX(), tx); setDouble("max", h2.getMaxX(), tx); setDouble("underflow", h2.getUnderflowHeightX(), tx); setDouble("overflow", h2.getOverflowHeightX(), tx); if ( h2.getAxisX() instanceof hep.aida.ref.histogram.VariableAxis ) { String tmp=sep; for (int j = 0; j < h2.getBinsX(); j++) { tmp=tmp+sep+DoubleS(h2.getLowerEdgeX(j)); } tmp=tmp+sep+h2.getUpperEdgeY(h2.getBinsX()-1); setString("variable-bins", tmp, tx); } tx.println(""); // Y-axis tx.println(""); setInt("bins", h2.getBinsY(), tx); setDouble("min", h2.getMinY(), tx); setDouble("max", h2.getMaxY(), tx); setDouble("underflow", h2.getUnderflowHeightY(), tx); setDouble("overflow", h2.getOverflowHeightY(), tx); if ( h2.getAxisY() instanceof hep.aida.ref.histogram.VariableAxis ) { tx.println(""); String tmp=sep; for (int j = 0; j < h2.getBinsY(); j++) { tmp=tmp+sep+DoubleS(h2.getLowerEdgeY(j)); } tmp=tmp+sep+h2.getUpperEdgeY(h2.getBinsY()-1); setString("variable-bins", tmp, tx); } tx.println(""); // out of range // 6 | 7 | 8 // ----------- // 3 | 4 | 5 // ----------- // 0 | 1 | 2 double[] outr = new double[9]; outr[0] = h2.getUnderflowEntriesX() + h2.getUnderflowEntriesY(); outr[3] = h2.getUnderflowEntriesX(); outr[6] = h2.getUnderflowEntriesX() + h2.getUnderflowEntriesY(); outr[0] = h2.getUnderflowEntriesY(); outr[1] = h2.getUnderflowEntriesY(); outr[2] = h2.getUnderflowEntriesY() + h2.getOverflowEntriesY(); outr[6] = h2.getOverflowEntriesY(); outr[7] = h2.getOverflowEntriesY(); outr[8] = h2.getOverflowEntriesY() + h2.getOverflowEntriesX(); outr[4] = h2.getOverflowEntriesX(); outr[8] = h2.getOverflowEntriesX() + h2.getOverflowEntriesY(); outr[5] = h2.getOverflowEntriesX(); // out of range tx.println(""); for (int i = 0; i < 9; i++) tx.println(sep + DoubleS(outr[i])); tx.println(""); // statistics tx.println(""); setInt("all-entries", h2.allEntries(), tx); setInt("in-range-entries", h2.entries(), tx); setInt("out-of-range-entries", h2.extraEntries(), tx); setDouble("all-hights", h2.sumAllBinHeights(), tx); setDouble("in-range-hights", (h2.sumExtraBinHeights() - h2.sumExtraBinHeights()), tx); setDouble("out-of-range-hights", h2.sumExtraBinHeights(), tx); setDouble("x-mean", h2.getMeanX(), tx); setDouble("x-rms", h2.getRmsX(), tx); setDouble("y-mean", h2.getMeanY(), tx); setDouble("y-rms", h2.getRmsY(), tx); tx.println(""); // bin content tx.println(""); tx.println(" bin,height,error,entries"); tx.println(""); tx.println(""); // System.out.println("h2.getBinsX()="+Integer.toString(h2.getBinsX())); // System.out.println("h2.getBinsY()="+Integer.toString(h2.getBinsY())); for (int j1 = 0; j1 < h2.getBinsX(); j1++) { for (int j2 = 0; j2 < h2.getBinsY(); j2++) { tx.println(j1 + sep + j2 + sep + DoubleS(h2.binHeight(j1, j2)) + sep + DoubleS(h2.binError(j1, j2)) + sep + IntS(h2.binEntries(j1, j2))); } } tx.println(""); tx.println(""); tx.println(""); } /** * write P1D * * @param tx * @param me * @param p1 */ private void writeP1D(PrintStream tx, String key, P1D p1) { tx.println(""); setString("id", key, tx); setString("title", p1.getTitle(), tx); if (p1.getLabelX().length() > 0) setString("labelx", p1.getLabelX(), tx); if (p1.getLabelY().length() > 0) setString("labely", p1.getLabelY(), tx); setInt("size", p1.size(), tx); setInt("dimen", p1.dimension(), tx); tx.println(""); for (int i = 0; i < p1.size(); i++) { if (p1.dimension() == 2) tx.println(DoubleS(p1.getX(i)) + sep + DoubleS(p1.getY(i))); if (p1.dimension() == 3) tx.println(DoubleS(p1.getX(i)) + sep + DoubleS(p1.getY(i)) + sep + DoubleS(p1.getYupper(i))); if (p1.dimension() == 4) tx.println(p1.getX(i) + sep + p1.getY(i) + sep + p1.getYupper(i) + sep + p1.getYlower(i)); if (p1.dimension() == 6) tx.println(DoubleS(p1.getX(i)) + sep + DoubleS(p1.getY(i)) + sep + DoubleS(p1.getXleft(i)) + sep + DoubleS(p1.getXright(i)) + sep + DoubleS(p1.getYupper(i)) + sep + DoubleS(p1.getYlower(i))); if (p1.dimension() == 10) tx.println(DoubleS(p1.getX(i)) + sep + DoubleS(p1.getY(i)) + sep + DoubleS(p1.getXleft(i)) + sep + DoubleS(p1.getXright(i)) + sep + DoubleS(p1.getYupper(i)) + sep + DoubleS(p1.getYlower(i)) + sep + DoubleS(p1.getXleftSys(i)) + sep + DoubleS(p1.getXrightSys(i)) + sep + DoubleS(p1.getYupperSys(i)) + sep + DoubleS(p1.getYlowerSys(i))); } tx.println(""); tx.println(""); tx.println(""); } /** * P0D * * @param tx * @param me * @param p0d */ private void writeP0D(PrintStream tx, String key, P0D p0d) { tx.println(""); setString("id", key, tx); setString("title", p0d.getTitle(), tx); setInt("size", p0d.size(), tx); tx.println(""); if (p0d.size() > 0) { if (p0d.size() > 1) { for (int i = 0; i < p0d.size() - 1; i++) { tx.print(DoubleS(p0d.get(i)) + sep); } } tx.println(DoubleS(p0d.get(p0d.size() - 1))); } tx.println(""); tx.println(""); tx.println(""); } /** * P0I * * @param tx * @param me * @param p0i */ private void writeP0I(PrintStream tx, String key, P0I p0i) { tx.println(""); setString("id", key, tx); setString("title", p0i.getTitle(), tx); setInt("size", p0i.size(), tx); tx.println(""); if (p0i.size() > 0) { if (p0i.size() > 1) { for (int i = 0; i < p0i.size() - 1; i++) { tx.print(DoubleS(p0i.get(i)) + sep); } } tx.println(DoubleS(p0i.get(p0i.size() - 1))); } tx.println(""); tx.println(""); tx.println(""); } /** * PND * * @param tx * @param me * @param pnd */ private void writePND(PrintStream tx, String key, PND pnd) { tx.println(""); setString("id", key, tx); setString("title", pnd.getTitle(), tx); setInt("size", pnd.size(), tx); setInt("dimen", pnd.getDimension(), tx); tx.println(""); ArrayList data = pnd.getArrayList(); if (data.size() > 0) { for (int i = 0; i < data.size(); i++) { double[] tt = (double[]) data.get(i); for (int j = 0; j < tt.length; j++) tx.print(DoubleS(tt[j]) + sep); tx.println(""); } } tx.println(""); tx.println(""); tx.println(""); } /** * Write PIN * * @param tx * @param me * @param pni */ private void writePNI(PrintStream tx, String key, PNI pni) { tx.println(""); setString("id", key, tx); setString("title", pni.getTitle(), tx); setInt("size", pni.size(), tx); setInt("dimen", pni.getDimension(), tx); tx.println(""); ArrayList data = pni.getArrayList(); if (data.size() > 0) { for (int i = 0; i < data.size(); i++) { int[] tt = (int[]) data.get(i); for (int j = 0; j < tt.length; j++) tx.print(DoubleS(tt[j]) + sep); tx.println(""); } } tx.println(""); tx.println(""); tx.println(""); } /** * Function F1D * * @param tx * @param me * @param p */ private void writeF1D(PrintStream tx, String key, F1D p) { tx.println(""); setString("id", key, tx); setString("title", p.getTitle(), tx); if (p.getLabelX().length() > 0) setString("labelx", p.getLabelX(), tx); if (p.getLabelY().length() > 0) setString("labely", p.getLabelY(), tx); setString("name", p.getName(), tx); setDouble("min", p.getMin(), tx); setDouble("max", p.getMax(), tx); tx.println(""); tx.println(""); } private void writeFPR(PrintStream tx, String key, FPR p) { tx.println(""); setString("id", key, tx); setString("title", p.getTitle(), tx); if (p.getLabelX().length() > 0) setString("labelx", p.getLabelX(), tx); if (p.getLabelY().length() > 0) setString("labely", p.getLabelY(), tx); if (p.getLabelZ().length() > 0) setString("labelz", p.getLabelZ(), tx); setString("name", p.getName(), tx); setInt("divX", p.getDivU(), tx); setInt("divY", p.getDivV(), tx); tx.println(""); tx.println(""); } /** * Function F2D * * @param tx * @param me * @param p */ private void writeF2D(PrintStream tx, String key, F2D p) { tx.println(""); setString("id", key, tx); setString("title", p.getTitle(), tx); if (p.getLabelX().length() > 0) setString("labelx", p.getLabelX(), tx); if (p.getLabelY().length() > 0) setString("labely", p.getLabelY(), tx); if (p.getLabelZ().length() > 0) setString("labelz", p.getLabelZ(), tx); setString("name", p.getName(), tx); setString("Xmin", DoubleS(p.getMinX()), tx); setString("Xmax", DoubleS(p.getMaxX()), tx); setString("Ymin", DoubleS(p.getMinY()), tx); setString("Ymax", DoubleS(p.getMaxY()), tx); tx.println(""); tx.println(""); } /** * Function F1D * * @param tx * @param me * @param p */ private void writeFND(PrintStream tx, String key, FND p) { tx.println(""); setString("id", key, tx); setString("title", p.getTitle(), tx); setString("name", p.getName(), tx); setString("vars", p.getVarString(), tx); tx.println(""); tx.println(""); } /** * Write PIN * * @param tx * @param me * @param pni */ private void writeP2D(PrintStream tx, String key, P2D pni) { tx.println(""); setString("id", key, tx); setString("title", pni.getTitle(), tx); if (pni.getLabelX().length() > 0) setString("labelx", pni.getLabelX(), tx); if (pni.getLabelY().length() > 0) setString("labely", pni.getLabelY(), tx); if (pni.getLabelZ().length() > 0) setString("labelz", pni.getLabelZ(), tx); setInt("size", pni.size(), tx); tx.println(""); for (int i = 0; i < pni.size(); i++) { tx.println(sep+DoubleS(pni.getX(i)) + sep + DoubleS(pni.getY(i)) + sep + DoubleS(pni.getZ(i))); } tx.println(""); tx.println(""); tx.println(""); } private void writeArrayD(PrintStream tx, String key, double[] pni) { tx.println(""); setString("id", key, tx); tx.println(""); for (int i = 0; i < pni.length; i++) { tx.println(sep+DoubleS(pni[i])); } tx.println(""); tx.println(""); tx.println(""); } /** * Array * * @param tx * @param me * @param pni */ private void writeArrayI(PrintStream tx, String key, int[] pni) { tx.println(""); setString("id", key, tx); tx.println(""); for (int i = 0; i < pni.length; i++) { tx.println(sep+DoubleS(pni[i])); } tx.println(""); tx.println(""); tx.println(""); } /** * Double array * * @param tx * @param me * @param array */ private void writeArrayDD(PrintStream tx, String key, double[][] array) { tx.println(""); setString("id", key, tx); tx.println(""); for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { tx.print(sep+DoubleS(array[i][j])); } tx.println(""); } tx.println(""); tx.println(""); tx.println(""); } /** * Get all objects as a map * * @return */ public Map getAll() { return map; } /** * Return time of file creation. */ public String getTimeCreation() { return time; } /** * Integer array * * @param tx * @param me * @param array */ private void writeArrayII(PrintStream tx, String key, int[][] array) { tx.println(""); setString("id", key, tx); tx.println(""); for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { tx.print(sep+DoubleS(array[i][j])); } tx.println(""); } tx.println(""); tx.println(""); tx.println(""); } /** * Adds data between within the scope of a tag. You can obtain this also by * using the methods open(), addData() and close(), but this method writes * the tags and data on a single line. You therefore prefer this method for * short (one-word) data. * * @see #open(String) * @see #addData(String) * @see #close() * @param key * key or name of the XML item */ private void setString(String key, String data, PrintStream tx) { // tx.println("<"+key+"=\""+ data + "\"/>"); tx.println("<" + key + ">" + data.trim() + ""); } private void setInt(String key, int data, PrintStream tx) { // tx.println("<"+key+"=\""+ Integer.toString(data) + "\"/>"); tx.println("<" + key + ">" + IntS(data) + ""); } private void setDouble(String key, Double data, PrintStream tx) { // tx.println("<"+key+"=\""+ Double.toString(data) + "\"/>"); tx.println("<" + key + ">" + DoubleS(data) + ""); } private void setBoolean(String key, boolean data, PrintStream tx) { // tx.println("<"+key+"=\""+ Double.toString(data) + "\"/>"); tx.println("<" + key + ">" + Boolean.toString(data) + ""); } // format only numbers iwth decimal part private String DoubleS(double d) { if (d % 1.0 > 0) return dfb.format(d); else return Integer.toString((int)d); } private String IntS(int d) { return Integer.toString(d); } }