SNAP Library 2.2, Developer Reference  2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
gnuplot.h
Go to the documentation of this file.
00001 #ifndef gnuplot_h
00002 #define gnuplot_h
00003 
00005 // GNU-Plot-Chart
00006 typedef enum {
00007   gpsNoAuto, gpsAuto, gpsLog, gpsLog2X, gpsLog2Y, gpsLog2XY, gpsLog10X,
00008   gpsLog10Y, gpsLog10XY
00009 } TGpScaleTy;
00010 
00011 typedef enum {
00012   gpwUndef, gpwLines, gpwPoints, gpwLinesPoints, gpwImpulses, gpwDots,
00013   gpwSteps, gpwFSteps, gpwHiSteps, gpwBoxes, gpwErrBars, gpwFilledCurves, gpwMax
00014 } TGpSeriesTy;
00015 
00016 class TGnuPlot {
00017 public:
00019   static TStr GnuPlotPath;
00021   static TStr GnuPlotFNm;
00022   static TStr DefPlotFNm;
00023   static TStr DefDataFNm;
00024 private:
00025   class TGpSeries {
00026   public:
00027     TGpSeriesTy SeriesTy;
00028     TFltKdV XYValV;
00029     TFltV ZValV; // error bar and 3d plots
00030     TStr Label, WithStyle, DataFNm;
00031     int XCol, YCol, ZCol;
00032   public:
00033     TGpSeries() : SeriesTy(gpwLines), XYValV(), ZValV(), Label(), WithStyle(), DataFNm(), XCol(0), YCol(0), ZCol(0) { }
00034     TGpSeries(const TGpSeries& Gps);
00035     TGpSeries& operator = (const TGpSeries& Gps);
00036     bool operator < (const TGpSeries& Gps) const;
00037     bool SaveXVals() const { return (YCol-XCol)==1; }
00038   };
00039   class TGpSeriesCmp {
00040   private:
00041     const TVec<TGpSeries>& SeriesV;
00042   public:
00043     TGpSeriesCmp(const TVec<TGpSeries>& _SeriesV) : SeriesV(_SeriesV) { }
00044     bool operator () (const int& Left, const int& Right) const {
00045       return SeriesV[Left] > SeriesV[Right]; }
00046   };
00047 private:
00048   static int Tics42;        // 1 - "set ticks", 0 - "set ticscale", -1 - unknown
00049   TStr DataFNm, PlotFNm;
00050   TStr Title, LblX, LblY;
00051   TGpScaleTy ScaleTy;
00052   TFltPr YRange, XRange;
00053   bool SetGrid, SetPause;
00054   TVec<TGpSeries> SeriesV;
00055   TStrV MoreCmds;
00056 public:
00057   static int GetTics42();
00058 public:
00059   TStr GetSeriesPlotStr(const int& PlotN);
00060   int IsSameXCol(const int& CurId, const int& PrevId) const;
00061   void CreatePlotFile(const TStr& Comment = TStr());
00062   void RunGnuPlot() const;
00063 public:
00064   TGnuPlot(const TStr& FileNm="gplot", const TStr& PlotTitle=TStr(), const bool& Grid=true);
00065   TGnuPlot(const TStr& DataFileNm, const TStr& PlotFileNm, const TStr& PlotTitle, const bool& Grid);
00066   TGnuPlot(const TGnuPlot& GnuPlot);
00067   TGnuPlot& operator = (const TGnuPlot& GnuPlot);
00068   
00069   void SetTitle(const TStr& PlotTitle) { Title = PlotTitle; }
00070   void SetXLabel(const TStr& XLabel) { LblX = XLabel; }
00071   void SetYLabel(const TStr& YLabel) { LblY = YLabel; }
00072   void SetXYLabel(const TStr& XLabel, const TStr& YLabel) { LblX = XLabel;  LblY = YLabel; }
00073   void SetDataPlotFNm(const TStr& DatFNm, const TStr& PltFNm) { DataFNm = DatFNm;  PlotFNm = PltFNm; }
00074   
00075   void ShowGrid(const bool& Show) { SetGrid = Show; }
00076   void Pause(const bool& DoPause) { SetPause = DoPause; }
00077   void SetScale(const TGpScaleTy& GpScaleTy) { ScaleTy = GpScaleTy;}
00078   void SetXRange(const double& Min, const double& Max) { XRange = TFltPr(Min, Max); }
00079   void SetYRange(const double& Min, const double& Max) { YRange = TFltPr(Min, Max); }
00080   void AddCmd(const TStr& Cmd) { MoreCmds.Add(Cmd); }
00081   TStr GetLineStyle(const int& PlotId) const { return SeriesV[PlotId].WithStyle; }
00082   void SetLineStyle(const int& PlotId, const TStr& StyleStr) { SeriesV[PlotId].WithStyle = StyleStr; }
00083 
00084   int AddFunc(const TStr& FuncStr, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00085   int AddPlot(const TIntV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00086   int AddPlot(const TFltV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00087   int AddPlot(const TFltV& XValV, const TFltV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00088   int AddPlot(const TIntPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00089   int AddPlot(const TFltPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00090   int AddPlot(const TIntKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00091   int AddPlot(const TFltKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00092   int AddPlot(const TIntFltKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00093   int AddPlot(const TIntFltPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00094   int AddPlot(const TStr& DataFNm, const int& ColY, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00095   int AddPlot(const TStr& DataFNm, const int& ColX, const int& ColY, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00096   template<class TKey, class TDat, class THashFunc>
00097   int AddPlot(const THash<TKey, TDat, THashFunc>& XYValH, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr(), const bool& ExpBucket = false);
00098   template<class TKey, class THashFunc>
00099   int AddPlot(const THash<TKey, TMom, THashFunc>& ValMomH, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr(),
00100     bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=false, const bool& ExpBucket=false);
00101 
00102   int AddErrBar(const TFltTrV& XYDValV, const TStr& Label=TStr());
00103   int AddErrBar(const TFltTrV& XYDValV, const TStr& DatLabel, const TStr& ErrLabel);
00104   int AddErrBar(const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00105   int AddErrBar(const TFltV& XValV, const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00106   int AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00107   int AddErrBar(const TFltKdV& XYValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00108   int AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& DatLabel, const TStr& ErrLabel);
00109 
00110   int AddLinFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00111   int AddPwrFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00112   int AddPwrFit1(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00113   int AddPwrFit2(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& MinX=-1.0, const TStr& Style=TStr());
00114   int AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& MinX=-1.0, const TStr& Style=TStr());
00115   int AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style, double& Intercept, double& Slope, double& R2);
00116   int AddLogFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00117   int AddExpFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& FitXOffset=0.0, const TStr& Style=TStr());
00118 
00119   void SavePng(const int& SizeX=1000, const int& SizeY=800, const TStr& Comment=TStr()) {
00120     SavePng(PlotFNm.GetFPath()+PlotFNm.GetFMid()+".png", SizeX, SizeY, Comment); }
00121   void SavePng(const TStr& FNm, const int& SizeX=1000, const int& SizeY=800, const TStr& Comment=TStr(), const TStr& Terminal=TStr());
00122   void SaveEps(const int& FontSz=30, const TStr& Comment=TStr()) {
00123     SaveEps(PlotFNm.GetFPath()+PlotFNm.GetFMid()+".eps", FontSz, Comment); }
00124   void SaveEps(const TStr& FNm, const int& FontSz=30, const TStr& Comment=TStr());
00125   void Plot(const TStr& Comment=TStr()) { CreatePlotFile(Comment);  RunGnuPlot(); }
00126 
00127   static void MakeExpBins(const TFltPrV& XYValV, TFltPrV& ExpXYValV,
00128     const double& BinFactor = 2, const double& MinYVal = 1);
00129   static void MakeExpBins(const TFltKdV& XYValV, TFltKdV& ExpXYValV,
00130     const double& BinFactor = 2, const double& MinYVal = 1);
00131   static void LoadTs(const TStr& FNm, TStrV& ColNmV, TVec<TFltKdV>& ColV);
00132 
00133   static TStr GetScaleStr(const TGpScaleTy& ScaleTy);
00134   static TStr GetSeriesTyStr(const TGpSeriesTy& SeriesTy);
00135 
00136   // save tab separated
00137   static void SaveTs(const TIntKdV& KdV, const TStr& FNm, const TStr& HeadLn = TStr());
00138   static void SaveTs(const TIntFltKdV& KdV, const TStr& FNm, const TStr& HeadLn = TStr());
00139   template <class TVal1, class TVal2>
00140   static void SaveTs(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& FNm, const TStr& HeadLn = TStr());
00141   template <class TVal1, class TVal2, class TVal3>
00142   static void SaveTs(const TVec<TTriple<TVal1, TVal2, TVal3> >& ValV, const TStr& FNm, const TStr& HeadLn = TStr());
00143   template <class TVal, int Vals>
00144   static void SaveTs(const TVec<TTuple<TVal, Vals> >& ValV, const TStr& FNm, const TStr& HeadLn = TStr());
00145   static void Test();
00146 
00147   // plot value-count tables, and pair vectors
00148   template <class TVal1, class TVal2>
00149   static void PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& OutFNmPref, const TStr& Desc="",
00150    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, 
00151    const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00152   template <class TVal1, class TVal2, class TVal3>
00153   static void PlotValV(const TVec<TTriple<TVal1, TVal2, TVal3> >& ValV, const TStr& OutFNmPref, const TStr& Desc="",
00154    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false,
00155    const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& ErrBarStr = "");
00156   template <class TVal1, class TVal2>
00157   static void PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV1, const TStr& Name1,
00158     const TVec<TPair<TVal1, TVal2> >& ValV2, const TStr& Name2, const TStr& OutFNmPref, const TStr& Desc="",
00159    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, 
00160    const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00161   template <class TVal1>
00162   static void PlotValV(const TVec<TVal1>& ValV, const TStr& OutFNmPref, const TStr& Desc="",
00163    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, 
00164    const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00165   template <class TKey, class TVal, class THashFunc>
00166   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH, const TStr& OutFNmPref, const TStr& Desc="",
00167    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, 
00168    const TGpSeriesTy& SeriesTy=gpwLinesPoints, const bool& PlotNCDF=false, const bool& ExpBucket=false);
00169   template <class TKey, class TVal, class THashFunc>
00170   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00171     const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00172     const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00173     const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00174   template <class TKey, class TVal, class THashFunc>
00175   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00176     const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00177     const THash<TKey, TVal, THashFunc>& ValCntH3, const TStr& Label3, 
00178     const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00179     const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00180   template <class TVal1>
00181   static void PlotValMomH(const THash<TVal1, TMom>& ValMomH, const TStr& OutFNmPref, const TStr& Desc="",
00182    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints,
00183    bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=true, bool PlotScatter=false);
00184   template <class TVal1>
00185   static void PlotValMomH(const THash<TVal1, TMom>& ValMomH1, const TStr& Label1, const THash<TVal1, TMom>& ValMomH2, const TStr& Label2,
00186    const TStr& OutFNmPref, const TStr& Desc="",
00187    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints,
00188    bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=true, bool PlotScatter=false);
00189 };
00190 
00191 //---------------------------------------------------------
00192 // useful commands
00193 // set terminal png small size 800,600
00194 // set terminal postscript enhanced eps 22
00195 // set output 'hopsasa.png'
00196 // set size 1,0.5
00197 // set pointsize 0.9
00198 // set key right bottom Left
00199 // set style fill solid 0.2
00200 // set ticscale 3 1   # longer axis tics
00201 // plot ... with points pointtype 6 pointsize 1
00202 // 1   +           7  (f) circle
00203 // 2  cross        8  (o) triangle
00204 // 3  *            9  (f) triangle
00205 // 4  (o) square  10  (o) inverse-triangle
00206 // 5  (f) square  11  (f) inverse-triangle
00207 // 6  (o) circle  12  (o) diamond
00208 //                13  (f) diamond
00209 // set label "Text" at 23,47000
00210 // set arrow from 28,45000 to 28,31000 lw 3
00211 // axes x1y2: http://t16web.lanl.gov/Kawano/gnuplot/plot1-e.html#5.2
00212 
00213 template <class TVal1, class TVal2>
00214 void TGnuPlot::SaveTs(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00215   FILE *F = fopen(FNm.CStr(), "wt");
00216   EAssert(F);
00217   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00218   for (int i = 0; i < ValV.Len(); i++) {
00219     fprintf(F, "%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2)); }
00220   fclose(F);
00221 }
00222 
00223 template <class TVal1, class TVal2, class TVal3>
00224 void TGnuPlot::SaveTs(const TVec<TTriple<TVal1, TVal2, TVal3> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00225   FILE *F = fopen(FNm.CStr(), "wt");
00226   EAssert(F);
00227   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00228   for (int i = 0; i < ValV.Len(); i++) {
00229     fprintf(F, "%g\t%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2), double(ValV[i].Val3)); }
00230   fclose(F);
00231 }
00232 
00233 template <class TVal, int Vals>
00234 void TGnuPlot::SaveTs(const TVec<TTuple<TVal, Vals> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00235   FILE *F = fopen(FNm.CStr(), "wt");
00236   EAssert(F);
00237   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00238   for (int i = 0; i < ValV.Len(); i++) {
00239     fprintf(F, "%g", double(ValV[i][0]));
00240     for (int v = 1; v < Vals; v++) {
00241       fprintf(F, "\t%g", double(ValV[i][v])); }
00242     fprintf(F, "\n");
00243   }
00244   fclose(F);
00245 }
00246 
00247 template<class TKey, class TDat, class THashFunc>
00248 int TGnuPlot::AddPlot(const THash<TKey, TDat, THashFunc>& XYValH, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style, const bool& ExpBucket) {
00249   TFltPrV XYFltValV(XYValH.Len(), 0);  
00250   for (int k = XYValH.FFirstKeyId();  XYValH.FNextKeyId(k); ) { 
00251     XYFltValV.Add(TFltPr(TFlt(XYValH.GetKey(k)), TFlt(XYValH[k]))); 
00252   }  
00253   XYFltValV.Sort();
00254   if (ExpBucket) {
00255     TFltPrV BucketV;
00256     TGnuPlot::MakeExpBins(XYFltValV, BucketV);
00257     BucketV.Swap(XYFltValV);
00258   }
00259   return AddPlot(XYFltValV, SeriesTy, Label, Style); 
00260 }
00261 
00262 template<class TKey, class THashFunc>
00263 int TGnuPlot::AddPlot(const THash<TKey, TMom, THashFunc>& ValMomH, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style, bool PlotAvg, bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, const bool& ExpBucket) {
00264   TFltTrV AvgV, StdErrV;
00265   TFltPrV AvgV2, MedV, MinV, MaxV, BucketV;
00266   for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) {
00267     TMom Mom(ValMomH[i]);
00268     if (! Mom.IsDef()) { Mom.Def(); }
00269     const double x = ValMomH.GetKey(i);
00270     if (PlotAvg) { 
00271       if (PlotSDev) { 
00272         AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00273       else if (PlotStdErr) {
00274         StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); 
00275       } else {
00276         AvgV2.Add(TFltPr(x, Mom.GetMean()));
00277       }
00278     }
00279     if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); }
00280     if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); }
00281     if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); }
00282   }
00283   AvgV.Sort();  AvgV2.Sort();
00284   MedV.Sort();  MinV.Sort();  MaxV.Sort(); 
00285   int PlotId=0;
00286   // exponential bucketing
00287   if (ExpBucket) {
00288     if (! AvgV2.Empty()) { TGnuPlot::MakeExpBins(AvgV2, BucketV);  BucketV.Swap(AvgV2); }
00289     if (! MedV.Empty()) { TGnuPlot::MakeExpBins(MedV, BucketV);  BucketV.Swap(MedV); }
00290     if (! MinV.Empty()) { TGnuPlot::MakeExpBins(MinV, BucketV);  BucketV.Swap(MinV); }
00291     if (! MaxV.Empty()) { TGnuPlot::MakeExpBins(MaxV, BucketV);  BucketV.Swap(MaxV); }
00292   }
00293   // plot
00294   if (! AvgV.Empty()) { PlotId = AddErrBar(AvgV, Label+" Average", Label+" StdDev"); }
00295   if (! AvgV2.Empty()) { PlotId = AddPlot(AvgV2, SeriesTy, Label+" Average", Style); }
00296   if (! MedV.Empty()) { PlotId = AddPlot(MedV, SeriesTy, Label+" Median", Style); }
00297   if (! MinV.Empty()) { PlotId = AddPlot(MinV, SeriesTy, Label+" Min", Style); }
00298   if (! MaxV.Empty()) { PlotId = AddPlot(MaxV, SeriesTy, Label+" Max", Style); }
00299   if (! StdErrV.Empty()) { PlotId = AddErrBar(StdErrV, Label+" Average", Label+" StdErr"); }
00300   return PlotId;
00301 }
00302 
00303 // plot value-count tables, and pair vectors
00304 template <class TKey, class TVal, class THashFunc>
00305 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH, const TStr& OutFNmPref, const TStr& Desc,
00306  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy, 
00307  const bool& PlotNCDF, const bool& ExpBucket) {
00308   TFltPrV IdCntV(ValCntH.Len(), 0), BucketV;
00309   for (int i = ValCntH.FFirstKeyId(); ValCntH.FNextKeyId(i); ) {
00310     IdCntV.Add(TFltPr(double(ValCntH.GetKey(i)), double(ValCntH[i]))); }
00311   IdCntV.Sort();
00312   if (ExpBucket) { 
00313     TGnuPlot::MakeExpBins(IdCntV, BucketV);
00314     BucketV.Swap(IdCntV);
00315   }
00316   if (PlotNCDF) { 
00317     TFltPrV NCdfV = IdCntV;
00318     for (int i = NCdfV.Len()-2; i >= 0; i--) {
00319       NCdfV[i].Val2 = NCdfV[i].Val2 + NCdfV[i+1].Val2; 
00320     }
00321     PlotValV(NCdfV, OutFNmPref, Desc, "NCDF "+XLabel, "NCDF "+YLabel, ScaleTy, PowerFit, SeriesTy);
00322   } else {
00323     PlotValV(IdCntV, OutFNmPref, Desc, XLabel, YLabel, ScaleTy, PowerFit, SeriesTy); 
00324   }
00325 }
00326 
00327 template <class TKey, class TVal, class THashFunc>
00328 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00329                            const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00330                            const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00331                            const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) {
00332   PlotValCntH(ValCntH1, Label1, ValCntH2, Label2, THash<TKey, TVal, THashFunc>(), "", OutFNmPref, Desc, XLabel, YLabel,
00333     ScaleTy, SeriesTy);
00334 }
00335 
00336 template <class TKey, class TVal, class THashFunc>
00337 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00338                            const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00339                            const THash<TKey, TVal, THashFunc>& ValCntH3, const TStr& Label3, 
00340                            const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00341                            const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) {
00342   TFltPrV IdCntV1(ValCntH1.Len(), 0), IdCntV2(ValCntH2.Len(), 0), IdCntV3(ValCntH3.Len(), 0);
00343   for (int i = ValCntH1.FFirstKeyId(); ValCntH1.FNextKeyId(i); ) {
00344     IdCntV1.Add(TFltPr(double(ValCntH1.GetKey(i)), double(ValCntH1[i]))); }
00345   for (int i = ValCntH2.FFirstKeyId(); ValCntH2.FNextKeyId(i); ) {
00346     IdCntV2.Add(TFltPr(double(ValCntH2.GetKey(i)), double(ValCntH2[i]))); }
00347   for (int i = ValCntH3.FFirstKeyId(); ValCntH3.FNextKeyId(i); ) {
00348     IdCntV3.Add(TFltPr(double(ValCntH3.GetKey(i)), double(ValCntH3[i]))); }
00349   IdCntV1.Sort();
00350   IdCntV2.Sort();
00351   IdCntV3.Sort();
00352   TGnuPlot GP(OutFNmPref, Desc);
00353   GP.SetXYLabel(XLabel, YLabel);
00354   GP.SetScale(ScaleTy);
00355   if (! IdCntV1.Empty()) { GP.AddPlot(IdCntV1, SeriesTy, Label1); }
00356   if (! IdCntV2.Empty()) { GP.AddPlot(IdCntV2, SeriesTy, Label2); }
00357   if (! IdCntV3.Empty()) { GP.AddPlot(IdCntV3, SeriesTy, Label3); }
00358   GP.SavePng();
00359 }
00360 
00361 template <class TVal1, class TVal2>
00362 void TGnuPlot::PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& OutFNmPref, const TStr& Desc,
00363  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00364   TFltKdV IdCntV(ValV.Len(), 0);
00365   for (int i = 0; i < ValV.Len(); i++) {
00366     IdCntV.Add(TFltKd(double(ValV[i].Val1), double(ValV[i].Val2))); }
00367   if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00368   IdCntV.Sort();
00369   TGnuPlot GP(OutFNmPref, Desc);
00370   GP.SetXYLabel(XLabel, YLabel);
00371   GP.SetScale(ScaleTy);
00372   const int Id = GP.AddPlot(IdCntV, SeriesTy);
00373   if (PowerFit) { 
00374     GP.AddPwrFit3(Id);
00375     double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat;
00376     if (MaxY < MinY) { Swap(MaxY, MinY); }
00377     //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0));
00378     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00379   }
00380   GP.SavePng();
00381 }
00382 
00383 template <class TVal1, class TVal2, class TVal3>
00384 void TGnuPlot::PlotValV(const TVec<TTriple<TVal1, TVal2, TVal3> >& ValV, const TStr& OutFNmPref, const TStr& Desc,
00385  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy, const TStr& ErrBarStr) {
00386   TFltKdV IdCntV(ValV.Len(), 0);
00387   TFltV DeltaYV(ValV.Len(), 0);
00388   for (int i = 0; i < ValV.Len(); i++) {
00389     IdCntV.Add(TFltKd(double(ValV[i].Val1), double(ValV[i].Val2)));
00390     DeltaYV.Add(double(ValV[i].Val3));
00391   }
00392   if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00393   IdCntV.Sort();
00394   TGnuPlot GP(OutFNmPref, Desc);
00395   GP.SetXYLabel(XLabel, YLabel);
00396   GP.SetScale(ScaleTy);
00397   const int Id = GP.AddPlot(IdCntV, SeriesTy);
00398   GP.AddErrBar(IdCntV, DeltaYV, ErrBarStr);
00399   if (PowerFit) {
00400     GP.AddPwrFit3(Id);
00401     double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat;
00402     if (MaxY < MinY) { Swap(MaxY, MinY); }
00403     //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0));
00404     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00405   }
00406   GP.SavePng();
00407 }
00408 
00409 
00410 template <class TVal1, class TVal2>
00411 void TGnuPlot::PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV1, const TStr& Name1, 
00412  const TVec<TPair<TVal1, TVal2> >& ValV2, const TStr& Name2, const TStr& OutFNmPref, const TStr& Desc,
00413  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00414   TFltKdV IdCntV1(ValV1.Len(), 0), IdCntV2(ValV2.Len(), 0);
00415   for (int i = 0; i < ValV1.Len(); i++) {
00416     IdCntV1.Add(TFltKd(double(ValV1[i].Val1), double(ValV1[i].Val2))); }
00417   for (int i = 0; i < ValV2.Len(); i++) {
00418     IdCntV2.Add(TFltKd(double(ValV2[i].Val1), double(ValV2[i].Val2))); }
00419   if (IdCntV1.Empty() || IdCntV2.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00420   IdCntV1.Sort();
00421   IdCntV2.Sort();
00422   TGnuPlot GP(OutFNmPref, Desc);
00423   GP.SetXYLabel(XLabel, YLabel);
00424   GP.SetScale(ScaleTy);
00425   { const int Id = GP.AddPlot(IdCntV1, SeriesTy, Name1);
00426   if (PowerFit) { 
00427     GP.AddPwrFit3(Id);
00428     double MaxY = IdCntV1.Last().Dat, MinY = IdCntV1[0].Dat;
00429     if (MaxY < MinY) { Swap(MaxY, MinY); }
00430     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00431   } }
00432   { const int Id = GP.AddPlot(IdCntV2, SeriesTy, Name2);
00433   if (PowerFit) { 
00434     GP.AddPwrFit3(Id);
00435     double MaxY = IdCntV2.Last().Dat, MinY = IdCntV2[0].Dat;
00436     if (MaxY < MinY) { Swap(MaxY, MinY); }
00437     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00438   } }
00439   GP.SavePng();
00440 }
00441 
00442            
00443 
00444 template <class TVal1>
00445 void TGnuPlot::PlotValV(const TVec<TVal1>& ValV, const TStr& OutFNmPref, const TStr& Desc,
00446  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00447   TFltKdV IdCntV(ValV.Len(), 0);
00448   for (int i = 0; i < ValV.Len(); i++) {
00449     IdCntV.Add(TFltKd(double(i+1), double(ValV[i]))); }
00450   if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00451   IdCntV.Sort();
00452   TGnuPlot GP(OutFNmPref, Desc);
00453   GP.SetXYLabel(XLabel, YLabel);
00454   GP.SetScale(ScaleTy);
00455   const int Id = GP.AddPlot(IdCntV, SeriesTy);
00456   if (PowerFit) { 
00457     GP.AddPwrFit3(Id);
00458     double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat;
00459     if (MaxY < MinY) { Swap(MaxY, MinY); }
00460     //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0));
00461     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00462   }
00463   GP.SavePng();
00464 }
00465 
00466 template <class TVal1>
00467 void TGnuPlot::PlotValMomH(const THash<TVal1, TMom>& ValMomH, const TStr& OutFNmPref, const TStr& Desc,
00468  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy,
00469  bool PlotAvg, bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, bool PlotScatter) {
00470   TFltTrV AvgV, StdErrV;
00471   TFltPrV AvgV2, MedV, MinV, MaxV;
00472   TFltPrV ScatterV;
00473   for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) {
00474     TMom Mom(ValMomH[i]);
00475     if (! Mom.IsDef()) { Mom.Def(); }
00476     const double x = ValMomH.GetKey(i);
00477     if (PlotAvg) { 
00478       if (PlotSDev) { 
00479         AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00480       else { 
00481         AvgV2.Add(TFltPr(x, Mom.GetMean())); 
00482       }
00483       if (PlotStdErr) {
00484         StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); 
00485       }
00486     }
00487     if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); }
00488     if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); }
00489     if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); }
00490     if (PlotScatter) {
00491       THashSet<TFlt> PointSet;
00492       for (int xi = 0; xi < ValMomH[i].GetVals(); xi++) {
00493         PointSet.AddKey(ValMomH[i].GetVal(xi)); }
00494         //ScatterV.Add(TFltPr(x, ValMomH[i].GetVal(xi)));  }
00495       for (int xi = 0; xi < PointSet.Len(); xi++) {
00496         ScatterV.Add(TFltPr(x, PointSet[xi]));  }
00497       //printf("S%d %d %d.", ValMomH[i].GetVals(), PointSet.Len(), ScatterV.Len());
00498     }
00499   }
00500   AvgV.Sort();  AvgV2.Sort();
00501   MedV.Sort();  MinV.Sort();  MaxV.Sort();  StdErrV.Sort();
00502   TGnuPlot GP(OutFNmPref, Desc);
00503   GP.SetScale(ScaleTy);
00504   GP.SetXYLabel(XLabel, YLabel);
00505   if (! ScatterV.Empty()) { GP.AddPlot(ScatterV, gpwPoints, "Scatter"); }
00506   if (! AvgV.Empty()) { GP.AddErrBar(AvgV, "Average", "StdDev"); }
00507   if (! AvgV2.Empty()) { GP.AddPlot(AvgV2, SeriesTy, "Average"); }
00508   if (! MedV.Empty()) { GP.AddPlot(MedV, SeriesTy, "Median"); }
00509   if (! MinV.Empty()) { GP.AddPlot(MinV, SeriesTy, "Min"); }
00510   if (! MaxV.Empty()) { GP.AddPlot(MaxV, SeriesTy, "Max"); }
00511   if (! StdErrV.Empty()) { GP.AddErrBar(StdErrV, "Standard error"); }
00512   GP.SavePng();
00513 }
00514 
00515 template <class TVal1>
00516 void TGnuPlot::PlotValMomH(const THash<TVal1, TMom>& ValMomH1, const TStr& Label1, const THash<TVal1, TMom>& ValMomH2, const TStr& Label2,
00517  const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy,
00518  bool PlotAvg, bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, bool PlotScatter) {
00519   TFltTrV AvgV1, AvgV2, StdErrV1, StdErrV2;
00520   TFltPrV AvgVM1, MedV1, MinV1, MaxV1;
00521   TFltPrV AvgVM2, MedV2, MinV2, MaxV2;
00522   TFltPrV ScatterV1, ScatterV2;
00523   // ValMom1
00524   for (int i = ValMomH1.FFirstKeyId(); ValMomH1.FNextKeyId(i); ) {
00525     TMom Mom(ValMomH1[i]);
00526     if (! Mom.IsDef()) { Mom.Def(); }
00527     const double x = ValMomH1.GetKey(i);
00528     if (PlotAvg) {
00529       if (PlotSDev) {
00530         AvgV1.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00531       else {
00532         AvgVM1.Add(TFltPr(x, Mom.GetMean()));
00533       }
00534       if (PlotStdErr) {
00535         StdErrV1.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals())));
00536       }
00537     }
00538     if (PlotMed) { MedV1.Add(TFltPr(x, Mom.GetMedian())); }
00539     if (PlotMin) { MinV1.Add(TFltPr(x, Mom.GetMn())); }
00540     if (PlotMax) { MaxV1.Add(TFltPr(x, Mom.GetMx())); }
00541     if (PlotScatter) {
00542       THashSet<TFlt> PointSet;
00543       for (int xi = 0; xi < ValMomH1[i].GetVals(); xi++) {
00544         PointSet.AddKey(ValMomH1[i].GetVal(xi)); }
00545       for (int xi = 0; xi < PointSet.Len(); xi++) {
00546         ScatterV1.Add(TFltPr(x, PointSet[xi]));  }
00547     }
00548   }
00549   AvgV1.Sort();  AvgVM1.Sort(); MedV1.Sort();  MinV1.Sort();  MaxV1.Sort();  StdErrV1.Sort();
00550   // ValMom2
00551   for (int i = ValMomH2.FFirstKeyId(); ValMomH2.FNextKeyId(i); ) {
00552     TMom Mom(ValMomH2[i]);
00553     if (! Mom.IsDef()) { Mom.Def(); }
00554     const double x = ValMomH2.GetKey(i);
00555     if (PlotAvg) {
00556       if (PlotSDev) {
00557         AvgV2.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00558       else {
00559         AvgVM2.Add(TFltPr(x, Mom.GetMean()));
00560       }
00561       if (PlotStdErr) {
00562         StdErrV2.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals())));
00563       }
00564     }
00565     if (PlotMed) { MedV2.Add(TFltPr(x, Mom.GetMedian())); }
00566     if (PlotMin) { MinV2.Add(TFltPr(x, Mom.GetMn())); }
00567     if (PlotMax) { MaxV2.Add(TFltPr(x, Mom.GetMx())); }
00568     if (PlotScatter) {
00569       THashSet<TFlt> PointSet;
00570       for (int xi = 0; xi < ValMomH2[i].GetVals(); xi++) {
00571         PointSet.AddKey(ValMomH2[i].GetVal(xi)); }
00572       for (int xi = 0; xi < PointSet.Len(); xi++) {
00573         ScatterV2.Add(TFltPr(x, PointSet[xi]));  }
00574     }
00575   }
00576   AvgV2.Sort();  AvgVM2.Sort(); MedV2.Sort();  MinV2.Sort();  MaxV2.Sort();  StdErrV2.Sort();
00577   // plot
00578   TGnuPlot GP(OutFNmPref, Desc);
00579   GP.SetScale(ScaleTy);
00580   GP.SetXYLabel(XLabel, YLabel);
00581   // ValMom1
00582   if (! ScatterV1.Empty()) { GP.AddPlot(ScatterV1, gpwPoints, Label1+": Scatter"); }
00583   if (! AvgV1.Empty()) { GP.AddErrBar(AvgV1, Label1+": Average", Label1+": StdDev"); }
00584   if (! AvgVM1.Empty()) { GP.AddPlot(AvgVM1, SeriesTy, Label1+": Average"); }
00585   if (! MedV1.Empty()) { GP.AddPlot(MedV1, SeriesTy, Label1+": Median"); }
00586   if (! MinV1.Empty()) { GP.AddPlot(MinV1, SeriesTy, Label1+": Min"); }
00587   if (! MaxV1.Empty()) { GP.AddPlot(MaxV1, SeriesTy, Label1+": Max"); }
00588   if (! StdErrV1.Empty()) { GP.AddErrBar(StdErrV1, Label1+": Std error"); }
00589   // ValMom2
00590   if (! ScatterV2.Empty()) { GP.AddPlot(ScatterV2, gpwPoints, Label2+": Scatter"); }
00591   if (! AvgV2.Empty()) { GP.AddErrBar(AvgV2, Label2+": Average", Label2+": StdDev"); }
00592   if (! AvgVM2.Empty()) { GP.AddPlot(AvgVM2, SeriesTy, Label2+": Average"); }
00593   if (! MedV2.Empty()) { GP.AddPlot(MedV2, SeriesTy, Label2+": Median"); }
00594   if (! MinV2.Empty()) { GP.AddPlot(MinV2, SeriesTy, Label2+": Min"); }
00595   if (! MaxV2.Empty()) { GP.AddPlot(MaxV2, SeriesTy, Label2+": Max"); }
00596   if (! StdErrV2.Empty()) { GP.AddErrBar(StdErrV2, Label2+": Std error"); }
00597   GP.SavePng();
00598 }
00599 
00600 #endif
00601