SNAP Library, User Reference  2012-10-02 12:56:23
SNAP, a general purpose network analysis and graph mining library
 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:
00018   static TStr DefPlotFNm;
00019   static TStr DefDataFNm;
00020 private:
00021   class TGpSeries {
00022   public:
00023     TGpSeriesTy SeriesTy;
00024     TFltKdV XYValV;
00025     TFltV ZValV; // error bar and 3d plots
00026     TStr Label, WithStyle, DataFNm;
00027     int XCol, YCol, ZCol;
00028   public:
00029     TGpSeries() : SeriesTy(gpwLines), XYValV(), ZValV(), Label(), WithStyle(), DataFNm(), XCol(0), YCol(0), ZCol(0) { }
00030     TGpSeries(const TGpSeries& Gps);
00031     TGpSeries& operator = (const TGpSeries& Gps);
00032     bool operator < (const TGpSeries& Gps) const;
00033     bool SaveXVals() const { return (YCol-XCol)==1; }
00034   };
00035   class TGpSeriesCmp {
00036   private:
00037     const TVec<TGpSeries>& SeriesV;
00038   public:
00039     TGpSeriesCmp(const TVec<TGpSeries>& _SeriesV) : SeriesV(_SeriesV) { }
00040     bool operator () (const int& Left, const int& Right) const {
00041       return SeriesV[Left] > SeriesV[Right]; }
00042   };
00043 private:
00044   static int Tics42;        // 1 - "set ticks", 0 - "set ticscale", -1 - unknown
00045   TStr DataFNm, PlotFNm;
00046   TStr Title, LblX, LblY;
00047   TGpScaleTy ScaleTy;
00048   TFltPr YRange, XRange;
00049   bool SetGrid, SetPause;
00050   TVec<TGpSeries> SeriesV;
00051   TStrV MoreCmds;
00052   static int GetTics42();
00053 public:
00054   TStr GetSeriesPlotStr(const int& PlotN);
00055   int IsSameXCol(const int& CurId, const int& PrevId) const;
00056   void CreatePlotFile(const TStr& Comment = TStr());
00057   void RunGnuPlot() const;
00058 public:
00059   TGnuPlot(const TStr& FileNm="gplot", const TStr& PlotTitle=TStr(), const bool& Grid=true);
00060   TGnuPlot(const TStr& DataFileNm, const TStr& PlotFileNm, const TStr& PlotTitle, const bool& Grid);
00061   TGnuPlot(const TGnuPlot& GnuPlot);
00062   TGnuPlot& operator = (const TGnuPlot& GnuPlot);
00063   
00064   void SetTitle(const TStr& PlotTitle) { Title = PlotTitle; }
00065   void SetXLabel(const TStr& XLabel) { LblX = XLabel; }
00066   void SetYLabel(const TStr& YLabel) { LblY = YLabel; }
00067   void SetXYLabel(const TStr& XLabel, const TStr& YLabel) { LblX = XLabel;  LblY = YLabel; }
00068   void SetDataPlotFNm(const TStr& DatFNm, const TStr& PltFNm) { DataFNm = DatFNm;  PlotFNm = PltFNm; }
00069   
00070   void ShowGrid(const bool& Show) { SetGrid = Show; }
00071   void Pause(const bool& DoPause) { SetPause = DoPause; }
00072   void SetScale(const TGpScaleTy& GpScaleTy) { ScaleTy = GpScaleTy;}
00073   void SetXRange(const double& Min, const double& Max) { XRange = TFltPr(Min, Max); }
00074   void SetYRange(const double& Min, const double& Max) { YRange = TFltPr(Min, Max); }
00075   void AddCmd(const TStr& Cmd) { MoreCmds.Add(Cmd); }
00076   TStr GetLineStyle(const int& PlotId) const { return SeriesV[PlotId].WithStyle; }
00077   void SetLineStyle(const int& PlotId, const TStr& StyleStr) { SeriesV[PlotId].WithStyle = StyleStr; }
00078 
00079   int AddFunc(const TStr& FuncStr, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00080   int AddPlot(const TIntV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00081   int AddPlot(const TFltV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00082   int AddPlot(const TFltV& XValV, const TFltV& YValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00083   int AddPlot(const TIntPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00084   int AddPlot(const TFltPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00085   int AddPlot(const TIntKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00086   int AddPlot(const TFltKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00087   int AddPlot(const TIntFltKdV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00088   int AddPlot(const TIntFltPrV& XYValV, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00089   int AddPlot(const TStr& DataFNm, const int& ColY, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00090   int AddPlot(const TStr& DataFNm, const int& ColX, const int& ColY, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr());
00091   template<class TKey, class TDat, class THashFunc>
00092   int AddPlot(const THash<TKey, TDat, THashFunc>& XYValH, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr(), const bool& ExpBucket = false);
00093   template<class TKey, class THashFunc>
00094   int AddPlot(const THash<TKey, TMom, THashFunc>& ValMomH, const TGpSeriesTy& SeriesTy=gpwLinesPoints, const TStr& Label=TStr(), const TStr& Style=TStr(),
00095     bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=false, const bool& ExpBucket=false);
00096 
00097   int AddErrBar(const TFltTrV& XYDValV, const TStr& Label=TStr());
00098   int AddErrBar(const TFltTrV& XYDValV, const TStr& DatLabel, const TStr& ErrLabel);
00099   int AddErrBar(const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00100   int AddErrBar(const TFltV& XValV, const TFltV& YValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00101   int AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00102   int AddErrBar(const TFltKdV& XYValV, const TFltV& DeltaYV, const TStr& Label=TStr());
00103   int AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& DatLabel, const TStr& ErrLabel);
00104 
00105   int AddLinFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00106   int AddPwrFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00107   int AddPwrFit1(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00108   int AddPwrFit2(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& MinX=-1.0, const TStr& Style=TStr());
00109   int AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& MinX=-1.0, const TStr& Style=TStr());
00110   int AddPwrFit3(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style, double& Intercept, double& Slope, double& R2);
00111   int AddLogFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const TStr& Style=TStr());
00112   int AddExpFit(const int& PlotId, const TGpSeriesTy& SeriesTy=gpwLines, const double& FitXOffset=0.0, const TStr& Style=TStr());
00113 
00114   void SavePng(const int& SizeX=1000, const int& SizeY=800, const TStr& Comment=TStr()) {
00115     SavePng(PlotFNm.GetFMid()+".png", SizeX, SizeY, Comment); }
00116   void SavePng(const TStr& FNm, const int& SizeX=1000, const int& SizeY=800, const TStr& Comment=TStr(), const TStr& Terminal=TStr());
00117   void SaveEps(const int& FontSz=30, const TStr& Comment=TStr()) {
00118     SaveEps(PlotFNm.GetFMid()+".eps", FontSz, Comment); }
00119   void SaveEps(const TStr& FNm, const int& FontSz=30, const TStr& Comment=TStr());
00120   void Plot(const TStr& Comment=TStr()) { CreatePlotFile(Comment);  RunGnuPlot(); }
00121 
00122   static void MakeExpBins(const TFltPrV& XYValV, TFltPrV& ExpXYValV,
00123     const double& BinFactor = 2, const double& MinYVal = 1);
00124   static void MakeExpBins(const TFltKdV& XYValV, TFltKdV& ExpXYValV,
00125     const double& BinFactor = 2, const double& MinYVal = 1);
00126   static void LoadTs(const TStr& FNm, TStrV& ColNmV, TVec<TFltKdV>& ColV);
00127 
00128   static TStr GetScaleStr(const TGpScaleTy& ScaleTy);
00129   static TStr GetSeriesTyStr(const TGpSeriesTy& SeriesTy);
00130 
00131   // save tab separated
00132   static void SaveTs(const TIntKdV& KdV, const TStr& FNm, const TStr& HeadLn = TStr());
00133   static void SaveTs(const TIntFltKdV& KdV, const TStr& FNm, const TStr& HeadLn = TStr());
00134   template <class TVal1, class TVal2>
00135   static void SaveTs(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& FNm, const TStr& HeadLn = TStr());
00136   template <class TVal1, class TVal2, class TVal3>
00137   static void SaveTs(const TVec<TTriple<TVal1, TVal2, TVal3> >& ValV, const TStr& FNm, const TStr& HeadLn = TStr());
00138   template <class TVal, int Vals>
00139   static void SaveTs(const TVec<TTuple<TVal, Vals> >& ValV, const TStr& FNm, const TStr& HeadLn = TStr());
00140   static void Test();
00141 
00142   // plot value-count tables, and pair vectors
00143   template <class TVal1, class TVal2>
00144   static void PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& OutFNmPref, const TStr& Desc="",
00145    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, 
00146    const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00147   template <class TVal1, class TVal2>
00148   static void PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV1, const TStr& Name1,
00149     const TVec<TPair<TVal1, TVal2> >& ValV2, const TStr& Name2, 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>
00153   static void PlotValV(const TVec<TVal1>& 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);
00156   template <class TKey, class TVal, class THashFunc>
00157   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH, const TStr& OutFNmPref, const TStr& Desc="",
00158    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, 
00159    const TGpSeriesTy& SeriesTy=gpwLinesPoints, const bool& PlotNCDF=false, const bool& ExpBucket=false);
00160   template <class TKey, class TVal, class THashFunc>
00161   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00162     const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00163     const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00164     const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00165   template <class TKey, class TVal, class THashFunc>
00166   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00167     const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00168     const THash<TKey, TVal, THashFunc>& ValCntH3, const TStr& Label3, 
00169     const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00170     const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00171   template <class TVal1>
00172   static void PlotValMomH(const THash<TVal1, TMom>& ValMomH, const TStr& OutFNmPref, const TStr& Desc="",
00173    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints,
00174    bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=true, bool PlotScatter=false);
00175   
00176 };
00177 
00178 //---------------------------------------------------------
00179 // useful commands
00180 // set terminal png small size 800,600
00181 // set terminal postscript enhanced eps 22
00182 // set output 'hopsasa.png'
00183 // set size 1,0.5
00184 // set pointsize 0.9
00185 // set key right bottom Left
00186 // set style fill solid 0.2
00187 // set ticscale 3 1   # longer axis tics
00188 // plot ... with points pointtype 6 pointsize 1
00189 // 1   +           7  (f) circle
00190 // 2  cross        8  (o) triangle
00191 // 3  *            9  (f) triangle
00192 // 4  (o) square  10  (o) inverse-triangle
00193 // 5  (f) square  11  (f) inverse-triangle
00194 // 6  (o) circle  12  (o) diamond
00195 //                13  (f) diamond
00196 // set label "Text" at 23,47000
00197 // set arrow from 28,45000 to 28,31000 lw 3
00198 // axes x1y2: http://t16web.lanl.gov/Kawano/gnuplot/plot1-e.html#5.2
00199 
00200 template <class TVal1, class TVal2>
00201 void TGnuPlot::SaveTs(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00202   FILE *F = fopen(FNm.CStr(), "wt");
00203   EAssert(F);
00204   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00205   for (int i = 0; i < ValV.Len(); i++) {
00206     fprintf(F, "%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2)); }
00207   fclose(F);
00208 }
00209 
00210 template <class TVal1, class TVal2, class TVal3>
00211 void TGnuPlot::SaveTs(const TVec<TTriple<TVal1, TVal2, TVal3> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00212   FILE *F = fopen(FNm.CStr(), "wt");
00213   EAssert(F);
00214   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00215   for (int i = 0; i < ValV.Len(); i++) {
00216     fprintf(F, "%g\t%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2), double(ValV[i].Val3)); }
00217   fclose(F);
00218 }
00219 
00220 template <class TVal, int Vals>
00221 void TGnuPlot::SaveTs(const TVec<TTuple<TVal, Vals> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00222   FILE *F = fopen(FNm.CStr(), "wt");
00223   EAssert(F);
00224   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00225   for (int i = 0; i < ValV.Len(); i++) {
00226     fprintf(F, "%g", double(ValV[i][0]));
00227     for (int v = 1; v < Vals; v++) {
00228       fprintf(F, "\t%g", double(ValV[i][v])); }
00229     fprintf(F, "\n");
00230   }
00231   fclose(F);
00232 }
00233 
00234 template<class TKey, class TDat, class THashFunc>
00235 int TGnuPlot::AddPlot(const THash<TKey, TDat, THashFunc>& XYValH, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style, const bool& ExpBucket) {
00236   TFltPrV XYFltValV(XYValH.Len(), 0);  
00237   for (int k = XYValH.FFirstKeyId();  XYValH.FNextKeyId(k); ) { 
00238     XYFltValV.Add(TFltPr(TFlt(XYValH.GetKey(k)), TFlt(XYValH[k]))); 
00239   }  
00240   XYFltValV.Sort();
00241   if (ExpBucket) {
00242     TFltPrV BucketV;
00243     TGnuPlot::MakeExpBins(XYFltValV, BucketV);
00244     BucketV.Swap(XYFltValV);
00245   }
00246   return AddPlot(XYFltValV, SeriesTy, Label, Style); 
00247 }
00248 
00249 template<class TKey, class THashFunc>
00250 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) {
00251   TFltTrV AvgV, StdErrV;
00252   TFltPrV AvgV2, MedV, MinV, MaxV, BucketV;
00253   for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) {
00254     TMom Mom(ValMomH[i]);
00255     if (! Mom.IsDef()) { Mom.Def(); }
00256     const double x = ValMomH.GetKey(i);
00257     if (PlotAvg) { 
00258       if (PlotSDev) { 
00259         AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00260       else { 
00261         AvgV2.Add(TFltPr(x, Mom.GetMean())); 
00262       }
00263       if (PlotStdErr) {
00264         StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); 
00265       }
00266     }
00267     if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); }
00268     if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); }
00269     if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); }
00270   }
00271   AvgV.Sort();  AvgV2.Sort();
00272   MedV.Sort();  MinV.Sort();  MaxV.Sort(); 
00273   int PlotId=0;
00274   // exponential bucketing
00275   if (! AvgV2.Empty()) { TGnuPlot::MakeExpBins(AvgV2, BucketV);  BucketV.Swap(AvgV2); }
00276   if (! MedV.Empty()) { TGnuPlot::MakeExpBins(MedV, BucketV);  BucketV.Swap(MedV); }
00277   if (! MinV.Empty()) { TGnuPlot::MakeExpBins(MinV, BucketV);  BucketV.Swap(MinV); }
00278   if (! MaxV.Empty()) { TGnuPlot::MakeExpBins(MaxV, BucketV);  BucketV.Swap(MaxV); }
00279   // plot
00280   if (! AvgV.Empty()) { PlotId = AddErrBar(AvgV, Label+" Average", "StdDev"); }
00281   if (! AvgV2.Empty()) { PlotId = AddPlot(AvgV2, SeriesTy, Label+" Average", Style); }
00282   if (! MedV.Empty()) { PlotId = AddPlot(MedV, SeriesTy, Label+" Median", Style); }
00283   if (! MinV.Empty()) { PlotId = AddPlot(MinV, SeriesTy, Label+" Min", Style); }
00284   if (! MaxV.Empty()) { PlotId = AddPlot(MaxV, SeriesTy, Label+" Max", Style); }
00285   if (! StdErrV.Empty()) { PlotId = AddErrBar(StdErrV, Label+" Standard error", Style); }
00286   return PlotId;
00287 }
00288 
00289 // plot value-count tables, and pair vectors
00290 template <class TKey, class TVal, class THashFunc>
00291 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH, const TStr& OutFNmPref, const TStr& Desc,
00292  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy, 
00293  const bool& PlotNCDF, const bool& ExpBucket) {
00294   TFltPrV IdCntV(ValCntH.Len(), 0), BucketV;
00295   for (int i = ValCntH.FFirstKeyId(); ValCntH.FNextKeyId(i); ) {
00296     IdCntV.Add(TFltPr(double(ValCntH.GetKey(i)), double(ValCntH[i]))); }
00297   IdCntV.Sort();
00298   if (ExpBucket) { 
00299     TGnuPlot::MakeExpBins(IdCntV, BucketV);
00300     BucketV.Swap(IdCntV);
00301   }
00302   if (PlotNCDF) { 
00303     TFltPrV NCdfV = IdCntV;
00304     for (int i = NCdfV.Len()-2; i >= 0; i--) {
00305       NCdfV[i].Val2 = NCdfV[i].Val2 + NCdfV[i+1].Val2; 
00306     }
00307     PlotValV(NCdfV, OutFNmPref, Desc, "NCDF "+XLabel, "NCDF "+YLabel, ScaleTy, PowerFit, SeriesTy);
00308   } else {
00309     PlotValV(IdCntV, OutFNmPref, Desc, XLabel, YLabel, ScaleTy, PowerFit, SeriesTy); 
00310   }
00311 }
00312 
00313 template <class TKey, class TVal, class THashFunc>
00314 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00315                            const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00316                            const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00317                            const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) {
00318   PlotValCntH(ValCntH1, Label1, ValCntH2, Label2, THash<TKey, TVal, THashFunc>(), "", OutFNmPref, Desc, XLabel, YLabel,
00319     ScaleTy, SeriesTy);
00320 }
00321 
00322 template <class TKey, class TVal, class THashFunc>
00323 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00324                            const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00325                            const THash<TKey, TVal, THashFunc>& ValCntH3, const TStr& Label3, 
00326                            const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00327                            const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) {
00328   TFltPrV IdCntV1(ValCntH1.Len(), 0), IdCntV2(ValCntH2.Len(), 0), IdCntV3(ValCntH3.Len(), 0);
00329   for (int i = ValCntH1.FFirstKeyId(); ValCntH1.FNextKeyId(i); ) {
00330     IdCntV1.Add(TFltPr(double(ValCntH1.GetKey(i)), double(ValCntH1[i]))); }
00331   for (int i = ValCntH2.FFirstKeyId(); ValCntH2.FNextKeyId(i); ) {
00332     IdCntV2.Add(TFltPr(double(ValCntH2.GetKey(i)), double(ValCntH2[i]))); }
00333   for (int i = ValCntH3.FFirstKeyId(); ValCntH3.FNextKeyId(i); ) {
00334     IdCntV3.Add(TFltPr(double(ValCntH3.GetKey(i)), double(ValCntH3[i]))); }
00335   IdCntV1.Sort();
00336   IdCntV2.Sort();
00337   IdCntV3.Sort();
00338   TGnuPlot GP(OutFNmPref, Desc);
00339   GP.SetXYLabel(XLabel, YLabel);
00340   GP.SetScale(ScaleTy);
00341   if (! IdCntV1.Empty()) { GP.AddPlot(IdCntV1, SeriesTy, Label1); }
00342   if (! IdCntV2.Empty()) { GP.AddPlot(IdCntV2, SeriesTy, Label2); }
00343   if (! IdCntV3.Empty()) { GP.AddPlot(IdCntV3, SeriesTy, Label3); }
00344   GP.SavePng();
00345 }
00346 
00347 template <class TVal1, class TVal2>
00348 void TGnuPlot::PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& OutFNmPref, const TStr& Desc,
00349  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00350   TFltKdV IdCntV(ValV.Len(), 0);
00351   for (int i = 0; i < ValV.Len(); i++) {
00352     IdCntV.Add(TFltKd(double(ValV[i].Val1), double(ValV[i].Val2))); }
00353   if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00354   IdCntV.Sort();
00355   TGnuPlot GP(OutFNmPref, Desc);
00356   GP.SetXYLabel(XLabel, YLabel);
00357   GP.SetScale(ScaleTy);
00358   const int Id = GP.AddPlot(IdCntV, SeriesTy);
00359   if (PowerFit) { 
00360     GP.AddPwrFit3(Id);
00361     double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat;
00362     if (MaxY < MinY) { Swap(MaxY, MinY); }
00363     //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0));
00364     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00365   }
00366   GP.SavePng();
00367 }
00368 
00369 template <class TVal1, class TVal2>
00370 void TGnuPlot::PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV1, const TStr& Name1, 
00371  const TVec<TPair<TVal1, TVal2> >& ValV2, const TStr& Name2, const TStr& OutFNmPref, const TStr& Desc,
00372  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00373   TFltKdV IdCntV1(ValV1.Len(), 0), IdCntV2(ValV2.Len(), 0);
00374   for (int i = 0; i < ValV1.Len(); i++) {
00375     IdCntV1.Add(TFltKd(double(ValV1[i].Val1), double(ValV1[i].Val2))); }
00376   for (int i = 0; i < ValV2.Len(); i++) {
00377     IdCntV2.Add(TFltKd(double(ValV2[i].Val1), double(ValV2[i].Val2))); }
00378   if (IdCntV1.Empty() || IdCntV2.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00379   IdCntV1.Sort();
00380   IdCntV2.Sort();
00381   TGnuPlot GP(OutFNmPref, Desc);
00382   GP.SetXYLabel(XLabel, YLabel);
00383   GP.SetScale(ScaleTy);
00384   { const int Id = GP.AddPlot(IdCntV1, SeriesTy, Name1);
00385   if (PowerFit) { 
00386     GP.AddPwrFit3(Id);
00387     double MaxY = IdCntV1.Last().Dat, MinY = IdCntV1[0].Dat;
00388     if (MaxY < MinY) { Swap(MaxY, MinY); }
00389     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00390   } }
00391   { const int Id = GP.AddPlot(IdCntV2, SeriesTy, Name2);
00392   if (PowerFit) { 
00393     GP.AddPwrFit3(Id);
00394     double MaxY = IdCntV2.Last().Dat, MinY = IdCntV2[0].Dat;
00395     if (MaxY < MinY) { Swap(MaxY, MinY); }
00396     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00397   } }
00398   GP.SavePng();
00399 }
00400 
00401            
00402 
00403 template <class TVal1>
00404 void TGnuPlot::PlotValV(const TVec<TVal1>& ValV, const TStr& OutFNmPref, const TStr& Desc,
00405  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00406   TFltKdV IdCntV(ValV.Len(), 0);
00407   for (int i = 0; i < ValV.Len(); i++) {
00408     IdCntV.Add(TFltKd(double(i+1), double(ValV[i]))); }
00409   if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00410   IdCntV.Sort();
00411   TGnuPlot GP(OutFNmPref, Desc);
00412   GP.SetXYLabel(XLabel, YLabel);
00413   GP.SetScale(ScaleTy);
00414   const int Id = GP.AddPlot(IdCntV, SeriesTy);
00415   if (PowerFit) { 
00416     GP.AddPwrFit3(Id);
00417     double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat;
00418     if (MaxY < MinY) { Swap(MaxY, MinY); }
00419     //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0));
00420     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00421   }
00422   GP.SavePng();
00423 }
00424 
00425 template <class TVal1>
00426 void TGnuPlot::PlotValMomH(const THash<TVal1, TMom>& ValMomH, const TStr& OutFNmPref, const TStr& Desc,
00427  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy,
00428  bool PlotAvg, bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, bool PlotScatter) {
00429   TFltTrV AvgV, StdErrV;
00430   TFltPrV AvgV2, MedV, MinV, MaxV;
00431   TFltPrV ScatterV;
00432   for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) {
00433     TMom Mom(ValMomH[i]);
00434     if (! Mom.IsDef()) { Mom.Def(); }
00435     const double x = ValMomH.GetKey(i);
00436     if (PlotAvg) { 
00437       if (PlotSDev) { 
00438         AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00439       else { 
00440         AvgV2.Add(TFltPr(x, Mom.GetMean())); 
00441       }
00442       if (PlotStdErr) {
00443         StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); 
00444       }
00445     }
00446     if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); }
00447     if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); }
00448     if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); }
00449     if (PlotScatter) {
00450       THashSet<TFlt> PointSet;
00451       for (int xi = 0; xi < ValMomH[i].GetVals(); xi++) {
00452         PointSet.AddKey(ValMomH[i].GetVal(xi)); }
00453         //ScatterV.Add(TFltPr(x, ValMomH[i].GetVal(xi)));  }
00454       for (int xi = 0; xi < PointSet.Len(); xi++) {
00455         ScatterV.Add(TFltPr(x, PointSet[xi]));  }
00456       //printf("S%d %d %d.", ValMomH[i].GetVals(), PointSet.Len(), ScatterV.Len());
00457     }
00458   }
00459   AvgV.Sort();  AvgV2.Sort();
00460   MedV.Sort();  MinV.Sort();  MaxV.Sort();  StdErrV.Sort();
00461   TGnuPlot GP(OutFNmPref, Desc);
00462   GP.SetScale(ScaleTy);
00463   GP.SetXYLabel(XLabel, YLabel);
00464   if (! ScatterV.Empty()) { GP.AddPlot(ScatterV, gpwPoints, "Scatter"); }
00465   if (! AvgV.Empty()) { GP.AddErrBar(AvgV, "Average", "StdDev"); }
00466   if (! AvgV2.Empty()) { GP.AddPlot(AvgV2, SeriesTy, "Average"); }
00467   if (! MedV.Empty()) { GP.AddPlot(MedV, SeriesTy, "Median"); }
00468   if (! MinV.Empty()) { GP.AddPlot(MinV, SeriesTy, "Min"); }
00469   if (! MaxV.Empty()) { GP.AddPlot(MaxV, SeriesTy, "Max"); }
00470   if (! StdErrV.Empty()) { GP.AddErrBar(StdErrV, "Standard error"); }
00471   GP.SavePng();
00472 }
00473 
00474 #endif
00475