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

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

GetDistanceToPoint cmv 17/03/04

File size: 8.7 KB
Line 
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 "pihisto.h"
11#include "hisprof.h"
12
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
38PIHisto::PIHisto(Histo* histo, bool ad)
39: PIDrawer(), mHisto(histo)
40{
41 mAdDO = ad; // Flag pour suppression automatique de mHisto
42 SetStats();
43 SetError();
44 SetFilled();
45 SetStatPosOffset();
46 SetName("HistoDrw");
47}
48
49PIHisto::~PIHisto()
50{
51 if(mAdDO) delete mHisto;
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
61 if( typeid(*mHisto) == typeid(HProf) ) {
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 }
72 if(hmax<=hmin) hmax = hmin+1.;
73 SetLimits(mHisto->XMin(), mHisto->XMax(), hmin, hmax);
74}
75
76void
77PIHisto::Draw(PIGraphicUC* g, double /*xmin*/, double /*ymin*/, double /*xmax*/, double /*ymax*/)
78{
79 if (axesFlags != kAxesNone) DrawAxes(g);
80
81 if (!mHisto) return;
82
83 if( typeid(*mHisto)==typeid(HProf) ) mHisto->UpdateHisto();
84
85 bool oktrace=false;
86 // Tracer d'une polyline si demandee
87 bool drawpline=false;
88 if(GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt) drawpline=true;
89 // Tracer des marqueurs si demande ou si HProf
90 bool drawmarker=false;
91 if( (GetGraphicAtt().GetMarker() != PI_NotDefMarker) ||
92 (typeid(*mHisto) == typeid(HProf)) ) drawmarker = true;
93 // Tracer des erreurs ?
94 bool drawerr=false;
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;
100 }
101 else if(error>0) drawerr=true;
102 else if(error<0) drawerr=false;
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;
108
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
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;
135 double bw = mHisto->BinWidth();
136 for(int i=0; i<mHisto->NBins(); i++) {
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 }
150
151 // Trace de la ligne continue si demandee
152 if(drawpline) {
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++;
160 for(int i=0; i<mHisto->NBins(); i++) {
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++;
167 }
168 x1[npt] = x1[npt-1];
169 y1[npt] = 0;
170 npt++;
171 g->DrawPolygon(x1,y1,npt,false);
172 delete [] x1; delete [] y1;
173 oktrace = true;
174 }
175
176 // Trace/Ecriture des statistiques
177 // A faire a la fin - DrawStats change l'attribut de ligne
178 if(stats) DrawStats(g);
179}
180
181int
182PIHisto::DecodeOptionString(vector<string> & opt, bool rmdecopt)
183{
184 int optsz1 = opt.size();
185 if(optsz1<1) return(0);
186 int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
187 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
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];
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);
200 else if(opts=="fill") SetFilled(true);
201 else if(opts=="nofill") SetFilled(false);
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 }
207 else {
208 ndec--;
209 // S'il faut supprimer les options decodees
210 if (rmdecopt) udopt.push_back(opts);
211 }
212 }
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;
216 return(ndec+ndec1);
217}
218
219void
220PIHisto::GetOptionsHelpInfo(string& info)
221{
222info += " ---- PIHisto options help info : \n" ;
223info += " sta,stat,stats: activate statistic display\n";
224info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
225info += " err / nerr : draw, do not draw error bars\n";
226info += " autoerr : draw error bars if Marker drawing requested OR Profile histo\n";
227info += " fill / nofill : fill, do not fill bars with selected color\n";
228info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
229info += " as a fraction of total size \n";
230// On recupere ensuite la chaine info de la classe de base
231PIDrawer::GetOptionsHelpInfo(info);
232return;
233}
234
235void
236PIHisto::DrawStats(PIGraphicUC* g)
237{
238 if (!mHisto) return;
239 // if (mLAtt == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
240 g->SelLine(PI_ThinLine);
241 g->SelFontSz((YMax() - YMin())/30);
242
243 // La hauteur de la cellule
244 PIGrCoord a, d;
245 double cH = (double)g->GetFontHeight(a,d);
246 double cellHeight = 3.6 * cH;
247
248 // Les labels et leurs longueurs -> largeur de la cellule
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());
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
258 double ofpx = spoX*(XMax()-XMin());
259 double ofpy = spoY*(YMax()-YMin());
260
261 double xu, yu, cw;
262 // Les limites du cadre
263 xu = g->DeltaUCX(XMax(), -cellWidth);
264 yu = g->DeltaUCY(YMax(), -cellHeight);
265 double recw = XMax()-xu;
266 double rech = YMax()-yu;
267 xu += ofpx; yu += ofpy;
268 g->DrawBox(xu, yu, recw, rech);
269
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);
276 xu += ofpx; yu += ofpy;
277 g->DrawString(xu, yu,label1);
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);
284
285}
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.