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