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

Last change on this file since 2460 was 2388, checked in by ansari, 22 years ago

Amelioration help/decodage options - Reza 20/5/2003

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