source: Sophya/trunk/SophyaPI/PIext/servnobjm.cc@ 3361

Last change on this file since 3361 was 3279, checked in by ansari, 18 years ago

correction commandes nt2dcn nt2dci et ajout commande plot2dc - Reza 05/07/2007

File size: 43.0 KB
RevLine 
[165]1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4
[295]5#include <typeinfo>
[2322]6#include <iostream>
[165]7#include <string>
8#include <list>
9#include <map>
10
[2615]11#include "sopnamsp.h"
[165]12#include "strutil.h"
13
14#include "nobjmgr.h"
15#include "servnobjm.h"
[330]16#include "nomgadapter.h"
[165]17#include "pistdimgapp.h"
18
[333]19#include "fct1dfit.h"
20#include "fct2dfit.h"
21
[544]22#ifdef SANS_EVOLPLANCK
[333]23#include "matrix.h"
24#include "cvector.h"
[544]25#else
26#include "tmatrix.h"
27#include "tvector.h"
[2922]28#include "matharr.h"
[584]29#include "pitvmaad.h"
[544]30#endif
31
[333]32#include "ntuple.h"
33#include "cimage.h"
34
[165]35#include "histos.h"
36#include "histos2.h"
[2792]37#include "hisprof.h"
[165]38#include "ntuple.h"
[2792]39#include "datatable.h"
[165]40
[1905]41#include "piyfxdrw.h"
[326]42#include "pisurfdr.h"
[333]43
44#include "pintuple.h"
45#include "pintup3d.h"
46
[326]47#include "pipodrw.h"
[165]48
49
[326]50
[165]51/* --Methode-- */
[2491]52Services2NObjMgr::Services2NObjMgr(string& tmpdir)
[165]53{
[1276]54SetTmpDir(tmpdir);
[333]55mImgapp = NULL;
[2491]56mOmg = NULL;
[171]57dynlink = NULL;
[165]58}
59
60/* --Methode-- */
61Services2NObjMgr::~Services2NObjMgr()
62{
63CloseDLL();
[2491]64if (mOmg) delete mOmg;
[165]65}
66
67/* --Methode-- */
[295]68void Services2NObjMgr::RegisterClass(AnyDataObj* o, NObjMgrAdapter* oa)
[165]69{
[295]70ObjAdaptList::iterator it;
71for(it = objadaplist.begin(); it != objadaplist.end(); it++)
[495]72#ifdef SANS_EVOLPLANCK
[295]73 if (typeid(*o) == typeid(*((*it).obj))) THROW(dupIdErr);
[495]74#else
75 if (typeid(*o) == typeid(*((*it).obj)))
76 throw(DuplicateIdExc("Services2NObjMgr::RegisterClass() - Duplicate class"));
77#endif
[295]78dataobj_adapter oba;
79oba.obj = o;
80oba.obja = oa;
81objadaplist.push_back(oba);
82}
[165]83
[295]84/* --Methode-- */
85NObjMgrAdapter* Services2NObjMgr::GetAdapter(AnyDataObj* o)
86{
87ObjAdaptList::iterator it;
88for(it = objadaplist.begin(); it != objadaplist.end(); it++)
89 if (typeid(*o) == typeid(*((*it).obj))) return((*it).obja->Clone(o));
90return(new NObjMgrAdapter(o));
91}
[165]92
[295]93/* --Methode-- */
[1276]94void Services2NObjMgr::SetTmpDir(string const & tmpdir)
95{
96TmpDir = tmpdir;
97PDynLinkMgr::SetTmpDir(tmpdir);
98return;
99}
100
101/* --Methode-- */
[333]102void Services2NObjMgr::PlotFunc(string const & expfunc, string & nom, double xmin, double xmax, int np, string dopt)
[295]103{
[165]104FILE *fip;
[326]105string fname = TmpDir + "func1_pia_dl.c";
106string cmd;
107int rc;
108
109if (!mImgapp) return;
110
[2755]111// Pour synchronisation d'execution simultanee
[2762]112ZSync zs(mutx_dynlink); zs.NOp();
[2755]113
[326]114cmd = "rm -f " + fname;
115rc = system(cmd.c_str());
116// printf("PlotFunc_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
117
118if ((fip = fopen(fname.c_str(), "w")) == NULL) {
119 string sn = fname;
[449]120 cout << "Services2NObjMgr/PlotFunc_Error: fopen( " << sn << endl;
[326]121 return;
122 }
123
124// constitution du fichier a compiler
125fputs("#include <math.h> \n", fip);
126fputs("double func1_pia_dl_func(double x) \n{\n", fip);
127fprintf(fip,"return(%s); \n}\n", expfunc.c_str());
128fclose(fip);
129
130string func = "func1_pia_dl_func";
131DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname, func);
132if (!f) return;
[333]133PlotFunc(f, nom, xmin, xmax, np, dopt);
[326]134CloseDLL();
135return;
136}
137
138/* --Methode-- */
[333]139void Services2NObjMgr::PlotFunc2D(string const & expfunc, string & nom, double xmin, double xmax,
140 double ymin, double ymax, int npx, int npy, string dopt)
[326]141{
142FILE *fip;
143string fname = TmpDir + "func2_pia_dl.c";
144string cmd;
145int rc;
146
147if (!mImgapp) return;
[2755]148// Pour synchronisation d'execution simultanee
[2762]149ZSync zs(mutx_dynlink); zs.NOp();
[326]150
151cmd = "rm " + fname;
152rc = system(cmd.c_str());
153// printf("PlotFunc2D_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
154
155if ((fip = fopen(fname.c_str(), "w")) == NULL) {
156 string sn = fname;
[449]157 cout << "Services2NObjMgr/PlotFunc2D_Error: fopen( " << sn << endl;
[326]158 return;
159 }
160
161// constitution du fichier a compiler
162fputs("#include <math.h> \n", fip);
163fputs("double func2_pia_dl_func(double x, double y) \n{\n", fip);
164fprintf(fip,"return(%s); \n}\n", expfunc.c_str());
165fclose(fip);
166
167string func = "func2_pia_dl_func";
168DlFunctionOfXY f = (DlFunctionOfXY) LinkFunctionFromFile(fname, func);
169if (!f) return;
[333]170PlotFunc2D(f, nom, xmin, xmax, ymin, ymax, npx, npy, dopt);
[326]171CloseDLL();
172return;
173}
174
175/* --Methode-- */
[333]176void Services2NObjMgr::PlotFuncFrCFile(string const & fname, string const & func, string & nom,
177 double xmin, double xmax, int np, string dopt)
[326]178{
[2755]179// Pour synchronisation d'execution simultanee
[2762]180ZSync zs(mutx_dynlink); zs.NOp();
[326]181DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname, func);
182if (!f) return;
[333]183PlotFunc(f, nom, xmin, xmax, np, dopt);
[326]184CloseDLL();
185return;
186}
187
188/* --Methode-- */
[333]189void Services2NObjMgr::PlotFunc2DFrCFile(string const & fname, string const & func, string & nom,
190 double xmin, double xmax, double ymin, double ymax, int npx, int npy, string dopt)
[326]191{
[2755]192// Pour synchronisation d'execution simultanee
[2762]193ZSync zs(mutx_dynlink); zs.NOp();
[326]194DlFunctionOfXY f = (DlFunctionOfXY) LinkFunctionFromFile(fname, func);
195if (!f) return;
[333]196PlotFunc2D(f, nom, xmin, xmax, ymin, ymax, npx, npy, dopt);
[326]197CloseDLL();
198return;
199}
200
201/* --Methode-- */
[333]202void Services2NObjMgr::PlotFunc(DlFunctionOfX f, string & nom, double xmin, double xmax, int np, string dopt)
[326]203{
204if (!mImgapp) return;
205
206int k;
207if (np < 1) np = 1;
208if (xmax <= xmin) xmax = xmin+1.;
209Vector* vpy = new Vector(np);
210
211double xx;
212double dx = (xmax-xmin)/np;
[500]213
214try {
215 for(k=0; k<np; k++) { xx = xmin+dx*k; (*vpy)(k) = f(xx); }
216}
[495]217#ifdef SANS_EVOLPLANCK
[500]218CATCH(merr) {
[326]219 fflush(stdout);
220 cout << endl;
221 cerr << endl;
222 string es = PeidaExc(merr);
223 cerr << "Services2NObjMgr::PlotFunc() Exception :" << merr << es;
224 delete vpy;
225 vpy = NULL;
226 } ENDTRY;
[495]227#else
228catch ( PException exc ) {
229 fflush(stdout);
230 cout << endl;
231 cerr << endl;
232 cerr << "Services2NObjMgr::PlotFunc() Exception :" << exc.Msg() << endl;
233 delete vpy;
234 vpy = NULL;
235}
236#endif
[326]237
238if (vpy) {
[344]239 string titre;
240 if (nom.length() < 1) {
241 titre = "Function f(x)";
[466]242 nom = "/autoc/f_x";
[344]243 }
244 else titre = nom;
[2924]245
246 MyObjMgr()->AddObj(vpy, nom);
247
248 if ( (dopt == "nodisp") || (dopt == "nodisplay") ) return;
249
[344]250 P1DArrayAdapter* vya = new POVectorAdapter(vpy, false);
[326]251 vya->DefineXCoordinate(xmin, (xmax-xmin)/np);
252 PIYfXDrawer* dr = new PIYfXDrawer(vya, NULL, true) ;
[1971]253 dopt = "thinline " + dopt;
254 int rsid = mImgapp->DispScDrawer(dr, titre, dopt);
[2924]255 MyObjMgr()->AddWRsId(nom, rsid);
256}
[326]257return;
258}
259
260/* --Methode-- */
[333]261void Services2NObjMgr::PlotFunc2D(DlFunctionOfXY f, string & nom, double xmin, double xmax, double ymin, double ymax,
[326]262 int npx, int npy, string dopt)
263{
264if (!mImgapp) return;
265
[2781]266if (npx < 2) npx = 2;
267if (npy < 2) npy = 2;
268//if (npx > 250) npx = 250;
269//if (npy > 250) npy = 250;
[326]270if (xmax <= xmin) xmax = xmin+1.;
271if (ymax <= ymin) ymax = ymin+1.;
272
273Matrix* mtx = new Matrix(npy, npx);
274
275int i,j;
276double xx, yy;
277double dx = (xmax-xmin)/npx;
278double dy = (ymax-ymin)/npy;
279// printf(" -- DBG -- %d %d , %g %g , %g %g \n", npx, npy, xmin, xmax, ymin, ymax);
[500]280try {
[326]281 for(j=0; j<npy; j++) {
282 yy = ymin+dy*j;
283 for(i=0; i<npx; i++) {
284 xx = xmin+dx*i;
285 (*mtx)(j, i) = f(xx, yy);
286 }
287 }
[500]288}
289#ifdef SANS_EVOLPLANCK
290CATCH(merr) {
[326]291 fflush(stdout);
292 cout << endl;
293 cerr << endl;
294 string es = PeidaExc(merr);
295 cerr << "Services2NObjMgr::PlotFunc2D() Exception :" << merr << es;
296 delete mtx; mtx = NULL;
297 } ENDTRY;
[495]298#else
299catch ( PException exc ) {
300 fflush(stdout);
301 cout << endl;
302 cerr << endl;
303 cerr << "Services2NObjMgr::PlotFunc2D() Exception :" << exc.Msg() << endl;
304 delete mtx; mtx = NULL;
305}
306#endif
[326]307
308if (mtx) {
[344]309 string titre;
310 if (nom.length() < 1) {
311 titre = "Function f(x,y)";
[466]312 nom = "/autoc/f2d_xy";
[344]313 }
314 else titre = nom;
[2781]315 int rsid = 0;
[344]316 P2DArrayAdapter* arr = new POMatrixAdapter(mtx, false);
[326]317 arr->DefineXYCoordinates(xmin, ymin, dx, dy);
[2781]318 if ( (npx <= 200) && (npy <= 200) ) {
319 PISurfaceDrawer* sdr = new PISurfaceDrawer(arr, true, true, true);
320 rsid = mImgapp->Disp3DDrawer(sdr, titre, dopt);
321 }
322 else rsid = mImgapp->DispImage(arr, titre, dopt);
323
[333]324 if (nom.length() > 0) {
[2491]325 MyObjMgr()->AddObj(mtx, nom);
326 MyObjMgr()->AddWRsId(nom, rsid);
[333]327 }
[326]328 }
329
330return;
331}
332
333/* --Methode-- */
[2180]334void Services2NObjMgr::ExpVal(string expval,string resultvarname)
335{
336 // Fill C-file to be executed
337 FILE *fip;
338 string func = "eval_pia_dl_func";
339 string fname = TmpDir + func; fname += ".c";
340 string cmd = "rm -f " + fname; system(cmd.c_str());
341 if((fip=fopen(fname.c_str(), "w"))==NULL) {
342 cout << "Services2NObjMgr/EvalExp_Error: fopen("<<fname<<")"<<endl;
343 return;
344 }
345 fprintf(fip,"#include <math.h>\n");
346 fprintf(fip,"double %s(double ___dummy_variable___) \n{\n",func.c_str());
347 // Add all variables already declared
[2491]348 DVList& varlist = MyObjMgr()->GetVarList();
[2180]349 DVList::ValList::const_iterator it;
350 for(it = varlist.Begin(); it != varlist.End(); it++) {
351#ifdef SANS_EVOLPLANCK
352 MuTyV mtv = (*it).second;
353 double value = (double)(mtv);
354#else
355 double value = (double)((*it).second.elval);
356#endif
357 string name_var = (*it).first;
358 fprintf(fip,"double %s = %.17f;\n",name_var.c_str(),value);
359 }
360 fprintf(fip,"return %s;\n}\n",expval.c_str());
361 fclose(fip);
362
363 // Dynamically link function
364 DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname,func);
365 if(!f) {
366 cout<<"Services2NObjMgr/EvalExp_Error: linking DlFunctionOfX"<<endl;
367 cout<<"...expval = "<<expval<<endl;
368 return;
369 }
370
371 // Evaluate function and close dynamic link
372 double result;
373 try {
374 result = f(0.);
375 CloseDLL();
376 } catch ( ... ) {
377 cout<<"Services2NObjMgr/EvalExp_Error: Arithmetic exception"<<endl;
378 CloseDLL();
379 return;
380 }
381
382 // Eventually store the result into variable or just print it
383 if(resultvarname.size()>0) {
[2491]384 if(MyObjMgr()->HasVar(resultvarname)) MyObjMgr()->DeleteVar(resultvarname);
[2180]385 char str[512];
386 if(result==0.) sprintf(str,"%f",result);
387 else {
388 double lr = log10(fabs(result));
389 if(lr>-17. && lr<17.) sprintf(str,"%17f",result);
390 else sprintf(str,"%.17e",result);
391 }
[2491]392 MyObjMgr()->SetVar(resultvarname,(string)str);
[2180]393 } else cout<<result<<" = "<<expval<<endl;
394
395}
396
397/* --Methode-- */
[333]398void Services2NObjMgr::DisplayPoints2D(string& nom, string& expx, string& expy,
399 string& experrx, string& experry,
[357]400 string& expcut, string dopt, string loop)
[333]401{
402NObjMgrAdapter* obja=NULL;
[2491]403obja = MyObjMgr()->GetObjAdapter(nom);
[333]404if (obja == NULL) {
[449]405 cout << "Services2NObjMgr::DisplayPoints2D() Error , No such object " << nom << endl;
[333]406 return;
407 }
408if (!mImgapp) return;
409
410// Creation NTuple
411char* ntn[4] = {"expx","expy","expex","expey",};
412NTuple* nt = NULL;
413bool haserr = false;
414
415if ( (experrx.length() > 0 ) && (experry.length() > 0 ) ) { haserr = true; nt = new NTuple(4, ntn); }
416else { haserr = false; experrx = experry = "0."; nt = new NTuple(2, ntn); }
417
[357]418ComputeExpressions(obja, expx, expy, experrx, experry, expcut, loop, nt, NULL, NULL);
[333]419
420if (nt->NEntry() < 1) {
421 cout << "Services2NObjMgr::DisplayPoints2D() Warning Zero points satisfy cut !" << endl;
422 delete nt;
423 return;
424 }
425
426// nt->Show();
427// nt->Print(0,10);
428PINTuple* pin = new PINTuple(nt, true);
429pin->SelectXY(ntn[0], ntn[1]);
430if ( haserr ) pin->SelectErrBar(ntn[2], ntn[3]);
431
[1971]432dopt = "defline " + dopt;
[333]433string titre = nom + ":" + expy + "%" + expx;
[1971]434mImgapp->DispScDrawer( (PIDrawer*)pin, titre, dopt);
[333]435return;
436}
437
438/* --Methode-- */
439void Services2NObjMgr::DisplayPoints3D(string& nom, string& expx, string& expy,
[357]440 string& expz, string& expcut, string dopt, string loop)
[333]441{
442NObjMgrAdapter* obja=NULL;
[2491]443obja = MyObjMgr()->GetObjAdapter(nom);
[333]444if (obja == NULL) {
[449]445 cout << "Services2NObjMgr::DisplayPoints3D() Error , No such object " << nom << endl;
[333]446 return;
447 }
448if (!mImgapp) return;
449
450char* ntn[3] = {"expx","expy","expz"};
451NTuple* nt = new NTuple(3,ntn); // Creation NTuple
452
453string expwt = "1.";
[357]454ComputeExpressions(obja, expx, expy, expz, expwt, expcut, loop, nt, NULL, NULL);
[333]455
456if (nt->NEntry() < 1) {
457 cout << "Services2NObjMgr::DisplayPoints3D() Warning Zero points satisfy cut !" << endl;
458 delete nt;
459 return;
460 }
[2494]461//DBG nt->Show();
462//DBG nt->Print(0,10);
[333]463PINTuple3D* pin = new PINTuple3D(nt, true);
464pin->SelectXYZ(ntn[0], ntn[1], ntn[2]);
[1971]465dopt = "defline " + dopt;
[333]466
467// Pour plot a partir de DispScDrawer
468// string nomdisp = "_NT3D_";
469// mImgapp->DispScDrawer( (PIDrawer*)pin, nomdisp, opt);
470// Pour plot a partir de Disp3DDrawer
471string titre = nom + ":" + expy + "%" + expx;
[1971]472mImgapp->Disp3DDrawer(pin, titre, dopt);
[333]473
474return;
475}
476
477/* --Methode-- */
478void Services2NObjMgr::DisplayPoints2DW(string& nom, string& expx, string& expy,
[3279]479 string& expwt, string& expcut, bool fgcolidx, string dopt, string loop)
[333]480{
481NObjMgrAdapter* obja=NULL;
[2491]482obja = MyObjMgr()->GetObjAdapter(nom);
[333]483if (obja == NULL) {
[449]484 cout << "Services2NObjMgr::DisplayPoints2DW() Error , No such object " << nom << endl;
[333]485 return;
486 }
487if (!mImgapp) return;
488
489char* ntn[3] = {"expx","expy","expw"};
490NTuple* nt = new NTuple(3,ntn); // Creation NTuple
491
492string exp = "1.";
[357]493ComputeExpressions(obja, expx, expy, expwt, exp, expcut, loop, nt, NULL, NULL);
[333]494
495if (nt->NEntry() < 1) {
496 cout << "Services2NObjMgr::DisplayPoints2DW() Warning Zero points satisfy cut !" << endl;
497 delete nt;
498 return;
499 }
500
501PINTuple* pin = new PINTuple(nt, true);
502pin->SelectXY(ntn[0], ntn[1]);
[3279]503if (fgcolidx) pin->SelectColorByIndex(ntn[2]); // Use the weight expression as a color index
504else pin->SelectWt(ntn[2]);
[333]505
506string titre = nom + ":" + expwt + "_" + expy + "%" + expx ;
[1971]507mImgapp->DispScDrawer( (PIDrawer*)pin, titre, dopt);
[333]508return;
509}
510
511/* --Methode-- */
[2999]512void Services2NObjMgr::DisplayPoints3DW(string& nom, string& expx, string& expy, string& expz,
513 string& expwt, string& expcut, string dopt, string loop)
514{
515NObjMgrAdapter* obja=NULL;
516obja = MyObjMgr()->GetObjAdapter(nom);
517if (obja == NULL) {
518 cout << "Services2NObjMgr::DisplayPoints3D() Error , No such object " << nom << endl;
519 return;
520 }
521if (!mImgapp) return;
522
523char* ntn[4] = {"expx","expy","expz","expw"};
524NTuple* nt = new NTuple(4,ntn); // Creation NTuple
525
526ComputeExpressions(obja, expx, expy, expz, expwt, expcut, loop, nt, NULL, NULL);
527
528if (nt->NEntry() < 1) {
529 cout << "Services2NObjMgr::DisplayPoints3DW() Warning Zero points satisfy cut !" << endl;
530 delete nt;
531 return;
532 }
533//DBG nt->Show();
534//DBG nt->Print(0,10);
535PINTuple3D* pin = new PINTuple3D(nt, true);
536pin->SelectXYZ(ntn[0], ntn[1], ntn[2]);
537pin->SelectWt(ntn[3]);
538pin->UseColorScale(true);
539dopt = "defline " + dopt;
540
541// Pour plot a partir de DispScDrawer
542// string nomdisp = "_NT3D_";
543// mImgapp->DispScDrawer( (PIDrawer*)pin, nomdisp, opt);
544// Pour plot a partir de Disp3DDrawer
545string titre = nom + ":" + expy + "%" + expx + " W=" + expwt;
546mImgapp->Disp3DDrawer(pin, titre, dopt);
547
548return;
549}
550
551/* --Methode-- */
[333]552void Services2NObjMgr::ProjectH1(string& nom, string& expx, string& expwt,
[357]553 string& expcut, string& nomh1, string dopt, string loop)
[333]554{
555NObjMgrAdapter* obja=NULL;
[2491]556obja = MyObjMgr()->GetObjAdapter(nom);
[333]557if (obja == NULL) {
[449]558 cout << "Services2NObjMgr::ProjectH1() Error , No such object " << nom << endl;
[333]559 return;
[2681]560}
[333]561if (!mImgapp) return;
562
563Histo* h1 = NULL;
564NTuple* nt = NULL;
565AnyDataObj* oh = NULL;
[2679]566bool h1_already_exist = false;
[2491]567if (nomh1.length() > 0) oh=MyObjMgr()->GetObj(nomh1);
[333]568else nomh1 = "/tmp/projh1d";
[2679]569if ( (oh != NULL) && (typeid(*oh) == typeid(Histo)) ) {
570 h1 = (Histo*)oh;
571 h1_already_exist = true;
572 // Pas de remise a zero ! h1->Zero();
573} else {
[333]574 char* ntn[2]= {"hxval", "hwt"};
575 nt = new NTuple(2,ntn); // Creation NTuple
[2681]576}
[333]577string expz = "0.";
[357]578ComputeExpressions(obja, expx, expwt, expz, expwt, expcut, loop, nt, h1, NULL);
[333]579
580if ((!h1) && (!nt)) return;
581if (!h1) {
582 if (nt->NEntry() < 1) {
583 cout << "Services2NObjMgr::ProjectH1() Warning Zero points satisfy cut !" << endl;
584 delete nt;
585 return;
[2681]586 }
[333]587 double xmin, xmax;
588 nt->GetMinMax(0, xmin, xmax);
589 h1 = new Histo(xmin, xmax, 100);
590 int k;
591 float* xn;
592 for(k=0; k<nt->NEntry(); k++) {
593 xn = nt->GetVec(k);
594 h1->Add(xn[0], xn[1]);
[2681]595 }
[333]596 delete nt;
[2491]597 MyObjMgr()->AddObj(h1, nomh1);
[2681]598}
[333]599
[2681]600if(!h1_already_exist || dopt.size()>0) MyObjMgr()->DisplayObj(nomh1, dopt);
[333]601return;
602}
603
604/* --Methode-- */
605void Services2NObjMgr::ProjectH2(string& nom, string& expx, string& expy, string& expwt,
[357]606 string& expcut, string& nomh2, string dopt, string loop)
[333]607{
608NObjMgrAdapter* obja=NULL;
[2491]609obja = MyObjMgr()->GetObjAdapter(nom);
[333]610if (obja == NULL) {
[449]611 cout << "Services2NObjMgr::ProjectH2() Error , No such object " << nom << endl;
[333]612 return;
[2681]613}
[333]614if (!mImgapp) return;
615
616Histo2D* h2 = NULL;
617NTuple* nt = NULL;
618AnyDataObj* oh = NULL;
[2679]619bool h2_already_exist = false;
[2491]620if (nomh2.length() > 0) oh=MyObjMgr()->GetObj(nomh2);
[333]621else nomh2 = "/tmp/projh2d";
[2679]622if ( (oh != NULL) && (typeid(*oh) == typeid(Histo2D)) ) {
623 h2 = (Histo2D*)oh;
624 h2_already_exist = true;
625 // Pas de remise a zero ! h2->Zero();
626} else {
[333]627 char* ntn[3]= {"hxval", "hyval", "hwt"};
628 nt = new NTuple(3,ntn); // Creation NTuple
[2681]629}
[333]630string expz = "0.";
[357]631ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, loop, nt, NULL, h2);
[333]632
633if ((!h2) && (!nt)) return;
634if (!h2) {
635 if (nt->NEntry() < 1) {
636 cout << "Services2NObjMgr::ProjectH2() Warning Zero points satisfy cut !" << endl;
637 delete nt;
638 return;
[2681]639 }
[333]640 double xmin, xmax, ymin, ymax;
641 nt->GetMinMax(0, xmin, xmax);
[466]642 nt->GetMinMax(1, ymin, ymax);
[333]643 h2 = new Histo2D(xmin, xmax, 50, ymin, ymax, 50);
644 int k;
645 float* xn;
646 for(k=0; k<nt->NEntry(); k++) {
647 xn = nt->GetVec(k);
648 h2->Add(xn[0], xn[1], xn[2]);
[2681]649 }
[333]650 delete nt;
[2491]651 MyObjMgr()->AddObj(h2, nomh2);
[2681]652}
[333]653
[2681]654if(!h2_already_exist || dopt.size()>0) MyObjMgr()->DisplayObj(nomh2, dopt);
[333]655return;
656
657}
658
659/* --Methode-- cmv 13/10/98 */
660void Services2NObjMgr::ProjectHProf(string& nom, string& expx, string& expy, string& expwt,
[357]661 string& expcut, string& nomprof, string dopt, string loop)
662// Pour remplir un ``GeneralFitData'' a partir de divers objets:
[333]663//| nom = nom de l'objet a projeter dans un HProf.
664//| expx = expression X de definition du bin.
665//| expy = expression Y a additionner dans le bin.
666//| expwt = expression W du poids a additionner.
667//| expcut = expression du test de selection.
668//| nomprof = nom du HProf engendre (optionnel). Si l'objet n'existe pas
669//| les limites Xmin,Xmax sont calculees automatiquement.
670//| sinon ce sont celles de l'objet preexistant.
671//| opt = options generales pour le display.
672{
673NObjMgrAdapter* obja=NULL;
[2491]674obja = MyObjMgr()->GetObjAdapter(nom);
[333]675if (obja == NULL) {
[449]676 cout << "Services2NObjMgr::ProjectHProf() Error , No such object " << nom << endl;
[333]677 return;
[2681]678}
[333]679if (!mImgapp) return;
680
681HProf* hprof = NULL;
682NTuple* nt = NULL;
683AnyDataObj* oh = NULL;
[2679]684bool hp_already_exist = false;
[2491]685if (nomprof.length() > 0) oh=MyObjMgr()->GetObj(nomprof);
[333]686else nomprof = "/tmp/projprof";
[2679]687if( (oh!=NULL) && (typeid(*oh) == typeid(HProf)) ) {
688 hprof = (HProf*)oh;
689 hp_already_exist = true;
690} else {
[333]691 char* ntn[3]= {"hxval", "hyval", "hwt"};
692 nt = new NTuple(3,ntn); // Creation NTuple
693}
694string expz = "0.";
[357]695ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, loop, nt, NULL, NULL, hprof);
[333]696
697if((!hprof) && (!nt)) return;
698if(!hprof) {
699 if (nt->NEntry() < 1) {
700 cout << "Services2NObjMgr::ProjectHProf() Warning Zero points satisfy cut !" << endl;
701 delete nt;
702 return;
703 }
[1091]704 r_8 xmin, xmax;
[333]705 nt->GetMinMax(0, xmin, xmax);
706 hprof = new HProf(xmin, xmax, 100);
707 int k;
708 float* xn;
709 for(k=0; k<nt->NEntry(); k++) {
710 xn = nt->GetVec(k);
711 hprof->Add(xn[0], xn[1], xn[2]);
[2681]712 }
[333]713 delete nt;
[2491]714 MyObjMgr()->AddObj(hprof, nomprof);
[2681]715}
[1090]716hprof->UpdateHisto();
[333]717
[2681]718if(!hp_already_exist || dopt.size()>0) MyObjMgr()->DisplayObj(nomprof, dopt);
[333]719return;
720}
721
[357]722
[333]723/* --Methode-- */
[357]724void Services2NObjMgr::FillVect(string& nom, string& expx, string& expv,
725 string& expcut, string& nomvec, string dopt, string loop)
[333]726{
727NObjMgrAdapter* obja=NULL;
[2491]728obja = MyObjMgr()->GetObjAdapter(nom);
[333]729if (obja == NULL) {
[357]730 cout << "Services2NObjMgr::FillVect() Error , No such object: " << nom << endl;
[333]731 return;
732 }
733if (!mImgapp) return;
734
[357]735Vector* v1 = NULL;
736AnyDataObj* ov = NULL;
[2491]737ov=MyObjMgr()->GetObj(nomvec);
[357]738if (ov != NULL) v1 = dynamic_cast<Vector *>(ov);
739if (v1 == NULL) {
740 cout << "Services2NObjMgr::FillVect() Error , No such object or not a vector: " << nomvec << endl;
741 return;
742 }
743
744char* ntn[2]= {"vi", "vv"};
745NTuple* nt = new NTuple(2,ntn); // Creation NTuple
746
747string expz = "0.";
748ComputeExpressions(obja, expx, expv, expz, expz, expcut, loop, nt);
749
750if (!nt) return;
751if (nt->NEntry() < 1) {
752 cout << "Services2NObjMgr::FillVect() Warning Zero points satisfy cut !" << endl;
753 delete nt;
754 return;
755 }
756
757 int i,k;
758 double* xn;
759 for(k=0; k<nt->NEntry(); k++) {
760 xn = nt->GetLineD(k);
[2180]761 i = int(xn[0]+0.5);
[357]762 if ( (i < 0) || i >= v1->NElts() ) continue;
763 (*v1)(i) = xn[1];
764 }
765 delete nt;
766
767
[2491]768MyObjMgr()->DisplayObj(nomvec, dopt);
[357]769return;
770}
771
772/* --Methode-- */
773void Services2NObjMgr::FillMatx(string& nom, string& expx, string& expy, string& expv,
774 string& expcut, string& nommtx, string dopt, string loop)
775{
776NObjMgrAdapter* obja=NULL;
[2491]777obja = MyObjMgr()->GetObjAdapter(nom);
[357]778if (obja == NULL) {
779 cout << "Services2NObjMgr::FillMatx() Error , No such objet " << nom << endl;
780 return;
781 }
782if (!mImgapp) return;
783
784Matrix* mtx = NULL;
785AnyDataObj* om = NULL;
[2491]786om=MyObjMgr()->GetObj(nommtx);
[357]787if (om != NULL) mtx = dynamic_cast<Matrix *>(om);
788if (mtx == NULL) {
789 cout << "Services2NObjMgr::FillMatx() Error , No such object or not a matrix " << nommtx << endl;
790 return;
791 }
792
793char* ntn[3]= {"mi", "mj", "mv"};
794NTuple* nt = new NTuple(3,ntn); // Creation NTuple
795
796string expz = "0.";
797ComputeExpressions(obja, expx, expy, expv, expz, expcut, loop, nt);
798
799if (!nt) return;
800if (nt->NEntry() < 1) {
801 cout << "Services2NObjMgr::FillMatx() Warning Zero points satisfy cut !" << endl;
802 delete nt;
803 return;
804 }
805
806 int ic, jl, k;
807 double* xn;
808 for(k=0; k<nt->NEntry(); k++) {
809 xn = nt->GetLineD(k);
[2180]810 ic = int(xn[0]+0.5);
811 jl = int(xn[1]+0.5);
[357]812 if ( (ic < 0) || ic >= mtx->NCol() ) continue;
813 if ( (jl < 0) || jl >= mtx->NRows() ) continue;
814 (*mtx)(jl, ic) = xn[2];
815 }
816 delete nt;
817
818
[2491]819MyObjMgr()->DisplayObj(nommtx, dopt);
[357]820return;
821
822}
823
824/* --Methode-- */
825void Services2NObjMgr::ExpressionToVector(string& nom, string& expx, string& expcut,
826 string& nomvec, string dopt, string loop)
827{
828NObjMgrAdapter* obja=NULL;
[2491]829obja = MyObjMgr()->GetObjAdapter(nom);
[357]830if (obja == NULL) {
[449]831 cout << "Services2NObjMgr::ExpressionToVector() Error , No such object " << nom << endl;
[357]832 return;
833 }
834if (!mImgapp) return;
835
[333]836NTuple* nt = NULL;
[2924]837// if (nomvec.length() < 1) nomvec = "/tmp/expvec";
[333]838
839char* ntn[2]= {"vecval", "vecwt"};
840nt = new NTuple(1,ntn); // Creation NTuple
841
842string expwt = "1.";
[1486]843string expz = "0.";
844string dumexpcut = expcut; if(dumexpcut.size()<=0) dumexpcut = "1.";
845ComputeExpressions(obja,expx,expz,expz,expwt,dumexpcut,loop,nt,NULL,NULL);
[333]846
847if (!nt) return;
848if (nt->NEntry() < 1) {
[357]849 cout << "Services2NObjMgr::ExpressionToVector() Warning Zero points satisfy cut !" << endl;
[333]850 delete nt;
851 return;
852 }
853
854Vector* vec = new Vector(nt->NEntry());
855int k;
856float* xn;
857for(k=0; k<nt->NEntry(); k++) {
858 xn = nt->GetVec(k);
859 (*vec)(k) = xn[0];
860 }
861delete nt;
[2922]862if (nomvec.size() > 0) {
863 MyObjMgr()->AddObj(vec, nomvec);
864 MyObjMgr()->DisplayObj(nomvec, dopt);
865}
866else {
867// On calcule et on affiche mean/sigma + min/max
868 double min, max, mean, sigma;
869 vec->MinMax(min, max);
870 MathArray<r_8> ma;
871 ma.MeanSigma(*vec, mean, sigma);
[2924]872 cout << "Size= " << vec->Size() << " Mean= " << mean << " Sigma= " << sigma
[2922]873 << " Min= " << min << " Max= " << max << endl;
874 delete vec;
875}
[333]876return;
877}
878
879/* --Methode-- */
[447]880void Services2NObjMgr::NtFromASCIIFile(string& nom,string& filename,double def_val)
[2792]881// Pour remplir un NTuple/DataTable "nom" existant a partir du fichier
[447]882// ASCII table "filename". Si il y a plus de variables dans le
883// ntuple que dans le fichier "filename",
[2792]884// les sur-numeraires numeriques sont mises a "def_val" par defaut.
[447]885{
[2491]886AnyDataObj* mobj = MyObjMgr()->GetObj(nom);
[2792]887if(mobj == NULL) {
888 cout<<"NtFromASCIIFile() Error, object "<<nom<<" not existing"<<endl;
889 return;
890}
891
892NTuple* nt = NULL;
893DataTable* dt = NULL;
894if(typeid(*mobj) == typeid(NTuple)) nt = (NTuple*) mobj;
895else if(typeid(*mobj) == typeid(DataTable)) dt = (DataTable*) mobj;
896
897if(nt==NULL && dt==NULL) {
898 cout<<"NtFromASCIIFile() Error, object "<<nom<<" not an NTuple nor a DataTable"<<endl;
899 return;
900}
[447]901if (!mImgapp) return;
902
[2792]903if(nt) nt->FillFromASCIIFile(filename, def_val);
[2833]904else if(dt) {
905 ifstream is(filename.c_str());
906 dt->FillFromASCIIFile(is);
907}
[447]908return;
909}
910
911/* --Methode-- */
[333]912void Services2NObjMgr::FillNT(string& nom, string& expx, string& expy, string& expz,
[357]913 string& expt, string& expcut, string& nomnt, string loop)
[333]914{
915NObjMgrAdapter* obja=NULL;
[2491]916obja = MyObjMgr()->GetObjAdapter(nom);
[333]917if (obja == NULL) {
[449]918 cout << "Services2NObjMgr::FillNT() Error , No such object " << nom << endl;
[333]919 return;
920 }
921if (!mImgapp) return;
922
923bool fgnnt = false;
924NTuple* nt = NULL;
925AnyDataObj* oh = NULL;
[2491]926if (nomnt.length() > 0) oh=MyObjMgr()->GetObj(nomnt);
[333]927else nomnt = "/tmp/fillnt";
928if ( (oh != NULL) && (typeid(*oh) == typeid(NTuple)) ) {
929 nt = (NTuple*)oh;
930 if (nt->NVar() > 10) {
[449]931 cout << "Services2NObjMgr::FillNT() Warning , Max 10 var in NTuple -> new NTuple" << endl;
[333]932 nt = NULL;
933 }
934 }
935if (nt == NULL) {
936 char* ntn[4]= {"x", "y","z","t"};
937 nt = new NTuple(4,ntn); // Creation NTuple
938 fgnnt = true;
939 }
940
[357]941ComputeExpressions(obja, expx, expy, expz, expt, expcut, loop, nt, NULL, NULL);
[333]942
[2491]943if (fgnnt) MyObjMgr()->AddObj(nt, nomnt);
[333]944return;
945
946}
947
948/* --Methode-- */
949void Services2NObjMgr::FillNTFrCFile(string & nom, string const & fname,
[357]950 string const & funcname, string & nomnt, string loop)
[333]951{
952if (!mImgapp) return;
953
954NObjMgrAdapter* obja=NULL;
[2491]955obja = MyObjMgr()->GetObjAdapter(nom);
[333]956if (obja == NULL) {
957 cout << "Services2NObjMgr::FillNTFrCFile( " << nom << "...) No such object" <<endl;
958 return;
959 }
[344]960bool adel = true;
961NTupleInterface* objnt = obja->GetNTupleInterface(adel);
[333]962if (objnt == NULL) {
[449]963 cout << "Services2NObjMgr::FillNTFrCFile( " << nom << "...) Not an NTupleInterface !" <<endl;
[333]964 return;
965 }
966
[2755]967// Pour synchronisation d'execution simultanee
[2762]968ZSync zs(mutx_dynlink); zs.NOp();
[2755]969
[333]970NTLoopExprFunc f = (NTLoopExprFunc)LinkFunctionFromFile(fname, funcname);
971if (!f) {
972 cerr << "Services2NObjMgr::FillNTFrCFile Error Creation NTLoopExprFunc" << endl;
[344]973 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]974 return;
975 }
976
977bool fgnnt = false;
978NTuple* nt = NULL;
979if (nomnt.length() > 0) {
980 AnyDataObj* oh = NULL;
[2491]981 oh=MyObjMgr()->GetObj(nomnt);
[333]982 if ( (oh != NULL) && (typeid(*oh) == typeid(NTuple)) ) {
983 nt = (NTuple*)oh;
984 if (nt->NVar() > 10) {
[449]985 cout << "Services2NObjMgr::FillNTFrCFile() Warning , Max 10 var in NTuple -> new NTuple" << endl;
[333]986 nt = NULL;
987 }
988 }
989 if (nt == NULL) {
990 char* ntn[4]= {"x", "y","z","t"};
991 nt = new NTuple(4,ntn); // Creation NTuple
992 fgnnt = true;
993 }
994 }
995
996double xnt[10];
997
998int i,k;
[2668]999for(i=0; i<10; i++) xnt[i] = 0.;
[333]1000
1001
1002// $CHECK$ A virer des que possible - Pb blocage application quand trop d'impression
1003// redirige - On redirige la sortie sur le terminal
1004bool red = mImgapp->HasRedirectedStdOutErr();
1005mImgapp->RedirectStdOutErr(false);
1006
[2419]1007int_8 k1,k2,dk;
[357]1008k1 = 0; k2 = objnt->NbLines(); dk = 1;
1009DecodeLoopParameters(loop, k1, k2, dk);
1010if (k1 < 0) k1 = 0;
1011if (k2 < 0) k2 = objnt->NbLines();
[2419]1012if (k2 > (int_8)objnt->NbLines()) k2 = objnt->NbLines();
[357]1013if (dk <= 0) dk = 1;
1014
[500]1015try {
[333]1016 double* xn;
[2419]1017 int_8 kstart = k1, kend = k2;
1018 for(k=kstart; k<kend; k+=dk) {
[333]1019 xn = objnt->GetLineD(k);
[2419]1020 if (f((int_8_exprf)k, xn, xnt, xnt+1, xnt+2, xnt+3, (int_8_exprf)kstart,(int_8_exprf) kend) != 0) {
[2668]1021 if (nt) nt->Fill(xnt);
[333]1022 }
1023 }
1024 }
[500]1025#ifdef SANS_EVOLPLANCK
[333]1026CATCH(merr) {
1027 fflush(stdout);
1028 cout << endl;
1029 cerr << endl;
1030 string es = PeidaExc(merr);
1031 cerr << "Services2NObjMgr::FillNTFrCFile() Exception :" << merr << es;
1032 } ENDTRY;
[500]1033#else
1034catch ( PException exc ) {
1035 fflush(stdout);
1036 cout << endl;
1037 cerr << endl;
1038 cerr << "Services2NObjMgr::FillNTFrCFile() Exception :" << exc.Msg() << endl;
1039}
1040#endif
[344]1041
1042if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1043CloseDLL();
1044
1045// $CHECK$ A virer des que possible On redirige la sortie sur la fenetre PIConsole
1046mImgapp->RedirectStdOutErr(red);
1047
[2491]1048if (fgnnt) MyObjMgr()->AddObj(nt, nomnt);
[333]1049return;
1050}
1051
1052/* --Methode-- */
1053void Services2NObjMgr::PrepareNTExpressionCFile(string & nom, string const & fname,
1054 string const & funcname)
1055{
1056NObjMgrAdapter* obja=NULL;
[2491]1057obja = MyObjMgr()->GetObjAdapter(nom);
[333]1058if (obja == NULL) {
1059 cout << "Services2NObjMgr::PrepareNTExpressionCFile( " << nom << "...) No such object" <<endl;
1060 return;
1061 }
[344]1062bool adel = true;
1063NTupleInterface* objnt = obja->GetNTupleInterface(adel);
[333]1064if (objnt == NULL) {
1065 cout << "Services2NObjMgr::PrepareNTExpressionCFile( " << nom
1066 << "...) No NTupleInterface !" <<endl;
1067 return;
1068 }
1069string vardec = objnt->VarList_C("_xnti_");
1070
1071FILE *fip;
1072if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1073 cout << "Services2NObjMgr::PrepareNTExpressionCFile()_Error: fopen " << fname << endl;
[344]1074 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1075 return;
1076 }
[344]1077
[333]1078// constitution du fichier des decalarations des variables de l'interface NTuple
1079fputs("#include <stdlib.h> \n", fip);
1080fputs("#include <stdio.h> \n", fip);
1081fputs("#include <math.h> \n\n", fip);
[344]1082
[1469]1083fputs("/* ------ Compare bits on double --------- */ \n", fip);
[2419]1084fputs("typedef long long int_8_exprf;\n", fip);
1085fputs("int_8_exprf BitCmp64(double v,int_8_exprf flg)\n", fip);
1086fputs("{return ((int_8_exprf)((v<0.) ? v-0.1 : v+0.1))&flg;}\n", fip);
[344]1087fputs("/* ------ Some random number generators --------- */ \n", fip);
[1931]1088fputs("#if defined(__ppc__) && defined(__MACH__) \n",fip);
1089fputs("#include <limits.h> \n", fip);
1090fputs("#define drand48() ((double)(random())/LONG_MAX) \n",fip);
1091fputs("#endif \n",fip);
[344]1092fputs("#define frand01() ( (float) drand48() ) \n", fip);
1093fputs("#define drand01() drand48() \n", fip);
1094fputs("#define rand01() drand48() \n", fip);
1095fputs("#define frandpm1() ( 2. * frand01() - 1.) \n", fip);
1096fputs("#define drandpm1() ( 2. * drand01() - 1.) \n", fip);
1097fputs("#define randpm1() ( 2. * drand01() - 1.) \n", fip);
1098fputs("double NorRand(void) \n", fip);
1099fputs(" { \n double x,A,B; \n LAB10: \n A = drand01(); \n", fip);
1100fputs(" if ( A == 0. ) goto LAB10; \n B = drand01(); \n", fip);
[345]1101fputs(" x = sqrt(-2.*log(A))*cos(2.*M_PI*B); \n", fip);
[344]1102fputs(" return(x); \n } \n", fip);
1103fputs("#define GauRand() NorRand() \n", fip);
1104fputs("#define gaurand() NorRand() \n\n", fip);
1105
[2419]1106fputs("/* NTupleInterface Variable declaration - Generated by piapp */\n", fip);
1107fputs("/* -- Services2NObjMgr::PrepareNTExpressionCFile() -- */ \n", fip);
1108fputs("/* _nl line number or sequential index : _nstart <= _nl < _nend */ \n\n", fip);
1109fprintf(fip,"int %s(int_8_exprf _nl, double* _xnti_, double* _rx_, double* _ry_, double* _rz_, \n",
[333]1110 funcname.c_str());
[2419]1111fprintf(fip," double* _rt_, int_8_exprf _nstart, int_8_exprf _nend) \n");
[333]1112fprintf(fip, "{ \n %s \n", vardec.c_str());
1113fputs(" if (!1) { /* Cut Expression failed */ \n", fip);
1114fputs(" *_rx_ = *_ry_ = *_rz_ = *_rt_ = 0.; return(0);", fip);
1115fputs(" } \n /* Cut expression satisfied */ \n", fip);
1116fputs(" *_rx_ = 1.; \n *_ry_ = 1.; \n *_rz_ = 1.; \n *_rt_ = 1.; \n", fip);
1117fputs(" return(1); \n} \n", fip);
1118
1119fclose(fip);
[344]1120
1121if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1122return;
1123}
1124
1125/* --Methode-- cmv 13/10/98 */
1126void Services2NObjMgr::FillGFD(string& nom, string& expx, string& expy, string& expz,
[357]1127 string& experr, string& expcut, string& nomgfd, string loop)
[333]1128// Pour remplir un ``GeneralFitData'' a partir de divers objets:
1129//| nom = nom de l'objet a transcrire selon 1D: Z=f(X) ou 2D: Z=f(X,Y) .
1130//| Vector,Matrix,Histo,HProf,Histo2D,Image<T>,StarList,NTuple,GeneralFitData
1131//| expx = expression X du GeneralFitData (1er abscisse)
1132//| expy = expression Y du GeneralFitData (2sd abscisse si non "", Z=f(X,Y))
1133//| expz = expression Z du GeneralFitData (valeur de l'ordonnee)
1134//| experr = expression de l'erreur sur l'ordonnee Z
1135//| expcut = expression du test de selection
1136//| nomgfd = nom du GeneralFitData engendre (optionnel)
1137{
1138NObjMgrAdapter* obja=NULL;
[2491]1139obja = MyObjMgr()->GetObjAdapter(nom);
[333]1140if (obja == NULL) {
[449]1141 cout << "Services2NObjMgr::FillGFD() Error , No such object "<<nom<<endl;
[333]1142 return;
1143 }
1144if(!mImgapp) return;
1145
1146// 2D ou 3D?
1147int nvar = 2;
1148if(expy.length()<=0) {nvar = 1; expy = "0.";}
1149
1150// Creation NTuple Buffer
1151char* ntn[4]= {"x","y","f","e"};
1152NTuple*nt = new NTuple(4,ntn);
1153
1154// Remplissage NTuple buffer
[357]1155ComputeExpressions(obja, expx, expy, expz, experr, expcut, loop, nt, NULL, NULL);
[333]1156if(nt->NEntry() < 1)
1157 {cout<<"Services2NObjMgr::FillGFD() Warning Zero points satisfy cut !"<<endl;
1158 delete nt; return;}
1159
1160//Remplissage de la structure GeneraFitData
1161if (nt->NEntry() <= 0) {
1162 cout<<"Services2NObjMgr::FillGFD() Warning - NData= " << nt->NEntry() << endl;
1163 delete nt;
1164 return;
1165 }
1166
1167GeneralFitData* gfd = new GeneralFitData(nvar,nt->NEntry(),0);
1168int k;
1169float* xn;
1170for(k=0; k<nt->NEntry(); k++) {
1171 xn = nt->GetVec(k);
1172 gfd->AddData(xn,xn[2],xn[3]);
1173}
1174
1175// Menage et table d'objets
1176delete nt;
[2491]1177MyObjMgr()->AddObj(gfd, nomgfd);
[333]1178return;
1179}
1180
[1067]1181/* --Methode-- cmv 12/07/00 */
1182void Services2NObjMgr::FillGFDfrVec(string nomgfd,string namx,string namy,string namz,string name)
1183// Pour remplir un ``GeneralFitData'' a partir de vecteurs
1184//| gdfrvec nomgd X Y ! !
1185//| gdfrvec nomgd X Y ! EY
1186//| gdfrvec nomgd X Y Z !
1187//| gdfrvec nomgd X Y Z EZ
1188//| - nomgfd = nom du generaldata a remplir
1189//| - namx = nom du vecteur contenant les valeurs X
1190//| - namy = nom du vecteur contenant les valeurs Y
1191//| - namz = nom du vecteur contenant les valeurs Z (ou "!")
1192//| - name = nom du vecteur contenant les valeurs des erreurs EY ou EZ
1193{
1194// Decodage des noms des vecteurs pour le remplissage du generaldata
1195if(nomgfd=="!" || nomgfd.length()<1)
1196 {cout<<"FillGFDfrVec_Error: bad GenaralData name "<<nomgfd<<endl; return;}
1197if(namx=="!" || namx.length()<1)
1198 {cout<<"FillGFDfrVec_Error: bad X vector name "<<namx<<endl; return;}
1199if(namy=="!" || namy.length()<1)
1200 {cout<<"FillGFDfrVec_Error: bad Y vector name "<<namy<<endl; return;}
1201if(namz.length()<1) namz = "!";
1202if(name.length()<1) name = "!";
1203int nvar = 0;
1204if(namz=="!") nvar = 1; else nvar = 2;
[333]1205
[1067]1206// Identify data
1207NamedObjMgr omg;
1208AnyDataObj* mobj = NULL;
1209Vector* v;
1210int nel = 0;
1211r_8 *x=NULL, *y=NULL, *z=NULL,* ez=NULL;
1212
1213if( (mobj=omg.GetObj(namx)) == NULL) {
1214 cout<<"FillGFDfrVec_Error: unknown X object "<<namx<<endl; return;
1215} else {
1216 v = (Vector*) mobj; x = v->Data(); nel=v->NElts();
1217}
1218
1219if( (mobj=omg.GetObj(namy)) == NULL) {
1220 cout<<"FillGFDfrVec_Error: unknown Y object "<<namy<<endl; return;
1221} else {
1222 v = (Vector*) mobj; y = z = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1223}
1224
1225if( nvar==2 && (mobj=omg.GetObj(namz)) == NULL) {
1226 cout<<"FillGFDfrVec_Error: unknown Z object "<<namz<<endl; return;
1227} else {
1228 v = (Vector*) mobj; z = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1229}
1230
1231if(name!="!") {
1232 if( (mobj=omg.GetObj(name)) == NULL) {
1233 cout<<"FillGFDfrVec_Error: unknown EZ object "<<name<<endl; return;
1234 } else {
1235 v = (Vector*) mobj; ez = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1236 }
1237}
1238
1239if(nel<=0)
1240 {cout<<"FillGFDfrVec_Error: bad number of elements "<<nel<<endl; return;}
1241
1242// Create GeneralData and fill it with vectors
1243GeneralFitData* gfd = new GeneralFitData(nvar,nel+5,0);
1244if(nvar==1) gfd->SetData1(nel,x,z,ez); // On remplit Y=f(X)
1245else gfd->SetData2(nel,x,y,z,ez); // On remplit Z=f(X,y)
1246
1247// Menage et table d'objets
1248if( omg.GetObj(nomgfd) != NULL ) omg.DelObj(nomgfd);
[2491]1249MyObjMgr()->AddObj(gfd,nomgfd);
[1067]1250return;
1251}
1252
[333]1253/* --Methode-- */
1254void Services2NObjMgr::ComputeExpressions(NObjMgrAdapter* obja, string& expx,
[357]1255 string& expy, string& expz, string& expt, string& expcut, string& loop,
[333]1256 NTuple* nt, Histo* h1, Histo2D* h2, HProf* hp)
1257{
1258if (obja == NULL) return;
[344]1259bool adel = true;
1260NTupleInterface* objnt = obja->GetNTupleInterface(adel);
[333]1261if (objnt == NULL) return;
1262string vardec = objnt->VarList_C("_zz6qi_");
1263
[2755]1264// Pour synchronisation d'execution simultanee
[2762]1265ZSync zs(mutx_dynlink); zs.NOp();
[2755]1266
[333]1267PlotExprFunc f = LinkExprFunc(vardec, expx, expy, expz, expt, expcut);
1268if (!f) {
1269 cerr << "Services2NObjMgr::::ComputeExpressions() Error Creation PlotExprFunc " << endl;
[344]1270 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1271 return;
1272 }
1273
1274double xnt[10];
1275
[2419]1276int_8 k;
1277for(k=0; k<10; k++) xnt[k] = 0.;
1278int_8 k1,k2,dk;
[357]1279k1 = 0; k2 = objnt->NbLines(); dk = 1;
1280DecodeLoopParameters(loop, k1, k2, dk);
1281if (k1 < 0) k1 = 0;
1282if (k2 < 0) k2 = objnt->NbLines();
[2419]1283if (k2 > (int_8)objnt->NbLines()) k2 = objnt->NbLines();
[357]1284if (dk <= 0) dk = 1;
[500]1285
1286try {
[333]1287 double* xn;
[357]1288 for(k=k1; k<k2; k += dk) {
[333]1289 xn = objnt->GetLineD(k);
[2419]1290 if (f((int_8_exprf)k,xn, xnt, xnt+1, xnt+2, xnt+3) != 0) {
[2668]1291 if (nt) nt->Fill(xnt);
[333]1292 if (h1) h1->Add(xnt[0], xnt[3]);
1293 if (h2) h2->Add(xnt[0], xnt[1], xnt[3]);
1294 if (hp) hp->Add(xnt[0], xnt[1], xnt[3]);
1295 }
1296 }
1297 }
[500]1298#ifdef SANS_EVOLPLANCK
[333]1299CATCH(merr) {
1300 fflush(stdout);
1301 cout << endl;
1302 cerr << endl;
1303 string es = PeidaExc(merr);
1304 cerr << "Services2NObjMgr::ComputeExpressions() Exception :" << merr << es;
1305 } ENDTRY;
[500]1306#else
1307catch ( PException exc ) {
1308 fflush(stdout);
1309 cout << endl;
1310 cerr << endl;
1311 cerr << "Services2NObjMgr::ComputeExpressions() Exception :" << exc.Msg() << endl;
1312}
1313#endif
[333]1314
[344]1315if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
[333]1316// Fermeture du fichier .so
1317CloseDLL();
1318return;
1319}
1320
1321
1322/* --Methode-- */
1323PlotExprFunc Services2NObjMgr::LinkExprFunc(string& vardec, string& expx, string& expy,
1324 string& expz, string& expt, string& cut)
1325{
1326FILE *fip;
1327string fname = TmpDir + "expf_pia_dl.c";
1328string cmd;
1329int rc;
1330
1331cmd = "rm -f " + fname;
1332rc = system(cmd.c_str());
1333//DBG printf("LinkExprFunc_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
1334
1335if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1336 string sn = fname;
[449]1337 cout << "Services2NObjMgr/LinkExprFunc_Error: fopen( " << sn << endl;
[333]1338 return(NULL);
1339 }
1340
1341// constitution du fichier a compiler
1342fputs("#include <stdlib.h> \n", fip);
1343fputs("#include <math.h> \n", fip);
[344]1344
[1469]1345fputs("/* ------ Compare bits on double --------- */ \n", fip);
[2419]1346fputs("typedef long long int_8_exprf;\n", fip);
1347fputs("int_8_exprf BitCmp64(double v,int_8_exprf flg)\n", fip);
1348fputs("{return ((int_8_exprf)((v<0.) ? v-0.1 : v+0.1))&flg;}\n", fip);
[344]1349fputs("/* ------ Some random number generators --------- */ \n", fip);
[1931]1350fputs("#if defined(__ppc__) && defined(__MACH__) \n",fip);
1351fputs("#include <limits.h> \n", fip);
1352fputs("#define drand48() ((double)(random())/LONG_MAX) \n",fip);
1353fputs("#endif \n",fip);
[344]1354fputs("#define frand01() ( (float) drand48() ) \n", fip);
1355fputs("#define drand01() drand48() \n", fip);
1356fputs("#define rand01() drand48() \n", fip);
1357fputs("#define frandpm1() ( 2. * frand01() - 1.) \n", fip);
1358fputs("#define drandpm1() ( 2. * drand01() - 1.) \n", fip);
1359fputs("#define randpm1() ( 2. * drand01() - 1.) \n", fip);
1360fputs("double NorRand(void) \n", fip);
1361fputs(" { \n double x,A,B; \n LAB10: \n A = drand01(); \n", fip);
1362fputs(" if ( A == 0. ) goto LAB10; \n B = drand01(); \n", fip);
[345]1363fputs(" x = sqrt(-2.*log(A))*cos(2.*M_PI*B); \n", fip);
[344]1364fputs(" return(x); \n } \n", fip);
1365fputs("#define GauRand() NorRand() \n", fip);
1366fputs("#define gaurand() NorRand() \n\n", fip);
1367
[2419]1368fputs("int expf_pia_dl_func(int_8_exprf _nl, double* _zz6qi_, double* _rx_6q_, double* _ry_6q_, double* _rz_6q_, double* _rt_6q_) \n{\n", fip);
[333]1369fprintf(fip,"%s \n", vardec.c_str());
1370fprintf(fip, "if (!(%s)) { *_rx_6q_ = *_ry_6q_ = *_rz_6q_ = *_rt_6q_ = 0.; return(0); } \n", cut.c_str());
1371fprintf(fip, "*_rx_6q_ = %s ; \n", expx.c_str());
1372fprintf(fip, "*_ry_6q_ = %s ; \n", expy.c_str());
1373fprintf(fip, "*_rz_6q_ = %s ; \n", expz.c_str());
1374fprintf(fip, "*_rt_6q_ = %s ; \n", expt.c_str());
1375fputs("return(1); \n} \n", fip);
1376fclose(fip);
1377string func = "expf_pia_dl_func";
1378return((PlotExprFunc)LinkFunctionFromFile(fname, func));
1379}
1380
1381
1382/* --Methode-- */
1383DlFunction Services2NObjMgr::LinkFunctionFromFile(string const & fname, string const & funcname)
1384{
1385// Le link dynamique
1386CloseDLL();
1387dynlink = PDynLinkMgr::BuildFromCFile(fname);
1388if (dynlink == NULL) {
1389 cerr << "Services2NObjMgr/LinkFunctionFromFile_Erreur: Erreur creation/Ouverture SO " << endl;
1390 return(NULL);
1391 }
1392
1393DlFunction retfunc = dynlink->GetFunction(funcname);
1394if (retfunc == NULL) {
1395 string sn = funcname;
1396 cerr << "Services2NObjMgr/LinkExprFunc_Erreur: Erreur linking " << sn << endl;
1397 CloseDLL();
1398 return(NULL);
1399 }
1400else return(retfunc);
1401}
1402
1403/* --Methode-- */
1404void Services2NObjMgr::CloseDLL()
1405{
1406if (dynlink) delete dynlink; dynlink = NULL;
1407}
1408
[1319]1409/* --Methode-- */
1410int Services2NObjMgr::ExecuteCommand(string line)
1411{
1412 if (mImgapp == NULL) return(99);
1413 return(mImgapp->CmdInterpreter()->Interpret(line));
1414}
1415
[357]1416// Fonction static
1417/* --Methode-- */
[2419]1418void Services2NObjMgr::DecodeLoopParameters(string& loop, int_8& i1, int_8& i2, int_8& di)
[357]1419{
1420// Decode des paramatres de boucle for(int i=i1; i<i2; i+=di) specifies
1421// sous forme i1[:i2[:di]]
1422// cout << "LoopParam() " << loop << " I1=" << i1 << " I2=" << i2 << " DI=" << di;
1423size_t l = loop.length();
1424if (l < 1) return;
1425size_t p = loop.find(':');
[2419]1426if (p >= l) { i1 = atol(loop.c_str()); return; }
1427i1 = atol(loop.substr(0, p).c_str());
[357]1428string aa = loop.substr(p+1);
1429p = aa.find(':');
1430if (p < aa.length() ) {
[2419]1431 i2 = atol(aa.substr(0,p).c_str());
1432 di = atol(aa.substr(p+1).c_str());
[357]1433 }
[2419]1434else i2 = atol(aa.c_str());
[357]1435// cout << "-> I1= " << i1 << " I2= " << i2 << " DI= " << di << endl;
1436return;
1437}
[333]1438
1439/* --Methode-- */
[165]1440string Services2NObjMgr::FileName2Name(string const & fn)
1441{
1442
1443char fsep[2] = {FILESEP, '\0'};
1444char tsep[2] = {'.', '\0'};
1445size_t p = fn.find_last_of(fsep);
[194]1446size_t l = fn.length();
1447if (p >= l) p = 0;
1448else p++;
1449size_t q = fn.find_first_of(tsep,p);
1450if (q < p) q = l;
[165]1451return(fn.substr(p,q-p));
1452}
1453
1454
[546]1455
[165]1456
[333]1457// SANS_EVOLPLANCK Attention !
[1105]1458#ifdef SANS_EVOLPLANCK
[333]1459#include "pclassids.h"
1460
1461/* --Methode-- */
1462char* Services2NObjMgr::PClassIdToClassName(int cid)
1463{
1464switch (cid) {
1465 case ClassId_Poly1 :
1466 return("Poly1");
1467 case ClassId_Poly2 :
1468 return("Poly2");
1469 case ClassId_Matrix :
1470 return("Matrix");
1471 case ClassId_Vector :
1472 return("Vector");
1473
1474 case ClassId_DVList :
1475 return("DVList");
1476
1477 case ClassId_Histo1D :
1478 return("Histo1D");
1479 case ClassId_Histo2D :
1480 return("Histo2D");
1481 case ClassId_HProf :
1482 return("HProf");
[2605]1483 case ClassId_HistoErr :
1484 return("HistoErr");
[333]1485 case ClassId_NTuple :
1486 return("NTuple");
[361]1487 case ClassId_XNTuple :
1488 return("XNTuple");
[333]1489 case ClassId_GeneralFitData :
1490 return("GeneralFitData");
1491
1492 case ClassId_Image :
1493 return("RzImage");
1494 case ClassId_Image + kuint_1 :
1495 return("ImageU1");
1496 case ClassId_Image + kint_1 :
1497 return("ImageI1");
1498 case ClassId_Image + kuint_2 :
1499 return("ImageU2");
1500 case ClassId_Image + kint_2 :
1501 return("ImageI2");
1502 case ClassId_Image + kuint_4 :
1503 return("ImageU4");
1504 case ClassId_Image + kint_4 :
1505 return("ImageI4");
1506 case ClassId_Image + kr_4 :
1507 return("ImageR4");
1508 case ClassId_Image + kr_8 :
1509 return("ImageR8");
1510 case ClassId_ZFidu :
1511 return("ZFidu");
1512
1513 case ClassId_StarList :
1514 return("StarList");
1515 case ClassId_Transfo :
1516 return("Transfo");
1517 case ClassId_PSF :
1518 return("PSF");
1519
1520
1521// - Ajout objet PPF
1522 default:
1523 return("AnyDataObj");
1524 }
1525}
1526
[1105]1527#else
1528char* Services2NObjMgr::PClassIdToClassName(int cid)
1529{
1530 return("AnyDataObj");
1531}
1532#endif
Note: See TracBrowser for help on using the repository browser.