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

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

1/ NTupleInterface mis ds Outils++ et complete -
2/ Les PINtuple et PINtup3D utilisent maintenant NTupleInterface
3/ Debut modification interface NObjMgr - Reza 23/6/99

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