Ejemplo n.º 1
0
 /// <summary>
 /// Evaluate the variable
 /// </summary>
 /// <param name="c"></param>
 /// <returns></returns>
 public object Evaluate(IScopeContext c)
 {
     return(c.GetVariableValue(VariableName));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Save the plot to output. This is where we do the heavy lifting of generating a plot.
        /// </summary>
        /// <param name="filenameStub"></param>
        /// <returns></returns>
        public IEnumerable <FileInfo> Save(IScopeContext ctx, string filenameStub)
        {
            // Now, do the pre-plot hook
            foreach (var act in _prePlotHook)
            {
                act(ctx, this);
            }

            // Initialize the canvas
            ROOTNET.NTCanvas c = null;
            _canvasIndex++;
            c = new ROOTNET.NTCanvas($"c{_canvasIndex}", GetTitle());
            c.SetCanvasSize(DefaultCanvasWidth, DefaultCanvasHeight);

            if (_plots.Length > 0)
            {
                _plots[0].Title = GetTitle();
            }


            // x and y axis titles
            if (_plots.Length > 0)
            {
                if (_xaxisTitle != null)
                {
                    _plots[0].Xaxis.Title = _xaxisTitle;
                }
                if (_yaxisTitle != null)
                {
                    _plots[0].Yaxis.Title = _yaxisTitle;
                }
            }

            // Plot everything.
            var optS = _drawOptions;

            foreach (var p in _plots)
            {
                if (_maxIsSet)
                {
                    p.Maximum = _plotMaximum;
                }
                if (_minIsSet)
                {
                    p.Minimum = _plotMinimum;
                }

                var perHistoOps = p.Option;
                p.Draw(optS + " " + perHistoOps);
                optS = _drawOptions + "SAME";
            }

            // And post-process
            foreach (var a in _postPlotHook)
            {
                a(ctx, this, c);
            }

            // Save it
            string[] formats     = null;
            var      formatsExpr = ctx.GetVariableValue("plotformats") as object[];

            if (formatsExpr != null)
            {
                formats = formatsExpr.Cast <string>().ToArray();
            }
            if (formats == null)
            {
                formats = new string[] { "png" }
            }
            ;

            var finfos = formats
                         .Select(fmt =>
            {
                var fout = new FileInfo(string.Format("{0}.{1}", filenameStub, fmt));
                if (fout.Exists)
                {
                    try
                    {
                        fout.Delete();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Had trouble removing the old file {0}", fout.FullName);
                        Console.WriteLine("  -> {0}", e.Message);
                    }
                }
                c.SaveAs(fout.FullName);
                return(fout);
            });

            return(finfos.ToArray());
        }
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Save to a file.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="filenameStub"></param>
        /// <returns></returns>
        public IEnumerable <FileInfo> Save(IScopeContext ctx, string filenameStub)
        {
            // Do pre-plot actions.
            foreach (var ph in _prePlotHook)
            {
                ph(ctx, this);
            }

            // Initialize the canvas
            var c = new ROOTNET.NTCanvas();

            c.SetCanvasSize(DefaultCanvasWidth, DefaultCanvasHeight);
            c.Title = GetTitle();

            c.Logx = _logx ? 1 : 0;
            c.Logy = _logy ? 1 : 0;

            // Get the plot maximum
            bool   setMaximum = false;
            double graphMax   = 0.0;

            if (_maxIsSet)
            {
                setMaximum = true;
                graphMax   = _plotMaximum;
            }

            // Do the plot minimum
            bool setMinimum = _minIsSet;
            var  graphMin   = setMaximum ? _plotMinimum : 0.0;


            var mp = new ROOTNET.NTMultiGraph("graph", GetTitle());
            // Plot everything.
            var optS = _drawOptions + "A";

            foreach (var p in _g)
            {
                if (!string.IsNullOrWhiteSpace(_xaxisTitle))
                {
                    p.Xaxis.Title = _xaxisTitle;
                }
                if (!string.IsNullOrWhiteSpace(_yaxisTitle))
                {
                    p.Yaxis.Title = _yaxisTitle;
                }
                if (setMaximum)
                {
                    p.Maximum = graphMax;
                }
                if (setMinimum)
                {
                    p.Minimum = graphMin;
                }

                mp.Add(p, _drawOptions);
                //p.Draw(optS);
                optS    = _drawOptions;
                p.Title = GetTitle();
            }
            mp.Draw("a");

            // Post plot actions
            foreach (var ph in _postPlotHook)
            {
                ph(ctx, this, c);
            }

            // Save it
            string[] formats     = null;
            var      formatsExpr = ctx.GetVariableValue("plotformats") as object[];

            if (formatsExpr != null)
            {
                formats = formatsExpr.Cast <string>().ToArray();
            }
            if (formats == null)
            {
                formats = new string[] { "png" }
            }
            ;

            var finfos = formats
                         .Select(fmt =>
            {
                var fout = new FileInfo(string.Format("{0}.{1}", filenameStub, fmt));
                if (fout.Exists)
                {
                    try
                    {
                        fout.Delete();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Had trouble removing the old file {0}", fout.FullName);
                        Console.WriteLine("  -> {0}", e.Message);
                    }
                }
                c.SaveAs(fout.FullName);
                return(fout);
            });

            return(finfos.ToArray());
        }
    }