I have nearly completed the conversion of one of my original ROOT scripts into python, except for one impasse. Within my fit routine, I have having difficulty translating the following code into python.
I learned that par is a buffer object instead of a list like I originally thought, so slices won’t work. I’ve also messed around with the built-in buffer module, but without success.
I’m sure many of you have had this problem and have found an easy solution. If so, would you mind sharing it?
assuming that I understand your situation correctly, I can’t think of anything better than:a = array.array( 'd', [ par[3] ] )
value = quadBackground(x, a) + gaussPeak(x, par);
par[3] = a[0]
return value
Which version of ROOT or you using?
I’ve sent the very small data file as an attachment.
The simple solution would be to write out everything, but in a few of my classes I have written functions to dynamically generate TF1 objects, and it would be a pain to write every possible function combination I might use!
just a quick reply, as I appear to have misunderstood (I’ll try your code tomorrow and look in more detail): if you only want to slice the array, and don’t care about modifying it or passing it to a C++ function (which is what I originally thought), just copying it into a list and slicing that:slice = list(par)[2:]will do the trick.
The slicing of the buffer directly fails b/c it is sliced on 1-byte boundaries instead of sizeof(double) boundaries and the returned as a string. I’ve put fixing that on the TODO list (ATLAS software workshop, next week, so busy ).
a related question I believe, maybe simply the same?
I am working in pyroot, and have a TGraph. From the TGraph I would like to get hold of the arrays that make the x and y-axis, and feed these into a new TGraphAsymmError object. I tried the following:
x_arr = graph.GetX()
y_arr = graph.GetY()
This seems to be an object of type buffer.
I want to use these arrays again to pass into a TGraphAsymmError object. How to convert the arrays properly?
the returns from GetX() and GetY() are buffers b/c the C++ header only declares a Double_t*, from which no further information (such as the size of the array) can be had. If you want to pass that buffer to a python list, you’ll first need to fix up its size (I think that the call GetN() on the TGraph hands the right size; use SetSize(newsize) on the buffer to set its size). The reason being that the list iterates over the buffer to copy the values, and so w/o its size being set, it will iterate beyond the end of the underlying array.
However, I’m puzzled as to why you want to construct a new array to pass into TGraphAsymmError? AFAICS, it takes a Double_t* in its ctor, so the buffer can be readily passed in, no?
Thank you, excellent. I did not understand that it could just be used like that.
Works very well, thank you.
I wonder about a second thing: Really what I would like is to port the values from a histogram into the TGraphAsymmErrors, but I did not find a way to elegantly get both the x- and y-values. So I first made the histogram into a TGraph and then used the GetX() and GetY().
Am I right that one cannot grab the array of x and y-values directly from the histogram itself?
the y-values can be hadd from the GetArray() function that histograms inherited from TArrayF/D. The x-values I think would have to be build up from a new array.
(I see that TGraphAsymErrors has a contructor that takes a TH1*, but it loops over the errors of the histogram, not the values, so I presume that’s not what you want.)