source: Sophya/trunk/SophyaPI/PIext/pihisto.cc@ 2517

Last change on this file since 2517 was 2517, checked in by cmv, 22 years ago

GetDistanceToPoint cmv 17/03/04

File size: 8.7 KB
RevLine 
[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]38PIHisto::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
49PIHisto::~PIHisto()
50{
[2229]51 if(mAdDO) delete mHisto;
[165]52}
53
54void
55PIHisto::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
76void
[205]77PIHisto::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]181int
182PIHisto::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);
198 else if(opts=="noerr") SetError(-1);
199 else if(opts=="autoerr") SetError(0);
[2517]200 else if(opts=="fill") SetFilled(true);
201 else if(opts=="nofill") 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
[165]219void
[2229]220PIHisto::GetOptionsHelpInfo(string& info)
221{
222info += " ---- PIHisto options help info : \n" ;
[2383]223info += " sta,stat,stats: activate statistic display\n";
[2229]224info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
[2383]225info += " err / nerr : draw, do not draw error bars\n";
[2229]226info += " autoerr : draw error bars if Marker drawing requested OR Profile histo\n";
[2517]227info += " fill / nofill : fill, do not fill bars with selected color\n";
[2383]228info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
229info += " as a fraction of total size \n";
[2388]230// On recupere ensuite la chaine info de la classe de base
231PIDrawer::GetOptionsHelpInfo(info);
[2229]232return;
233}
234
235void
[165]236PIHisto::DrawStats(PIGraphicUC* g)
237{
238 if (!mHisto) return;
[203]239 // if (mLAtt == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
240 g->SelLine(PI_ThinLine);
[1905]241 g->SelFontSz((YMax() - YMin())/30);
[1644]242
243 // La hauteur de la cellule
[544]244 PIGrCoord a, d;
245 double cH = (double)g->GetFontHeight(a,d);
[1645]246 double cellHeight = 3.6 * cH;
[1644]247
248 // Les labels et leurs longueurs -> largeur de la cellule
[1645]249 char *label, label1[64], label2[64], label3[64];
250 sprintf(label1, "N= %-g",mHisto->NData());
251 sprintf(label2, "m= %-g",mHisto->Mean());
252 sprintf(label3, "s= %-g",mHisto->Sigma());
[1644]253 label = label1;
254 if(strlen(label)<strlen(label2)) label = label2;
255 if(strlen(label)<strlen(label3)) label = label3;
256 double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
257
[2383]258 double ofpx = spoX*(XMax()-XMin());
259 double ofpy = spoY*(YMax()-YMin());
260
[1645]261 double xu, yu, cw;
[1644]262 // Les limites du cadre
[1645]263 xu = g->DeltaUCX(XMax(), -cellWidth);
264 yu = g->DeltaUCY(YMax(), -cellHeight);
[2383]265 double recw = XMax()-xu;
266 double rech = YMax()-yu;
267 xu += ofpx; yu += ofpy;
268 g->DrawBox(xu, yu, recw, rech);
[1644]269
[1645]270 // L'ecriture des labels (attention aux inversions possibles des axes!)
271 cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
272 xu = g->DeltaUCX(XMax(),cw);
273
274 cw = (g->isAxeYDirUpDown()) ? -0.15*cH : -1.15*cH;
275 yu = g->DeltaUCY(YMax(),cw);
[2383]276 xu += ofpx; yu += ofpy;
[1645]277 g->DrawString(xu, yu,label1);
[2383]278 cw += -1.15*cH;
279 yu = g->DeltaUCY(YMax(),cw); yu += ofpy;
280 g->DrawString(xu, yu,label2);
281 cw += -1.15*cH;
282 yu = g->DeltaUCY(YMax(),cw); yu += ofpy;
283 g->DrawString(xu, yu,label3);
[165]284
285}
[2517]286
287
288
289/* --Methode-- */
290double PIHisto::GetDistanceToPoint(double x, double y)
291{
292 if (!mHisto) return 1.e+9;
293
294 double dist = -1.e+18;
295 for(int i=0; i<mHisto->NBins(); i++) {
296 double xp=mHisto->BinCenter(i);
297 double yp=(*mHisto)(i);
298 xp = (xp-x)/(XMax()-XMin())/0.5;
299 yp = (yp-y)/(YMax()-YMin())/0.5;
300 xp = xp*xp+yp*yp;
301 if(dist<0. || xp<dist) dist = xp;
302 }
303 dist=sqrt(fabs(dist));
304 //cout<<dist<<"PIHisto: xlim="<<XMin()<<","<<XMax()<<" ylim="<<YMin()<<","<<YMax()
305 // <<" NBins="<<mHisto->NBins()<<endl;
306 //cout<<"....d="<<dist<<" x="<<x<<" y="<<y<<endl;
307
308 return dist;
309}
Note: See TracBrowser for help on using the repository browser.