Fit of an array

Hi sorry to open a new topic, but I think that in this one Particle direction there is a lot confusion, so it’s better to write a new one.

I’ve this ROOT file
si-500596.root (1.4 MB)

by this macro

TBAlign.C (22.4 KB) TBAlign.h (4.3 KB)

and thanks to @Wile_E_Coyote who helped me to run it, I generete a new ROOT file in which I traslated the xh coordinate from local one to laboratory one.

By this code

if(nx33==1) hx33->Fill(x33[0]);
       if(nx35==1) hx35->Fill(x35[0]);
       if(nx37==1) hx37->Fill(x37[0]);

I also produced 3 histograms of the x33[0] @z33=1833.73, x35[0] @z35=1995.43 and x37[0] @z37=2125.43 where
x33[0], x35[0] and x37[0] are the x coordinates and z33, z35 and z37 the z coordinate of hits in 3 silicon trackers called 33,35 and 37, as showed in this image

. In each histogram I get more than 4000 events (in the ROOT file there are 5237 entries, but surely due to no-hit, there are empty entries so I get less than 5000 events in each histogram).

Now I’ve a problem.

In the ROOT file I’ve 5237 entries.
When I’ve a hit in the three silicon tracker (i.e. if(nx33==1 && nx35==1 && nx37==1)) for each event, I’ve to fit by this by this fuction
fit_line.C (1.2 KB)
the 3 the triad

Ai=(z33, x33[0])
Bi=(z35, x35[0])
Ci=(z35, x37[0])

where x33[0], x35[0], x37[0] values are stored in the ROOT file, instead I’ve to set z33=1873.93, z35= 1995.43 and z37=2125.43.

You see that given that I’ve 5237 entries, I can’t write by hand 5237 times the values in the macro and the fit results in the output file. So I need a way to read all the 5237 entries of x33[0], x35[0], x37[0] to fit the points Ai, Bi, Ci and write the results of the fit in a txt file.

I was trying in this way.

#include <fstream>

        ofstream results;
 	results.open("C:/si-calo-sep18/Fit_parameters.txt",  ios::out);
 	char s[]="Fit Parameters";
       int i;
        int n=3;
       int ne=5237; 			//number of entries in the ROOT file 
 x[0]=x33[0];
        x[1]=x35[0];
        x[2]=x37[0];
        
			results << "i \t a \t \ae \t b \t be \t chi2 \t \q" << endl;
			for( i=0; i<=fChain->GetEntries(); i++){
				z[0]=1873.93;
				z[1]=1995.43;
				z[2]=2125.43;
				ex[0]=0.1;
				ex[1]=0.1;
				ex[2]=0.1;
				if(nx33==1 && nx35==1 && nx37==1){
					fit_line(z, x, n, ex, a, b, ae, be, chi2, q); // hits x versus z
						results << i << "\t " << a << "\t " << ae << "\t " << b << "\t " << be <<  "\t " << chi2 << "\t "<< q << endl;
		}	
			}
results.close(); 

What I would to do was:

  1. To fill a 5237 rows and 3 culomns array z[] with the values
  • 1873.93 in the 5237 rows of the first culomn;
  • 1995.43 in the 5237 rows of the second culomn;
  • 2125.43 in the 5237 rows of the third culomn;

To fill a 5237 rows and 3 culomns array ex[] with the values

  • 0.1 in the 5237 rows of the first culomn;
  • 0.1 in the 5237 rows of the second culomn;
  • 0.1 in the 5237 rows of the third culomn;

(ex[] is the error on the x-coordinate that I must set 0.1 for the 3 silicon trackers)

  1. Only when I have an hit in the 3 silicon trackers (that’s because I wrote if(nx33==1 && nx35==1 && nx37==1)
  • Fit the point the 3 point of each event and write the fit parameters (a,b,chi2, etc) on a txt file

I runned the macro whitout errors, and I get the txt file but

  1. The results of all rows are identical, so I guess that the macro doesn’t fit all the line, but just the firs one
  2. I should get a negative value of the a parameter (because the x values are all negative), instead I get a positive one!
  3. I get 5237 lines in the result file, instead surely the are rows without hit in some silicon tracker (so I’ve not fit these rows)

Here the results

Fit_parameters.txt (310.9 KB)

@Wile_E_Coyote, @bellenot, @couet, @moneta please someone can check the macro?

Thank you


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20/24
Platform: Windows
Compiler: Not Provided


yes … and it has some errors:

Processing TBAlign.C...
In file included from input_line_12:1:
/Users/couet/Downloads/TBAlign.C:557:51: warning: unknown escape sequence '\q' [-Wunknown-escape-sequence]
                        results << "i \t a \t ae \t b \t be \t chi2 \t \q" << endl;
                                                                       ^~
/Users/couet/Downloads/TBAlign.C:565:30: error: expected expression
                                std::cout << "x = " << x[] << std::endl;
                                                         ^

Sorry @couet

this one


wasn’t showed in my ROOT (it looks like that ROOT for windows doesn’t show some errors/warnings, because it isn’t the first time…)

The second one


was made later that I runned it …I was trying to modify the macro and I deleted for error the 0.

Here the macro
TBAlign.C (22.3 KB)

I get:

root [0] .x TBAlign.C
(TBAlign) @0x7fa586af8420
root [1] 

Then what should we look at ?

Thank you @couet! yes … few days ago, @Wile_E_Coyote explained me that to run this macro we have to do

root [0] .L TBAlign.c++g
root [1] TBAlign t
root [2] t.Loop();

He explained me the reason here Particle direction

I also didn’t know it, because my supervisor’s colleague gave me the original macro and I’ve to modify it…but he didn’t say me how to run the macro…

I get:

root [2] t.Loop();
0 0 10
0 0 10
0 0 11
root [3] 

yes @couet, it’s correct! in the terminal you just get that!

But it also creates :

  1. The root File in wich the xh[] array is traslated to laboratory coordinate system
    TFile *newfile = new TFile("C:/si-calo-sep18/si-500596-trasl.root","recreate");

  2. The Fit parameters result file
    results.open("C:/si-calo-sep18/Fit_parameters.txt", ios::out);

maybe you have to change the directory in the TBAling.c macro…
I setted the directory C:/si-calo-sep18/
if you don’t get the 2 files, try to modify the directory …

The file si-500596-trasl.root is:

root [1] _file0->ls()
TFile**		si-500596-trasl.root	
TFile*		si-500596-trasl.root	
 KEY: TTree	lemma;1	silicon ntuple
 KEY: TH1F	hnx10;1	N hits in Si10, X coord
 KEY: TH1F	hnx20;1	N hits in Si20, X coord
 KEY: TH1F	hnx30;1	N hits in Si30, X coord
 KEY: TH1F	hnx31;1	N hits in Si31, X coord
 KEY: TH1F	hnx33;1	N hits in Si33, X coord
 KEY: TH1F	hnx35;1	N hits in Si35, X coord
 KEY: TH1F	hnx37;1	N hits in Si37, X coord
 KEY: TH1F	hx10;1	X cood, Si10
 KEY: TH1F	hx20;1	X cood, Si20
 KEY: TH1F	hx30;1	X cood, Si30
 KEY: TH1F	hx31;1	X cood, Si31
 KEY: TH1F	hx33;1	X cood, Si33
 KEY: TH1F	hx35;1	X cood, Si35
 KEY: TH1F	hx37;1	X cood, Si37
 KEY: TH2F	h2x20v10;1	X, x20:x10
 KEY: TH2F	h2x30v20;1	X, x30:x20
 KEY: TH2F	h2x31v30;1	X, x31:x30
 KEY: TH2F	h2x33v31;1	X, x33:x31
 KEY: TH2F	h2x35v33;1	X, x35:x33
 KEY: TH2F	h2x37v35;1	X, x37:x35
 KEY: TH2F	h2y20v10;1	Y, y20:y10
 KEY: TH2F	h2y30v20;1	Y, y30:y20
 KEY: TH2F	h2y31v30;1	Y, y31:y30
 KEY: TH2F	h2y33v31;1	Y, y33:y31
 KEY: TH2F	h2y35v33;1	Y, y35:y33
 KEY: TH2F	h2y37v35;1	Y, y37:y35
 KEY: TH1F	h20diff;1	X: x20-x10
 KEY: TH1F	h30diff;1	X: x30-x20
 KEY: TH1F	h31diff;1	X: x31-x30
 KEY: TH1F	h33diff;1	X: x33-x31
 KEY: TH1F	h35diff;1	X: x35-x33
 KEY: TH1F	h37diff;1	X: x37-x35
 KEY: TH1F	h20xz;1	X: x20-x10, mrad
 KEY: TH1F	h20yz;1	Y: y20-y10, mrad
 KEY: TH1F	h33xz;1	X: x33-x31, mrad
 KEY: TH1F	h35xz;1	X: x35-x33, mrad
 KEY: TH1F	h37xz;1	X: x37-x35, mrad
 KEY: TH1F	h33yz;1	Y: y33-y31, mrad
 KEY: TH1F	h35yz;1	Y: y35-y33, mrad
 KEY: TH1F	h37yz;1	Y: y37-y35, mrad
 KEY: TH2F	h2x20v10all;1	X, x20:x10
 KEY: TH2F	h2x30v20all;1	X, x30:x20
 KEY: TH2F	h2x31v30all;1	X, x31:x30
 KEY: TH2F	h2x33v31all;1	X, x33:x31
 KEY: TH2F	h2x35v33all;1	X, x35:x33
 KEY: TH2F	h2x37v35all;1	X, x37:x35
 KEY: TH2F	h2y20v10all;1	Y, y20:y10
 KEY: TH2F	h2y30v20all;1	Y, y30:y20
 KEY: TH2F	h2y31v30all;1	Y, y31:y30
 KEY: TH2F	h2y33v31all;1	Y, y33:y31
 KEY: TH2F	h2y35v33all;1	Y, y35:y33
 KEY: TH2F	h2y37v35all;1	Y, y37:y35
root [2] 

yes @couet! In that file there is the TTree lemma

and some histograms made by me or by my supervisor’s colleague.

For example I made these histograms stored in the ROOT file

       if(nx33==1) hx33->Fill(x33[0]);
       if(nx35==1) hx35->Fill(x35[0]);
       if(nx37==1) hx37->Fill(x37[0]);

Filling these histograms, I get three histograms having more than 4000 events each one. IN particular,

  1. x33[0] is consituted by 4832 values in the range [-12;-3]
  2. x35[0] is constituted by 4984 values in the range [-18;-10]
  3. x37[0] is constituted by 4661 values in the range [-30;-16]

Now I need this.

You see the lemma TTree has 5237 entries for each entry, if in the entry, I’ve values in x33[0], x35[0] and x37[0] (i.e. if(nx33==1 && nx35==1 && nx37==1) ) i’ve to fit the points:

Ai= (1833.73, x33[0]);
Bi=(1995.43, x35[0]);
Ci=(2125.43, x35[0]);

For example, let’s image that first two entries of x33[0], x35[0] and x37[0] are

  1. x33[0]= -6, x35[0]= - 17, x37[0] = -20
  2. x33[0]= -12, x35[0]= - 10, x37[0] = -30

I’ve the first triad to fit by the fit_line.c

A1= (1833.73, -6);
B1=(1995.43, -17);
C1=(2125.43,-20);

and I’ve to print the fit parameters (a, b, chi2 ecc) on the file

The second triad to fit and to get the fit parameters is

A2= (1833.73, -12);
B2=(1995.43, -10);
C2=(2125.43,-30);

and so on for all the 5237 entries.

Notice that all the x33[0], x35[0] and x37[0] values must have error 0.1, that’s because I wrote

ex[0]=0.1;
ex[1]=0.1;
ex[2]=0.1;

You need to move the code which calls “fit_line” and then saves the calculated values into “results” (for the current tree entry) to a place before the “} // loop in jentry” line (i.e. to inside of the “for” loop over all tree entries).

Hi @Wile_E_Coyote thank you

I moved the code

results << "i \t a \t ae \t b \t be \t chi2 \t q" << endl;
			 for( i=0; i<=fChain->GetEntries(); i++){
			 	x[0]=x33[0];
        		x[1]=x35[0];
        		x[2]=x37[0];
				z[0]=1873.93;
				z[1]=1995.43;
				z[2]=2125.43;
				ex[0]=0.1;
				ex[1]=0.1;
				ex[2]=0.1;
				//std::cout << "x = " << x[0] << std::endl;
				if(nx33==1 && nx35==1 && nx37==1){
					fit_line(z, x, n, ex, a, b, ae, be, chi2, q); // hits x versus z
					results << i << "\t " << a << "\t " << ae << "\t " << b << "\t " << be <<  "\t " << chi2 << "\t "<< q << endl;
				}	
			}

inside the loop, but I get a 1294176kB txt file

and the editor can’t open it… then, surely ROOT doesn’t wrote just the fit results…

This is the updated macro

TBAlign.C (22.2 KB)

So, you now have a “for” loop over all entries inside of another “for” loop over all entries. Is this normal? :face_with_thermometer:

Than you @Wile_E_Coyote I deleted my for loop so now I’ve

x[0]=x33[0];
        		x[1]=x35[0];
        		x[2]=x37[0];
				z[0]=1873.93;
				z[1]=1995.43;
				z[2]=2125.43;
				ex[0]=0.1;
				ex[1]=0.1;
				ex[2]=0.1;
				std::cout << "x0 = " << x[0] << "\t x1 = " << x[1]  << " \t x2 = " << x[2] <<  "\t z0 = " << z[0] << "\t z1 = " << z[1]  << "\t z2 = " << z[2] << std::endl;
				if(nx33==1 && nx35==1 && nx37==1){
					fit_line(z, x, n, ex, a, b, ae, be, chi2, q); // hits x versus z
					results << i << "\t " << a << "\t " << ae << "\t " << b << "\t " << be <<  "\t " << chi2 << "\t "<< q << endl;
				}

by the terminal I see the values x[0], x[1], x[2] and z[0], z[1], z[2] looks like reasonable

But it looks like there is the last problem

i.e. by opening the fit_parameters.txt file I see the a values are all high and positive! Instead they should be low and negative (because the x values are low and negative) …maybe, did I set wrong the line

fit_line(z, x, n, ex, a, b, ae, be, chi2, q); // hits x versus z

Here the updated macro and the results

TBAlign.C (22.2 KB)
Fit_parameters.txt (272.5 KB)

The “if(nx33==1 && nx35==1 && nx37==1){” line should be moved to before “x[0]=x33[0];”.
And use ("i" is meaningless): results << jentry << "\t " ...

thank you @Wile_E_Coyote

I moved the if(nx33==1 && nx35==1 && nx37==1){

and I deleted the i (it was a remnant from the other for )

then now my code is

if(nx33==1 && nx35==1 && nx37==1){
	  			if(x[0]>-999 && x[1]>-999 && x[2]>-999){
			 	x[0]=x33[0];
        		x[1]=x35[0];
        		x[2]=x37[0];
				z[0]=1873.93;
				z[1]=1995.43;
				z[2]=2125.43;
				ex[0]=0.1;
				ex[1]=0.1;
				ex[2]=0.1;
					std::cout << "x0 = " << x[0] << "\t x1 = " << x[1]  << " \t x2 = " << x[2] <<  "\t z0 = " << z[0] << "\t z1 = " << z[1]  << "\t z2 = " << z[2] << std::endl;
					fit_line(z, x, n, ex, a, b, ae, be, chi2, q); // hits x versus z
					results << jentry << "\t " <<  a << "\t " << ae << "\t " << b << "\t " << be <<  "\t " << chi2 << "\t "<< q << endl;
						}
				}

but I still get bad a parameters…

Fit_parameters.txt (247.1 KB) TBAlign.C (22.3 KB)

@Wile_E_Coyote Sorry! I just checked ! the a and b parameters are correct because the fit function is a+bz! I remembered b+az (but I was wrong!)
Then thank you! I think it works fine!

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