peaks2.C

Go to the documentation of this file.
00001 // Example to illustrate the 2-d peak finder (class TSpectrum2).
00002 // This script generates a random number of 2-d gaussian peaks
00003 // The position of the peaks is found via TSpectrum2
00004 // To execute this example, do
00005 //  root > .x peaks2.C  (generate up to 50 peaks by default)
00006 //  root > .x peaks2.C(10) (generate up to 10 peaks)
00007 //  root > .x peaks2.C+(200) (generate up to 200 peaks via ACLIC)
00008 //
00009 // The script will iterate generating a new histogram having
00010 // between 5 and the maximun number of peaks specified.
00011 // Double Click on the bottom right corner of the pad to go to a new spectrum
00012 // To Quit, select the "quit" item in the canvas "File" menu
00013 //
00014 //Author: Rene Brun
00015    
00016 #include "TSpectrum2.h"
00017 #include "TCanvas.h"
00018 #include "TRandom.h"
00019 #include "TH2.h"
00020 #include "TF2.h"
00021 #include "TMath.h"
00022 #include "TROOT.h"
00023 
00024 TSpectrum2 *s;
00025 TH2F *h2 = 0;
00026 Int_t npeaks = 30;
00027 Double_t fpeaks2(Double_t *x, Double_t *par) {
00028    Double_t result = 0.1;
00029    for (Int_t p=0;p<npeaks;p++) {
00030       Double_t norm   = par[5*p+0];
00031       Double_t mean1  = par[5*p+1];
00032       Double_t sigma1 = par[5*p+2];
00033       Double_t mean2  = par[5*p+3];
00034       Double_t sigma2 = par[5*p+4];
00035       result += norm*TMath::Gaus(x[0],mean1,sigma1)*TMath::Gaus(x[1],mean2,sigma2);
00036    }
00037    return result;
00038 }
00039 void findPeak2() {
00040    printf("Generating histogram with %d peaks\n",npeaks);
00041    Int_t nbinsx = 200;
00042    Int_t nbinsy = 200;
00043    Double_t xmin   = 0;
00044    Double_t xmax   = (Double_t)nbinsx;
00045    Double_t ymin   = 0;
00046    Double_t ymax   = (Double_t)nbinsy;
00047    Double_t dx = (xmax-xmin)/nbinsx;
00048    Double_t dy = (ymax-ymin)/nbinsy;
00049    delete h2;
00050    h2 = new TH2F("h2","test",nbinsx,xmin,xmax,nbinsy,ymin,ymax);
00051    h2->SetStats(0);
00052    //generate n peaks at random
00053    Double_t par[3000];
00054    Int_t p;
00055    for (p=0;p<npeaks;p++) {
00056       par[5*p+0] = gRandom->Uniform(0.2,1);
00057       par[5*p+1] = gRandom->Uniform(xmin,xmax);
00058       par[5*p+2] = gRandom->Uniform(dx,5*dx);
00059       par[5*p+3] = gRandom->Uniform(ymin,ymax);
00060       par[5*p+4] = gRandom->Uniform(dy,5*dy);
00061    }
00062    TF2 *f2 = new TF2("f2",fpeaks2,xmin,xmax,ymin,ymax,5*npeaks);
00063    f2->SetNpx(100);
00064    f2->SetNpy(100);
00065    f2->SetParameters(par);
00066    TCanvas *c1 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c1");
00067    if (!c1) c1 = new TCanvas("c1","c1",10,10,1000,700);
00068    h2->FillRandom("f2",500000);
00069    
00070    //now the real stuff: Finding the peaks
00071    Int_t nfound = s->Search(h2,2,"col");
00072    
00073    //searching good and ghost peaks (approximation)
00074    Int_t pf,ngood = 0;
00075    Float_t *xpeaks = s->GetPositionX();
00076    Float_t *ypeaks = s->GetPositionY();
00077    for (p=0;p<npeaks;p++) {
00078       for (Int_t pf=0;pf<nfound;pf++) {
00079          Double_t diffx = TMath::Abs(xpeaks[pf] - par[5*p+1]);
00080          Double_t diffy = TMath::Abs(ypeaks[pf] - par[5*p+3]);
00081          if (diffx < 2*dx && diffy < 2*dy) ngood++;
00082       }
00083    }
00084    if (ngood > nfound) ngood = nfound;
00085    //Search ghost peaks (approximation)
00086    Int_t nghost = 0;
00087    for (pf=0;pf<nfound;pf++) {
00088       Int_t nf=0;
00089       for (Int_t p=0;p<npeaks;p++) {
00090          Double_t diffx = TMath::Abs(xpeaks[pf] - par[5*p+1]);
00091          Double_t diffy = TMath::Abs(ypeaks[pf] - par[5*p+3]);
00092          if (diffx < 2*dx && diffy < 2*dy) nf++;
00093       }
00094       if (nf == 0) nghost++;
00095    }
00096    c1->Update();
00097    
00098    s->Print();
00099    printf("Gener=%d, Found=%d, Good=%d, Ghost=%d\n",npeaks,nfound,ngood,nghost);
00100    printf("\nDouble click in the bottom right corner of the pad to continue\n");
00101    c1->WaitPrimitive();
00102 }
00103 void peaks2(Int_t maxpeaks=50) {
00104    s = new TSpectrum2(2*maxpeaks);
00105    for (int i=0; i<10; ++i) {
00106       npeaks = (Int_t)gRandom->Uniform(5,maxpeaks);
00107       findPeak2();
00108    }
00109 }
00110    
00111    

Generated on Tue Jul 5 15:45:10 2011 for ROOT_528-00b_version by  doxygen 1.5.1