/*
 * HistogramFactory.java
 *
 * Created on February 14, 2001, 1:09 PM
 */

package hep.aida.ref.histogram;
import hep.aida.IAnnotation;
import hep.aida.IAxis;
import hep.aida.IBaseHistogram;
import hep.aida.ICloud1D;
import hep.aida.ICloud2D;
import hep.aida.ICloud3D;
import hep.aida.IHistogram1D;
import hep.aida.IHistogram2D;
import hep.aida.IHistogram3D;
import hep.aida.IHistogramFactory;
import hep.aida.IManagedObject;
import hep.aida.IProfile1D;
import hep.aida.IProfile2D;
import hep.aida.ITree;
import hep.aida.ref.AidaUtils;
import hep.aida.ref.Annotation;
import hep.aida.ref.ManagedObject;
import hep.aida.ref.histogram.binner.AbstractBinner1D;
import hep.aida.ref.tree.Tree;

/**
 * @author The AIDA team @ SLAC.
 * @version $Id: HistogramFactory.java 13370 2007-10-10 00:50:26Z serbo $
 */
public class HistogramFactory implements IHistogramFactory {
    
    private Tree tree;
    private final static int defaultMaxEntries=100000;
    private HistMath histMath;
    
    private String nameInPath( String path ) {
        return AidaUtils.parseName(path);
    }
    
    private String parentPath( String path ) {
        return AidaUtils.parseDirName(path);
    }
    
    /**
     * Create a new HistogramFactory.
     * This constructor is used by AnalysisFactory, the Master Factory.
     * @param tree the ITree where the histogram is added.
     */
    public HistogramFactory(ITree tree) {
        this((Tree)tree);
    }
    
    /**
     * Create a new HistogramFactory.
     * This constructor is used by AnalysisFactory, the Master Factory.
     * @param tree the Tree where the histogram is added.
     */
    public HistogramFactory(Tree tree) {
        this.tree = tree;
        histMath = new HistMath();
    }
    
    /**
     * Destroy an IBaseHistogram ogject.
     *
     */
    public void destroy(IBaseHistogram hist) throws IllegalArgumentException {
	if (tree != null) {
	    String path = tree.findPath((IManagedObject) hist);
	    tree.rm(path);
	}
    }
    
    /**
     * Create a Cloud1D, and unbinned 1-dimensional Histogram.
     * @param path The path of the Cloud1D.
     * @param title The title of the Cloud1D.
     * @param nMax The maximum number of entries after which the Cloud1D will convert to an Histogram1D.
     *             If nMax = -1 then the Cloud1D will not convert automatically to an Histogram1D.
     * @param options The options for creating a Cloud1D.
     *
     */
    public ICloud1D createCloud1D(String path, String title, int nMax, String options) {
        Cloud1D result= new Cloud1D(nameInPath(path),title,nMax,options);
        if (tree != null) tree.addFromFactory(parentPath(path),result);
        return result;
    }
    
    public ICloud1D createCloud1D(String path, String title, int nMax) {
        return createCloud1D(path, title, nMax, "");
    }
    
    public ICloud1D createCloud1D(String path, String title) {
        return createCloud1D(path, title, defaultMaxEntries);
    }
    
    public ICloud1D createCloud1D(String pathAndTitle) {
        return createCloud1D(pathAndTitle, nameInPath(pathAndTitle));
    }
    
    public ICloud1D createCopy( String path, ICloud1D cloud) {
        Cloud1D newCloud = copy(nameInPath(path), cloud);
        if (tree != null) tree.addFromFactory(parentPath(path),newCloud);
        return newCloud;
    }
    
    private Cloud1D copy( String path, ICloud1D cloud) {
        boolean hAida = !(cloud instanceof Cloud1D);
        String options = null;
        if (!hAida) options = ((Cloud1D) cloud).getOptions();
        Cloud1D newCloud = new Cloud1D(nameInPath(path), cloud.title(), cloud.maxEntries(), options );
        copy(newCloud.annotation(), cloud.annotation());
        
        if ( cloud.isConverted() ) 
            newCloud.setHistogram( copy( nameInPath(path), cloud.histogram() ) );
        else 
            for ( int i = 0; i < cloud.entries(); i++ )
                newCloud.fill( cloud.value(i), cloud.weight(i) );
        newCloud.setLowerEdge( cloud.lowerEdge() );
        newCloud.setUpperEdge( cloud.upperEdge() );
        if (cloud instanceof ManagedObject) newCloud.setFillable( ((ManagedObject) cloud).isFillable() );
        return newCloud;
    }
    

    
    /**
     * Create a Cloud2D, and unbinned 2-dimensional Histogram.
     * @param path The path of the Cloud2D.
     * @param title The title of the Cloud2D.
     * @param nMax The maximum number of entries after which the Cloud2D will convert to an Histogram2D.
     *             If nMax = -1 then the Cloud2D will not convert automatically to an Histogram2D.
     * @param options The options for creating a Cloud2D.
     *
     */
    public ICloud2D createCloud2D(String path, String title, int nMax, String options) {
        Cloud2D result= new Cloud2D(nameInPath(path),title,nMax,options);
        if (tree != null) tree.addFromFactory(parentPath(path),result);
        return result;
    }
    
    public ICloud2D createCloud2D(String path, String title, int nMax) {
        return createCloud2D(path, title, nMax, "");
    }
    
    public ICloud2D createCloud2D(String path, String title) {
        return createCloud2D(path, title, defaultMaxEntries);
    }
    
    public ICloud2D createCloud2D(String pathAndTitle) {
        return createCloud2D(pathAndTitle, nameInPath(pathAndTitle));
    }
    
    public ICloud2D createCopy( String path, ICloud2D cloud) {
        Cloud2D newCloud = copy(nameInPath(path), cloud);
        if (tree != null) tree.addFromFactory(parentPath(path),newCloud);
        return newCloud;
    }
    
    private Cloud2D copy( String path, ICloud2D cloud) {
        boolean hAida = !(cloud instanceof Cloud2D);
        String options = null;
        if (!hAida) options = ((Cloud2D) cloud).getOptions();
        Cloud2D newCloud = new Cloud2D(nameInPath(path), cloud.title(), cloud.maxEntries(), options );
        copy(newCloud.annotation(), cloud.annotation());

        if ( cloud.isConverted() ) 
            newCloud.setHistogram( copy( nameInPath(path), cloud.histogram() ) );
        else 
            for ( int i = 0; i < cloud.entries(); i++ )
                newCloud.fill( cloud.valueX(i), cloud.valueY(i), cloud.weight(i) );
        newCloud.setLowerEdgeX( cloud.lowerEdgeX() );
        newCloud.setUpperEdgeX( cloud.upperEdgeX() );
        newCloud.setLowerEdgeY( cloud.lowerEdgeY() );
        newCloud.setUpperEdgeY( cloud.upperEdgeY() );
        if (cloud instanceof ManagedObject) newCloud.setFillable( ((ManagedObject) cloud).isFillable() );
        return newCloud;
    }


    /**
     * Create a Cloud3D, and unbinned 3-dimensional Histogram.
     * @param path The path of the Cloud3D.
     * @param title The title of the Cloud3D.
     * @param nMax The maximum number of entries after which the Cloud3D will convert to an Histogram3D.
     *             If nMax = -1 then the Cloud3D will not convert automatically to an Histogram3D.
     * @param options The options for creating a Cloud3D.
     *
     */
    public ICloud3D createCloud3D(String path, String title, int nMax, String options) {
        Cloud3D result= new Cloud3D(nameInPath(path),title,nMax,options);
        if (tree != null) tree.addFromFactory(parentPath(path),result);
        return result;
    }
    
    public ICloud3D createCloud3D(String path, String title, int nMax) {
        return createCloud3D(path, title, nMax, "");
    }
    
    public ICloud3D createCloud3D(String path, String title) {
        return createCloud3D(path, title, defaultMaxEntries);
    }
    
    public ICloud3D createCloud3D(String pathAndTitle) {
        return createCloud3D(pathAndTitle, nameInPath(pathAndTitle));
    }
        
    public ICloud3D createCopy( String path, ICloud3D cloud) {
        Cloud3D newCloud = copy(nameInPath(path), cloud);
        if (tree != null) tree.addFromFactory(parentPath(path),newCloud);
        return newCloud;
    }
        
    private Cloud3D copy( String path, ICloud3D cloud) {
        boolean hAida = !(cloud instanceof Cloud3D);
        String options = null;
        if (!hAida) options = ((Cloud3D) cloud).getOptions();
        Cloud3D newCloud = new Cloud3D(nameInPath(path), cloud.title(), cloud.maxEntries(), options );
        copy(newCloud.annotation(), cloud.annotation());

        if ( cloud.isConverted() ) 
            newCloud.setHistogram( copy( nameInPath(path), cloud.histogram() ) );
        else 
            for ( int i = 0; i < cloud.entries(); i++ )
                newCloud.fill( cloud.valueX(i), cloud.valueY(i), cloud.valueZ(i), cloud.weight(i) );
        newCloud.setLowerEdgeX( cloud.lowerEdgeX() );
        newCloud.setUpperEdgeX( cloud.upperEdgeX() );
        newCloud.setLowerEdgeY( cloud.lowerEdgeY() );
        newCloud.setUpperEdgeY( cloud.upperEdgeY() );
        newCloud.setLowerEdgeZ( cloud.lowerEdgeZ() );
        newCloud.setUpperEdgeZ( cloud.upperEdgeZ() );
        if (cloud instanceof ManagedObject) newCloud.setFillable( ((ManagedObject) cloud).isFillable() );
        return newCloud;
    }



    /**
     * Create a IHistogram1D.
     *
     */
    private IAxis copy( IAxis axis ) {
        if ( axis.isFixedBinning())
            return new FixedAxis( axis.bins(), axis.lowerEdge(),  axis.upperEdge() );
        else {
            double[] edges = new double[ axis.bins() + 1 ];
            edges[0] = axis.binLowerEdge(0);
            for ( int i = 0; i < axis.bins(); i ++ )
                edges[i+1] = axis.binUpperEdge(i);
            return new VariableAxis( edges );
        }
    }
    
    private void copy(IAnnotation newAn, IAnnotation an) {
        int size = an.size();
        for (int i=0; isliceX(UNDERFLOW_BIN,OVERFLOW_BIN).
     */
    public IHistogram1D projectionX(String path, IHistogram2D h) {
        IHistogram1D result = histMath.sliceX(h, nameInPath(path), IAxis.UNDERFLOW_BIN, IAxis.OVERFLOW_BIN);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Create a projection from a 2D histogram parallel to the Y axis.
     * Equivalent to sliceY(UNDERFLOW_BIN,OVERFLOW_BIN).
     */
    public IHistogram1D projectionY(String path, IHistogram2D h) {
        IHistogram1D result = histMath.sliceY(h, nameInPath(path), IAxis.UNDERFLOW_BIN, IAxis.OVERFLOW_BIN);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Slice parallel to the Y axis from a 2D histogram at bin indexY and one bin wide.
     * Equivalent to sliceX(indexY,indexY).
     */
    public IHistogram1D sliceX(String path, IHistogram2D h, int indexY) {
        IHistogram1D result = histMath.sliceX(h, nameInPath(path),indexY, indexY);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Slice parallel to the X axis from a 2D histogram at bin indexX and one bin wide.
     * Equivalent to sliceY(indexX,indexX).
     */
    public IHistogram1D sliceY(String path, IHistogram2D h, int indexX) {
        IHistogram1D result = histMath.sliceY(h, nameInPath(path),indexX, indexX);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }

    /**
     * Create a slice parallel to the X axis from a 2D histogram,
     * between "indexY1" and "indexY2" (inclusive).
     * The returned IHistogram1D represents an instantaneous snapshot of the
     * histogram at the time the slice was created.
     */
    public IHistogram1D sliceX(String path, IHistogram2D h, int indexY1, int indexY2) {
        //Check the order of the indexes
        IHistogram1D result = histMath.sliceX(h, nameInPath(path),indexY1, indexY2);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Create a slice parallel to the Y axis from a 2D histogram,
     * between "indexX1" and "indexX2" (inclusive).
     * The returned IHistogram1D represents an instantaneous snapshot of the
     * histogram at the time the slice was created.
     */
    public IHistogram1D sliceY(String path, IHistogram2D h, int indexX1, int indexX2) {
        IHistogram1D result = histMath.sliceY(h, nameInPath(path),indexX1, indexX2);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Create a projection parallel to the X axis from a 3D histogram.
     * Equivalent to sliceXY(UNDERFLOW_BIN,OVERFLOW_BIN).
     */
    public IHistogram2D projectionXY(String path, IHistogram3D h) {
        IHistogram2D result = histMath.sliceXY(h, nameInPath(path),IAxis.UNDERFLOW_BIN, IAxis.OVERFLOW_BIN);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Create a projection parallel to the Z axis from a 3D histogram.
     * Equivalent to sliceXZ(UNDERFLOW_BIN,OVERFLOW_BIN).
     */
    public IHistogram2D projectionXZ(String path, IHistogram3D h) {
        IHistogram2D result = histMath.sliceXZ(h, nameInPath(path),IAxis.UNDERFLOW_BIN, IAxis.OVERFLOW_BIN);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }

    /**
     * Create a projection parallel to the Y axis from a 3D histogram.
     * Equivalent to sliceYZ(UNDERFLOW_BIN,OVERFLOW_BIN).
     */
    public IHistogram2D projectionYZ(String path, IHistogram3D h) {
        IHistogram2D result = histMath.sliceYZ(h, nameInPath(path),IAxis.UNDERFLOW_BIN, IAxis.OVERFLOW_BIN);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Create a slice perpendicular to the Z axis from a 3D histogram,
     * between "indexZ1" and "indexZ2" (inclusive).
     * The returned IHistogram2D represents an instantaneous snapshot of the
     * histogram at the time the slice was created.
     * The X axis of the returned histogram corresponds to the X axis of this histogram.
     * The Y axis of the returned histogram corresponds to the Y axis of this histogram.
     */
    public IHistogram2D sliceXY(String path, IHistogram3D h, int indexZ1, int indexZ2) {
        IHistogram2D result = histMath.sliceXY(h, nameInPath(path),indexZ1, indexZ2);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
    /**
     * Create a slice perpendicular to the Y axis from a 3D histogram,
     * between "indexY1" and "indexY2" (inclusive).
     * The returned IHistogram2D represents an instantaneous snapshot of the
     * histogram at the time the slice was created.
     * The X axis of the returned histogram corresponds to the X axis of this histogram.
     * The Y axis of the returned histogram corresponds to the Z axis of this histogram.
     */
    public IHistogram2D sliceXZ(String path, IHistogram3D h, int indexY1, int indexY2) {
        IHistogram2D result = histMath.sliceXZ(h, nameInPath(path),indexY1, indexY2);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    /**
     * Create a slice perpendicular to the X axis from a 3D histogram,
     * between "indexX1" and "indexX2" (inclusive).
     * The returned IHistogram2D represents an instantaneous snapshot of the
     * histogram at the time the slice was created.
     * The X axis of the returned histogram corresponds to the Y axis of this histogram.
     * The Y axis of the returned histogram corresponds to the Z axis of this histogram.
     */
    public IHistogram2D sliceYZ(String path, IHistogram3D h, int indexX1, int indexX2) {
        IHistogram2D result = histMath.sliceYZ(h, nameInPath(path),indexX1, indexX2);
        if (tree != null && result instanceof IManagedObject) tree.addFromFactory( parentPath(path),(IManagedObject) result);
        return result;
    }
    
}

 

Ads help maintain this website.