source: Sophya/trunk/SophyaPI/PIext/pawexecut.cc@ 466

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

Changement nom /func en /autoc
Nouvelles commandes PAW-like n/plot,...,reset,h/plot/2d etc..
Creation de helpfit pour commenatires fit,fitlin,fitw,crfitfun etc..

cmv 14/10/99

File size: 13.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4#include <iostream.h>
5#include <typeinfo>
6
7#include "strutil.h"
8#include "cvector.h"
9#include "matrix.h"
10#include "histos.h"
11#include "histos.h"
12#include "histos2.h"
13#include "hisprof.h"
14#include "ntuple.h"
15
16#include "pawexecut.h"
17#include "nobjmgr.h"
18#include "servnobjm.h"
19#include "pistdimgapp.h"
20
21/* Reza + cmv 13/10/99 */
22
23/* methode */
24PAWExecutor::PAWExecutor(PIACmd *piac, PIStdImgApp* app)
25: mApp(app)
26{
27string kw, usage;
28string hgrp = "pawCmd";
29
30kw = "reset";
31usage = "Reset histograms vectors or matrix";
32usage += "\n reset nameobj";
33piac->RegisterCommand(kw,usage,this,hgrp);
34
35kw = "n/plot";
36usage = "Plot NTuple variable a la paw";
37usage += "\n n/plot nameobj.x_exp [cut] [w_exp] [gratt]";
38usage += "\n n/plot nameobj.y_exp%x_exp [cut] [w_exp] [gratt]";
39usage += "\n n/plot nameobj.z_exp%y_exp%x_exp [cut] [gratt]";
40usage += "\n Related commands: plot2dw plot3d";
41piac->RegisterCommand(kw,usage,this,hgrp);
42
43kw = "n/proj";
44usage = "Project NTuple in histogram (1D or 2D) a la paw";
45usage += "\n n/proj nameproj nameobj.x_exp [cut] [w_exp] [gratt]";
46usage += "\n n/proj nameproj nameobj.y_exp%x_exp [cut] [w_exp] [gratt]";
47usage += "\n Related commands: projh1d projh2d projprof";
48piac->RegisterCommand(kw,usage,this,hgrp);
49
50kw = "h/integ";
51usage = "Integrate a 1D histogram or profile";
52usage += "\n h/integ nameh1d [norm]";
53usage += "\n Related commands: h/deriv";
54piac->RegisterCommand(kw,usage,this,hgrp);
55
56kw = "h/deriv";
57usage = "Derivate a 1D histogram or profile";
58usage += "\n h/deriv nameh1d";
59usage += "\n Related commands: h/integ";
60piac->RegisterCommand(kw,usage,this,hgrp);
61
62kw = "h/rebin";
63usage = "Rebin a 1D histogram or profile";
64usage += "\n h/rebin nbin";
65piac->RegisterCommand(kw,usage,this,hgrp);
66
67kw = "h/cadd";
68usage = "Add a constant to an histogramme";
69usage += "\n h/cadd val";
70usage += "\n Related commands: h/cmult";
71piac->RegisterCommand(kw,usage,this,hgrp);
72
73kw = "h/cmult";
74usage = "Multiply an histogramme by a constant";
75usage += "\n h/cmult val";
76usage += "\n Related commands: h/cadd";
77piac->RegisterCommand(kw,usage,this,hgrp);
78
79kw = "h/plot/2d";
80usage = "Specific plot for 2D histogrammes";
81usage += "\n h/plot/2d nameh2d show : infos on 2D histogramme";
82usage += "\n h/plot/2d nameh2d h [dopt] : plot 2D histogramme";
83usage += "\n h/plot/2d nameh2d px [dopt] : plot X projection";
84usage += "\n h/plot/2d nameh2d py [dopt] : plot Y projection";
85usage += "\n h/plot/2d nameh2d bx n [dopt] : plot X band number n";
86usage += "\n h/plot/2d nameh2d by n [dopt] : plot Y band number n";
87usage += "\n h/plot/2d nameh2d sx n [dopt] : plot X slice number n";
88usage += "\n h/plot/2d nameh2d sy n [dopt] : plot Y slice number n";
89usage += "\n n < 0 means Show Info";
90piac->RegisterCommand(kw,usage,this,hgrp);
91}
92
93/* methode */
94PAWExecutor::~PAWExecutor()
95{
96}
97
98/* methode */
99int PAWExecutor::Execute(string& kw, vector<string>& tokens)
100{
101if(kw == "reset") {
102 reset(tokens); return(0);
103} else if(kw == "n/plot") {
104 n_plot(tokens); return(0);
105} else if(kw == "n/proj") {
106 n_proj(tokens); return(0);
107} else if(kw == "h/integ") {
108 h_integ(tokens); return(0);
109} else if(kw == "h/deriv") {
110 h_deriv(tokens); return(0);
111} else if(kw == "h/rebin") {
112 h_rebin(tokens); return(0);
113} else if(kw == "h/cadd") {
114 h_cadd(tokens); return(0);
115} else if(kw == "h/cmult") {
116 h_cmult(tokens); return(0);
117} else if(kw == "h/plot/2d") {
118 h_plot_2d(tokens); return(0);
119} else return(1);
120}
121
122/* methode */
123void PAWExecutor::reset(vector<string>& tokens)
124// Reset d'histogrammes, vecteurs et matrices
125{
126if(tokens.size() < 1)
127 {cout<<"Usage: reset nameobj"<<endl;
128 return;}
129NamedObjMgr omg;
130AnyDataObj* mobj = omg.GetObj(tokens[0]);
131if(mobj == NULL)
132 {cout<<"PAWExecutor::reset Error , Pas d'objet de nom "<<tokens[0]<<endl;
133 return;}
134string ctyp = typeid(*mobj).name();
135
136if(typeid(*mobj)==typeid(Vector)) {Vector* ob=(Vector*) mobj; ob->Zero();}
137else if(typeid(*mobj)==typeid(Matrix)) {Matrix* ob=(Matrix*) mobj; ob->Zero();}
138else if(typeid(*mobj)==typeid(Histo)) {Histo* ob=(Histo*) mobj; ob->Zero();}
139else if(typeid(*mobj)==typeid(HProf)) {HProf* ob=(HProf*) mobj; ob->Zero();}
140else if(typeid(*mobj)==typeid(Histo2D)) {Histo2D* ob=(Histo2D*)mobj; ob->Zero();}
141else {
142 cout<<"PAWExecutor::reset Error , No reset possible on "<<ctyp<<endl;
143 return;
144}
145
146return;
147}
148
149/* methode */
150void PAWExecutor::n_plot(vector<string>& tokens)
151// Equivalent n/plot de paw
152// Plot 1D
153// n/plot nameobj.x_exp [cut] [w_exp] [gratt]
154// Plot 2D (plot2dw)
155// n/plot nameobj.y_exp%x_exp [cut] [w_exp] [gratt]
156// Plot 3D (plot3d)
157// n/plot nameobj.z_exp%y_exp%x_exp [cut] [gratt]
158{
159if(tokens.size() < 1) {
160 cout
161 <<"Usage: n/plot nameobj.[z_exp%y_exp%]x_exp [cut] [w_exp] [gratt]"<<endl
162 <<" n/plot nameobj.z_exp%y_exp%x_exp [cut] [gratt]"<<endl;
163 return;
164}
165string nameobj,expx,expy,expz;
166int nvar = decodepawstring(tokens[0],nameobj,expx,expy,expz);
167string expcut = "1";
168string expwt = "1.";
169string dopt = "";
170string loop = "";
171if(tokens.size()>=2) expcut = tokens[1];
172if(tokens.size()>=3) expwt = tokens[2];
173if(tokens.size()>=4) dopt = tokens[3];
174
175
176NamedObjMgr omg;
177Services2NObjMgr* srvo = omg.GetServiceObj();
178
179if(nvar<=0) {
180 cout<<"PAWExecutor::n_plot Error: bad coding "<<tokens[0]<<endl;
181} else if(nvar==1) { // c'est un plot 1D
182 string nameproj = "/autoc/paw_n_plot1D";
183 AnyDataObj* mobj = omg.GetObj(nameproj);
184 if(mobj!=NULL) omg.DelObj(nameproj);
185 srvo->ProjectH1(nameobj,expx,expwt,expcut,nameproj,dopt,loop);
186} else if(nvar==2) { // c'est un plot 2D
187 srvo->DisplayPoints2DW(nameobj,expx,expy,expwt,expcut,dopt,loop);
188} else { // c'est un plot 3D
189 srvo->DisplayPoints3D(nameobj,expx,expy,expz,expcut,dopt,loop);
190}
191
192return;
193}
194
195/* methode */
196void PAWExecutor::n_proj(vector<string>& tokens)
197// Equivalent n/proj de paw
198// Project NTuple in histogram a la paw
199// Dans un Histo 1D
200// n/proj nameproj nameobj.x_exp [cut] [w_exp] [gratt]
201// Dans un Histo 2D ou un HProf (dans ce cas nameproj doit etre cree).
202// n/proj nameproj nameobj.y_exp%x_exp [cut] [w_exp] [gratt]
203{
204if(tokens.size()<2)
205 {cout<<"Usage: n/proj nameproj nameobj.[y_exp%]x_exp [cut] [w_exp] [gratt]"<<endl;
206 return;}
207string nameproj = tokens[0];
208string nameobj,expx,expy,expz;
209int nvar = decodepawstring(tokens[1],nameobj,expx,expy,expz);
210string expcut = "1";
211string expwt = "1.";
212string dopt = "";
213string loop = "";
214if(tokens.size()>=3) expcut = tokens[2];
215if(tokens.size()>=4) expwt = tokens[3];
216if(tokens.size()>=5) dopt = tokens[4];
217
218NamedObjMgr omg;
219Services2NObjMgr* srvo = omg.GetServiceObj();
220
221if(nvar<=0) {
222 cout<<"PAWExecutor::n_proj Error: bad coding "<<tokens[1]<<endl;
223} else if(nvar==1) {
224 // c'est une projection dans un histo 1D
225 srvo->ProjectH1(nameobj,expx,expwt,expcut,nameproj,dopt,loop);
226} else {
227 // c'est une projection dans un histo2D
228 // OU un HProf si nameproj est HProf un deja defini
229 AnyDataObj* mobj = omg.GetObj(nameproj);
230 if(mobj==NULL)
231 srvo->ProjectH2(nameobj,expx,expy,expwt,expcut,nameproj,dopt,loop);
232 else if(dynamic_cast<HProf*>(mobj))
233 srvo->ProjectHProf(nameobj,expx,expy,expwt,expcut,nameproj,dopt,loop);
234 else
235 srvo->ProjectH2(nameobj,expx,expy,expwt,expcut,nameproj,dopt,loop);
236}
237
238return;
239}
240
241/* methode */
242void PAWExecutor::h_integ(vector<string>& tokens)
243// Pour remplacer le contenu d'un histo 1D par son integrale
244{
245if(tokens.size()<1)
246 {cout<<"Usage: h/integ nameh1d [norm]"<<endl; return;}
247NamedObjMgr omg;
248AnyDataObj* mobj = omg.GetObj(tokens[0]);
249if(mobj==NULL)
250 {cout<<"PAWExecutor::h_integ Error: unknow object"<<tokens[0]<<endl;
251 return;}
252double norm = 1.;
253if(tokens.size()>=2) norm = atof(tokens[1].c_str());
254Histo* h1 = dynamic_cast<Histo*>(mobj);
255// Le HProf se fait tout seul avec Histo: dynamic_cast<Histo*>(HProf)=Vrai!
256//HProf* hp = dynamic_cast<HProf*>(mobj);
257if(h1) h1->HInteg(norm);
258//else if(hp) hp->HInteg(norm);
259else cout<<"PAWExecutor::h_integ Error: "<<tokens[0]<<" not an Histo/HProf"<<endl;
260}
261
262/* methode */
263void PAWExecutor::h_deriv(vector<string>& tokens)
264// Pour remplacer le contenu d'un histo 1D par sa derivee
265{
266if(tokens.size()<1)
267 {cout<<"Usage: h/deriv nameh1d"<<endl; return;}
268NamedObjMgr omg;
269AnyDataObj* mobj = omg.GetObj(tokens[0]);
270if(mobj==NULL)
271 {cout<<"PAWExecutor::h_deriv Error: unknow object"<<tokens[0]<<endl;
272 return;}
273Histo* h1 = dynamic_cast<Histo*>(mobj);
274//HProf* hp = dynamic_cast<HProf*>(mobj);
275if(h1) h1->HDeriv();
276//else if(hp) hp->HDeriv();
277else cout<<"PAWExecutor::h_deriv Error: "<<tokens[0]<<" not an Histo/HProf"<<endl;
278}
279
280/* methode */
281void PAWExecutor::h_rebin(vector<string>& tokens)
282// Pour re-binner un histogramme 1D
283{
284if(tokens.size()<2)
285 {cout<<"Usage: h/rebin nameh1d nbin"<<endl; return;}
286NamedObjMgr omg;
287AnyDataObj* mobj = omg.GetObj(tokens[0]);
288if(mobj==NULL)
289 {cout<<"PAWExecutor::h_rebin Error: unknow object"<<tokens[0]<<endl;
290 return;}
291int nbin = atoi(tokens[1].c_str());
292Histo* h1 = dynamic_cast<Histo*>(mobj);
293//HProf* hp = dynamic_cast<HProf*>(mobj);
294if(h1) h1->HRebin(nbin);
295//else if(hp) hp->HRebin(nbin);
296else cout<<"PAWExecutor::h_rebin Error: "<<tokens[0]<<" not an Histo/HProf"<<endl;
297}
298
299/* methode */
300void PAWExecutor::h_cadd(vector<string>& tokens)
301// Additionne une constante a un histogramme
302{
303if(tokens.size()<2)
304 {cout<<"Usage: h/cadd nameh1d val"<<endl; return;}
305NamedObjMgr omg;
306AnyDataObj* mobj = omg.GetObj(tokens[0]);
307if(mobj==NULL)
308 {cout<<"PAWExecutor::h_cadd Error: unknow object"<<tokens[0]<<endl;
309 return;}
310double val = atof(tokens[1].c_str());
311Histo* h1 = dynamic_cast<Histo*>(mobj);
312Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
313if(h1) *h1 += val;
314else if(h2) *h2 += val;
315else cout<<"PAWExecutor::h_cadd Error: "<<tokens[0]<<" not an Histo/HProf/Histo2D"<<endl;
316}
317
318/* methode */
319void PAWExecutor::h_cmult(vector<string>& tokens)
320// Multiplie un histogramme par une constante
321{
322if(tokens.size()<2)
323 {cout<<"Usage: h/cmult nameh1d val"<<endl; return;}
324NamedObjMgr omg;
325AnyDataObj* mobj = omg.GetObj(tokens[0]);
326if(mobj==NULL)
327 {cout<<"PAWExecutor::h_cmult Error: unknow object"<<tokens[0]<<endl;
328 return;}
329double val = atof(tokens[1].c_str());
330Histo* h1 = dynamic_cast<Histo*>(mobj);
331Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
332if(h1) *h1 *= val;
333else if(h2) *h2 *= val;
334else cout<<"PAWExecutor::h_cmult Error: "<<tokens[0]
335 <<" not an Histo/HProf/Histo2D"<<endl;
336}
337
338/* methode */
339void PAWExecutor::h_plot_2d(vector<string>& tokens)
340// plot for 2D histogramme: plot histo, bandx/y, slicex/y or projx/y
341{
342if(tokens.size()<2)
343 {cout<<"Usage: h/plot/2d nameh2d to_plot [n/s] [dopt]"<<endl; return;}
344NamedObjMgr omg;
345AnyDataObj* mobj = omg.GetObj(tokens[0]);
346if(mobj==NULL)
347 {cout<<"PAWExecutor::h_plot_2d Error: unknow object"<<tokens[0]<<endl;
348 return;}
349Histo2D* h2 = dynamic_cast<Histo2D*>(mobj);
350if(!h2)
351 {cout<<"PAWExecutor::h_plot_2d Error: "<<tokens[0]<<" not an Histo2D"<<endl;
352 return;}
353
354Histo* h1p = NULL; string nametoplot = "/autoc/h_plot_2d_h1";
355AnyDataObj* mobjh1 = omg.GetObj(nametoplot);
356if(mobjh1!=NULL) omg.DelObj(nametoplot);
357
358string dopt = ""; if(tokens.size()>=3) dopt = tokens[2];
359if(tokens[1] == "show") {
360 h2->ShowProj();
361 h2->ShowBand(2);
362 h2->ShowSli(2);
363 return;
364} else if(tokens[1] == "h") {
365 nametoplot = tokens[0];
366} else if(tokens[1] == "px") {
367 if((h1p=h2->HProjX())) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
368 else {h2->ShowProj(); return;}
369} else if(tokens[1] == "py") {
370 if((h1p=h2->HProjY())) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
371 else {h2->ShowProj(); return;}
372} else {
373 if(tokens.size()<3)
374 {cout<<"Usage: h/plot/2d nameh2d bx/by/sx/sy n [dopt]"<<endl; return;}
375 int n = atoi(tokens[2].c_str());
376 dopt = ""; if(tokens.size()>=4) dopt = tokens[3];
377 if(tokens[1] == "bx") {
378 if((h1p=h2->HBandX(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
379 else {h2->ShowBand(); return;}
380 } else if(tokens[1] == "by") {
381 if((h1p=h2->HBandY(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
382 else {h2->ShowBand(); return;}
383 } else if(tokens[1] == "sx") {
384 if((h1p=h2->HSliX(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
385 else {h2->ShowSli(); return;}
386 } else if(tokens[1] == "sy") {
387 if((h1p=h2->HSliY(n))) {Histo* h1=new Histo(*h1p); omg.AddObj(h1,nametoplot);}
388 else {h2->ShowSli(); return;}
389 }
390}
391
392omg.DisplayObj(nametoplot,dopt);
393}
394
395/* methode */
396int PAWExecutor::decodepawstring(string tokens,string& nameobj
397 ,string& xexp,string& yexp,string& zexp)
398// Decodage general de "nameobj.xexp"
399// "nameobj.yexp%xexp"
400// "nameobj.zexp%yexp%xexp"
401// Return: nombre de variables trouvees, -1 si probleme
402{
403nameobj = ""; xexp= ""; yexp= ""; zexp= "";
404
405int lt = (int) tokens.length();
406if(lt<=0) return -1;
407
408// decodage de la chaine de type PAW.
409char *str = new char[lt+2];
410strcpy(str,tokens.c_str()); strip(str,'B',' '); lt = strlen(str);
411//cout<<"chaine1["<<lt<<"] :"<<str<<":"<<endl;
412char *c[3] = {NULL,NULL,NULL};
413int i, np=0; bool namefound = false;
414for(int i=0;i<lt;i++) {
415 if(!namefound && str[i]=='.') {
416 str[i]='\0';
417 namefound=true;
418 c[np] = str+i+1; np++;
419 }
420 if( namefound && str[i]=='%') {
421 str[i]='\0';
422 if(np<3) {c[np] = str+i+1; np++;}
423 }
424}
425//cout<<"chaine2 :"; for(i=0;i<lt;i++) cout<<str[i]; cout<<":"<<endl;
426
427// Remplissage du nom et des variables
428nameobj = str;
429if(np==1) xexp=c[0];
430if(np==2) {yexp=c[0]; xexp=c[1];}
431if(np==3) {zexp=c[0]; yexp=c[1]; xexp=c[2];}
432//cout<<"pawstring str,c[0-2] "<<str<<" "<<c[0]<<" "<<c[1]<<" "<<c[2]<<endl;
433delete [] str;
434
435// Comptage des variables
436np = -1;
437if(nameobj.length()>0)
438 {np = 0; if(xexp.length()>0)
439 {np++; if(yexp.length()>0)
440 {np++; if(zexp.length()>0) np++;}}}
441cout<<"pawstring["<<np<<"] name="<<nameobj
442 <<" xexp="<<xexp<<" yexp="<<yexp<<" zexp="<<zexp<<endl;
443return np;
444}
Note: See TracBrowser for help on using the repository browser.