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

Last change on this file since 2709 was 2690, checked in by ansari, 20 years ago

re-petite modif int->long et format printf/sprintf pintuple.cc - Reza 26/4/2005

File size: 13.5 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;
200for (long i=0; i<(long)mNT->NbLines(); i++) {
201 xl = xp; yl = yp;
202 xp = mNT->GetCell(i, xK);
203 yp = mNT->GetCell(i, yK);
204 if ( (xp < xmin2) || (xp > xmax2) || (yp < ymin2) || (yp > ymax2) ) continue;
205 nok++;
206 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
207
208 NptDraw++;
209// Taille - couleur de marker en fonction du poids
210 if (wK >= 0) wp = mNT->GetCell(i, wK);
211 if (mrkSzScale && (wK >= 0)) { // Changement de taille
212 sz = (int)((wp-wMin)/dw);
213 if (sz < 0) sz = 0;
214 if (sz > nWbins) sz = nWbins;
215 sz += msz;
216 if (sz < 2) g->SelMarker(sz, PI_DotMarker);
217 else g->SelMarker(sz, mrk);
218 }
219// Couleur du marker en fonction du poids
220 if( colorScale && (wK >= 0) && cmap ) {
221 int cid = (int)((wp-wMin)/dwc);
222 if (cid < 0) cid = 0;
223 if (cid >= nwc) cid = nwc-1;
224 g->SelForeground(*cmap, cid);
225 }
226
227 // Trace d'une ligne reliant les points
228 if( connectPts ) {
229 if((npolyg==0) && (i>0)) {xpolyg[0]=xl; ypolyg[0]=yl; npolyg=1;}
230 if(npolyg<NMXMULTP_LOCAL)
231 {xpolyg[npolyg]=xp; ypolyg[npolyg]=yp; npolyg++;}
232 if(npolyg==NMXMULTP_LOCAL)
233 {g->DrawPolygon(xpolyg,ypolyg,npolyg,false); npolyg=0;}
234 }
235
236 // Trace des erreurs selon X et Y
237 if ( xebK >= 0 ) {
238 xer = mNT->GetCell(i, xebK);
239 if(xer>0.) g->DrawLine(xp-xer, yp, xp+xer, yp);
240 }
241 if ( yebK >= 0 ) {
242 yer = mNT->GetCell(i, yebK);
243 if(yer>0.) g->DrawLine(xp, yp-yer, xp, yp+yer);
244 }
245
246 // Trace du marker
247 if ((wK >= 0)||(lK < 0)||(mmrk != PI_NotDefMarker)) g->DrawMarker(xp, yp);
248
249 // Trace eventuel du label
250 if (lK >= 0) g->DrawString(xp, yp, mNT->GetCelltoString(i, lK).c_str());
251
252}
253
254// Fin du trace d'une ligne reliant les points si necessaire
255if( connectPts && npolyg>1 )
256 {g->DrawPolygon(xpolyg,ypolyg,npolyg,false); npolyg=0;}
257
258if (stats) { // Trace de stats
259 g->SelFontSz((YMax() - YMin())/30);
260 // La hauteur de la cellule
261 PIGrCoord a,d;
262 double cH = (double)g->GetFontHeight(a,d);
263 double cellHeight = 1.2 * cH;
264 // Les labels et leurs longueurs -> largeur de la cellule
265 char label[64];
266 sprintf(label, "N=%ld (/%ld)", (long)nok, (long)mNT->NbLines());
267 double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
268 double xu, yu, cw;
269 double ofpx = spoX*(XMax()-XMin());
270 double ofpy = spoY*(YMax()-YMin());
271 // Les limites du cadre
272 xu = g->DeltaUCX(XMax(), - cellWidth);
273 yu = g->DeltaUCY(YMax(), - cellHeight);
274 double recw = XMax()-xu;
275 double rech = YMax()-yu;
276 xu += ofpx; yu += ofpy;
277 g->DrawBox(xu, yu, recw, rech);
278 // L'ecriture des labels (attention aux inversions possibles des axes!)
279 cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
280 xu = g->DeltaUCX(XMax(),cw);
281 cw = (g->isAxeYDirUpDown()) ? -0.1*cH : -1.1*cH;
282 yu = g->DeltaUCY(YMax(),cw);
283 xu += ofpx; yu += ofpy;
284 g->DrawString(xu,yu,label);
285}
286
287if (cmap) delete cmap;
288return;
289}
290#undef NMXMULTP_LOCAL
291
292/* --Methode-- */
293void PINTuple::AppendTextInfo(string& info, double xmin, double ymin, double xmax, double ymax)
294{
295if (!mNT) return;
296if ( (xK < 0) || (yK < 0) ) return;
297
298long ncnt = 0;
299double xp,yp;
300char buff[128];
301sprintf(buff,"PINTuple: NLines= %ld NCol= %ld \n", (long)mNT->NbLines(),
302 (long)mNT->NbColumns());
303info += buff;
304info += mNT->LineHeaderToString();
305for(long i=0; i<(long)mNT->NbLines(); i++) {
306 xp = mNT->GetCell(i, xK);
307 yp = mNT->GetCell(i, yK);
308 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
309 ncnt++;
310 if (ncnt > 101) continue;
311 info += mNT->LineToString(i);
312 }
313if (ncnt >= 101) info += " .... \n";
314sprintf(buff," %ld points inside selected region \n", (long)ncnt);
315info += buff;
316// printf("PINTuple::AppendTextInfo()-DBG %g %g %g %g - %d\n", xmin, ymin, xmax, ymax, ncnt);
317return;
318}
319
320/* La methode DecodeOptionString permet de decoder un ensemble d'options
321 et de parametre d'affichage specifie sous forme d'un vecteur de string.
322 Si rmdecopt == true, les options decodees sont supprimees du vecteur
323 de string fourni en entree - ce qui permet l'enchainement eventuel
324 de plusieurs decodages de string.
325 Les options peuvent etre sous forme de flag : "stat" "nostat"
326 ou plus complexes, par exemple "dynamic=-3,3"
327 Rc: La methode renvoie le nombre d'options decodees
328*/
329
330/* --Methode-- */
331int PINTuple::DecodeOptionString(vector<string> & opt, bool rmdecopt)
332{
333 int optsz1 = opt.size();
334 if(optsz1<1) return(0);
335 // On appelle d'abord le decodage de la classe PIDrawer de laquelle
336 // on herite. (Pas obligatoire) on decode donc ici les attributs de
337 // couleur, fontes ...
338 int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
339 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
340
341 vector<string> udopt; // On gardera ici les options non decodees
342 unsigned int k = 0;
343 int ndec = opt.size();
344 for( k=0; k<opt.size(); k++ ) {
345 string opts = opt[k];
346 if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
347 else if( opts=="nsta" || opts=="nstat"
348 || opts=="nostat" || opts=="nostats") SetStats(false);
349 else if(opts.substr(0,11) == "statposoff=") {
350 float xo=0., yo=0.;
351 sscanf(opts.substr(11).c_str(),"%g,%g",&xo, &yo);
352 SetStatPosOffset(xo, yo);
353 }
354 else if (opts == "connectpoints") ConnectPoints(true);
355 else if (opts == "noconnectpoints") ConnectPoints(false);
356 else if (opts == "colorscale") UseColorScale(true);
357 else if (opts == "nocolorscale") UseColorScale(false);
358 else if (opts == "sizescale") UseSizeScale(true);
359 else if (opts == "nosizescale") UseSizeScale(false);
360 else if (opts.substr(0,10) == "sizescale=") {
361 int nbn = atoi(opts.substr(10).c_str());
362 UseSizeScale(true, nbn);
363 }
364
365 else {
366 // Si option non decode
367 ndec--;
368 // S'il faut supprimer les options decodees
369 if (rmdecopt) udopt.push_back(opts);
370 }
371 }
372 // S'il faut supprimer les options decodees, on remplace l'argument opt
373 // par le vecteur des options non decodees.
374 if (rmdecopt) opt = udopt;
375 return(ndec+ndec1);
376}
377
378int PINTuple::OptionToString(vector<string> & opt) const
379{
380 PIDrawer::OptionToString(opt);
381
382 char str[256];
383
384 if(stats) opt.push_back("stat"); else opt.push_back("nstat");
385
386 sprintf(str,"statposoff=%g,%g",spoX,spoY); opt.push_back(str);
387
388 if(connectPts) opt.push_back("connectpoints");
389 else opt.push_back("noconnectpoints");
390
391 if(colorScale) opt.push_back("colorscale");
392 else opt.push_back("nocolorscale");
393
394 if(mrkSzScale) {
395 if(nWbins>0) {sprintf(str,"sizescale=%d",nWbins); opt.push_back(str);}
396 else opt.push_back("sizescale");
397 } else opt.push_back("nosizescale");
398
399 return 1;
400}
401
402/* La methode GetOptionsHelpInfo(string& info) renvoie une chaine
403 avec la description des options comprises par ce drawer
404 Note: Il est preferable de ne pas initialiser la chaine
405 string info au depart, afin de permettre de mettre bout a
406 bout les aides de differents Drawer */
407
408/* --Methode-- */
409void PINTuple::GetOptionsHelpInfo(string& info)
410{
411info += " ---- PINTuple options help info : \n" ;
412info += " sta,stat,stats: activate statistic display\n";
413info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
414info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
415info += " as a fraction of total size \n";
416info += " connectpoints: The points are connected by a line \n";
417info += " noconnectpoints (this is the default) \n";
418info += " colorscale/nocolorscale (Use color scale for weight) \n";
419info += " sizescale/sizescale=nbins/nosizescale (Use marker size for weight) \n";
420info += " (and usual color/line/marker/... attribute decoding) \n";
421// On recupere ensuite la chaine info de la classe de base
422PIDrawer::GetOptionsHelpInfo(info);
423return;
424}
425
426
427/* --Methode-- */
428double PINTuple::GetDistanceToPoint(double x, double y)
429{
430 if(!mNT) return 1.e+9;
431 if( xK<0 || yK<0 ) return 1.e+9;
432
433 const int nessai = 100;
434 long inc = (NptDraw>nessai) ? (long)(NptDraw/nessai)+1 : 1;
435
436 double dist = -1.e+18;
437 long n = 0;
438 for(long i=0; i<(long)mNT->NbLines(); i++) {
439 double xp=mNT->GetCell(i,xK);
440 if(xp<XMin() || xp>XMax()) continue;
441 double yp=mNT->GetCell(i,yK);
442 if(yp<YMin() || yp>YMax()) continue;
443 if(n%inc==0) {
444 xp = (xp-x)/(XMax()-XMin())/0.5;
445 yp = (yp-y)/(YMax()-YMin())/0.5;
446 xp = xp*xp+yp*yp;
447 if(dist<0. || xp<dist) dist = xp;
448 }
449 n++;
450 }
451 dist=sqrt(fabs(dist));
452 //cout<<"PINTuple: xlim="<<XMin()<<","<<XMax()<<" ylim="<<YMin()<<","<<YMax()
453 // <<" NbLines="<<mNT->NbLines()<<" inc="<<inc<<endl;
454 //cout<<"....d="<<dist<<" x="<<x<<" y="<<y<<" NptDraw="<<NptDraw<<endl;
455
456 return dist;
457}
Note: See TracBrowser for help on using the repository browser.