SNAP Library 2.1, Developer Reference  2013-09-25 10:47:25
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>
00153   static void PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV1, const TStr& Name1,
00154     const TVec<TPair<TVal1, TVal2> >& ValV2, const TStr& Name2, const TStr& OutFNmPref, const TStr& Desc="",
00155    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const bool& PowerFit=false, 
00156    const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00157   template <class TVal1>
00158   static void PlotValV(const TVec<TVal1>& ValV, 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 TKey, class TVal, class THashFunc>
00162   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH, 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, const bool& PlotNCDF=false, const bool& ExpBucket=false);
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 TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00169     const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00170   template <class TKey, class TVal, class THashFunc>
00171   static void PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00172     const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00173     const THash<TKey, TVal, THashFunc>& ValCntH3, const TStr& Label3, 
00174     const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00175     const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints);
00176   template <class TVal1>
00177   static void PlotValMomH(const THash<TVal1, TMom>& ValMomH, const TStr& OutFNmPref, const TStr& Desc="",
00178    const TStr& XLabel="", const TStr& YLabel="", const TGpScaleTy& ScaleTy=gpsAuto, const TGpSeriesTy& SeriesTy=gpwLinesPoints,
00179    bool PlotAvg=true, bool PlotMed=true, bool PlotMin=false, bool PlotMax=false, bool PlotSDev=false, bool PlotStdErr=true, bool PlotScatter=false);
00180   
00181 };
00182 
00183 //---------------------------------------------------------
00184 // useful commands
00185 // set terminal png small size 800,600
00186 // set terminal postscript enhanced eps 22
00187 // set output 'hopsasa.png'
00188 // set size 1,0.5
00189 // set pointsize 0.9
00190 // set key right bottom Left
00191 // set style fill solid 0.2
00192 // set ticscale 3 1   # longer axis tics
00193 // plot ... with points pointtype 6 pointsize 1
00194 // 1   +           7  (f) circle
00195 // 2  cross        8  (o) triangle
00196 // 3  *            9  (f) triangle
00197 // 4  (o) square  10  (o) inverse-triangle
00198 // 5  (f) square  11  (f) inverse-triangle
00199 // 6  (o) circle  12  (o) diamond
00200 //                13  (f) diamond
00201 // set label "Text" at 23,47000
00202 // set arrow from 28,45000 to 28,31000 lw 3
00203 // axes x1y2: http://t16web.lanl.gov/Kawano/gnuplot/plot1-e.html#5.2
00204 
00205 template <class TVal1, class TVal2>
00206 void TGnuPlot::SaveTs(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00207   FILE *F = fopen(FNm.CStr(), "wt");
00208   EAssert(F);
00209   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00210   for (int i = 0; i < ValV.Len(); i++) {
00211     fprintf(F, "%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2)); }
00212   fclose(F);
00213 }
00214 
00215 template <class TVal1, class TVal2, class TVal3>
00216 void TGnuPlot::SaveTs(const TVec<TTriple<TVal1, TVal2, TVal3> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00217   FILE *F = fopen(FNm.CStr(), "wt");
00218   EAssert(F);
00219   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00220   for (int i = 0; i < ValV.Len(); i++) {
00221     fprintf(F, "%g\t%g\t%g\n", double(ValV[i].Val1), double(ValV[i].Val2), double(ValV[i].Val3)); }
00222   fclose(F);
00223 }
00224 
00225 template <class TVal, int Vals>
00226 void TGnuPlot::SaveTs(const TVec<TTuple<TVal, Vals> >& ValV, const TStr& FNm, const TStr& HeadLn) {
00227   FILE *F = fopen(FNm.CStr(), "wt");
00228   EAssert(F);
00229   if (! HeadLn.Empty()) { fprintf(F, "# %s\n", HeadLn.CStr()); }
00230   for (int i = 0; i < ValV.Len(); i++) {
00231     fprintf(F, "%g", double(ValV[i][0]));
00232     for (int v = 1; v < Vals; v++) {
00233       fprintf(F, "\t%g", double(ValV[i][v])); }
00234     fprintf(F, "\n");
00235   }
00236   fclose(F);
00237 }
00238 
00239 template<class TKey, class TDat, class THashFunc>
00240 int TGnuPlot::AddPlot(const THash<TKey, TDat, THashFunc>& XYValH, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style, const bool& ExpBucket) {
00241   TFltPrV XYFltValV(XYValH.Len(), 0);  
00242   for (int k = XYValH.FFirstKeyId();  XYValH.FNextKeyId(k); ) { 
00243     XYFltValV.Add(TFltPr(TFlt(XYValH.GetKey(k)), TFlt(XYValH[k]))); 
00244   }  
00245   XYFltValV.Sort();
00246   if (ExpBucket) {
00247     TFltPrV BucketV;
00248     TGnuPlot::MakeExpBins(XYFltValV, BucketV);
00249     BucketV.Swap(XYFltValV);
00250   }
00251   return AddPlot(XYFltValV, SeriesTy, Label, Style); 
00252 }
00253 
00254 template<class TKey, class THashFunc>
00255 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) {
00256   TFltTrV AvgV, StdErrV;
00257   TFltPrV AvgV2, MedV, MinV, MaxV, BucketV;
00258   for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) {
00259     TMom Mom(ValMomH[i]);
00260     if (! Mom.IsDef()) { Mom.Def(); }
00261     const double x = ValMomH.GetKey(i);
00262     if (PlotAvg) { 
00263       if (PlotSDev) { 
00264         AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00265       else { 
00266         AvgV2.Add(TFltPr(x, Mom.GetMean())); 
00267       }
00268       if (PlotStdErr) {
00269         StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); 
00270       }
00271     }
00272     if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); }
00273     if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); }
00274     if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); }
00275   }
00276   AvgV.Sort();  AvgV2.Sort();
00277   MedV.Sort();  MinV.Sort();  MaxV.Sort(); 
00278   int PlotId=0;
00279   // exponential bucketing
00280   if (ExpBucket) {
00281     if (! AvgV2.Empty()) { TGnuPlot::MakeExpBins(AvgV2, BucketV);  BucketV.Swap(AvgV2); }
00282     if (! MedV.Empty()) { TGnuPlot::MakeExpBins(MedV, BucketV);  BucketV.Swap(MedV); }
00283     if (! MinV.Empty()) { TGnuPlot::MakeExpBins(MinV, BucketV);  BucketV.Swap(MinV); }
00284     if (! MaxV.Empty()) { TGnuPlot::MakeExpBins(MaxV, BucketV);  BucketV.Swap(MaxV); }
00285   }
00286   // plot
00287   if (! AvgV.Empty()) { PlotId = AddErrBar(AvgV, Label+" Average", "StdDev"); }
00288   if (! AvgV2.Empty()) { PlotId = AddPlot(AvgV2, SeriesTy, Label+" Average", Style); }
00289   if (! MedV.Empty()) { PlotId = AddPlot(MedV, SeriesTy, Label+" Median", Style); }
00290   if (! MinV.Empty()) { PlotId = AddPlot(MinV, SeriesTy, Label+" Min", Style); }
00291   if (! MaxV.Empty()) { PlotId = AddPlot(MaxV, SeriesTy, Label+" Max", Style); }
00292   if (! StdErrV.Empty()) { PlotId = AddErrBar(StdErrV, Label+" Standard error", Style); }
00293   return PlotId;
00294 }
00295 
00296 // plot value-count tables, and pair vectors
00297 template <class TKey, class TVal, class THashFunc>
00298 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH, const TStr& OutFNmPref, const TStr& Desc,
00299  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy, 
00300  const bool& PlotNCDF, const bool& ExpBucket) {
00301   TFltPrV IdCntV(ValCntH.Len(), 0), BucketV;
00302   for (int i = ValCntH.FFirstKeyId(); ValCntH.FNextKeyId(i); ) {
00303     IdCntV.Add(TFltPr(double(ValCntH.GetKey(i)), double(ValCntH[i]))); }
00304   IdCntV.Sort();
00305   if (ExpBucket) { 
00306     TGnuPlot::MakeExpBins(IdCntV, BucketV);
00307     BucketV.Swap(IdCntV);
00308   }
00309   if (PlotNCDF) { 
00310     TFltPrV NCdfV = IdCntV;
00311     for (int i = NCdfV.Len()-2; i >= 0; i--) {
00312       NCdfV[i].Val2 = NCdfV[i].Val2 + NCdfV[i+1].Val2; 
00313     }
00314     PlotValV(NCdfV, OutFNmPref, Desc, "NCDF "+XLabel, "NCDF "+YLabel, ScaleTy, PowerFit, SeriesTy);
00315   } else {
00316     PlotValV(IdCntV, OutFNmPref, Desc, XLabel, YLabel, ScaleTy, PowerFit, SeriesTy); 
00317   }
00318 }
00319 
00320 template <class TKey, class TVal, class THashFunc>
00321 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00322                            const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00323                            const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00324                            const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) {
00325   PlotValCntH(ValCntH1, Label1, ValCntH2, Label2, THash<TKey, TVal, THashFunc>(), "", OutFNmPref, Desc, XLabel, YLabel,
00326     ScaleTy, SeriesTy);
00327 }
00328 
00329 template <class TKey, class TVal, class THashFunc>
00330 void TGnuPlot::PlotValCntH(const THash<TKey, TVal, THashFunc>& ValCntH1, const TStr& Label1, 
00331                            const THash<TKey, TVal, THashFunc>& ValCntH2, const TStr& Label2, 
00332                            const THash<TKey, TVal, THashFunc>& ValCntH3, const TStr& Label3, 
00333                            const TStr& OutFNmPref, const TStr& Desc, const TStr& XLabel, const TStr& YLabel, 
00334                            const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy) {
00335   TFltPrV IdCntV1(ValCntH1.Len(), 0), IdCntV2(ValCntH2.Len(), 0), IdCntV3(ValCntH3.Len(), 0);
00336   for (int i = ValCntH1.FFirstKeyId(); ValCntH1.FNextKeyId(i); ) {
00337     IdCntV1.Add(TFltPr(double(ValCntH1.GetKey(i)), double(ValCntH1[i]))); }
00338   for (int i = ValCntH2.FFirstKeyId(); ValCntH2.FNextKeyId(i); ) {
00339     IdCntV2.Add(TFltPr(double(ValCntH2.GetKey(i)), double(ValCntH2[i]))); }
00340   for (int i = ValCntH3.FFirstKeyId(); ValCntH3.FNextKeyId(i); ) {
00341     IdCntV3.Add(TFltPr(double(ValCntH3.GetKey(i)), double(ValCntH3[i]))); }
00342   IdCntV1.Sort();
00343   IdCntV2.Sort();
00344   IdCntV3.Sort();
00345   TGnuPlot GP(OutFNmPref, Desc);
00346   GP.SetXYLabel(XLabel, YLabel);
00347   GP.SetScale(ScaleTy);
00348   if (! IdCntV1.Empty()) { GP.AddPlot(IdCntV1, SeriesTy, Label1); }
00349   if (! IdCntV2.Empty()) { GP.AddPlot(IdCntV2, SeriesTy, Label2); }
00350   if (! IdCntV3.Empty()) { GP.AddPlot(IdCntV3, SeriesTy, Label3); }
00351   GP.SavePng();
00352 }
00353 
00354 template <class TVal1, class TVal2>
00355 void TGnuPlot::PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV, const TStr& OutFNmPref, const TStr& Desc,
00356  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00357   TFltKdV IdCntV(ValV.Len(), 0);
00358   for (int i = 0; i < ValV.Len(); i++) {
00359     IdCntV.Add(TFltKd(double(ValV[i].Val1), double(ValV[i].Val2))); }
00360   if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00361   IdCntV.Sort();
00362   TGnuPlot GP(OutFNmPref, Desc);
00363   GP.SetXYLabel(XLabel, YLabel);
00364   GP.SetScale(ScaleTy);
00365   const int Id = GP.AddPlot(IdCntV, SeriesTy);
00366   if (PowerFit) { 
00367     GP.AddPwrFit3(Id);
00368     double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat;
00369     if (MaxY < MinY) { Swap(MaxY, MinY); }
00370     //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0));
00371     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00372   }
00373   GP.SavePng();
00374 }
00375 
00376 template <class TVal1, class TVal2>
00377 void TGnuPlot::PlotValV(const TVec<TPair<TVal1, TVal2> >& ValV1, const TStr& Name1, 
00378  const TVec<TPair<TVal1, TVal2> >& ValV2, const TStr& Name2, const TStr& OutFNmPref, const TStr& Desc,
00379  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00380   TFltKdV IdCntV1(ValV1.Len(), 0), IdCntV2(ValV2.Len(), 0);
00381   for (int i = 0; i < ValV1.Len(); i++) {
00382     IdCntV1.Add(TFltKd(double(ValV1[i].Val1), double(ValV1[i].Val2))); }
00383   for (int i = 0; i < ValV2.Len(); i++) {
00384     IdCntV2.Add(TFltKd(double(ValV2[i].Val1), double(ValV2[i].Val2))); }
00385   if (IdCntV1.Empty() || IdCntV2.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00386   IdCntV1.Sort();
00387   IdCntV2.Sort();
00388   TGnuPlot GP(OutFNmPref, Desc);
00389   GP.SetXYLabel(XLabel, YLabel);
00390   GP.SetScale(ScaleTy);
00391   { const int Id = GP.AddPlot(IdCntV1, SeriesTy, Name1);
00392   if (PowerFit) { 
00393     GP.AddPwrFit3(Id);
00394     double MaxY = IdCntV1.Last().Dat, MinY = IdCntV1[0].Dat;
00395     if (MaxY < MinY) { Swap(MaxY, MinY); }
00396     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00397   } }
00398   { const int Id = GP.AddPlot(IdCntV2, SeriesTy, Name2);
00399   if (PowerFit) { 
00400     GP.AddPwrFit3(Id);
00401     double MaxY = IdCntV2.Last().Dat, MinY = IdCntV2[0].Dat;
00402     if (MaxY < MinY) { Swap(MaxY, MinY); }
00403     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00404   } }
00405   GP.SavePng();
00406 }
00407 
00408            
00409 
00410 template <class TVal1>
00411 void TGnuPlot::PlotValV(const TVec<TVal1>& ValV, const TStr& OutFNmPref, const TStr& Desc,
00412  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const bool& PowerFit, const TGpSeriesTy& SeriesTy) {
00413   TFltKdV IdCntV(ValV.Len(), 0);
00414   for (int i = 0; i < ValV.Len(); i++) {
00415     IdCntV.Add(TFltKd(double(i+1), double(ValV[i]))); }
00416   if (IdCntV.Empty()) { printf("*** Empty plot %s\n", OutFNmPref.CStr());  return; }
00417   IdCntV.Sort();
00418   TGnuPlot GP(OutFNmPref, Desc);
00419   GP.SetXYLabel(XLabel, YLabel);
00420   GP.SetScale(ScaleTy);
00421   const int Id = GP.AddPlot(IdCntV, SeriesTy);
00422   if (PowerFit) { 
00423     GP.AddPwrFit3(Id);
00424     double MaxY = IdCntV.Last().Dat, MinY = IdCntV[0].Dat;
00425     if (MaxY < MinY) { Swap(MaxY, MinY); }
00426     //GP.SetYRange(MinY, pow(10.0, floor(log10(MaxY))+1.0));
00427     GP.AddCmd(TStr::Fmt("set yrange[%f:]", MinY));
00428   }
00429   GP.SavePng();
00430 }
00431 
00432 template <class TVal1>
00433 void TGnuPlot::PlotValMomH(const THash<TVal1, TMom>& ValMomH, const TStr& OutFNmPref, const TStr& Desc,
00434  const TStr& XLabel, const TStr& YLabel, const TGpScaleTy& ScaleTy, const TGpSeriesTy& SeriesTy,
00435  bool PlotAvg, bool PlotMed, bool PlotMin, bool PlotMax, bool PlotSDev, bool PlotStdErr, bool PlotScatter) {
00436   TFltTrV AvgV, StdErrV;
00437   TFltPrV AvgV2, MedV, MinV, MaxV;
00438   TFltPrV ScatterV;
00439   for (int i = ValMomH.FFirstKeyId(); ValMomH.FNextKeyId(i); ) {
00440     TMom Mom(ValMomH[i]);
00441     if (! Mom.IsDef()) { Mom.Def(); }
00442     const double x = ValMomH.GetKey(i);
00443     if (PlotAvg) { 
00444       if (PlotSDev) { 
00445         AvgV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev())); } // std deviation
00446       else { 
00447         AvgV2.Add(TFltPr(x, Mom.GetMean())); 
00448       }
00449       if (PlotStdErr) {
00450         StdErrV.Add(TFltTr(x, Mom.GetMean(), Mom.GetSDev()/sqrt((double)Mom.GetVals()))); 
00451       }
00452     }
00453     if (PlotMed) { MedV.Add(TFltPr(x, Mom.GetMedian())); }
00454     if (PlotMin) { MinV.Add(TFltPr(x, Mom.GetMn())); }
00455     if (PlotMax) { MaxV.Add(TFltPr(x, Mom.GetMx())); }
00456     if (PlotScatter) {
00457       THashSet<TFlt> PointSet;
00458       for (int xi = 0; xi < ValMomH[i].GetVals(); xi++) {
00459         PointSet.AddKey(ValMomH[i].GetVal(xi)); }
00460         //ScatterV.Add(TFltPr(x, ValMomH[i].GetVal(xi)));  }
00461       for (int xi = 0; xi < PointSet.Len(); xi++) {
00462         ScatterV.Add(TFltPr(x, PointSet[xi]));  }
00463       //printf("S%d %d %d.", ValMomH[i].GetVals(), PointSet.Len(), ScatterV.Len());
00464     }
00465   }
00466   AvgV.Sort();  AvgV2.Sort();
00467   MedV.Sort();  MinV.Sort();  MaxV.Sort();  StdErrV.Sort();
00468   TGnuPlot GP(OutFNmPref, Desc);
00469   GP.SetScale(ScaleTy);
00470   GP.SetXYLabel(XLabel, YLabel);
00471   if (! ScatterV.Empty()) { GP.AddPlot(ScatterV, gpwPoints, "Scatter"); }
00472   if (! AvgV.Empty()) { GP.AddErrBar(AvgV, "Average", "StdDev"); }
00473   if (! AvgV2.Empty()) { GP.AddPlot(AvgV2, SeriesTy, "Average"); }
00474   if (! MedV.Empty()) { GP.AddPlot(MedV, SeriesTy, "Median"); }
00475   if (! MinV.Empty()) { GP.AddPlot(MinV, SeriesTy, "Min"); }
00476   if (! MaxV.Empty()) { GP.AddPlot(MaxV, SeriesTy, "Max"); }
00477   if (! StdErrV.Empty()) { GP.AddErrBar(StdErrV, "Standard error"); }
00478   GP.SavePng();
00479 }
00480 
00481 #endif
00482