package hep.aida.ref.hbook;
import hep.aida.*;
import hep.aida.ref.histogram.Histogram1D;
import hep.aida.ref.histogram.FixedAxis;
import hep.aida.ref.histogram.Histogram2D;
import hep.aida.ref.tuple.AbstractTuple;
import hep.io.hbook.*;

/**
 * Adapts hbook histograms and Tuples to their corresponding AIDA interfaces
 * @author tonyj
 * @version $Id
 */
class Converter
{
   private static boolean useIdForName = true;
   
   static void setUseIdForName(boolean b) { useIdForName = b; }
   
   private static Histogram1D convert(OneDHistogram h1)
   {
      Histogram1D hist = new Histogram1D();
      String name = (useIdForName) ? String.valueOf(h1.id()) : h1.getName();
      hist.setName(name);
      // This is to prevent problems with paw histograms with zero bins 
      // See JAS-156
      if ( h1.getXNBins() < 1 )
          return null;
      IAxis xAxis = new FixedAxis(h1.getXNBins(),h1.getXMin(),h1.getXMax()); 
      hist.initHistogram1D(xAxis, null);
      double[] heights1d = h1.getBins();
      double[] errors1d = h1.getErrors();
      hist.setContents(heights1d, errors1d,null,null,null);
      hist.setNEntries(h1.getNEntries()-hist.extraEntries());
      hist.setValidEntries(h1.getNEntries());
      hist.setMean(h1.getXMean());
      hist.setRms(h1.getXRMS());
      hist.setTitle(h1.getName());
      return hist;
   }
   private static Histogram2D convert(TwoDHistogram h2)
   {
      Histogram2D hist = new Histogram2D();
      String name = (useIdForName) ? String.valueOf(h2.id()) : h2.getName();
      hist.setName(name);
      // This is to prevent problems with paw histograms with zero bins 
      // See JAS-156
      if ( h2.getXNBins() < 1 || h2.getYNBins() < 1)
          return null;
      IAxis xAxis = new FixedAxis(h2.getXNBins(),h2.getXMin(),h2.getXMax()); 
      IAxis yAxis = new FixedAxis(h2.getYNBins(),h2.getYMin(),h2.getYMax()); 
      hist.initHistogram2D(xAxis, yAxis, null);
      double[][] heights2d = h2.getBins();
      double[][] errors2d = h2.getErrors();
      hist.setContents(heights2d, errors2d,null,null,null,null,null);
      int nEntries = h2.getNEntries();
      hist.setNEntries(nEntries);
      hist.setValidEntries(nEntries-hist.extraEntries());
      if (h2.getXRMS() != 0 && h2.getXMean() != 0)
      {
         hist.setMeanX(h2.getXMean());
         hist.setRmsX(h2.getXRMS());
      }
      if (h2.getYRMS() != 0 && h2.getYMean() != 0)
      {
         hist.setMeanY(h2.getYMean());
         hist.setRmsY(h2.getYRMS());
      }
      hist.setTitle(h2.getName());
      return hist;
   }
   private static AbstractTuple convert(ColumnwiseTuple t)
   {
      return new HBookColumnwiseTuple(t);
   }
   private static AbstractTuple convert(RowwiseTuple t)
   {
      return new HBookRowwiseTuple(t);
   }
   static IManagedObject convert(Object o)
   {
      if      (o instanceof OneDHistogram)   return convert((OneDHistogram) o);
      else if (o instanceof TwoDHistogram)   return convert((TwoDHistogram) o);
      else if (o instanceof ColumnwiseTuple) return convert((ColumnwiseTuple) o);
      else if (o instanceof RowwiseTuple)    return convert((RowwiseTuple) o);
      else return null;
   }
}
