| 1 | // Classe traceur d histogramme                 96-99
 | 
|---|
| 2 | // CEA-DAPNIA      LAL-IN2P3/CNRS
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include <stdio.h>
 | 
|---|
| 5 | #include <stdlib.h>
 | 
|---|
| 6 | #include <iostream>
 | 
|---|
| 7 | #include <math.h>
 | 
|---|
| 8 | #include <typeinfo>
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "sopnamsp.h"
 | 
|---|
| 11 | #include "pihisto.h"
 | 
|---|
| 12 | #include "hisprof.h"
 | 
|---|
| 13 | 
 | 
|---|
| 14 | //++
 | 
|---|
| 15 | // Class        PIHisto
 | 
|---|
| 16 | // Lib          PIext
 | 
|---|
| 17 | // include      pihisto.h
 | 
|---|
| 18 | //
 | 
|---|
| 19 | //      Classe traceur d'objet histogramme (classe *Histo*)
 | 
|---|
| 20 | //--
 | 
|---|
| 21 | //++
 | 
|---|
| 22 | // Links        Parents
 | 
|---|
| 23 | // PIDrawer
 | 
|---|
| 24 | //--
 | 
|---|
| 25 | //++
 | 
|---|
| 26 | // Titre        Constructeur, méthodes
 | 
|---|
| 27 | //--
 | 
|---|
| 28 | //++
 | 
|---|
| 29 | // PIHisto(Histo* histo, bool ad=false)
 | 
|---|
| 30 | //      Constructeur. Si "ad == true", l'objet "histo" est détruit par 
 | 
|---|
| 31 | //      le destructeur de l'objet "PIHisto"
 | 
|---|
| 32 | //      Note : "histo" doit être créé par new
 | 
|---|
| 33 | //
 | 
|---|
| 34 | // void  SetStats(bool fg=true) 
 | 
|---|
| 35 | //      Active/ désactive l'indication des statistiques d'histogramme
 | 
|---|
| 36 | //--
 | 
|---|
| 37 | 
 | 
|---|
| 38 | 
 | 
|---|
| 39 | PIHisto::PIHisto(Histo* histo, bool ad)
 | 
|---|
| 40 | : PIDrawer(), mHisto(histo)
 | 
|---|
| 41 | {
 | 
|---|
| 42 |   mAdDO = ad;     // Flag pour suppression automatique de mHisto
 | 
|---|
| 43 |   SetStats();
 | 
|---|
| 44 |   SetError();
 | 
|---|
| 45 |   SetFilled();
 | 
|---|
| 46 |   SetStatPosOffset();
 | 
|---|
| 47 |   SetName("HistoDrw");
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | PIHisto::~PIHisto()
 | 
|---|
| 51 | {
 | 
|---|
| 52 |   if(mAdDO) delete mHisto;
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | void
 | 
|---|
| 56 | PIHisto::UpdateLimits()
 | 
|---|
| 57 | {
 | 
|---|
| 58 |   if (!mHisto)  return;
 | 
|---|
| 59 |   float hmin = mHisto->VMin();
 | 
|---|
| 60 |   float hmax = mHisto->VMax()+0.2*(mHisto->VMax()-mHisto->VMin());
 | 
|---|
| 61 |   // si HBProf min,max calcules en tenant compte des erreurs
 | 
|---|
| 62 |   if( typeid(*mHisto) == typeid(HProf) ) {
 | 
|---|
| 63 |     float v1,v2;
 | 
|---|
| 64 |     for (int i=1; i<mHisto->NBins(); i++) {
 | 
|---|
| 65 |       v1 = (*mHisto)(i) - mHisto->Error(i);
 | 
|---|
| 66 |       v2 = (*mHisto)(i) + mHisto->Error(i);
 | 
|---|
| 67 |       if(v1<hmin) hmin = v1;
 | 
|---|
| 68 |       if(v2>hmax) hmax = v2;
 | 
|---|
| 69 |     }
 | 
|---|
| 70 |     v1 = 0.1*(hmax-hmin);
 | 
|---|
| 71 |     hmin -= v1;   hmax += v1;
 | 
|---|
| 72 |   }
 | 
|---|
| 73 |   if(hmax<=hmin) hmax = hmin+1.;
 | 
|---|
| 74 |   SetLimits(mHisto->XMin(), mHisto->XMax(), hmin, hmax);
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | void
 | 
|---|
| 78 | PIHisto::Draw(PIGraphicUC* g, double /*xmin*/, double /*ymin*/, double /*xmax*/, double /*ymax*/)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   if (axesFlags != kAxesNone) DrawAxes(g);
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   if (!mHisto)  return;
 | 
|---|
| 83 |  
 | 
|---|
| 84 |   if( typeid(*mHisto)==typeid(HProf) ) mHisto->UpdateHisto();
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   bool oktrace=false;
 | 
|---|
| 87 |   // Tracer d'une polyline si demandee
 | 
|---|
| 88 |   bool drawpline=false;
 | 
|---|
| 89 |   if(GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt) drawpline=true;
 | 
|---|
| 90 |   // Tracer des marqueurs si demande ou si HProf
 | 
|---|
| 91 |   bool drawmarker=false;
 | 
|---|
| 92 |   if( (GetGraphicAtt().GetMarker() != PI_NotDefMarker) ||  
 | 
|---|
| 93 |       (typeid(*mHisto) == typeid(HProf)) ) drawmarker = true;
 | 
|---|
| 94 |   // Tracer des erreurs ?
 | 
|---|
| 95 |   bool drawerr=false;
 | 
|---|
| 96 |   if(error==0) {  // Gestion automatique des erreurs
 | 
|---|
| 97 |     // Tracer les erreurs si HProf
 | 
|---|
| 98 |     if( typeid(*mHisto)==typeid(HProf) ) drawerr=true;
 | 
|---|
| 99 |     // Trace les erreurs si marqueurs demandes
 | 
|---|
| 100 |     if(drawmarker) drawerr=true;
 | 
|---|
| 101 |   }
 | 
|---|
| 102 |   else if(error>0) drawerr=true;
 | 
|---|
| 103 |   else if(error<0) drawerr=false;
 | 
|---|
| 104 |   // Fill de l'histo ?
 | 
|---|
| 105 |   bool drawfill=false;
 | 
|---|
| 106 |   if(filled) drawfill=true; else drawfill=false;
 | 
|---|
| 107 |   // Et aussi si on ne demande ni ligne ni marqueur ?
 | 
|---|
| 108 |   if( !drawmarker && !drawpline && !drawerr ) drawfill=true;
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   // Remplissage des bins avec la couleur courante (trace en premier)
 | 
|---|
| 111 |   if(drawfill) {
 | 
|---|
| 112 |     oktrace = true;
 | 
|---|
| 113 |     for(int i=0; i<mHisto->NBins(); i++) {
 | 
|---|
| 114 |       double left   = mHisto->BinLowEdge(i);
 | 
|---|
| 115 |       double width  = mHisto->BinWidth();
 | 
|---|
| 116 |       double bottom = 0;
 | 
|---|
| 117 |       double height = (*mHisto)(i);
 | 
|---|
| 118 |       g->DrawFBox(left,bottom,width,height);
 | 
|---|
| 119 |     }
 | 
|---|
| 120 |   }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   // Trace des marqeurs
 | 
|---|
| 123 |   if(drawmarker) {
 | 
|---|
| 124 |     double x1,y1; oktrace = true;
 | 
|---|
| 125 |     for(int i=0; i<mHisto->NBins(); i++) {
 | 
|---|
| 126 |       x1 = mHisto->BinCenter(i);
 | 
|---|
| 127 |       y1 = (*mHisto)(i);
 | 
|---|
| 128 |       g->DrawMarker(x1,y1);
 | 
|---|
| 129 |     }
 | 
|---|
| 130 |   }
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   // Trace des erreurs
 | 
|---|
| 133 |   if(drawerr) {
 | 
|---|
| 134 |     if(GetGraphicAtt().GetLineAtt()==PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
 | 
|---|
| 135 |     double x1,x2,y1,y2;  oktrace = true;
 | 
|---|
| 136 |     double bw = mHisto->BinWidth();
 | 
|---|
| 137 |     for(int i=0; i<mHisto->NBins(); i++) {
 | 
|---|
| 138 |       if(mHisto->Error(i)>0.) {
 | 
|---|
| 139 |         // barres d'erreur verticales
 | 
|---|
| 140 |         x1 = x2 = mHisto->BinCenter(i);
 | 
|---|
| 141 |         y1 = (*mHisto)(i) - mHisto->Error(i);
 | 
|---|
| 142 |         y2 = (*mHisto)(i) + mHisto->Error(i);
 | 
|---|
| 143 |         g->DrawLine(x1,y1, x1, y2);
 | 
|---|
| 144 |         // limites de barres d'erreurs (horizontales)
 | 
|---|
| 145 |         x1 -= bw/3.;  x2 += bw/3.;
 | 
|---|
| 146 |         g->DrawLine(x1,y1, x2, y1);
 | 
|---|
| 147 |         g->DrawLine(x1,y2, x2, y2);
 | 
|---|
| 148 |       }
 | 
|---|
| 149 |     }
 | 
|---|
| 150 |   }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |   // Trace de la ligne continue si demandee
 | 
|---|
| 153 |   if(drawpline) {
 | 
|---|
| 154 |     PIGrCoord* x1 = new PIGrCoord[2*mHisto->NBins()+2];
 | 
|---|
| 155 |     PIGrCoord* y1 = new PIGrCoord[2*mHisto->NBins()+2];
 | 
|---|
| 156 |     double dx = mHisto->BinWidth();
 | 
|---|
| 157 |     int npt=0;
 | 
|---|
| 158 |     x1[npt] = mHisto->BinLowEdge(0);
 | 
|---|
| 159 |     y1[npt] = 0;
 | 
|---|
| 160 |     npt++;
 | 
|---|
| 161 |     for(int i=0; i<mHisto->NBins(); i++) {
 | 
|---|
| 162 |       x1[npt] = mHisto->BinLowEdge(i);
 | 
|---|
| 163 |       y1[npt] = (*mHisto)(i);
 | 
|---|
| 164 |       npt++;
 | 
|---|
| 165 |       x1[npt] = (double)x1[npt-1] + dx;
 | 
|---|
| 166 |       y1[npt] = y1[npt-1];
 | 
|---|
| 167 |       npt++;
 | 
|---|
| 168 |     }
 | 
|---|
| 169 |     x1[npt] = x1[npt-1];
 | 
|---|
| 170 |     y1[npt] = 0;
 | 
|---|
| 171 |     npt++;
 | 
|---|
| 172 |     g->DrawPolygon(x1,y1,npt,false);
 | 
|---|
| 173 |     delete [] x1; delete [] y1;
 | 
|---|
| 174 |     oktrace = true;
 | 
|---|
| 175 |   }
 | 
|---|
| 176 | 
 | 
|---|
| 177 |   // Trace/Ecriture des statistiques
 | 
|---|
| 178 |   // A faire a la fin - DrawStats change l'attribut de ligne 
 | 
|---|
| 179 |   if(stats) DrawStats(g);
 | 
|---|
| 180 | }
 | 
|---|
| 181 | 
 | 
|---|
| 182 | int
 | 
|---|
| 183 | PIHisto::DecodeOptionString(vector<string> & opt, bool rmdecopt)
 | 
|---|
| 184 | {
 | 
|---|
| 185 |   int optsz1 = opt.size();
 | 
|---|
| 186 |   if(optsz1<1) return(0); 
 | 
|---|
| 187 |   int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
 | 
|---|
| 188 |   if(optsz1-ndec1<1) return(ndec1);  // si tout a ete decode
 | 
|---|
| 189 | 
 | 
|---|
| 190 |   vector<string> udopt;  // On gardera ici les options non decodees
 | 
|---|
| 191 |   unsigned int k = 0;
 | 
|---|
| 192 |   int ndec = opt.size();
 | 
|---|
| 193 |   for( k=0; k<opt.size(); k++ ) {
 | 
|---|
| 194 |     string opts = opt[k];
 | 
|---|
| 195 |     if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
 | 
|---|
| 196 |     else if(  opts=="nsta"   || opts=="nstat"
 | 
|---|
| 197 |            || opts=="nostat" || opts=="nostats") SetStats(false);
 | 
|---|
| 198 |     else if(opts=="err")      SetError(1);
 | 
|---|
| 199 |     else if(opts=="noerr" || opts=="nerr")    SetError(-1);
 | 
|---|
| 200 |     else if(opts=="autoerr")  SetError(0);
 | 
|---|
| 201 |     else if(opts=="fill")     SetFilled(true);
 | 
|---|
| 202 |     else if(opts=="nofill" || opts=="nfill")   SetFilled(false);
 | 
|---|
| 203 |     else if(opts.substr(0,11) == "statposoff=") {
 | 
|---|
| 204 |       float xo=0., yo=0.;
 | 
|---|
| 205 |       sscanf(opts.substr(11).c_str(),"%g,%g",&xo, &yo);
 | 
|---|
| 206 |       SetStatPosOffset(xo, yo);
 | 
|---|
| 207 |     }
 | 
|---|
| 208 |     else {
 | 
|---|
| 209 |       ndec--;
 | 
|---|
| 210 |       // S'il faut supprimer les options decodees
 | 
|---|
| 211 |       if (rmdecopt)  udopt.push_back(opts);
 | 
|---|
| 212 |     }
 | 
|---|
| 213 |   }
 | 
|---|
| 214 |   // S'il faut supprimer les options decodees, on remplace l'argument opt
 | 
|---|
| 215 |   // par le vecteur des options non decodees.
 | 
|---|
| 216 |   if (rmdecopt)  opt = udopt;
 | 
|---|
| 217 |   return(ndec+ndec1);
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|
| 220 | int
 | 
|---|
| 221 | PIHisto::OptionToString(vector<string> & opt) const
 | 
|---|
| 222 | {
 | 
|---|
| 223 |  PIDrawer::OptionToString(opt);
 | 
|---|
| 224 | 
 | 
|---|
| 225 |  if(stats) opt.push_back("stat"); else opt.push_back("nstat");
 | 
|---|
| 226 |  if(error==-1) opt.push_back("noerr");
 | 
|---|
| 227 |    else if(error==0) opt.push_back("autoerr");
 | 
|---|
| 228 |      else if(error==1) opt.push_back("err");
 | 
|---|
| 229 |  if(filled) opt.push_back("fill"); else opt.push_back("nofill");
 | 
|---|
| 230 | 
 | 
|---|
| 231 |  char str[256]; sprintf(str,"statposoff=%g,%g",spoX,spoY);
 | 
|---|
| 232 |  opt.push_back(str);
 | 
|---|
| 233 | 
 | 
|---|
| 234 |  return 1;
 | 
|---|
| 235 | }
 | 
|---|
| 236 | 
 | 
|---|
| 237 | void
 | 
|---|
| 238 | PIHisto::GetOptionsHelpInfo(string& info)
 | 
|---|
| 239 | {
 | 
|---|
| 240 | info += " ---- PIHisto options help info : \n" ;
 | 
|---|
| 241 | info += "  sta,stat,stats:            activate   statistic display\n";
 | 
|---|
| 242 | info += "  nsta,nstat,nostat,nostats: deactivate statistic display\n";
 | 
|---|
| 243 | info += "  err / noerr,nerr : draw, do not draw error bars\n";
 | 
|---|
| 244 | info += "  autoerr : draw error bars if Marker drawing requested OR Profile histo\n";
 | 
|---|
| 245 | info += "  fill / nofill,nfill : fill, do not fill bars with selected color\n";
 | 
|---|
| 246 | info += "  statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
 | 
|---|
| 247 | info += "       as a fraction of total size \n";
 | 
|---|
| 248 | // On recupere ensuite la chaine info de la classe de base
 | 
|---|
| 249 | PIDrawer::GetOptionsHelpInfo(info);
 | 
|---|
| 250 | return;
 | 
|---|
| 251 | }
 | 
|---|
| 252 | 
 | 
|---|
| 253 | void
 | 
|---|
| 254 | PIHisto::DrawStats(PIGraphicUC* g)
 | 
|---|
| 255 | {
 | 
|---|
| 256 |   if (!mHisto) return;
 | 
|---|
| 257 |   //  if (mLAtt == PI_NotDefLineAtt)  g->SelLine(PI_ThinLine);
 | 
|---|
| 258 |   g->SelLine(PI_ThinLine);
 | 
|---|
| 259 |   g->SelFontSz((YMax() - YMin())/30);
 | 
|---|
| 260 | 
 | 
|---|
| 261 |   // La hauteur de la cellule
 | 
|---|
| 262 |   PIGrCoord a, d;
 | 
|---|
| 263 |   double cH = (double)g->GetFontHeight(a,d);
 | 
|---|
| 264 |   double cellHeight = 3.6 * cH;
 | 
|---|
| 265 | 
 | 
|---|
| 266 |   // Les labels et leurs longueurs -> largeur de la cellule
 | 
|---|
| 267 |   char *label, label1[64], label2[64], label3[64];
 | 
|---|
| 268 |   sprintf(label1, "N= %-g",mHisto->NData());
 | 
|---|
| 269 |   sprintf(label2, "m= %-g",mHisto->Mean());
 | 
|---|
| 270 |   sprintf(label3, "s= %-g",mHisto->Sigma());
 | 
|---|
| 271 |   label = label1;
 | 
|---|
| 272 |   if(strlen(label)<strlen(label2)) label = label2;
 | 
|---|
| 273 |   if(strlen(label)<strlen(label3)) label = label3;
 | 
|---|
| 274 |   double cellWidth =  1.1 * (double)g->CalcStringWidth(label);
 | 
|---|
| 275 | 
 | 
|---|
| 276 |   double ofpx = spoX*(XMax()-XMin());
 | 
|---|
| 277 |   double ofpy = spoY*(YMax()-YMin());
 | 
|---|
| 278 |   
 | 
|---|
| 279 |   double xu, yu, cw;
 | 
|---|
| 280 |   // Les limites du cadre
 | 
|---|
| 281 |   xu = g->DeltaUCX(XMax(), -cellWidth);
 | 
|---|
| 282 |   yu = g->DeltaUCY(YMax(), -cellHeight);
 | 
|---|
| 283 |   double recw = XMax()-xu;
 | 
|---|
| 284 |   double rech = YMax()-yu;
 | 
|---|
| 285 |   xu += ofpx;  yu += ofpy;
 | 
|---|
| 286 |   g->DrawBox(xu, yu, recw, rech);
 | 
|---|
| 287 | 
 | 
|---|
| 288 |   // L'ecriture des labels (attention aux inversions possibles des axes!)
 | 
|---|
| 289 |   cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
 | 
|---|
| 290 |   xu = g->DeltaUCX(XMax(),cw);
 | 
|---|
| 291 | 
 | 
|---|
| 292 |   cw = (g->isAxeYDirUpDown()) ? -0.15*cH : -1.15*cH;
 | 
|---|
| 293 |   yu = g->DeltaUCY(YMax(),cw);
 | 
|---|
| 294 |   xu += ofpx;  yu += ofpy;
 | 
|---|
| 295 |   g->DrawString(xu, yu,label1);
 | 
|---|
| 296 |   cw +=  -1.15*cH;
 | 
|---|
| 297 |   yu = g->DeltaUCY(YMax(),cw);  yu += ofpy;
 | 
|---|
| 298 |   g->DrawString(xu, yu,label2);
 | 
|---|
| 299 |   cw +=  -1.15*cH;
 | 
|---|
| 300 |   yu = g->DeltaUCY(YMax(),cw);  yu += ofpy;
 | 
|---|
| 301 |   g->DrawString(xu, yu,label3);
 | 
|---|
| 302 |                        
 | 
|---|
| 303 | }
 | 
|---|
| 304 | 
 | 
|---|
| 305 | 
 | 
|---|
| 306 | 
 | 
|---|
| 307 | /* --Methode-- */
 | 
|---|
| 308 | double PIHisto::GetDistanceToPoint(double x, double y)
 | 
|---|
| 309 | {
 | 
|---|
| 310 |  if (!mHisto) return 1.e+9;
 | 
|---|
| 311 | 
 | 
|---|
| 312 |  double dist = -1.e+18;
 | 
|---|
| 313 |  for(int i=0; i<mHisto->NBins(); i++) {
 | 
|---|
| 314 |    double xp=mHisto->BinCenter(i);
 | 
|---|
| 315 |    double yp=(*mHisto)(i);
 | 
|---|
| 316 |    xp = (xp-x)/(XMax()-XMin())/0.5;
 | 
|---|
| 317 |    yp = (yp-y)/(YMax()-YMin())/0.5;
 | 
|---|
| 318 |    xp = xp*xp+yp*yp;
 | 
|---|
| 319 |    if(dist<0. || xp<dist) dist = xp;
 | 
|---|
| 320 |  }
 | 
|---|
| 321 |  dist=sqrt(fabs(dist));
 | 
|---|
| 322 |  //cout<<dist<<"PIHisto: xlim="<<XMin()<<","<<XMax()<<" ylim="<<YMin()<<","<<YMax()
 | 
|---|
| 323 |  //    <<" NBins="<<mHisto->NBins()<<endl;
 | 
|---|
| 324 |  //cout<<"....d="<<dist<<" x="<<x<<" y="<<y<<endl;
 | 
|---|
| 325 | 
 | 
|---|
| 326 |  return dist;
 | 
|---|
| 327 | }
 | 
|---|