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