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

Last change on this file since 2642 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 9.2 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
[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]39PIHisto::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
50PIHisto::~PIHisto()
51{
[2229]52 if(mAdDO) delete mHisto;
[165]53}
54
55void
56PIHisto::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
77void
[205]78PIHisto::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;
83
84 if( typeid(*mHisto)==typeid(HProf) ) mHisto->UpdateHisto();
[203]85
[2469]86 bool oktrace=false;
[2517]87 // Tracer d'une polyline si demandee
88 bool drawpline=false;
89 if(GetGraphicAtt().GetLineAtt() != PI_NotDefLineAtt) drawpline=true;
[2469]90 // Tracer des marqueurs si demande ou si HProf
91 bool drawmarker=false;
[2229]92 if( (GetGraphicAtt().GetMarker() != PI_NotDefMarker) ||
93 (typeid(*mHisto) == typeid(HProf)) ) drawmarker = true;
[2469]94 // Tracer des erreurs ?
95 bool drawerr=false;
[2229]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;
[2469]101 }
102 else if(error>0) drawerr=true;
103 else if(error<0) drawerr=false;
[2517]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;
[2229]109
[2517]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
[2229]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;
[205]136 double bw = mHisto->BinWidth();
[1057]137 for(int i=0; i<mHisto->NBins(); i++) {
[165]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 }
[2229]151
[2517]152 // Trace de la ligne continue si demandee
153 if(drawpline) {
[2469]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++;
[1057]161 for(int i=0; i<mHisto->NBins(); i++) {
[2469]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++;
[165]168 }
[2469]169 x1[npt] = x1[npt-1];
170 y1[npt] = 0;
171 npt++;
[2517]172 g->DrawPolygon(x1,y1,npt,false);
173 delete [] x1; delete [] y1;
174 oktrace = true;
[165]175 }
[2229]176
177 // Trace/Ecriture des statistiques
[203]178 // A faire a la fin - DrawStats change l'attribut de ligne
[1057]179 if(stats) DrawStats(g);
[165]180}
181
[1971]182int
183PIHisto::DecodeOptionString(vector<string> & opt, bool rmdecopt)
184{
[2229]185 int optsz1 = opt.size();
186 if(optsz1<1) return(0);
[1971]187 int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
[2229]188 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
[1971]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];
[2229]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);
[2523]199 else if(opts=="noerr" || opts=="nerr") SetError(-1);
[2229]200 else if(opts=="autoerr") SetError(0);
[2517]201 else if(opts=="fill") SetFilled(true);
[2523]202 else if(opts=="nofill" || opts=="nfill") SetFilled(false);
[2383]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 }
[1971]208 else {
209 ndec--;
210 // S'il faut supprimer les options decodees
211 if (rmdecopt) udopt.push_back(opts);
212 }
[2229]213 }
[1971]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;
[2229]217 return(ndec+ndec1);
[1971]218}
219
[2523]220int
221PIHisto::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
[165]237void
[2229]238PIHisto::GetOptionsHelpInfo(string& info)
239{
240info += " ---- PIHisto options help info : \n" ;
[2383]241info += " sta,stat,stats: activate statistic display\n";
[2229]242info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
[2523]243info += " err / noerr,nerr : draw, do not draw error bars\n";
[2229]244info += " autoerr : draw error bars if Marker drawing requested OR Profile histo\n";
[2523]245info += " fill / nofill,nfill : fill, do not fill bars with selected color\n";
[2383]246info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
247info += " as a fraction of total size \n";
[2388]248// On recupere ensuite la chaine info de la classe de base
249PIDrawer::GetOptionsHelpInfo(info);
[2229]250return;
251}
252
253void
[165]254PIHisto::DrawStats(PIGraphicUC* g)
255{
256 if (!mHisto) return;
[203]257 // if (mLAtt == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
258 g->SelLine(PI_ThinLine);
[1905]259 g->SelFontSz((YMax() - YMin())/30);
[1644]260
261 // La hauteur de la cellule
[544]262 PIGrCoord a, d;
263 double cH = (double)g->GetFontHeight(a,d);
[1645]264 double cellHeight = 3.6 * cH;
[1644]265
266 // Les labels et leurs longueurs -> largeur de la cellule
[1645]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());
[1644]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
[2383]276 double ofpx = spoX*(XMax()-XMin());
277 double ofpy = spoY*(YMax()-YMin());
278
[1645]279 double xu, yu, cw;
[1644]280 // Les limites du cadre
[1645]281 xu = g->DeltaUCX(XMax(), -cellWidth);
282 yu = g->DeltaUCY(YMax(), -cellHeight);
[2383]283 double recw = XMax()-xu;
284 double rech = YMax()-yu;
285 xu += ofpx; yu += ofpy;
286 g->DrawBox(xu, yu, recw, rech);
[1644]287
[1645]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);
[2383]294 xu += ofpx; yu += ofpy;
[1645]295 g->DrawString(xu, yu,label1);
[2383]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);
[165]302
303}
[2517]304
305
306
307/* --Methode-- */
308double 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}
Note: See TracBrowser for help on using the repository browser.