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

Last change on this file since 3055 was 2755, checked in by ansari, 20 years ago

1/ Correction bug trace des lignes ds PINTuple
2/ Ajout methode IsThreadable() (declaration conformite thread separe pour certaines commandes) ds baseexecut.h .cc (plot2d plot3d ...) , ds pawexecut.cc (n/plot ...) et ds cxxexecutor.h .cc (c++exec c++execfrf ...)
3/ Ajout de ZMutex (et ZSync) pour la gestion des commandes threadable - ds les
executeurs et ds servnobjm.h .cc
4/ bug d'execution en thread identifie et corrige ds nobjmgr.h .cc (ajout des
methodes GetObj_P() GetObjAdapter_P() et ReadObj_P()
5/ Ajout de l'appel a ZSync::NOp() pour eviter les warnings 'unused variable zs ...'

Reza 23 Mai 2005

File size: 14.3 KB
RevLine 
[537]1// Peida Interactive - PI R. Ansari 97-99
2// Traceur (Drawer) pour NTupleInterface
3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
4
[165]5#include <stdio.h>
[2517]6#include <stdlib.h>
7#include <iostream>
8#include <math.h>
[2615]9#include "sopnamsp.h"
[165]10#include "pintuple.h"
11
[537]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
[2373]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.
[537]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)
[544]40// Constructeur. Si "ad == true", l'objet "nt" est détruit par
[537]41// le destructeur de l'objet "PINTuple"
[2373]42// Note : "nt" doit être créé par new dans ce cas.
[537]43//--
[165]44
45/* --Methode-- */
[326]46PINTuple::PINTuple(NTupleInterface* nt, bool ad)
[165]47: PIDrawer()
48{
49 mNT = nt;
50 mAdDO = ad;
[544]51 SetStats(true);
[2383]52 SetStatPosOffset();
[2350]53 ConnectPoints(false);
[2373]54 UseSizeScale(true, 5);
55 UseColorScale(true);
[165]56 SelectXY(NULL, NULL);
[2373]57 SelectWt(NULL);
[165]58 SelectErrBar();
[486]59 SelectLabel(NULL);
[1856]60 SetName("NTupleDrw");
[2517]61 NptDraw = 0;
62
[165]63}
64
65PINTuple::~PINTuple()
66{
67 if (mAdDO && mNT) delete mNT;
68}
69
[537]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
[165]87/* --Methode-- */
88void PINTuple::SelectXY(const char* px, const char* py)
89{
[326]90string name;
[165]91if (mNT == NULL) xK = yK = -1;
92if (px == NULL) xK = -1;
[326]93else { name = px; xK = mNT->ColumnIndex(name); }
[165]94if (py == NULL) yK = -1;
[326]95else { name = py; yK = mNT->ColumnIndex(name); }
[165]96}
97
98/* --Methode-- */
[2373]99void PINTuple::SelectWt(const char* pw)
[333]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-- */
[486]109void PINTuple::SelectLabel(const char* plabel)
110{
111if (plabel == NULL) lK = -1;
112else { string name = plabel; lK = mNT->ColumnIndex(name); }
113}
114
115/* --Methode-- */
[165]116void PINTuple::SelectErrBar(const char* erbx, const char* erby)
117{
[326]118string name;
[165]119if (mNT == NULL) xebK = yebK = -1;
120if (erbx == NULL) xebK = -1;
[326]121else { name = erbx; xebK = mNT->ColumnIndex(name); }
[165]122if (erby == NULL) yebK = -1;
[326]123else { name = erby; yebK = mNT->ColumnIndex(name); }
[165]124}
125
126
127/* --Methode-- */
128void PINTuple::UpdateLimits()
129{
130 if (!mNT) return;
[326]131 if (mNT->NbLines() <= 0) return;
[165]132 if ( (xK < 0) || (yK < 0) ) return;
133
134 // Commencer par trouver nos limites
[326]135 double xmin, xmax, ymin, ymax;
[165]136 xmin = ymin = 9.e19;
137 xmax = ymax = -9.e19;
138 mNT->GetMinMax(xK, xmin, xmax);
139 mNT->GetMinMax(yK, ymin, ymax);
[2115]140 PIAxes::ReSizeMinMax(isLogScaleX(),xmin,xmax);
141 PIAxes::ReSizeMinMax(isLogScaleY(),ymin,ymax);
142 SetLimits(xmin,xmax,ymin,ymax);
[548]143// SetAxesFlags(kBoxAxes | kExtTicks | kLabels); Ne pas faire - Reza 11/99
[165]144}
145
146
147/* --Methode-- */
[2469]148#define NMXMULTP_LOCAL 30 // Pour multipoint sans new
[205]149void PINTuple::Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax)
[165]150{
[333]151double xp,yp,xer,yer,wp;
[326]152double xl,yl;
[2689]153long nok;
154int npolyg;
[165]155
156if (!mNT) return;
[548]157if (axesFlags != kAxesNone) DrawAxes(g);
[165]158if ( (xK < 0) || (yK < 0) ) return;
[1905]159if (GetGraphicAtt().GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
[326]160
[333]161// Pour tracer des markers avec taille fonction de Wt (poids)
162double dw = (wMax-wMin)/nWbins;
[2373]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.;
[2469]168int nwc = 1;
[2373]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();
[2469]175 dwc = (wMax-wMin)/(double)nwc;
[2373]176}
177
[333]178int msz,sz;
179
[1905]180PIMarker mmrk = GetGraphicAtt().GetMarker();
[333]181PIMarker mrk;
[1905]182if (wK >= 0) mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_CircleMarker;
183else mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_DotMarker;
[2373]184msz = GetGraphicAtt().GetMarkerSz();
[333]185if (msz < 1) msz = 1;
[344]186g->SelMarker(msz, mrk);
[333]187
[685]188PIGrCoord uxmin, uxmax, uymin, uymax;
189g->GetGrSpace(uxmin, uxmax, uymin, uymax);
190double xmin2 = uxmin;
191double ymin2 = uymin;
192double xmax2 = uxmax;
193double ymax2 = uymax;
194
[165]195nok = 0;
[326]196xp = yp = xl = yl = 0;
[2469]197PIGrCoord xpolyg[NMXMULTP_LOCAL], ypolyg[NMXMULTP_LOCAL];
[2517]198npolyg = 0;
199NptDraw = 0;
[2755]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
[2115]205for (long i=0; i<(long)mNT->NbLines(); i++) {
[2755]206 xl = xp; yl = yp; fgoklastpt = fgokcurpt;
207
[326]208 xp = mNT->GetCell(i, xK);
209 yp = mNT->GetCell(i, yK);
[2373]210
[2755]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
[2373]218// Taille - couleur de marker en fonction du poids
[2755]219 if ( fgokcurpt &&(wK >= 0) ) wp = mNT->GetCell(i, wK);
220 if ( fgokcurpt && mrkSzScale && (wK >= 0) ) { // Changement de taille
[2373]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
[2755]229 if( fgokcurpt && colorScale && (wK >= 0) && cmap ) {
[2373]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
[2469]236 // Trace d'une ligne reliant les points
237 if( connectPts ) {
[2755]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))
[2469]242 {xpolyg[npolyg]=xp; ypolyg[npolyg]=yp; npolyg++;}
[2755]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) )
[2469]246 {g->DrawPolygon(xpolyg,ypolyg,npolyg,false); npolyg=0;}
247 }
248
[2755]249 // Plus rien a faire si point en dehors -- on s'en va
250 if (!fgokcurpt) continue;
251
[2469]252 // Trace des erreurs selon X et Y
[2755]253 if (xebK >= 0) {
[326]254 xer = mNT->GetCell(i, xebK);
[165]255 if(xer>0.) g->DrawLine(xp-xer, yp, xp+xer, yp);
256 }
[2755]257 if (yebK >= 0) {
[326]258 yer = mNT->GetCell(i, yebK);
[165]259 if(yer>0.) g->DrawLine(xp, yp-yer, xp, yp+yer);
260 }
[2469]261
[486]262 // Trace du marker
[1905]263 if ((wK >= 0)||(lK < 0)||(mmrk != PI_NotDefMarker)) g->DrawMarker(xp, yp);
[2469]264
[486]265 // Trace eventuel du label
266 if (lK >= 0) g->DrawString(xp, yp, mNT->GetCelltoString(i, lK).c_str());
267
[165]268}
269
[2469]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
[544]274if (stats) { // Trace de stats
[1905]275 g->SelFontSz((YMax() - YMin())/30);
[1645]276 // La hauteur de la cellule
[544]277 PIGrCoord a,d;
278 double cH = (double)g->GetFontHeight(a,d);
[1645]279 double cellHeight = 1.2 * cH;
280 // Les labels et leurs longueurs -> largeur de la cellule
281 char label[64];
[2689]282 sprintf(label, "N=%ld (/%ld)", (long)nok, (long)mNT->NbLines());
[1644]283 double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
[1645]284 double xu, yu, cw;
[2383]285 double ofpx = spoX*(XMax()-XMin());
286 double ofpy = spoY*(YMax()-YMin());
[1645]287 // Les limites du cadre
[548]288 xu = g->DeltaUCX(XMax(), - cellWidth);
289 yu = g->DeltaUCY(YMax(), - cellHeight);
[2383]290 double recw = XMax()-xu;
291 double rech = YMax()-yu;
292 xu += ofpx; yu += ofpy;
293 g->DrawBox(xu, yu, recw, rech);
[1645]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);
[2383]299 xu += ofpx; yu += ofpy;
[1645]300 g->DrawString(xu,yu,label);
[544]301}
302
[2373]303if (cmap) delete cmap;
[165]304return;
305}
[2469]306#undef NMXMULTP_LOCAL
[165]307
[344]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
[2690]314long ncnt = 0;
[344]315double xp,yp;
316char buff[128];
[2689]317sprintf(buff,"PINTuple: NLines= %ld NCol= %ld \n", (long)mNT->NbLines(),
318 (long)mNT->NbColumns());
[344]319info += buff;
320info += mNT->LineHeaderToString();
[2115]321for(long i=0; i<(long)mNT->NbLines(); i++) {
[344]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";
[2690]330sprintf(buff," %ld points inside selected region \n", (long)ncnt);
[344]331info += buff;
332// printf("PINTuple::AppendTextInfo()-DBG %g %g %g %g - %d\n", xmin, ymin, xmax, ymax, ncnt);
333return;
334}
[1971]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{
[2229]349 int optsz1 = opt.size();
350 if(optsz1<1) return(0);
[1971]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);
[2229]355 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
[1971]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];
[2229]362 if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
363 else if( opts=="nsta" || opts=="nstat"
364 || opts=="nostat" || opts=="nostats") SetStats(false);
[2383]365 else if(opts.substr(0,11) == "statposoff=") {
366 float xo=0., yo=0.;
367 sscanf(opts.substr(11).c_str(),"%g,%g",&xo, &yo);
368 SetStatPosOffset(xo, yo);
369 }
[2350]370 else if (opts == "connectpoints") ConnectPoints(true);
371 else if (opts == "noconnectpoints") ConnectPoints(false);
[2373]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 }
[2383]380
[1971]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}
[1975]393
[2524]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=%g,%g",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
[1975]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{
[2229]427info += " ---- PINTuple options help info : \n" ;
[2350]428info += " sta,stat,stats: activate statistic display\n";
[2229]429info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
[2383]430info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
431info += " as a fraction of total size \n";
[2350]432info += " connectpoints: The points are connected by a line \n";
433info += " noconnectpoints (this is the default) \n";
[2373]434info += " colorscale/nocolorscale (Use color scale for weight) \n";
435info += " sizescale/sizescale=nbins/nosizescale (Use marker size for weight) \n";
[2383]436info += " (and usual color/line/marker/... attribute decoding) \n";
[2388]437// On recupere ensuite la chaine info de la classe de base
438PIDrawer::GetOptionsHelpInfo(info);
[2229]439return;
[1975]440}
[2373]441
442
[2517]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;
[2373]448
[2517]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.