source: Sophya/trunk/SophyaPI/PIext/pintuple.cc@ 3221

Last change on this file since 3221 was 3149, checked in by cmv, 19 years ago

suite adaptation travail reza 2D et adapteur + adaptation aux modifs HistoErr cmv 18/01/2007

File size: 14.3 KB
Line 
1// Peida Interactive - PI R. Ansari 97-99
2// Traceur (Drawer) pour NTupleInterface
3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <iostream>
8#include <math.h>
9#include "sopnamsp.h"
10#include "pintuple.h"
11
12//++
13// Class PINTuple
14// Lib PIext
15// include pintuple.h
16//
17// Classe de traceur 2D (dans un plan) à partir des données
18// d'un objet implémentant l'interface *NTupleInterface*.
19// Les objets "PINTuple" peuvent tracer des signes (markers)
20// éventuellement avec des barres d'erreur et une étiquette
21// pour chaque point. Si un nom de colonne poids est spécifié,
22// la taille et/ou la couleur des signes (markers) sera fonction
23// de la valeur de poids.
24//--
25//++
26// Links Parents
27// PIDrawer
28//--
29//++
30// Links Voir aussi
31// NTupleInterface
32// PINTuple3D
33//--
34
35//++
36// Titre Constructeur
37//--
38//++
39// PINTuple(NTupleInterface* nt, bool ad)
40// Constructeur. Si "ad == true", l'objet "nt" est détruit par
41// le destructeur de l'objet "PINTuple"
42// Note : "nt" doit être créé par new dans ce cas.
43//--
44
45/* --Methode-- */
46PINTuple::PINTuple(NTupleInterface* nt, bool ad)
47: PIDrawer()
48{
49 mNT = nt;
50 mAdDO = ad;
51 SetStats(true);
52 SetStatPosOffset();
53 ConnectPoints(false);
54 UseSizeScale(true, 5);
55 UseColorScale(true);
56 SelectXY(NULL, NULL);
57 SelectWt(NULL);
58 SelectErrBar();
59 SelectLabel(NULL);
60 SetName("NTupleDrw");
61 NptDraw = 0;
62
63}
64
65PINTuple::~PINTuple()
66{
67 if (mAdDO && mNT) delete mNT;
68}
69
70//++
71// Titre Méthodes
72//--
73//++
74// void SelectXY(const char* px, const char* py)
75// Choix des noms de colonnes X,Y définissant les coordonnées des points.
76// Ces deux colonnes doivent être spécifiées pour obtenir un tracé.
77// void SelectErrBar(const char* erbx=NULL, const char* erby=NULL)
78// Choix des noms de colonnes pour le tracé des barres d'erreur.
79// void SelectWt(const char* pw=NULL, int nbins=10)
80// Choix du nom de colonne poids. Dans ce cas, la taille du signe
81// (marker) sera proportionnel à la valeur de cette colonne pour
82// chaque point.
83// void SelectLabel(const char* plabel=NULL)
84// Choix du nom de colonne correspondant à l'étiquette.
85//--
86
87/* --Methode-- */
88void PINTuple::SelectXY(const char* px, const char* py)
89{
90string name;
91if (mNT == NULL) xK = yK = -1;
92if (px == NULL) xK = -1;
93else { name = px; xK = mNT->ColumnIndex(name); }
94if (py == NULL) yK = -1;
95else { name = py; yK = mNT->ColumnIndex(name); }
96}
97
98/* --Methode-- */
99void PINTuple::SelectWt(const char* pw)
100{
101if (pw == NULL) wK = -1;
102else { string name = pw; wK = mNT->ColumnIndex(name); }
103
104if (wK >= 0) mNT->GetMinMax(wK, wMin, wMax);
105else { wMin = 0.; wMax = 1.; }
106}
107
108/* --Methode-- */
109void PINTuple::SelectLabel(const char* plabel)
110{
111if (plabel == NULL) lK = -1;
112else { string name = plabel; lK = mNT->ColumnIndex(name); }
113}
114
115/* --Methode-- */
116void PINTuple::SelectErrBar(const char* erbx, const char* erby)
117{
118string name;
119if (mNT == NULL) xebK = yebK = -1;
120if (erbx == NULL) xebK = -1;
121else { name = erbx; xebK = mNT->ColumnIndex(name); }
122if (erby == NULL) yebK = -1;
123else { name = erby; yebK = mNT->ColumnIndex(name); }
124}
125
126
127/* --Methode-- */
128void PINTuple::UpdateLimits()
129{
130 if (!mNT) return;
131 if (mNT->NbLines() <= 0) return;
132 if ( (xK < 0) || (yK < 0) ) return;
133
134 // Commencer par trouver nos limites
135 double xmin, xmax, ymin, ymax;
136 xmin = ymin = 9.e19;
137 xmax = ymax = -9.e19;
138 mNT->GetMinMax(xK, xmin, xmax);
139 mNT->GetMinMax(yK, ymin, ymax);
140 PIAxes::ReSizeMinMax(isLogScaleX(),xmin,xmax);
141 PIAxes::ReSizeMinMax(isLogScaleY(),ymin,ymax);
142 SetLimits(xmin,xmax,ymin,ymax);
143// SetAxesFlags(kBoxAxes | kExtTicks | kLabels); Ne pas faire - Reza 11/99
144}
145
146
147/* --Methode-- */
148#define NMXMULTP_LOCAL 30 // Pour multipoint sans new
149void PINTuple::Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax)
150{
151double xp,yp,xer,yer,wp;
152double xl,yl;
153long nok;
154int npolyg;
155
156if (!mNT) return;
157if (axesFlags != kAxesNone) DrawAxes(g);
158if ( (xK < 0) || (yK < 0) ) return;
159if (GetGraphicAtt().GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
160
161// Pour tracer des markers avec taille fonction de Wt (poids)
162double dw = (wMax-wMin)/nWbins;
163if (dw < 1.e-19) dw = 1.e-19;
164
165// Pour tracer des markers avec couleur en fonction de Wt (poids)
166PIColorMap * cmap = NULL;
167double dwc = 1.;
168int nwc = 1;
169bool revcmap;
170CMapId mcmapid = GetGraphicAtt().GetColMapId(revcmap);
171if( colorScale && (wK >= 0) && (mcmapid != CMAP_OTHER) ) {
172 cmap = new PIColorMap(mcmapid);
173 cmap->ReverseColorIndex(revcmap);
174 nwc = cmap->NCol();
175 dwc = (wMax-wMin)/(double)nwc;
176}
177
178int msz,sz;
179
180PIMarker mmrk = GetGraphicAtt().GetMarker();
181PIMarker mrk;
182if (wK >= 0) mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_CircleMarker;
183else mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_DotMarker;
184msz = GetGraphicAtt().GetMarkerSz();
185if (msz < 1) msz = 1;
186g->SelMarker(msz, mrk);
187
188PIGrCoord uxmin, uxmax, uymin, uymax;
189g->GetGrSpace(uxmin, uxmax, uymin, uymax);
190double xmin2 = uxmin;
191double ymin2 = uymin;
192double xmax2 = uxmax;
193double ymax2 = uymax;
194
195nok = 0;
196xp = yp = xl = yl = 0;
197PIGrCoord xpolyg[NMXMULTP_LOCAL], ypolyg[NMXMULTP_LOCAL];
198npolyg = 0;
199NptDraw = 0;
200
201// Mai 2005: Correction bug trace des lignes avec le pts en dehors de la zone (Reza)
202// flags pour la gestion des points/lignes a tracer
203bool fgokcurpt = true; // Point courant dans la zone a tracer
204bool fgoklastpt = true; // Le dernier point etait dans la zone a tracer
205for (long i=0; i<(long)mNT->NbLines(); i++) {
206 xl = xp; yl = yp; fgoklastpt = fgokcurpt;
207
208 xp = mNT->GetCell(i, xK);
209 yp = mNT->GetCell(i, yK);
210
211 // Comptage du nombre de pts dans la zone graphique du widget
212 if ( !((xp < xmin2) || (xp > xmax2) || (yp < ymin2) || (yp > ymax2)) ) nok++;
213 // Gestion des limites a tracer
214 fgokcurpt = true;
215 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) fgokcurpt = false;
216 else NptDraw++;
217
218// Taille - couleur de marker en fonction du poids
219 if ( fgokcurpt &&(wK >= 0) ) wp = mNT->GetCell(i, wK);
220 if ( fgokcurpt && mrkSzScale && (wK >= 0) ) { // Changement de taille
221 sz = (int)((wp-wMin)/dw);
222 if (sz < 0) sz = 0;
223 if (sz > nWbins) sz = nWbins;
224 sz += msz;
225 if (sz < 2) g->SelMarker(sz, PI_DotMarker);
226 else g->SelMarker(sz, mrk);
227 }
228// Couleur du marker en fonction du poids
229 if( fgokcurpt && colorScale && (wK >= 0) && cmap ) {
230 int cid = (int)((wp-wMin)/dwc);
231 if (cid < 0) cid = 0;
232 if (cid >= nwc) cid = nwc-1;
233 g->SelForeground(*cmap, cid);
234 }
235
236 // Trace d'une ligne reliant les points
237 if( connectPts ) {
238 // On initialise le polygone avec le dernier point, si ce dernier ok
239 if((npolyg==0) && (i>0) && fgokcurpt) {xpolyg[0]=xl; ypolyg[0]=yl; npolyg=1;}
240 // On ajoute le point courant au polygone - sauf si celui-ci est vide
241 if((npolyg<NMXMULTP_LOCAL) && (npolyg>0))
242 {xpolyg[npolyg]=xp; ypolyg[npolyg]=yp; npolyg++;}
243 // On trace le polygone s'il est plein - ou le point courant en dehors
244 // (point courant en dehors -> discontinuite de la ligne
245 if( !fgokcurpt || (npolyg==NMXMULTP_LOCAL) )
246 {g->DrawPolygon(xpolyg,ypolyg,npolyg,false); npolyg=0;}
247 }
248
249 // Plus rien a faire si point en dehors -- on s'en va
250 if (!fgokcurpt) continue;
251
252 // Trace des erreurs selon X et Y
253 if (xebK >= 0) {
254 xer = mNT->GetCell(i, xebK);
255 if(xer>0.) g->DrawLine(xp-xer, yp, xp+xer, yp);
256 }
257 if (yebK >= 0) {
258 yer = mNT->GetCell(i, yebK);
259 if(yer>0.) g->DrawLine(xp, yp-yer, xp, yp+yer);
260 }
261
262 // Trace du marker
263 if ((wK >= 0)||(lK < 0)||(mmrk != PI_NotDefMarker)) g->DrawMarker(xp, yp);
264
265 // Trace eventuel du label
266 if (lK >= 0) g->DrawString(xp, yp, mNT->GetCelltoString(i, lK).c_str());
267
268}
269
270// Fin du trace d'une ligne reliant les points si necessaire
271if( connectPts && npolyg>1 )
272 {g->DrawPolygon(xpolyg,ypolyg,npolyg,false); npolyg=0;}
273
274if (stats) { // Trace de stats
275 g->SelFontSz((YMax() - YMin())/30);
276 // La hauteur de la cellule
277 PIGrCoord a,d;
278 double cH = (double)g->GetFontHeight(a,d);
279 double cellHeight = 1.2 * cH;
280 // Les labels et leurs longueurs -> largeur de la cellule
281 char label[64];
282 sprintf(label, "N=%ld (/%ld)", (long)nok, (long)mNT->NbLines());
283 double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
284 double xu, yu, cw;
285 double ofpx = spoX*(XMax()-XMin());
286 double ofpy = spoY*(YMax()-YMin());
287 // Les limites du cadre
288 xu = g->DeltaUCX(XMax(), - cellWidth);
289 yu = g->DeltaUCY(YMax(), - cellHeight);
290 double recw = XMax()-xu;
291 double rech = YMax()-yu;
292 xu += ofpx; yu += ofpy;
293 g->DrawBox(xu, yu, recw, rech);
294 // L'ecriture des labels (attention aux inversions possibles des axes!)
295 cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
296 xu = g->DeltaUCX(XMax(),cw);
297 cw = (g->isAxeYDirUpDown()) ? -0.1*cH : -1.1*cH;
298 yu = g->DeltaUCY(YMax(),cw);
299 xu += ofpx; yu += ofpy;
300 g->DrawString(xu,yu,label);
301}
302
303if (cmap) delete cmap;
304return;
305}
306#undef NMXMULTP_LOCAL
307
308/* --Methode-- */
309void PINTuple::AppendTextInfo(string& info, double xmin, double ymin, double xmax, double ymax)
310{
311if (!mNT) return;
312if ( (xK < 0) || (yK < 0) ) return;
313
314long ncnt = 0;
315double xp,yp;
316char buff[128];
317sprintf(buff,"PINTuple: NLines= %ld NCol= %ld \n", (long)mNT->NbLines(),
318 (long)mNT->NbColumns());
319info += buff;
320info += mNT->LineHeaderToString();
321for(long i=0; i<(long)mNT->NbLines(); i++) {
322 xp = mNT->GetCell(i, xK);
323 yp = mNT->GetCell(i, yK);
324 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
325 ncnt++;
326 if (ncnt > 101) continue;
327 info += mNT->LineToString(i);
328 }
329if (ncnt >= 101) info += " .... \n";
330sprintf(buff," %ld points inside selected region \n", (long)ncnt);
331info += buff;
332// printf("PINTuple::AppendTextInfo()-DBG %g %g %g %g - %d\n", xmin, ymin, xmax, ymax, ncnt);
333return;
334}
335
336/* La methode DecodeOptionString permet de decoder un ensemble d'options
337 et de parametre d'affichage specifie sous forme d'un vecteur de string.
338 Si rmdecopt == true, les options decodees sont supprimees du vecteur
339 de string fourni en entree - ce qui permet l'enchainement eventuel
340 de plusieurs decodages de string.
341 Les options peuvent etre sous forme de flag : "stat" "nostat"
342 ou plus complexes, par exemple "dynamic=-3,3"
343 Rc: La methode renvoie le nombre d'options decodees
344*/
345
346/* --Methode-- */
347int PINTuple::DecodeOptionString(vector<string> & opt, bool rmdecopt)
348{
349 int optsz1 = opt.size();
350 if(optsz1<1) return(0);
351 // On appelle d'abord le decodage de la classe PIDrawer de laquelle
352 // on herite. (Pas obligatoire) on decode donc ici les attributs de
353 // couleur, fontes ...
354 int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
355 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
356
357 vector<string> udopt; // On gardera ici les options non decodees
358 unsigned int k = 0;
359 int ndec = opt.size();
360 for( k=0; k<opt.size(); k++ ) {
361 string opts = opt[k];
362 if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
363 else if( opts=="nsta" || opts=="nstat"
364 || opts=="nostat" || opts=="nostats") SetStats(false);
365 else if(opts.substr(0,11) == "statposoff=") {
366 double xo=0., yo=0.;
367 sscanf(opts.substr(11).c_str(),"%lf,%lf",&xo, &yo);
368 SetStatPosOffset(xo, yo);
369 }
370 else if (opts == "connectpoints") ConnectPoints(true);
371 else if (opts == "noconnectpoints") ConnectPoints(false);
372 else if (opts == "colorscale") UseColorScale(true);
373 else if (opts == "nocolorscale") UseColorScale(false);
374 else if (opts == "sizescale") UseSizeScale(true);
375 else if (opts == "nosizescale") UseSizeScale(false);
376 else if (opts.substr(0,10) == "sizescale=") {
377 int nbn = atoi(opts.substr(10).c_str());
378 UseSizeScale(true, nbn);
379 }
380
381 else {
382 // Si option non decode
383 ndec--;
384 // S'il faut supprimer les options decodees
385 if (rmdecopt) udopt.push_back(opts);
386 }
387 }
388 // S'il faut supprimer les options decodees, on remplace l'argument opt
389 // par le vecteur des options non decodees.
390 if (rmdecopt) opt = udopt;
391 return(ndec+ndec1);
392}
393
394int PINTuple::OptionToString(vector<string> & opt) const
395{
396 PIDrawer::OptionToString(opt);
397
398 char str[256];
399
400 if(stats) opt.push_back("stat"); else opt.push_back("nstat");
401
402 sprintf(str,"statposoff=%lf,%lf",spoX,spoY); opt.push_back(str);
403
404 if(connectPts) opt.push_back("connectpoints");
405 else opt.push_back("noconnectpoints");
406
407 if(colorScale) opt.push_back("colorscale");
408 else opt.push_back("nocolorscale");
409
410 if(mrkSzScale) {
411 if(nWbins>0) {sprintf(str,"sizescale=%d",nWbins); opt.push_back(str);}
412 else opt.push_back("sizescale");
413 } else opt.push_back("nosizescale");
414
415 return 1;
416}
417
418/* La methode GetOptionsHelpInfo(string& info) renvoie une chaine
419 avec la description des options comprises par ce drawer
420 Note: Il est preferable de ne pas initialiser la chaine
421 string info au depart, afin de permettre de mettre bout a
422 bout les aides de differents Drawer */
423
424/* --Methode-- */
425void PINTuple::GetOptionsHelpInfo(string& info)
426{
427info += " ---- PINTuple options help info : \n" ;
428info += " sta,stat,stats: activate statistic display\n";
429info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
430info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
431info += " as a fraction of total size \n";
432info += " connectpoints: The points are connected by a line \n";
433info += " noconnectpoints (this is the default) \n";
434info += " colorscale/nocolorscale (Use color scale for weight) \n";
435info += " sizescale/sizescale=nbins/nosizescale (Use marker size for weight) \n";
436info += " (and usual color/line/marker/... attribute decoding) \n";
437// On recupere ensuite la chaine info de la classe de base
438PIDrawer::GetOptionsHelpInfo(info);
439return;
440}
441
442
443/* --Methode-- */
444double PINTuple::GetDistanceToPoint(double x, double y)
445{
446 if(!mNT) return 1.e+9;
447 if( xK<0 || yK<0 ) return 1.e+9;
448
449 const int nessai = 100;
450 long inc = (NptDraw>nessai) ? (long)(NptDraw/nessai)+1 : 1;
451
452 double dist = -1.e+18;
453 long n = 0;
454 for(long i=0; i<(long)mNT->NbLines(); i++) {
455 double xp=mNT->GetCell(i,xK);
456 if(xp<XMin() || xp>XMax()) continue;
457 double yp=mNT->GetCell(i,yK);
458 if(yp<YMin() || yp>YMax()) continue;
459 if(n%inc==0) {
460 xp = (xp-x)/(XMax()-XMin())/0.5;
461 yp = (yp-y)/(YMax()-YMin())/0.5;
462 xp = xp*xp+yp*yp;
463 if(dist<0. || xp<dist) dist = xp;
464 }
465 n++;
466 }
467 dist=sqrt(fabs(dist));
468 //cout<<"PINTuple: xlim="<<XMin()<<","<<XMax()<<" ylim="<<YMin()<<","<<YMax()
469 // <<" NbLines="<<mNT->NbLines()<<" inc="<<inc<<endl;
470 //cout<<"....d="<<dist<<" x="<<x<<" y="<<y<<" NptDraw="<<NptDraw<<endl;
471
472 return dist;
473}
Note: See TracBrowser for help on using the repository browser.