package hep.aida.ref.tuple;

import hep.aida.IEvaluator;
import hep.aida.IFilter;
import hep.aida.IManagedObject;
import hep.aida.ITree;
import hep.aida.ITuple;
import hep.aida.ITupleFactory;
import hep.aida.ref.tree.Tree;

/**
 *
 * @author The FreeHEP Team @ SLAC.
 *
 */
public class TupleFactory implements ITupleFactory {
    
    private Tree tree;
    
    private final static char separatorChar = '/';
    
    private String nameInPath( String path ) {
        int index = path.lastIndexOf( separatorChar );
        if ( index == -1 )
            return path;
        return path.substring( index+1 );
    }
    
    private String parentPath( String path ) {
        int index = path.lastIndexOf( separatorChar );
        if ( index == -1 )
            return null;
        return path.substring(0,index);
    }
    
    public TupleFactory(ITree tree) {
        this.tree = tree instanceof Tree ? (Tree) tree : null;
    }
    
    /**
     * Create an NTuple
     * @param path The persistency path of the n-tuple
     * @param title The title of the n-tuple
     * @param columnName The names of the columns
     * @param columnType The types of  the columns
     * @param options NTuple options (currently undefined)
     */
    public ITuple create(String path, String title, String[] columnName, Class[] columnType, String options) {
        Tuple tuple = new Tuple(nameInPath(path), title, columnName, columnType, options);
        if ( tree != null ) tree.addFromFactory(parentPath(path),tuple);
        return tuple;
    }
    
    public ITuple create(String path, String title, String[] columnName, Class[] columnType) {
        return create(path,title,columnName,columnType,"");
    }
    
    /**
     * Create an NTuple
     * @param path The persistency path of the n-tuple
     * @param title The title of the n-tuple
     * @param columns The names and types of the columns e.g. "float px, py, pz, float energy, int charge"
     * @param options NTuple options (currently undefined)
     */
    public ITuple create(String path, String title, String columns, String options) {
        Tuple tuple = new Tuple(nameInPath(path), title, columns, options);
        if ( tree != null ) tree.addFromFactory(parentPath(path),tuple);
        return tuple;
    }
    public ITuple create(String path, String title, String columns) {
        return create(path,title,columns,"");
    }
    
    /**
     * Create a logical chain of ITuples. All ITuples in the set must
     * have the same structure. Chained ITuple can not be filled.
     * @param path The persistency name of the new n-tuple
     * @param title The title of the new n-tuple
     * @param set The array of ITuples to chain
     */
    public ITuple createChained(String path, String title, ITuple[] set) {
        ITuple tuple = new ChainedTuple(nameInPath(path), title, set);
        if ( tree != null ) tree.addFromFactory(parentPath(path),(IManagedObject) tuple);
        return tuple;
    }
    public ITuple createChained(String path, String title, String[] setName) {
        if ( tree == null )
            throw new IllegalArgumentException("This TupleFactory does not have a Tree. Can not find Tuple by name.");
        
        ITuple[] tupleSet = new ITuple[setName.length];
        for (int i=0; i tuple.columns() )
            throw new IllegalArgumentException("Original ITuple has less columns ("+tuple.columns()+
            ") than requested for copy ("+nColumns+")");
        int[] columnId = new int[nColumns];
        String[] fullColumnNames = new String[nColumns];
        Class[] columnTypes = new Class[nColumns];
        
        // To get Default Column String
        AbstractTuple cdsTuple = null;
        if (tuple instanceof Tuple) {
            cdsTuple = (AbstractTuple) tuple;
        }
        for (int i=0; i 0) {
            int nColumns = newTuple.columns();
            int[] columnId = new int[nColumns];
            Class[] columnTypes = new Class[nColumns];
            for (int i=0; i 

Ads help maintain this website.