source: Sophya/trunk/SophyaPI/PIext/nobjmgr.cc@ 330

Last change on this file since 330 was 330, checked in by ercodmgr, 26 years ago

Amelioration du Help pour piapp (Groupe de Help) Reza 24/6/99

File size: 46.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4
5#include <typeinfo>
6#include <iostream.h>
7#include <string>
8#include <list>
9#include <map>
10#if defined(__KCC__)
11using std::string ;
12#include <list.h>
13#include <map.h>
14#endif
15
16#include "strutil.h"
17#include "datatypes.h"
18
19#include "nobjmgr.h"
20#include "servnobjm.h"
21#include "nomgadapter.h"
22#include "pistdimgapp.h"
23
24#include "matrix.h"
25#include "cvector.h"
26#include "ntuple.h"
27
28// EVOL-PLANCK
29#ifdef SANS_EVOLPLANCK
30#include "fitsimage.h"
31#endif
32
33#include "fct1dfit.h"
34#include "fct2dfit.h"
35
36#include "pisurfdr.h"
37#include "pipodrw.h"
38#include "pihisto.h"
39#include "hisprof.h"
40#include "pihisto2d.h"
41#include "pintuple.h"
42#include "pintup3d.h"
43#include "pigfd1.h"
44#include "pigfd2.h"
45
46
47//++
48// Class NamedObjMgr
49// Lib PI
50// include nobjmgr.h
51//
52// Cette classe fournit les services nécéssaire àla gestion des objets
53// (l'ensemble des objets PPersist de PEIDA++) au sein du programme
54// d'analyse interactive *piapp* . Elle constitue en outre l'interface
55// entre les fonctions utilisateur et l'application graphique.
56//--
57//++
58// Links Voir aussi
59// PIStdImgApp
60// Services2NObjMgr
61// PIACmd
62//--
63
64
65// ..................................................................
66// ...... Gestion des objets nommes, variables globales ............
67struct nobj_item {
68 AnyDataObj* obj;
69 NObjMgrAdapter* obja;
70 int num;
71 list<int> wrsid;
72 bool operator==(nobj_item const& b) const
73 { return (this->obj == b.obj); }
74};
75
76typedef map<string, nobj_item, less<string> > NObjList;
77
78static NObjList* myObjs;
79static int fgOInit = 0;
80static int myNObj = 0;
81static string* lastobj = NULL;
82
83static PIStdImgApp* myImgApp=NULL;
84static Services2NObjMgr* servnobjm=NULL;
85
86static string* TmpDir; // Repertoire pour les compilations / link dynamique
87
88//++
89// Titre Constructeurs
90//--
91//++
92// NamedObjMgr()
93// Constructeur. Les différents instantiation de la classe "NamedObjMgr"
94// dans une même application créent des objets qui travaillent sur la même
95// liste d'objets. Les objets de cette classe ne possedent en effet pas
96// de variables membres.
97//--
98
99/* --Methode-- */
100NamedObjMgr::NamedObjMgr()
101{
102if (fgOInit == 0) {
103 myObjs = new NObjList;
104 lastobj = new string("");
105 char* varenv;
106 TmpDir = new string("");
107 if ( (varenv=getenv("PEIDA_TMP")) != NULL ) (*TmpDir) = varenv;
108 else if ( (varenv=getenv("TMPDIR")) != NULL ) (*TmpDir) = varenv;
109 int l = (*TmpDir).length();
110 if ( (l>0) && ((*TmpDir)[l-1] != '/') ) (*TmpDir) += '/';
111 servnobjm = new Services2NObjMgr(NULL, (*TmpDir));
112 }
113fgOInit++;
114}
115
116
117/* --Methode-- */
118NamedObjMgr::~NamedObjMgr()
119{
120fgOInit--;
121if (fgOInit == 0) delete myObjs;
122}
123
124//++
125// Titre Méthodes
126//--
127//++
128// void SetImgApp(PIStdImgApp* app)
129// Spécifie l'objet "PIStdImgApp" associé.
130// PIStdImgApp* GetImgApp()
131// Accès à l'objet "PIStdImgApp" associé.
132//--
133
134/* --Methode-- */
135void NamedObjMgr::SetImgApp(PIStdImgApp* app)
136{
137myImgApp = app;
138servnobjm->SetImgApp(app);
139}
140
141/* --Methode-- */
142PIStdImgApp* NamedObjMgr::GetImgApp()
143{
144return(myImgApp);
145}
146
147/* --Methode-- */
148Services2NObjMgr* NamedObjMgr::GetServiceObj()
149{
150return(servnobjm);
151}
152
153//++
154// Titre Gestion de la liste des objets
155//--
156//++
157// void AddObj(AnyDataObj* obj, string& nom)
158// Ajoute l'objet "obj" à la liste, identifié par "nom".
159// Si un objet de même nom existe, l'ancien objet est renommé en concaténant
160// un numéro à son nom.
161// void DelObj(string const& nom, bool fgd=true)
162// Supprime l'objet "nom" de la liste. L'objet est détruit si "fgd==true" ("delete obj")
163// void DelObjects(string const& patt, bool fgd=true)
164// Supprime l'ensemble des objets dont le nom correspond au patron "patt".
165// Le patron peut contenir les caractères "*" et "?" . Les objets sont détruits si "fgd==true"
166// AnyDataObj* GetObj(string const& nom)
167// Retourne l'objet identifié par "nom" dans la liste. Retourne "NULL" si "nom" n'est
168// pas dans la liste.
169// void RenameObj(string const& nom, string& nomnew)
170// Change le nom d'un objet dans la liste.
171// string LastObjName()
172// Renvoie le nom du dernier objet ajouté à la liste
173//--
174
175
176/* --Methode-- */
177void NamedObjMgr::AddObj(AnyDataObj* obj, string& nom, bool)
178{
179
180if (obj == NULL) return;
181// on supprime les blancs de debut et de fin
182size_t p = nom.find_first_not_of(" ");
183if(p>nom.length()) {
184 nom = "";
185} else {
186 nom = nom.substr(p);
187 p = nom.find_first_of(" ");
188 if(p>nom.length()) p=nom.length();
189 nom = nom.substr(0, p);
190}
191
192myNObj++;
193if (nom.length() < 1) nom = typeid(*obj).name();
194NObjList::iterator it = myObjs->find(nom);
195if (it != myObjs->end()) { // l'objet existe deja
196 char strg[16];
197 sprintf(strg, "%d", myNObj);
198 string nomnew = nom + strg;
199 RenameObj(nom, nomnew);
200 }
201
202(*lastobj) = nom;
203nobj_item no;
204no.obj = obj;
205no.obja = servnobjm->GetAdapter(obj); // L'adaptateur
206no.num = myNObj;
207(*myObjs)[(*lastobj)] = no;
208if (myImgApp) {
209 string str = (*lastobj) + " (T= " + typeid(*obj).name() + ")" ;
210 (myImgApp->ObjMgrW())->AddObj(str.c_str(), myNObj+1000);
211}
212
213cout << "NamedObjMgr::AddObj() Object " << (*lastobj) << " ( "
214 << typeid(*obj).name() << " ) added (Total= " << myObjs->size() << ")" << endl;
215return;
216}
217
218/* --Methode-- */
219void NamedObjMgr::DelObj(string const& nom, bool fgd)
220{
221NObjList::iterator it = myObjs->find(nom);
222if (it == myObjs->end()) return;
223list<int>::iterator ii;
224if (myImgApp) {
225//DBG cerr << " *DBG* NamedObjMgr::DelObj Sz= " << (*it).second.wrsid.size() << endl;
226 for(ii=(*it).second.wrsid.begin(); ii != (*it).second.wrsid.end(); ii++)
227 myImgApp->DelWRsId((*ii));
228 (myImgApp->ObjMgrW())->DelObj((*it).second.num+1000);
229}
230delete (*it).second.obja; // destruction de l'adaptateur
231if (fgd) delete (*it).second.obj;
232myObjs->erase(it);
233if (fgd) cout << "NamedObjMgr::DelObj() Object " << nom << " deleted (Total= " << myObjs->size() << ")" << endl;
234else cout << "NamedObjMgr::DelObj() Object " << nom << " removed (Total= " << myObjs->size() << ")" << endl;
235return;
236}
237
238/* --Methode-- */
239void NamedObjMgr::DelObjects(string const& patt, bool fgd)
240{
241NObjList::iterator it;
242list<string> odel;
243string cn;
244for(it = myObjs->begin(); it != myObjs->end(); it++) {
245 cn = (*it).first;
246 if (csh_parse(cn.c_str(), patt.c_str()) != 0) odel.push_back(cn);
247 }
248list<string>::iterator ii;
249for(ii=odel.begin(); ii != odel.end(); ii++) DelObj(*ii, fgd);
250}
251
252/* --Methode-- */
253AnyDataObj* NamedObjMgr::GetObj(string const& nom)
254{
255NObjList::iterator it = myObjs->find(nom);
256if (it == myObjs->end()) return(NULL);
257return((*it).second.obj);
258}
259
260/* --Methode-- */
261NObjMgrAdapter* NamedObjMgr::GetObjAdapter(string const& nom)
262{
263NObjList::iterator it = myObjs->find(nom);
264if (it == myObjs->end()) return(NULL);
265return((*it).second.obja);
266}
267
268/* --Methode-- */
269void NamedObjMgr::RenameObj(string const& nom, string& nomnew)
270{
271AnyDataObj* obj = GetObj(nom);
272if (obj == NULL) return;
273DelObj(nom, false);
274AddObj(obj, nomnew);
275return;
276}
277
278/* --Methode-- */
279string NamedObjMgr::LastObjName()
280{
281return((*lastobj));
282}
283
284//++
285// Titre Entrées-Sorties (I/O) sur les objets
286//--
287//++
288// void ReadObj(PInPersist& s, int num=-1)
289// Lit l'objet à partir avec le tag numéro "num" dans le flot "PInPersist s"
290// et l'ajoute à la liste. Si "num" est négatif, tous les objets présents
291// sur le flot "s" sont créés et ajoutés à la liste.
292// void ReadObj(string const & nomppf, string nobj="")
293// Lit le premier objet à partir du fichier PPF "nomppf". L'objet est ajouté
294// à la liste avec le nom "nobj". Si "nobj" est une chaîne vide, un nom est
295// composé à partir du nom de fichier.
296//--
297
298/* --Methode-- */
299void NamedObjMgr::ReadObj(string const & flnm, string nobj)
300{
301PPersist* obj=NULL;
302bool ok = true;
303
304TRY{
305 PInPersist pis(flnm);
306 obj = PPersistMgr::ReadObject(pis);
307 if (obj == NULL) ok = false;
308} CATCH(merr)
309 { printf("NamedObjMgr::ReadObj()/Error Exception= %ld (%s) \n",
310 (long)merr, PeidaExc(merr)); ok = false; } ENDTRY;
311
312if (!ok) return;
313if (nobj.length()<1) nobj = servnobjm->FileName2Name(flnm);
314AddObj(obj->DataObj(), nobj);
315return;
316}
317
318/* --Methode-- */
319void NamedObjMgr::ReadObj(PInPersist& s, int num)
320{
321int i, cid, key, ln;
322int n0, n1;
323bool ok = true;
324PPersist* obj=NULL;
325string nom;
326
327if ( (s.NbTags() < 1) || (num >= s.NbTags()) ) {
328 if (num >= 0) {
329 printf("NamedObjMgr::ReadObj(PInPersist, %d) Error! NbTags = %d \n", num, s.NbTags());
330 return;
331 }
332 TRY {
333 obj = PPersistMgr::ReadObject(s);
334 if (obj == NULL) ok = false;
335 } CATCH(merr) {
336 printf("NamedObjMgr::ReadObj()/Error Exception= %ld (%s) \n", (long)merr, PeidaExc(merr));
337 ok = false;
338 } ENDTRY;
339 if (!ok) return;
340 nom = "";
341 AddObj(obj->DataObj(), nom);
342}
343
344if (num < 0) { n0 = 0; n1 = s.NbTags(); }
345else { n0 = num; n1 = num+1; }
346for(i=n0; i<n1; i++) {
347 key = s.TagKey(i, cid, ln);
348 if (ln <= 0) nom = "";
349 else nom = s.TagName(i);
350 s.GotoTag(i);
351 TRY {
352 obj = PPersistMgr::ReadObject(s);
353 if (obj == NULL) ok = false;
354 } CATCH(merr) {
355 printf("NamedObjMgr::ReadObj()/Error Exception= %ld (%s) \n", (long)merr, PeidaExc(merr));
356 ok = false;
357 } ENDTRY;
358 if (ok) AddObj(obj->DataObj(), nom);
359}
360
361return;
362}
363/* --Methode-- */
364void NamedObjMgr::ReadAll(string const & flnm)
365{
366bool ok = true;
367PPersist* obj=NULL;
368
369PInPersist* ppin;
370TRY{
371 ppin = new PInPersist(flnm);
372 if (ppin->NbTags() < 1) obj = PPersistMgr::ReadObject((*ppin));
373 else obj = NULL;
374} CATCH(merr)
375 { printf("NamedObjMgr::ReadAll()/Error Exception= %ld (%s) \n",
376 (long)merr, PeidaExc(merr)); ok = false; } ENDTRY;
377
378if (!ok) return;
379if (obj) {
380 string nom = servnobjm->FileName2Name(flnm);
381 AddObj(obj->DataObj(), nom);
382 }
383else ReadObj((*ppin), -1);
384delete ppin;
385return;
386}
387
388/* --Methode-- */
389void NamedObjMgr::ReadFits(string const & flnm, string nobj)
390{
391bool ok = true;
392RzImage* obj;
393
394TRY{
395// obj = RzReadFits((char*)flnm.c_str(), ImgOffX, ImgOffY, ImgSizX, ImgSizY, ImgBitSgn);
396#ifdef SANS_EVOLPLANCK
397 obj = RzReadFits((char*)flnm.c_str());
398#else
399 printf("NamedObjMgr::ReadFITS( NON-Disponible EVOL-PLANCK) \n");
400 obj = NULL;
401#endif
402 if (obj == NULL) ok = false;
403} CATCH(merr) {
404 printf("NamedObjMgr::ReadFITS(_Error Exception= %ld (%s) \n", (long)merr, PeidaExc(merr));
405 ok = false;
406} ENDTRY;
407if (ok) {
408 if (nobj.length()<1) nobj = servnobjm->FileName2Name(flnm);
409 AddObj((AnyDataObj*)obj, nobj);
410}
411return;
412}
413
414
415static int key_for_write = 5000;
416/* --Methode-- */
417void NamedObjMgr::SaveObj(string const& nom, POutPersist& s)
418{
419NObjMgrAdapter* obja=NULL;
420obja = GetObjAdapter(nom);
421if (obja == NULL) return;
422printf("NamedObjMgr::SaveObj(%s, ) (Type=%s) \n",
423 nom.c_str(), typeid(*(obja->GetDataObj())).name());
424obja->SavePPF(s, nom);
425return;
426}
427
428/* --Methode-- */
429void NamedObjMgr::SaveAll(string const& flnm)
430{
431bool ok = true;
432
433POutPersist* pout;
434TRY{
435 pout = new POutPersist(flnm);
436} CATCH(merr)
437 { printf("NamedObjMgr::SaveAll()/Error Exception= %ld (%s) \n",
438 (long)merr, PeidaExc(merr)); ok = false; } ENDTRY;
439if (!ok) return;
440NObjList::iterator it;
441for(it = myObjs->begin(); it != myObjs->end(); it++) SaveObj((*it).first, (*pout));
442delete pout;
443return;
444}
445
446/* --Methode-- */
447void NamedObjMgr::SaveFits(string const& nom, string const & flnm)
448{
449NObjMgrAdapter* obja=NULL;
450obja = GetObjAdapter(nom);
451if (obja == NULL) return;
452obja->SaveFits(flnm);
453return;
454}
455
456
457/* --Methode-- */
458void NamedObjMgr::ListObjs()
459{
460int k;
461AnyDataObj* obj=NULL;
462string ctyp;
463char strg[256];
464
465cout << "NamedObjMgr::ListObjs() NObjs= " << myObjs->size() << "\n" ;
466NObjList::iterator it; k = 0;
467for(it = myObjs->begin(); it != myObjs->end(); it++) {
468 obj = (*it).second.obj;
469
470 ctyp = typeid(*obj).name();
471 sprintf(strg, "%2d/ %16s : %s", k, typeid(*obj).name(), ((*it).first).c_str());
472 ctyp = strg;
473 cout << ctyp << "\n" ;
474 k++;
475}
476cout << endl;
477return;
478}
479
480/* --Methode-- */
481void NamedObjMgr::PrintObj(string const& nom)
482{
483NObjMgrAdapter* obja=NULL;
484obja = GetObjAdapter(nom);
485if (obja == NULL) return;
486
487string ctyp = typeid(*obja->GetDataObj()).name();
488cout << "NamedObjMgr::PrintObj(" << nom << ") Type: " << ctyp << endl;
489obja->Print(cout);
490
491return;
492}
493
494/* --Methode-- */
495void NamedObjMgr::DisplayObj(string const& nom, string dopt)
496{
497NObjMgrAdapter* obja=NULL;
498obja = GetObjAdapter(nom);
499if (obja == NULL) {
500 cout << "NamedObjMgr::DisplayObj() Error , Pas d'objet de nom " << nom << endl;
501 return;
502}
503if (!myImgApp) return;
504
505PIDrawer * dr = NULL;
506P2DArrayAdapter* arr = NULL;
507dr = obja->GetDrawer(dopt);
508if (!dr) arr = obja->Get2DArray(dopt);
509
510if (!dr && !arr) {
511 string ctyp = typeid(*(obja->GetDataObj())).name();
512 cout << "NamedObjMgr::DisplayObj() Error , Pas de display pour " << ctyp << endl;
513 return;
514 }
515
516int wrsid = 0;
517bool fgsr = true;
518int opt = servnobjm->DecodeDispOption(dopt, fgsr);
519
520if (dr) {
521 PIDrawer3D * dr3 = dynamic_cast<PIDrawer3D *>(dr);
522 if(dr3) wrsid = myImgApp->Disp3DDrawer(dr3, nom, opt);
523 else wrsid = myImgApp->DispScDrawer( dr, nom, opt);
524 }
525else if (arr) wrsid = myImgApp->DispImage(arr, nom, opt);
526
527if(wrsid != 0) {
528 NObjList::iterator it = myObjs->find(nom);
529 if (it == myObjs->end()) return;
530 (*it).second.wrsid.push_back(wrsid);
531 }
532if (fgsr) myImgApp->RestoreGraphicAtt();
533return;
534}
535
536/* --Methode-- */
537void NamedObjMgr::DisplayImage(string const& nom, string dopt)
538{
539NObjMgrAdapter* obja=NULL;
540obja = GetObjAdapter(nom);
541if (obja == NULL) {
542 cout << "NamedObjMgr::DisplayImage() Error , Pas d'objet de nom " << nom << endl;
543 return;
544}
545if (!myImgApp) return;
546
547P2DArrayAdapter* arr = obja->Get2DArray(dopt);
548
549if (!arr) {
550 string ctyp = typeid(*(obja->GetDataObj())).name();
551 cout << "NamedObjMgr::DisplayImage() Error , Non supporte pour " << ctyp << endl;
552 return;
553 }
554
555int wrsid = 0;
556bool fgsr = true;
557int opt = servnobjm->DecodeDispOption(dopt, fgsr);
558wrsid = myImgApp->DispImage(arr, nom, opt);
559
560if(wrsid != 0) {
561 NObjList::iterator it = myObjs->find(nom);
562 if (it == myObjs->end()) return;
563 (*it).second.wrsid.push_back(wrsid);
564 }
565if (fgsr) myImgApp->RestoreGraphicAtt();
566return;
567}
568/* --Methode-- */
569void NamedObjMgr::DisplaySurf3D(string const& nom, string dopt)
570{
571NObjMgrAdapter* obja=NULL;
572obja = GetObjAdapter(nom);
573if (obja == NULL) {
574 cout << "NamedObjMgr::DisplayImage() Error , Pas d'objet de nom " << nom << endl;
575 return;
576}
577if (!myImgApp) return;
578
579P2DArrayAdapter* arr = obja->Get2DArray(dopt);
580
581if (!arr) {
582 string ctyp = typeid(*(obja->GetDataObj())).name();
583 cout << "NamedObjMgr::DisplaySurf3D() Error , Non supporte pour " << ctyp << endl;
584 return;
585 }
586
587if ((arr->XSize() > 250) || (arr->YSize() > 250)) {
588 cout << "NamedObjMgr::DisplaySurf3D() Error , 2D-Array(" << arr->XSize()
589 << "x" << arr->YSize() << ") trop grand (max=250x250)" << endl;
590 delete arr;
591 return;
592 }
593
594int wrsid = 0;
595bool fgsr = true;
596int opt = servnobjm->DecodeDispOption(dopt, fgsr);
597PISurfaceDrawer* sdr = new PISurfaceDrawer(arr, true, true, true);
598wrsid = myImgApp->Disp3DDrawer(sdr, nom, opt);
599if(wrsid >= 0) {
600 NObjList::iterator it = myObjs->find(nom);
601 if (it == myObjs->end()) return;
602 (*it).second.wrsid.push_back(wrsid);
603 }
604
605if (fgsr) myImgApp->RestoreGraphicAtt();
606return;
607}
608
609/* --Methode-- */
610void NamedObjMgr::DisplayNT(string const& nom, string& nmx, string& nmy, string& nmz,
611 string& erx, string& ery, string& erz, string dopt)
612{
613AnyDataObj* obj=GetObj(nom);
614if (obj == NULL) {
615 cout << "NamedObjMgr::DisplayNT() Error , Pas d'objet de nom " << nom << endl;
616 return;
617}
618if (!myImgApp) return;
619
620NTupleInterface * nt = dynamic_cast<NTupleInterface *>(obj);
621if (nt == NULL) {
622// if (typeid(*obj) != typeid(NTupleInterface)) {
623 string ctyp = typeid(*obj).name();
624 cout << "NamedObjMgr::DisplayNT() Error , Objet n'est pas un NTuple " << ctyp << endl;
625 return;
626 }
627
628int wrsid = 0;
629bool fgsr = true;
630dopt = "defline," + dopt;
631int opt = servnobjm->DecodeDispOption(dopt, fgsr);
632
633if (nmz.length()>0) { // Display 3D
634 PINTuple3D* pin = new PINTuple3D(nt, false);
635 pin->SelectXYZ(nmx.c_str(), nmy.c_str(), nmz.c_str());
636 pin->SelectErrBar(erx.c_str(), ery.c_str(), erz.c_str());
637 string titre = nmz + "%" + nmy + "%" + nmz;
638 wrsid = myImgApp->Disp3DDrawer(pin, nom, opt, titre);
639}
640else {
641 PINTuple* pin = new PINTuple(nt, false);
642 pin->SelectXY(nmx.c_str(), nmy.c_str());
643 pin->SelectErrBar(erx.c_str(), ery.c_str());
644 string titre = nmy + "%" + nmz;
645 wrsid = myImgApp->DispScDrawer( (PIDrawer*)pin, nom, opt, titre);
646 }
647
648if(wrsid >= 0) {
649 NObjList::iterator it = myObjs->find(nom);
650 if (it == myObjs->end()) return;
651 (*it).second.wrsid.push_back(wrsid);
652 }
653
654if (fgsr) myImgApp->RestoreGraphicAtt();
655return;
656}
657
658/* --Methode-- cmv 13/10/98 */
659void NamedObjMgr::DisplayGFD(string const& nom, string& numvarx, string& numvary, string& err, string dopt)
660// Pour le display 2D ou 3D d'un ``GeneralFitData''.
661//| nom = nom de l'objet GeneralFitData a representer.
662//| numvarx = numero (nombre entier) de la 1ere variable d'abscisse.
663//| numvary = numero (nombre entier) de la 2sd variable d'abscisse (3D).
664//| Pour le display 2D, numvary="" string vide.
665//| err = qu'elles erreurs faut il representer ?
666//| - 2D : x y xy (display y=f(x))
667//| - 3D : x y z xy xz yz xzy (display z=f(x,y))
668//| Ceci n'est suivi que si on a PI_NotDefLineAtt, sinon toutes
669//| les barres d'erreurs sont representees.
670//| opt = options generales pour le display.
671{
672AnyDataObj* obj=GetObj(nom);
673if(obj == NULL)
674 {cout << "NamedObjMgr::DisplayGFD() Error , Pas d'objet de nom " << nom << endl;
675 return;}
676if(!myImgApp) return;
677if(typeid(*obj) != typeid(GeneralFitData))
678 {string ctyp = typeid(*obj).name();
679 cout<<"NamedObjMgr::DisplayGFD() Error , Objet n'est pas un GeneralFitData "<<ctyp<<endl;
680 return;}
681
682// Decodage des options classiques
683bool fgsr = true;
684int opt = servnobjm->DecodeDispOption(dopt, fgsr);
685// Decodage des erreurs a representer
686bool errx=false, erry=false, errz=false;
687if(err.length()>0) {
688 for(int i=0;i<err.length();i++)
689 if (err[i]=='x' || err[i]=='X') errx = true;
690 else if(err[i]=='y' || err[i]=='Y') erry = true;
691 else if(err[i]=='z' || err[i]=='Z') errz = true;
692}
693// Decodage des numeros de variables en abscisse
694int numvx=-1, numvy=-1;
695if(numvarx.length()>0) numvx = atoi(numvarx.c_str());
696if(numvary.length()>0) numvy = atoi(numvary.c_str());
697
698int wrsid = 0;
699if(numvy>=0) { // Display 3D
700 PIGenFitDat3D* pigfd = new PIGenFitDat3D(((GeneralFitData*)obj),false);
701 pigfd->SelectXY(numvx,numvy);
702 pigfd->SelectErrBar(errx,erry,errz);
703 wrsid = myImgApp->Disp3DDrawer(pigfd,nom,opt);
704} else { // Display 2D
705 PIGenFitDat* pigfd = new PIGenFitDat(((GeneralFitData*)obj),false);
706 pigfd->SelectX(numvx);
707 pigfd->SelectErrBar(errx,erry);
708 wrsid = myImgApp->DispScDrawer((PIDrawer*)pigfd,nom,opt);
709}
710
711if(wrsid >= 0) {
712 NObjList::iterator it = myObjs->find(nom);
713 if (it == myObjs->end()) return;
714 (*it).second.wrsid.push_back(wrsid);
715}
716if (fgsr) myImgApp->RestoreGraphicAtt();
717return;
718}
719
720/* --Methode--
721void NamedObjMgr::DisplayImage(string const& nom, string dopt)
722{
723 cout << "NamedObjMgr::DisplayImage() a faire ! " << endl;
724}
725*/
726
727
728
729
730/* --Methode-- */
731void NamedObjMgr::SetGraphicAttributes(string gratt)
732{
733bool fg = false;
734servnobjm->DecodeDispOption(gratt, fg);
735}
736/* --Methode-- */
737void NamedObjMgr::SetGraphicWinZone(int nzx, int nzy, bool fcr)
738{
739if (!myImgApp) return;
740if (fcr) myImgApp->CreateGraphWin(nzx, nzy);
741else myImgApp->SetZone(nzx, nzy);
742}
743
744/* --Methode-- */
745void NamedObjMgr::DisplayPoints2D(string const& nom, string& expx, string& expy,
746 string& experrx, string& experry,
747 string& expcut, string dopt)
748{
749NObjMgrAdapter* obja=NULL;
750obja = GetObjAdapter(nom);
751if (obja == NULL) {
752 cout << "NamedObjMgr::DisplayPoints2D() Error , Pas d'objet de nom " << nom << endl;
753 return;
754 }
755if (!myImgApp) return;
756
757// Creation NTuple
758char* ntn[4] = {"expx","expy","expex","expey",};
759NTuple* nt = NULL;
760bool haserr = false;
761
762if ( (experrx.length() > 0 ) && (experry.length() > 0 ) ) { haserr = true; nt = new NTuple(4, ntn); }
763else { haserr = false; experrx = experry = "0."; nt = new NTuple(2, ntn); }
764
765servnobjm->Nobj_ComputeExpressions(obja, expx, expy, experrx, experry, expcut, nt, NULL, NULL);
766
767if (nt->NEntry() < 1) {
768 cout << "NamedObjMgr::DisplayPoints2D() Warning Zero points satisfy cut !" << endl;
769 delete nt;
770 return;
771 }
772
773// nt->Show();
774// nt->Print(0,10);
775PINTuple* pin = new PINTuple(nt, true);
776pin->SelectXY(ntn[0], ntn[1]);
777if ( haserr ) pin->SelectErrBar(ntn[2], ntn[3]);
778
779bool fgsr = true;
780dopt = "defline," + dopt;
781int opt = servnobjm->DecodeDispOption(dopt, fgsr);
782string titre = nom + ":" + expy + "%" + expx;
783myImgApp->DispScDrawer( (PIDrawer*)pin, titre, opt);
784if (fgsr) myImgApp->RestoreGraphicAtt();
785return;
786}
787
788/* --Methode-- */
789void NamedObjMgr::DisplayPoints3D(string const& nom, string& expx, string& expy, string& expz,
790 string& expcut, string dopt)
791{
792NObjMgrAdapter* obja=NULL;
793obja = GetObjAdapter(nom);
794if (obja == NULL) {
795 cout << "NamedObjMgr::DisplayPoints3D() Error , Pas d'objet de nom " << nom << endl;
796 return;
797 }
798if (!myImgApp) return;
799
800char* ntn[3] = {"expx","expy","expz"};
801NTuple* nt = new NTuple(3,ntn); // Creation NTuple
802
803string expwt = "1.";
804servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expz, expwt, expcut, nt, NULL, NULL);
805
806if (nt->NEntry() < 1) {
807 cout << "NamedObjMgr::DisplayPoints3D() Warning Zero points satisfy cut !" << endl;
808 delete nt;
809 return;
810 }
811nt->Show();
812nt->Print(0,10);
813PINTuple3D* pin = new PINTuple3D(nt, true);
814pin->SelectXYZ(ntn[0], ntn[1], ntn[2]);
815bool fgsr = true;
816dopt = "defline," + dopt;
817int opt = servnobjm->DecodeDispOption(dopt, fgsr);
818
819// Pour plot a partir de DispScDrawer
820// string nomdisp = "_NT3D_";
821// myImgApp->DispScDrawer( (PIDrawer*)pin, nomdisp, opt);
822// Pour plot a partir de Disp3DDrawer
823string titre = nom + ":" + expy + "%" + expx;
824myImgApp->Disp3DDrawer(pin, titre, opt);
825
826if (fgsr) myImgApp->RestoreGraphicAtt();
827return;
828}
829
830/* --Methode-- */
831void NamedObjMgr::ProjectH1(string const& nom, string& expx, string& expwt, string& expcut, string& nomh1, string dopt)
832{
833NObjMgrAdapter* obja=NULL;
834obja = GetObjAdapter(nom);
835if (obja == NULL) {
836 cout << "NamedObjMgr::ProjectH1() Error , Pas d'objet de nom " << nom << endl;
837 return;
838 }
839if (!myImgApp) return;
840
841Histo* h1 = NULL;
842NTuple* nt = NULL;
843AnyDataObj* oh = NULL;
844if (nomh1.length() > 0) oh=GetObj(nomh1);
845else nomh1 = "H1Proj";
846if ( (oh != NULL) && (typeid(*oh) == typeid(Histo)) ) h1 = (Histo*)oh; // Pas de remise a zero ! h1->Zero();
847else {
848 char* ntn[2]= {"hxval", "hwt"};
849 nt = new NTuple(2,ntn); // Creation NTuple
850 }
851string expz = "0.";
852servnobjm->Nobj_ComputeExpressions(obja, expx, expwt, expz, expwt, expcut, nt, h1, NULL);
853
854if ((!h1) && (!nt)) return;
855if (!h1) {
856 if (nt->NEntry() < 1) {
857 cout << "NamedObjMgr::ProjectH1() Warning Zero points satisfy cut !" << endl;
858 delete nt;
859 return;
860 }
861 double xmin, xmax;
862 nt->GetMinMax(0, xmin, xmax);
863 h1 = new Histo(xmin, xmax, 100);
864 int k;
865 float* xn;
866 for(k=0; k<nt->NEntry(); k++) {
867 xn = nt->GetVec(k);
868 h1->Add(xn[0], xn[1]);
869 }
870 delete nt;
871 AddObj(h1, nomh1);
872 }
873
874DisplayObj(nomh1, dopt);
875return;
876}
877
878/* --Methode-- */
879void NamedObjMgr::ProjectH2(string const& nom, string& expx, string& expy, string& expwt, string& expcut,
880 string& nomh2, string dopt)
881{
882NObjMgrAdapter* obja=NULL;
883obja = GetObjAdapter(nom);
884if (obja == NULL) {
885 cout << "NamedObjMgr::ProjectH2() Error , Pas d'objet de nom " << nom << endl;
886 return;
887 }
888if (!myImgApp) return;
889
890Histo2D* h2 = NULL;
891NTuple* nt = NULL;
892AnyDataObj* oh = NULL;
893if (nomh2.length() > 0) oh=GetObj(nomh2);
894else nomh2 = "H2Proj";
895if ( (oh != NULL) && (typeid(*oh) == typeid(Histo2D)) ) h2 = (Histo2D*)oh; // Pas de remise a zero ! h2->Zero();
896else {
897 char* ntn[3]= {"hxval", "hyval", "hwt"};
898 nt = new NTuple(3,ntn); // Creation NTuple
899 }
900string expz = "0.";
901servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, nt, NULL, h2);
902
903if ((!h2) && (!nt)) return;
904if (!h2) {
905 if (nt->NEntry() < 1) {
906 cout << "NamedObjMgr::ProjectH2() Warning Zero points satisfy cut !" << endl;
907 delete nt;
908 return;
909 }
910 double xmin, xmax, ymin, ymax;
911 nt->GetMinMax(0, xmin, xmax);
912 nt->GetMinMax(0, ymin, ymax);
913 h2 = new Histo2D(xmin, xmax, 50, ymin, ymax, 50);
914 int k;
915 float* xn;
916 for(k=0; k<nt->NEntry(); k++) {
917 xn = nt->GetVec(k);
918 h2->Add(xn[0], xn[1], xn[2]);
919 }
920 delete nt;
921 AddObj(h2, nomh2);
922 }
923
924DisplayObj(nomh2, dopt);
925return;
926
927}
928
929/* --Methode-- cmv 13/10/98 */
930void NamedObjMgr::ProjectHProf(string const& nom, string& expx, string& expy, string& expwt, string& expcut,
931 string& nomprof, string dopt)
932// Pour remplir un ``GeneralFitData'' a partir de divers objets:
933//| nom = nom de l'objet a projeter dans un HProf.
934//| expx = expression X de definition du bin.
935//| expy = expression Y a additionner dans le bin.
936//| expwt = expression W du poids a additionner.
937//| expcut = expression du test de selection.
938//| nomprof = nom du HProf engendre (optionnel). Si l'objet n'existe pas
939//| les limites Xmin,Xmax sont calculees automatiquement.
940//| sinon ce sont celles de l'objet preexistant.
941//| opt = options generales pour le display.
942{
943NObjMgrAdapter* obja=NULL;
944obja = GetObjAdapter(nom);
945if (obja == NULL) {
946 cout << "NamedObjMgr::ProjectHProf() Error , Pas d'objet de nom " << nom << endl;
947 return;
948 }
949if (!myImgApp) return;
950
951HProf* hprof = NULL;
952NTuple* nt = NULL;
953AnyDataObj* oh = NULL;
954if (nomprof.length() > 0) oh=GetObj(nomprof);
955else nomprof = "ProfProj";
956if( (oh!=NULL) && (typeid(*oh) == typeid(HProf)) ) hprof = (HProf*)oh;
957else {
958 char* ntn[3]= {"hxval", "hyval", "hwt"};
959 nt = new NTuple(3,ntn); // Creation NTuple
960}
961string expz = "0.";
962servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, nt, NULL, NULL, hprof);
963
964if((!hprof) && (!nt)) return;
965if(!hprof) {
966 if (nt->NEntry() < 1) {
967 cout << "NamedObjMgr::ProjectHProf() Warning Zero points satisfy cut !" << endl;
968 delete nt;
969 return;
970 }
971 double xmin, xmax;
972 nt->GetMinMax(0, xmin, xmax);
973 hprof = new HProf(xmin, xmax, 100);
974 int k;
975 float* xn;
976 for(k=0; k<nt->NEntry(); k++) {
977 xn = nt->GetVec(k);
978 hprof->Add(xn[0], xn[1], xn[2]);
979 }
980 delete nt;
981 AddObj(hprof, nomprof);
982 }
983hprof->UpdateHisto();
984
985DisplayObj(nomprof, dopt);
986return;
987}
988
989/* --Methode-- */
990void NamedObjMgr::FillVect(string const& nom, string& expx, string& expcut, string& nomvec, string dopt)
991{
992NObjMgrAdapter* obja=NULL;
993obja = GetObjAdapter(nom);
994if (obja == NULL) {
995 cout << "NamedObjMgr::FillVect() Error , Pas d'objet de nom " << nom << endl;
996 return;
997 }
998if (!myImgApp) return;
999
1000NTuple* nt = NULL;
1001if (nomvec.length() < 1) nomvec = "VecFill";
1002
1003char* ntn[2]= {"vecval", "vecwt"};
1004nt = new NTuple(1,ntn); // Creation NTuple
1005
1006string expwt = "1.";
1007string expz = "0.";
1008servnobjm->Nobj_ComputeExpressions(obja, expx, expz, expz, expwt, expcut, nt, NULL, NULL);
1009
1010if (!nt) return;
1011if (nt->NEntry() < 1) {
1012 cout << "NamedObjMgr::FillVect() Warning Zero points satisfy cut !" << endl;
1013 delete nt;
1014 return;
1015 }
1016
1017Vector* vec = new Vector(nt->NEntry());
1018int k;
1019float* xn;
1020for(k=0; k<nt->NEntry(); k++) {
1021 xn = nt->GetVec(k);
1022 (*vec)(k) = xn[0];
1023 }
1024delete nt;
1025AddObj(vec, nomvec);
1026DisplayObj(nomvec, dopt);
1027return;
1028}
1029
1030/* --Methode-- */
1031void NamedObjMgr::FillNT(string const& nom, string& expx, string& expy, string& expz, string& expt,
1032 string& expcut, string& nomnt)
1033{
1034NObjMgrAdapter* obja=NULL;
1035obja = GetObjAdapter(nom);
1036if (obja == NULL) {
1037 cout << "NamedObjMgr::FillNT() Error , Pas d'objet de nom " << nom << endl;
1038 return;
1039 }
1040if (!myImgApp) return;
1041
1042bool fgnnt = false;
1043NTuple* nt = NULL;
1044AnyDataObj* oh = NULL;
1045if (nomnt.length() > 0) oh=GetObj(nomnt);
1046else nomnt = "NTFill";
1047if ( (oh != NULL) && (typeid(*oh) == typeid(HProf)) ) {
1048 nt = (NTuple*)oh;
1049 if (nt->NVar() > 10) {
1050 cout << "NamedObjMgr::FillNT() Warning , Max 10 var ds NTuple -> new NTuple" << endl;
1051 nt = NULL;
1052 }
1053 }
1054if (nt == NULL) {
1055 char* ntn[4]= {"x", "y","z","t"};
1056 nt = new NTuple(4,ntn); // Creation NTuple
1057 fgnnt = true;
1058 }
1059
1060servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expz, expt, expcut, nt, NULL, NULL);
1061
1062if (fgnnt) AddObj(nt, nomnt);
1063return;
1064
1065}
1066
1067/* --Methode-- cmv 13/10/98 */
1068void NamedObjMgr::FillGFD(string const& nom, string& expx, string& expy, string& expz,
1069 string& experr, string& expcut, string& nomgfd)
1070// Pour remplir un ``GeneralFitData'' a partir de divers objets:
1071//| nom = nom de l'objet a transcrire selon 1D: Z=f(X) ou 2D: Z=f(X,Y) .
1072//| Vector,Matrix,Histo,HProf,Histo2D,Image<T>,StarList,NTuple,GeneralFitData
1073//| expx = expression X du GeneralFitData (1er abscisse)
1074//| expy = expression Y du GeneralFitData (2sd abscisse si non "", Z=f(X,Y))
1075//| expz = expression Z du GeneralFitData (valeur de l'ordonnee)
1076//| experr = expression de l'erreur sur l'ordonnee Z
1077//| expcut = expression du test de selection
1078//| nomgfd = nom du GeneralFitData engendre (optionnel)
1079{
1080NObjMgrAdapter* obja=NULL;
1081obja = GetObjAdapter(nom);
1082if (obja == NULL) {
1083 cout << "NamedObjMgr::FillGFD() Error , Pas d'objet de nom "<<nom<<endl;
1084 return;
1085 }
1086if(!myImgApp) return;
1087
1088// 2D ou 3D?
1089int nvar = 2;
1090if(expy.length()<=0) {nvar = 1; expy = "0.";}
1091
1092// $CHECK$ - cmv calculait le nombre d'entree ndata
1093// en fonction de chaque objet - Je l'ai vire Reza 11/05/99
1094
1095// Creation NTuple Buffer
1096char* ntn[4]= {"x","y","f","e"};
1097NTuple*nt = new NTuple(4,ntn);
1098
1099// Remplissage NTuple buffer
1100servnobjm->Nobj_ComputeExpressions(obja, expx, expy, expz, experr, expcut, nt, NULL, NULL);
1101if(nt->NEntry() < 1)
1102 {cout<<"NamedObjMgr::FillGFD() Warning Zero points satisfy cut !"<<endl;
1103 delete nt; return;}
1104
1105//Remplissage de la structure GeneraFitData
1106if (nt->NEntry() <= 0) {
1107 cout<<"NamedObjMgr::FillGFD() Warning - NData= " << nt->NEntry() << endl;
1108 delete nt;
1109 return;
1110 }
1111
1112GeneralFitData* gfd = new GeneralFitData(nvar,nt->NEntry(),0);
1113int k;
1114float* xn;
1115for(k=0; k<nt->NEntry(); k++) {
1116 xn = nt->GetVec(k);
1117 gfd->AddData(xn,xn[2],xn[3]);
1118}
1119
1120// Menage et table d'objets
1121delete nt;
1122AddObj(gfd, nomgfd);
1123return;
1124}
1125
1126
1127///////////////////// Fit 1D et 2D //////////////////////////
1128/* --Function static propre aux routines de fit 1D et 2D-- cmv 13/10/98 */
1129struct DFOptions {
1130 int okres, okfun;
1131 int polcx,polcy; double xc,yc;
1132 double err_e, err_E;
1133 double stc2;
1134 int lp,lpg;
1135 int i1,i2,j1,j2;
1136};
1137typedef struct DFOptions DFOPTIONS;
1138static void DecodeFitsOptions(string par,string step,string min,string max,string opt
1139 ,Vector& Par,Vector& Step,Vector& Min,Vector& Max,DFOPTIONS& O);
1140void DecodeFitsOptions(string par,string step,string min,string max,string opt
1141 ,Vector& Par,Vector& Step,Vector& Min,Vector& Max,DFOPTIONS& O)
1142//| Pour decoder les "string" et remplir les vecteurs du fit (cf commentaires dans Fit1D)
1143{
1144// set des vecteurs et decodage des string correspondantes
1145int NParMax = 100;
1146Par.Realloc(NParMax); Step.Realloc(NParMax);
1147Min.Realloc(NParMax); Max.Realloc(NParMax);
1148{
1149 Vector* v; string* s;
1150 {for(int i=0;i<NParMax;i++) {Par(i)=0.; Step(i)=1.; Min(i)=1.; Max(i)=-1.;}}
1151 for(int j=0;j<4;j++) {
1152 if(j==0) {v=&Par; s=&par;}
1153 else if(j==1) {v=&Step; s=&step;}
1154 else if(j==2) {v=&Min; s=&min;}
1155 else if(j==3) {v=&Max; s=&max;}
1156 if(s->length()>0) *s += ",";
1157 for(int i=0;i<NParMax;i++) {
1158 if(s->length()<=0) break;
1159 sscanf(s->c_str(),"%lf",&(*v)(i));
1160 size_t p = s->find_first_of(',') + 1;
1161 if(p>=s->length()) *s = ""; else *s = s->substr(p);
1162 }
1163 }
1164}
1165
1166// Decodage de options de opt
1167O.okres = O.okfun = 0;
1168O.polcx = O.polcy = 0;
1169O.xc = O.yc = 0.;
1170O.stc2 = 1.e-3;
1171O.err_e = O.err_E = -1.;
1172O.lp = 1; O.lpg = 0;
1173O.i1 = O.j1 = O.i2 = O.j2 = -1;
1174
1175if(opt.length()<=0) return;
1176opt = "," + opt + ",";
1177
1178if(strstr(opt.c_str(),",r,")) O.okres = 1; // residus
1179if(strstr(opt.c_str(),",f,")) O.okfun = 1; // fonction fittee
1180if(strstr(opt.c_str(),",x")) { // Demande de centrage (fit X=x-xc)
1181 O.polcx = 2; // Le centrage est calcule automatiquement
1182 size_t p = opt.find(",x");
1183 size_t q = opt.find_first_of(',',p+1);
1184 string dum = opt.substr(p,q-p);
1185 if(dum.length()>2) {
1186 sscanf(dum.c_str(),",x%lf",&O.xc);
1187 O.polcx = 1; // Le centrage est fixe par la valeur lue
1188 }
1189}
1190if(strstr(opt.c_str(),",y")) { // Demande de centrage (fit Y=y-yc)
1191 O.polcy = 2; // Le centrage est calcule automatiquement
1192 size_t p = opt.find(",y");
1193 size_t q = opt.find_first_of(',',p+1);
1194 string dum = opt.substr(p,q-p);
1195 if(dum.length()>2) {
1196 sscanf(dum.c_str(),",y%lf",&O.yc);
1197 O.polcy = 1; // Le centrage est fixe par la valeur lue
1198 }
1199}
1200if(strstr(opt.c_str(),",E")) { // Erreurs imposees a "sqrt(val)" ou "aa.b*sqrt(val)"
1201 size_t p = opt.find(",E");
1202 size_t q = opt.find_first_of(',',p+1);
1203 string dum = opt.substr(p,q-p);
1204 if(dum.length()>2) sscanf(dum.c_str(),",E%lf",&O.err_E);
1205 if(O.err_E<=0.) O.err_E = 1.;
1206 O.err_e=-1.;
1207}
1208if(strstr(opt.c_str(),",e")) { // Erreurs imposees a "1" ou "aa.b"
1209 size_t p = opt.find(",e");
1210 size_t q = opt.find_first_of(',',p+1);
1211 string dum = opt.substr(p,q-p);
1212 if(dum.length()>2) sscanf(dum.c_str(),",e%lf",&O.err_e);
1213 if(O.err_e<=0.) O.err_e = 1.;
1214 O.err_E=-1.;
1215}
1216if(strstr(opt.c_str(),",X")) { // Valeur du StopChi2
1217 size_t p = opt.find(",X");
1218 size_t q = opt.find_first_of(',',p+1);
1219 string dum = opt.substr(p,q-p);
1220 if(dum.length()>2) sscanf(dum.c_str(),",X%lf",&O.stc2);
1221 if(O.stc2<=0.) O.stc2 = 1.e-3;
1222}
1223if(strstr(opt.c_str(),",l")) { // niveau de print
1224 size_t p = opt.find(",l");
1225 size_t q = opt.find_first_of(',',p+1);
1226 string dum = opt.substr(p,q-p);
1227 float ab;
1228 if(dum.length()>2) sscanf(dum.c_str(),",l%f",&ab);
1229 if(ab<0) ab = 0.;
1230 O.lp = (int) ab; O.lpg = (int) 10.*(ab-O.lp);
1231}
1232if(strstr(opt.c_str(),",I")) { // intervalle de fit selon X
1233 size_t p = opt.find(",I");
1234 size_t q = opt.find_first_of(',',p+1);
1235 string dum = opt.substr(p,q-p);
1236 if(dum.length()>2) sscanf(dum.c_str(),",I%d/%d",&O.i1,&O.i2);
1237}
1238if(strstr(opt.c_str(),",J")) { // intervalle de fit selon Y
1239 size_t p = opt.find(",J");
1240 size_t q = opt.find_first_of(',',p+1);
1241 string dum = opt.substr(p,q-p);
1242 if(dum.length()>2) sscanf(dum.c_str(),",J%d/%d",&O.j1,&O.j2);
1243}
1244return;
1245}
1246
1247/* --Methode-- cmv 13/10/98 */
1248void NamedObjMgr:: Fit12D(string const& nom, string& func,
1249 string par,string step,string min,string max,
1250 string opt)
1251//| --------------- Fit d'objets a 1 et 2 dimensions ---------------
1252//| nom : nom de l'objet qui peut etre:
1253//| fit-1D: Vector,Histo1D,HProf ou GeneraFitData(1D)
1254//| fit-2D: Matrix,Histo2D,Imagexx ou GeneraFitData(2D)
1255//| func : pnn = fit polynome degre nn avec classe Poly (lineaire) 1D ou 2D
1256//| : Pnn = fit polynome degre nn avec GeneralFit (non-lineaire) 1D ou 2D
1257//| : gnn = fit gaussienne (hauteur) + polynome de degre nn 1D
1258//| : g = fit gaussienne (hauteur) 1D
1259//| : enn = fit exponentielle + polynome de degre nn 1D
1260//| : e = fit exponentielle 1D
1261//| : Gnn = fit gaussienne (volume) + polynome de degre nn 1D
1262//| : G = fit gaussienne (volume) 1D
1263//| : = fit gaussienne+fond (volume) 2D
1264//| : Gi = fit gaussienne+fond integree (volume) 2D
1265//| : d = fit DL de gaussienne+fond (volume) 2D
1266//| : di = fit DL de gaussienne+fond integree (volume) 2D
1267//| : D = fit DL de gaussienne+fond avec coeff variable p6 (volume) 2D
1268//| : Di = fit DL de gaussienne+fond integree avec coeff variable p6 (volume) 2D
1269//| : M = fit Moffat+fond (expos=p6) (volume) 2D
1270//| : Mi = fit Moffat+fond integree (expos=p6) (volume) 2D
1271//| par : p1,...,pn valeur d'initialisation des parametres (def=0)
1272//| step : s1,...,sn valeur des steps de depart (def=1)
1273//| min : m1,...,mn valeur des minima (def=1)
1274//| max : M1,...,Mn valeur des maxima (def=-1) (max<=min : pas de limite)
1275//| opt : options "Eaa.b,eaa.b,f,r,caa.b,Xaa.b"
1276//| f = generation d'un Objet identique contenant la fonction fittee
1277//| r = generation d'un Objet identique contenant les residus
1278//| Xaa.b = aa.b valeur du DXi2 d'arret (def=1.e-3)
1279//| la.b = niveau "a.b" de print: a=niveau de print Fit1/2D
1280//| b=niveau de debug GeneralFit
1281//| Ii1/i2 numeros des bins X de l'histos utilises pour le fit [i1,i2]
1282//|2D Jj1/j2 numeros des bins Y de l'histos utilises pour le fit [j1,j2]
1283//| - L'erreur est celle associee a l'objet si existe, 1 sinon sauf si
1284//| E = erreur prise comme la racine de la valeur a fitter
1285//| Eaa.b = erreur prise aa.b*sqrt(val)
1286//| e = erreur prise egale a 1 pour toutes les valeurs
1287//| eaa.b = erreur prise egale a aa.b
1288//| xaa.b = demande de centrage: on fit x-aa.b au lieu de x)
1289//| x = demande de centrage: on fit x-xc au lieu de x
1290//| avec xc=abscisse du milieu de l'histogramme
1291//| Actif pour: exp+poly 1D, poly 1D
1292//| gauss+poly 1D (mais xc est le centre de la gaussienne)
1293//|2D yaa.b et y = idem "xaa.b et x" mais pour y
1294{
1295AnyDataObj* obj=GetObj(nom);
1296if (obj == NULL) {
1297 cout<<"NamedObjMgr::Fit12D() Error , Pas d'objet de nom "<<nom<<endl;
1298 return;
1299}
1300if (!myImgApp) return;
1301if(func.length()<=0)
1302 {cout<<"NamedObjMgr::Fit12D() Donnez un nom de fonction a fitter."<<endl;
1303 return;}
1304string ctyp = typeid(*obj).name();
1305
1306int ndim = 0, nbinx=0, nbiny=0, ndata = 0;
1307Vector* v = NULL; Histo* h = NULL;
1308Matrix* m = NULL; Histo2D* h2 = NULL; RzImage* im = NULL;
1309GeneralFitData* g = NULL;
1310
1311 // 1D
1312if (typeid(*obj) == typeid(Vector)) {
1313 ndim = 1;
1314 v = (Vector*) obj; nbinx = v->NElts(); nbiny = 1;
1315 }
1316else if ( (typeid(*obj) == typeid(HProf)) || (typeid(*obj) == typeid(Histo)) ) {
1317 ndim = 1;
1318 h = (Histo*) obj; nbinx = h->NBins(); nbiny = 1;
1319 }
1320else if (typeid(*obj) == typeid(Matrix)) {
1321 ndim = 2;
1322 m = (Matrix*) obj; nbinx = m->NCol(); nbiny = m->NRows();
1323 }
1324else if (typeid(*obj) == typeid(Histo2D)) {
1325 ndim = 2;
1326 h2 = (Histo2D*) obj; nbinx = h2->NBinX(); nbiny = h2->NBinY();
1327 }
1328else if (typeid(*obj) == typeid(GeneralFitData)) {
1329 g = (GeneralFitData*) obj; nbinx = g->NData(); nbiny = 1;
1330 if( g->NVar()==1) ndim = 1;
1331 else if(g->NVar()==2) ndim = 2;
1332 else {
1333 cout<<"GeneralFitData ne peut avoir que 1 ou 2 variables d'abscisse: "
1334 <<((GeneralFitData*) obj)->NVar()<<endl; return; }
1335 }
1336else if (dynamic_cast<RzImage*>(obj)) {
1337 ndim = 2;
1338 im = (RzImage*) obj; nbinx = im->XSize(); nbiny = im->YSize();
1339 }
1340else {
1341 cout<<"NamedObjMgr::Fit12D() Error , Objet n'est pas un "
1342 <<"Histo1D/HProf/Vector/Histo2D/Image/Matrix/GeneralFitData "<<ctyp<<endl;
1343 return;
1344 }
1345
1346ndata = nbinx*nbiny;
1347if(ndata<=0)
1348 {cout<<"L'objet a "<<nbinx<<","<<nbiny<<" bins ("<<ndata<<")"<<endl; return;}
1349
1350// Decodage des options et des parametres, mise en forme
1351Vector Par(1); Vector Step(1); Vector Min(1); Vector Max(1); DFOPTIONS O;
1352DecodeFitsOptions(par,step,min,max,opt,Par,Step,Min,Max,O);
1353O.i1 = (O.i1<0||O.i1>=nbinx)? 0: O.i1;
1354O.i2 = (O.i2<0||O.i2>=nbinx||O.i2<O.i1)? nbinx-1: O.i2;
1355if(ndim>=2) {
1356 O.j1 = (O.j1<0||O.j1>=nbiny)? 0: O.j1;
1357 O.j2 = (O.j2<0||O.j2>=nbiny||O.j2<O.j1)? nbiny-1: O.j2;
1358} else O.j2 = O.j1 = 0;
1359if(O.polcx==2) {
1360 if(v||m) O.xc = (O.i2-O.i1+1)/2.;
1361 else if(h) O.xc = (h->XMin()+h->XMax())/2.;
1362 else if(h2) O.xc = (h2->XMin()+h2->XMax())/2.;
1363 else if(g) {double mini,maxi; g->GetMinMax(2,mini,maxi); O.xc=(mini+maxi)/2.;}
1364 else if(im) {O.xc = im->XOrg() * im->XPxSize()*(O.i2-O.i1+1)/2.;}
1365}
1366if(O.polcy==2 && ndim>=2) {
1367 if(m) O.yc = (O.j2-O.j1+1)/2.;
1368 if(h2) O.yc = (h2->YMin()+h2->YMax())/2.;
1369 if(g) {double mini,maxi; g->GetMinMax(12,mini,maxi); O.yc=(mini+maxi)/2.;}
1370 if(im) {O.yc = im->YOrg() * im->YPxSize()*(O.j2-O.j1+1)/2.;}
1371}
1372if(O.lp>0)
1373 cout<<"Fit["<<nbinx<<","<<nbiny<<"] ("<<ndata<<") dim="<<ndim<<":"
1374 <<" Int=["<<O.i1<<","<<O.i2<<"],["<<O.j1<<","<<O.j2<<"]"<<endl
1375 <<" Cent="<<O.polcx<<","<<O.polcy<<","<<O.xc<<"+x"<<","<<O.yc<<"+y"
1376 <<" TypE="<<O.err_e<<","<<O.err_E
1377 <<" StpX2="<<O.stc2
1378 <<" lp,lpg="<<O.lp<<","<<O.lpg<<endl;
1379
1380///////////////////////////////////
1381// Remplissage de GeneralFitData //
1382///////////////////////////////////
1383GeneralFitData mydata(ndim,ndata,0);
1384{for(int i=O.i1;i<=O.i2;i++) for(int j=O.j1;j<=O.j2;j++) {
1385 double x,y,f,e;
1386
1387 if(v)
1388 {x= (double) i; f=(*v)(i); e=1.;}
1389 else if(h)
1390 {x=h->BinCenter(i); f=(*h)(i); e=(h->HasErrors())?h->Error(i):1.;}
1391 else if(m)
1392 {x=(double) i; y=(double) j; f=(*m)(j,i); e=1.;}
1393 else if(h2)
1394 {float xf,yf; h2->BinCenter(i,j,xf,yf); x=(double)xf; y=(double)yf;
1395 f=(*h2)(i,j); e=(h2->HasErrors())?h2->Error(i,j):1.;}
1396 else if(im)
1397 {x=im->XOrg()+(i+0.5)*im->XPxSize(); y=im->YOrg()+(j+0.5)*im->YPxSize();
1398 f=im->DValue(i,j); e=1.;}
1399 else if(g&&ndim==1) {x= g->X(i); f=g->Val(i); e=g->EVal(i);}
1400 else if(g&&ndim==2) {x= g->X(i); y= g->Y(i); f=g->Val(i); e=g->EVal(i);}
1401 else x=y=f=e=0.;
1402
1403 // Gestion des erreurs a utiliser
1404 if(O.err_e>0.) e=O.err_e;
1405 else if(O.err_E>0.) {e=(y<-1.||y>1.)?O.err_E*sqrt(fabs(y)):O.err_E;}
1406
1407 // Remplissage de generalfit
1408 if(func[0]=='p') {x -= O.xc; if(ndim>=2) y -= O.yc;}
1409 if(ndim==1) mydata.AddData1(x,f,e);
1410 else if(ndim==2) mydata.AddData2(x,y,f,e);
1411}}
1412if(mydata.NData()<=0)
1413 {cout<<"Pas de donnees dans GeneralFitData: "<<mydata.NData()<<endl;
1414 return;}
1415if(O.lpg>1) {
1416 mydata.PrintStatus();
1417 mydata.PrintData(0);
1418 mydata.PrintData(mydata.NData()-1);
1419}
1420
1421////////////////////////////////////////////
1422// Identification de la fonction a fitter //
1423////////////////////////////////////////////
1424GeneralFunction* myfunc = NULL;
1425if(func[0]=='p' && ndim==1) {
1426 // Fit de polynome sans passer par les GeneralFit
1427 int degre = 0;
1428 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1429 cout<<"Fit (lineaire) 1D polynome de degre "<<degre<<endl;
1430 Poly p1(0);
1431 double c2rl = mydata.PolFit(0,p1,degre);
1432 cout<<"C2r_lineaire = "<<c2rl<<endl;
1433 if(O.lp>0) cout<<p1<<endl;
1434 return;
1435
1436} else if(func[0]=='P' && ndim==1) {
1437 // Fit de polynome
1438 int degre = 0;
1439 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1440 cout<<"Fit polynome 1D de degre "<<degre<<endl;
1441 Polyn1D* myf = new Polyn1D(degre,O.xc);
1442 myfunc = myf;
1443
1444} else if(func[0]=='e' && ndim==1) {
1445 // Fit d'exponentielle
1446 int degre =-1;
1447 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1448 cout<<"Fit d'exponentielle+polynome 1D de degre "<<degre<<endl;
1449 Exp1DPol* myf;
1450 if(degre>=0) myf = new Exp1DPol((unsigned int)degre,O.xc);
1451 else myf = new Exp1DPol(O.xc);
1452 myfunc = myf;
1453
1454} else if(func[0]=='g' && ndim==1) {
1455 // Fit de gaussienne en hauteur
1456 int degre =-1;
1457 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1458 cout<<"Fit de Gaussienne_en_hauteur+polynome 1D de degre "<<degre<<endl;
1459 Gauss1DPol* myf;
1460 if(degre>=0) myf = new Gauss1DPol((unsigned int)degre,((O.polcx)?true:false));
1461 else { bool bfg = (O.polcx)?true:false; myf = new Gauss1DPol(bfg); }
1462 myfunc = myf;
1463
1464} else if(func[0]=='G' && ndim==1) {
1465 // Fit de gaussienne en volume
1466 int degre =-1;
1467 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1468 cout<<"Fit de Gaussienne_en_volume+polynome 1D de degre "<<degre<<endl;
1469 GaussN1DPol* myf;
1470 if(degre>=0) myf = new GaussN1DPol((unsigned int)degre,((O.polcx)?true:false));
1471 else { bool bfg = (O.polcx)?true:false; myf = new GaussN1DPol(bfg); }
1472 myfunc = myf;
1473
1474} else if(func[0]=='p' && ndim==2) {
1475 // Fit de polynome 2D sans passer par les GeneralFit
1476 int degre = 0;
1477 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1478 cout<<"Fit (lineaire) polynome 2D de degre "<<degre<<endl;
1479 Poly2 p2(0);
1480 double c2rl = mydata.PolFit(0,1,p2,degre);
1481 cout<<"C2r_lineaire = "<<c2rl<<endl;
1482 if(O.lp>0) cout<<p2<<endl;
1483 return;
1484
1485} else if(func[0]=='P' && ndim==2) {
1486 // Fit de polynome 2D
1487 int degre = 0;
1488 if(func.length()>1) sscanf(func.c_str()+1,"%d",&degre);
1489 cout<<"Fit polynome 2D de degre "<<degre<<endl;
1490 Polyn2D* myf = new Polyn2D(degre,O.xc,O.yc);
1491 myfunc = myf;
1492
1493} else if(func[0]=='G' && ndim==2) {
1494 // Fit de gaussienne+fond en volume
1495 int integ = 0;
1496 if(func.length()>1) if(func[1]=='i') integ=1;
1497 cout<<"Fit de Gaussienne+Fond 2D integ="<<integ<<endl;
1498 if(integ) {GauRhInt2D* myf = new GauRhInt2D; myfunc = myf;}
1499 else {GauRho2D* myf = new GauRho2D; myfunc = myf;}
1500
1501} else if(func[0]=='d' && ndim==2) {
1502 // Fit de DL gaussienne+fond en volume
1503 int integ = 0;
1504 if(func.length()>1) if(func[1]=='i') integ=1;
1505 cout<<"Fit de DL de Gaussienne+Fond 2D integ="<<integ<<endl;
1506 if(integ) {GdlRhInt2D* myf = new GdlRhInt2D; myfunc = myf;}
1507 else {GdlRho2D* myf = new GdlRho2D; myfunc = myf;}
1508
1509} else if(func[0]=='D' && ndim==2) {
1510 // Fit de DL gaussienne+fond avec coeff variable p6 en volume
1511 int integ = 0;
1512 if(func.length()>1) if(func[1]=='i') integ=1;
1513 cout<<"Fit de DL de Gaussienne+Fond avec coeff variable (p6) 2D integ="<<integ<<endl;
1514 if(integ) {Gdl1RhInt2D* myf = new Gdl1RhInt2D; myfunc = myf;}
1515 else {Gdl1Rho2D* myf = new Gdl1Rho2D; myfunc = myf;}
1516
1517} else if(func[0]=='M' && ndim==2) {
1518 // Fit de Moffat+fond (volume)
1519 int integ = 0;
1520 if(func.length()>1) if(func[1]=='i') integ=1;
1521 cout<<"Fit de Moffat+Fond (expos=p6) 2D integ="<<integ<<endl;
1522 if(integ) {MofRhInt2D* myf = new MofRhInt2D; myfunc = myf;}
1523 else {MofRho2D* myf = new MofRho2D; myfunc = myf;}
1524
1525} else {
1526 cout<<"Fonction "<<func<<" inconnue pour la dim "<<ndim<<endl;
1527 return;
1528}
1529
1530/////////////////////////
1531// Fit avec generalfit //
1532/////////////////////////
1533if(myfunc->NPar()>Par.NElts())
1534 {cout<<"Trop de parametres: "<<myfunc->NPar()<<">"<<Par.NElts()<<endl;
1535 if(myfunc) delete myfunc; return;}
1536GeneralFit myfit(myfunc);
1537myfit.SetDebug(O.lpg);
1538myfit.SetData(&mydata);
1539myfit.SetStopChi2(O.stc2);
1540{for(int i=0;i<myfunc->NPar();i++) {
1541 char str[10];
1542 sprintf(str,"P%d",i);
1543 myfit.SetParam(i,str,Par(i),Step(i),Min(i),Max(i));
1544}}
1545if(O.lp>1) myfit.PrintFit();
1546double c2r = (double) myfit.Fit();
1547if(O.lp>0) myfit.PrintFit();
1548if(c2r>0.) {
1549 c2r = myfit.GetChi2Red();
1550 cout<<"C2r_Reduit = "<<c2r<<" nstep="<<myfit.GetNStep()<<endl;
1551 Vector ParFit(myfunc->NPar());
1552 for(int i=0;i<myfunc->NPar();i++) ParFit(i)=myfit.GetParm(i);
1553} else {
1554 cout<<"echec Fit, rc = "<<c2r<<" nstep="<<myfit.GetNStep()<<endl;
1555}
1556
1557// Mise a disposition des resultats
1558if(c2r>=0. && myfunc && (O.okres>0||O.okfun>0)) {
1559 string nomres = nom + "res";
1560 string nomfun = nom + "fun";
1561 if(v) {
1562 if(O.okres) {Vector* ob = v->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1563 if(O.okfun) {Vector* ob = v->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1564 } else if(h) {
1565 if(O.okres) {Histo* ob = h->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1566 if(O.okfun) {Histo* ob = h->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1567 } else if(m) {
1568 if(O.okres) {Matrix* ob = m->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1569 if(O.okfun) {Matrix* ob = m->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1570 } else if(h2) {
1571 if(O.okres) {Histo2D* ob = h2->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1572 if(O.okfun) {Histo2D* ob = h2->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1573 } else if(im) {
1574 if(O.okres) {RzImage* ob = im->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1575 if(O.okfun) {RzImage* ob = im->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1576 } else if(g) {
1577 if(O.okres) {GeneralFitData* ob = g->FitResidus(myfit); if(ob) AddObj(ob,nomres);}
1578 if(O.okfun) {GeneralFitData* ob = g->FitFunction(myfit); if(ob) AddObj(ob,nomfun);}
1579 }
1580}
1581
1582// Nettoyage
1583if(myfunc) delete myfunc;
1584return;
1585}
1586
Note: See TracBrowser for help on using the repository browser.