Problem to picture


_ROOT Version:v5.34.38.win32.vc12
_Platform:windows
Compiler: Not Provided


I write this code,and succeed to open it by root,but the diagram just appeared a moment,I can’t see it,what the problem with my code?
thanks

#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <cmath>
#include <TH1F> 
#include <TCanvas>
using namespace std;
void filegauss()
{
	int i,y=0;
	double x=0.0,b=0.0,q,s,a[10000];
    ifstream infile;
	infile.open("C:\\Users\\asus\\Desktop\\1bg.txt",ios::in);   
    assert(infile.is_open());
    double c,pi=3.14159265;
    while (!infile.eof())
    {
        infile >> c;
        a[y]=c;
        x=x+c;
        y=y+1;
		
    }
    q=x/y;
    for(i=0;i<y;i++)
    {
        b=b+(a[i]-q)*(a[i]-q);
    }
    s=sqrt(b/y);

	TH1F*h=new TH1F("h","f",10000,-20.0,20.0);
    for(int k=;k<y;k++)
    {
    	h->(exp(-(a[k]-q)*(a[k]-q)/(2*s*s))/(sqrt(2*pi)*s));
	}
	h->Draw();
	infile.close(); 
}
Warning: wrong member access operator '.' D:\necessary application\root\root\macros\filegauss.cpp(33)
Error: Can't call TH1F::fill() in current scope D:\necessary application\root\root\macros\filegauss.cpp(33)
Possible candidates are...
(in TH1F)
(in TH1)

now I see this,what’s problem with this code?

The error message says there is an error with TH1F::fill()
but I don’t that call in trou code. Are you really running the code you posted ?
Anyway TH1F::fill() is wrong. It should be TH1F::Fill()

of course,I change tihs code many times,now it give me this result:

root[0]
Processing D:\necessary application\root\root\macros\filegauss.cpp...
Info in <TCanvas::MakeDefCanvas>:created TCanvas with name cl

here is my new code

#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <cmath>
#include <TH1F> 
#include <TCanvas>
using namespace std;
void filegauss()
{
	int i,y=0;
	double x=0.0,b=0.0,q,s,a[10000];
    ifstream infile;
	infile.open("C:\\Users\\asus\\Desktop\\1bg.txt",ios::in);   
    assert(infile.is_open());
    double c,pi=3.14159265;
    while (!infile.eof())
    {
        infile >> c;
        a[y]=c;
        x=x+c;
        y=y+1;
		
    }
    q=x/y;
    for(i=0;i<y;i++)
    {
        b=b+(a[i]-q)*(a[i]-q);
    }
    s=sqrt(b/y);

	TH1F*h=new TH1F("h","f",10000,-20.0,20.0);
    for(int k=;k<y;k++)
    {
    	h->(exp(-(a[k]-q)*(a[k]-q)/(2*s*s))/(sqrt(2*pi)*s));
	}
	h->DrawClone(); 
}

When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

I guess you should change h->(exp(...)/(...)); into h->Fill(exp(...)/(...));.

Also, “DrawClone” is not needed. Use simply “Draw”.

1 Like

Sorry,I will remember it.
And I change source code according to your suggestion,but it seens like the first result I has posted.What I need to do to see the picture?
thanks for your help.

To try your macro we need the file 1bg.txt

Can it be that you call root using the “-q” option?

1bg.txt (82.8 KB)

I use windows and I open it through bin/root.exe

what is the initial value of k ?

Ok I set it to 0.

Now I have:

#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <cmath>

using namespace std;

void filegauss()
{
   int i,y=0;
   double x=0.0,b=0.0,q,s,a[10000];
   ifstream infile;
   infile.open("1bg.txt",ios::in);
   assert(infile.is_open());
   double c,pi=3.14159265;

   while (!infile.eof()) {
      infile >> c;
      a[y]=c;
      x=x+c;
      y=y+1;
   }

   q=x/y;

   for (i=0;i<y;i++) b=b+(a[i]-q)*(a[i]-q);

   s=sqrt(b/y);

   TH1F*h=new TH1F("h","f",10000,-20.0,20.0);

   for (int k=0;k<y;k++) h->Fill(exp(-(a[k]-q)*(a[k]-q)/(2*s*s))/(sqrt(2*pi)*s));

   h->Draw();
}

It works. I get this plot:

Well,maybe my pc don’t like me,I even can’t running the same code…whatever,thanks for your help

Can you copy here the error messages you get ?

If you run exactly the macro I sent you you should put the file 1bg.txt and the macro filegauss.C in the same folder .

yes and so do I,I guess it’s because my file type?It’s c++source file

The “h” histogram in @couet plot has 10001 entries but you have a[10000] only.

I suspect the problem on windows is that when a macro is automatically opened (by clicking on it) then root.exe is executed with “-q” (and so it automatically exits as soon as the macro finishes),

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.