/**************************************************************** * * Function: minArray * Input: int *A Pointer to input array * int n Size of the array * * Output: int * * Description: Finds the minimum of a given Array A of size * n. This function uses the balanced tree method to determine * the minimum. It returns the minimum when the algorithm * terminates. * *****************************************************************/ int minArray(int *A, int n){ int j, min, m; int i, output; min = INT_MAX; m = ceil(log2(n)); int *B = malloc((n>>1)*sizeof(int)); if(n == 1){ return A[0]; } /*First Case to save A to B*/ if(n%2){ min = A[n-1]; } n = n>>1; minima(A, n, B); /*Cycle through the rest of Log2 levels of the B array*/ for(j=2;j<=m;j++){ /*Check to see if odd and save odd element*/ if((n%2) && (B[n-1] < min)){ min = B[n-1]; } /*calculate the min using balanced tree with even n*/ n = n>>1; minima(B, n, B); /*check to see if the result of the tree is greater than min*/ if(B[0]>min) B[0] = min; } //just to make sure if( min > B[0]) min = B[0]; free(B); return min; }
int minima( int *a,int l, int r) { printf("%d,%d!!\n",l,r); if(l>r)return -1; else if(l==r-1||l==r) { printf("EQUALITY\n"); if((a[l-1]>a[l] && a[l]<=a[l+1])||(a[l-1]>=a[l] && a[l]<a[l+1]) ) return l; else return -1; } else { int mid= (l+r)>>1; printf("l<r...%d\n",mid); if((a[mid-1]>a[mid] && a[mid]<=a[mid+1])|| (a[mid-1]>=a[mid] && a[mid]<a[mid+1])) {printf("HERE mid=%d\n",mid); return mid;} else if(a[mid-1]<a[mid]) {printf("l..mid-1\n");return minima(a,l,mid);} else if (a[mid]<=a[mid-1]) {printf("mid+1..r\n");return minima(a,mid,r);} } return 0; }
int main() { int i,len,f; scanf("%d",&len); int *a=(int *)calloc(sizeof(int),len); for(i=0;i<len;i++) { scanf("%d",(a+i)); } f=minima(a,0,len-1); if(f==-1) printf("No Minima found!\n"); else printf("Minima found at %d. %d->%d->%d!!\n",f,a[f-1],a[f],a[f+1]); return 0; }
int main(){ //////////////////// // 1. Set Up PDFs // //////////////////// // Set up binning AxisCollection axes; axes.AddAxis(PdfAxis("energy", 2, 3, 10, "Energy")); // Only interested in first bit of data ntuple DataRepresentation dataRep(0); // Set up pdf with these bins in this observable BinnedPdf bgPdf(axes); bgPdf.SetDataRep(dataRep); BinnedPdf signalPdf(axes); signalPdf.SetDataRep(dataRep); std::cout << "Initialised Pdfs" << std::endl; ///////////////////////////////////// // 2. Fill with data and normalise // ///////////////////////////////////// ROOTNtuple bgMC("filename.root", "treename"); ROOTNtuple signalMC("filename.root", "treename"); for(size_t i = 0; i < 10; i++){ bgPdf.Fill(bgMC.GetEntry(i)); } for(size_t i = 0; i < 10; i++){ signalPdf.Fill(signalMC.GetEntry(i)); } bgPdf.Normalise(); signalPdf.Normalise(); std::cout << "Filled pdfs " << std::endl; //////////////////////////// // 3. Set Up LH function // //////////////////////////// BinnedNLLH lhFunction; lhFunction.SetDataSet(&signalMC); // initialise withe the data set lhFunction.AddPdf(bgPdf); lhFunction.AddPdf(signalPdf); std::cout << "Built LH function " << std::endl; // Set up the optimisation GridSearch gSearch(&lhFunction); std::vector<double> minima(2, 0); std::vector<double> maxima(2, 100); std::vector<double> stepsizes(2, 1); gSearch.SetMaxima(maxima); gSearch.SetMinima(minima); gSearch.SetStepSizes(stepsizes); //////////// // 4. Fit // //////////// gSearch.Optimise(); std::vector<double> fit = gSearch.GetFitResult().GetBestFit(); std::cout << "Best Fit: " << std::endl; for(size_t i = 0; i < fit.size(); i++) std::cout << fit.at(i) << "\t"; std::cout << std::endl; return 0; }