package beast.evolution.tree;



import java.io.PrintStream;

import beast.core.CalculationNode;
import beast.core.Description;
import beast.core.Function;
import beast.core.Input;
import beast.core.Input.Validate;
import beast.core.Loggable;


@Description("Logger to report height of a tree")
public class TreeHeightLogger extends CalculationNode implements Loggable, Function {
    final public Input<Tree> treeInput = new Input<>("tree", "tree to report height for.", Validate.REQUIRED);

    @Override
    public void initAndValidate() {
        // nothing to do
    }

    @Override
    public void init(PrintStream out) {
        final Tree tree = treeInput.get();
        if (getID() == null || getID().matches("\\s*")) {
            out.print(tree.getID() + ".height\t");
        } else {
            out.print(getID() + "\t");
        }
    }

    @Override
    public void log(int sample, PrintStream out) {
        final Tree tree = treeInput.get();
        out.print(tree.getRoot().getHeight() + "\t");
    }

    @Override
    public void close(PrintStream out) {
        // nothing to do
    }

    @Override
    public int getDimension() {
        return 1;
    }

    @Override
    public double getArrayValue() {
        return treeInput.get().getRoot().getHeight();
    }

    @Override
    public double getArrayValue(int dim) {
        return treeInput.get().getRoot().getHeight();
    }
}

@@ -484,16 +484,146 @@
 QUIRED);
+%0A    final public Input%3CBoolean%3E logLengthInput = new Input%3C%3E(%22logLength%22, %22If true, tree length will be logged as well.%22, false);
 %0A%0A    @O
@@ -970,16 +970,117 @@
       %7D%0A
+        if (logLengthInput.get()) %7B%0A            out.print(tree.getID() + %22.treeLength%5Ct%22);%0A        %7D%0A
     %7D%0A%0A 
@@ -1240,31 +1240,334 @@
  %22%5Ct%22);%0A    
-%7D%0A%0A
     
+if (logLengthInput.get()) %7B%0A            out.print(getLength(tree) + %22%5Ct%22);%0A        %7D%0A    %7D%0A%0A    private double getLength(Tree tree) %7B%0A    %09double length = 0;%0A    %09for (Node node : tree.getNodesAsArray()) %7B%0A    %09%09if (!node.isRoot()) %7B%0A    %09%09%09length += node.getLength();%0A    %09%09%7D%0A    %09%7D%0A%09%09return length;%0A%09%7D%0A%0A%09
 @Override%0A  

allow tree height logger to log length as well. closes #577