Storing histograms in root file and produce png file

Hello, I’ve this root file WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free (a tree)

I’ve to produce histograms saving them in root file and plotting them in png files.

In particular, I’ve to produce, plot and save 7 histograms, that because I defined the energy vector

std::vector<int> Epeak={1824,4028,5014,5045,5060,6414,6997};

then, depending on the peak energy, I define the energy window and I’ve to make an histogram for each energy window ie:

for (int i=0; i<Epeak.size();i++){
			Epeaki=Epeak.at(i);
			cout << "Start processing Epeak= " << Epeaki << endl;
			if (Epeaki==1824){
				x1=1816;
				x2=1832;
			}
			else if (Epeaki==4028){
				x1=4015;
				x2=4032;
			}
etc

and I produce the histogram corresponding to the energy window by the TCut i.e.

TString string = TString::Format("e0 >> htemp(7500, 0, 7500)");
				TCut pulser0 = TString::Format("e2==0").Data();
		    	TCut pulser1 = TString::Format("e3==0").Data();
		    	TCut detection = TString::Format("e0>0").Data();
			    TCut winsx = TString::Format("e1>%d",x1).Data();
			    TCut windx= TString::Format("e1<%d",x2).Data();
				te->Draw(string, detection && winsx && windx && pulser0 && pulser1);
	 			te->GetHistogram()->SetTitle("");
 				te->SetScanField(0);
  	 			TH1F *hGe[i]= (TH1F*)gPad->GetPrimitive("htemp"); 
  				hGe[i]->GetXaxis()->SetTitle("Energy (keV)");

etc.

then, for example, hGe[0] should be the histogram when Epeak==1824 (i.e. x1=1816 and x2=1832), similarly, hGe[1] should be the histogram when Epeak==4028 (i.e. x1==4015 and x2==4032), etc

then I’ve to store the 7 histograms in the root file (i.e. in the Root file I should have hGe[0], hGe[1]… hGe[6]) and I’ve to plot them in png files, ie. I should get the 7 png files given by

TString outfile = TString::Format("%s_Spectrum_when_%s=[%d,%d]_Run0%d.png",det,detfixed,x1,x2,run).Data();

i.e.

GePD_Spectrum_when_GeDD=[1816,1832]_Run0610.png
GePD_Spectrum_when_GeDD=[4015,4032]_Run0610.png
etc

I wrote the macro, but I get this error

Processing edepcoincroot.cpp...
In file included from input_line_8:1:
edepcoincroot.cpp:104:14: error: variable-sized object may not be initialized
                                TH1F *hGe[i]= (TH1F*)gPad->GetPrimitive("htemp"); 
                                      ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
edepcoincroot.cpp:108:11: error: source file is not valid UTF-8
                                hGe[<EC>]->GetYaxis()->SetTitleFont(43);
                                    ^
edepcoincroot.cpp:108:12: error: expected expression
                                hGe[<EC>]->GetYaxis()->SetTitleFont(43);

edepcoincroot.cpp (4.6 KB)

Thank you


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Try to “google”
C++ error: variable-sized object may not be initialized
There is a lot of answers.

Hi @couet,
I used

TH1F *hGe[i];
				hGe[i] = 0;
....

hGe[i]= (TH1F*)gPad->GetPrimitive("htemp"); 

to initialize, but now I get

Processing edepcoincroot.cpp...
In file included from input_line_8:1:
edepcoincroot.cpp:111:11: error: source file is not valid UTF-8
                                hGe[<EC>]->GetYaxis()->SetTitleFont(43);
                                    ^
edepcoincroot.cpp:111:12: error: expected expression
                                hGe[<EC>]->GetYaxis()->SetTitleFont(43);
                                        ^

edepcoincroot.cpp (4.7 KB)

First:

      int nhist = Epeak.size();
      TH1F *hGe[nhist]; 
      for (int i=0; i<nhist;i++) {

And then:

         hGe[i]= (TH1F*)gPad->GetPrimitive("htemp"); 

Thank you @bellenot, but unfortunately I get

Processing edepcoincroot.cpp...
In file included from input_line_8:1:
edepcoincroot.cpp:113:11: error: source file is not valid UTF-8
                                hGe[<EC>]->GetYaxis()->SetTitleFont(43);
                                    ^
edepcoincroot.cpp:113:12: error: expected expression
                                hGe[<EC>]->GetYaxis()->SetTitleFont(43);

even using your code

edepcoincroot.cpp (4.7 KB)

BTW, using an array is not even needed in your case… You could simply use:

      for (int i=0; i<Epeak.size();i++) {

and then:

         TH1F *hGe= (TH1F*)gPad->GetPrimitive("htemp");

and replace all the hGe[i] by hGe in your code, that will also fix the error you get (you mix different font encoding in your editor…)

And you can also fix this warning:

edepcoincroot.cpp:84:10: warning: 'sprintf' will always overflow; destination buffer has size 50, but format string expands to at least 74
      [-Wfortify-source]
         sprintf(restit,"***************** Spectrum of %s when fixing %s energy***********************...
         ^

Thank you @bellenot , it works!

Just a question…how is it possible I was mixiging different font? I’m using always the same editor (dev c++ to write the code), then I upload the code on laboratory machine and I run the macro…

If I will need it in future, how to avoid that problem???

Thank you, I replaced

char restit[200];

I didn’t notice it because my compiler didn’t show that warning.

Well, that’s hard to catch, but usually simply re-typing the code will fix it. If you look carefully at the error:

edepcoincroot.cpp:113:11: error: source file is not valid UTF-8
                                hGe[<EC>]->GetYaxis()->SetTitleFont(43);
                                    ^

It says not valid UTF-8, but if you look at line 113, you can see

                                hGe[ì]->GetYaxis()->SetTitleFont(43);

which look fine, but if you look carefully, you can see the ì is slightly different, so simply delete the ì and re-type i, it should be fine. Note that in some editors, you will see it:
image

Thank you @bellenot!
maybe, given that I’ve italian keyboard, I clicked the accented i, without realizing it!
which editor did you use to see in that way?

Most probably

Visual Studio Code Editor

Thank you for all @bellenot!

1 Like

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