mcr_included/HistogramCreators.C

00001 #include "Riostream.h"
00002 #include "TH1F.h"
00003 #include "TRandom.h"
00004 #include "TThread.h"
00005 
00006 TCanvas *fShowCanv;
00007 
00008 Bool_t his_creator_using_display = kFALSE;
00009 
00010 void UpdateDisplay(TH1F *h_start=0,TH1F *h_bck=0,TH1F *h_res=0){
00011   
00012   if(!his_creator_using_display) return;
00013 
00014    if(!fShowCanv){
00015      fShowCanv = new TCanvas("ShowCanv","Show Canvas",100,50,900,700);
00016      fShowCanv->Draw();
00017      fShowCanv->Divide(1,2);
00018    }
00019 
00020    if(h_start){
00021      fShowCanv->cd(1);
00022      h_start->Draw();
00023      if(h_bck) h_bck->Draw("same");
00024 
00025      gPad->Modified();
00026      gPad->Update();
00027    }
00028 
00029    if(h_res){
00030      fShowCanv->cd(2);
00031      h_res->Draw();
00032      gPad->Modified();
00033      gPad->Update();
00034    }
00035   
00036 }
00037 
00038 
00039 void FillPeak(TH1F *his,const Double_t &mean,const Double_t &sigma,const Int_t &ntimes){
00040 
00041   for(Int_t i=0;i<ntimes;i++){
00042     his->Fill(gRandom->Gaus(mean,sigma));
00043   }
00044 }
00045 
00046 
00047 void FillNoiseUniform(TH1F *his,const Int_t &ntimes){
00048   
00049   for(Int_t i=0;i<ntimes;i++){
00050     his->Fill(gRandom->Uniform(0,1));
00051   }
00052   
00053 }
00054 
00055 void FillNoise2(TH1F *his,const Int_t &ntimes){
00056   
00057   for(Int_t nn=0;nn<3;nn++){
00058     Double_t curmean = gRandom->Uniform(0.2,0.8);
00059     Double_t cursigma = gRandom->Uniform(0.1,0.05);
00060     for(Int_t i=0;i<ntimes;i++){
00061       his->Fill( gRandom->Gaus(gRandom->Gaus(curmean,0.1),gRandom->Gaus(cursigma,0.1)) );
00062     }
00063     
00064   }
00065 }
00066 
00067 TH1F *BuildExampleHistogram(const Int_t &n_simul_peaks,const Double_t &simul_sigma,const Double_t &sig_noise_ratio,Bool_t use_display=kFALSE){
00068 
00069   his_creator_using_display =use_display;
00070 
00071   UpdateDisplay();
00072 
00073   Int_t sleepms = 700;
00074 
00075   if(gDirectory->FindObject("his")){
00076     TThread::Printf("removing previous histogram");
00077     delete gDirectory->FindObject("his");
00078   }
00079   TH1F *his = new TH1F("his","test histogram",2000,0,1);
00080   
00081   Double_t step = 1/( (Double_t)(n_simul_peaks+1) );
00082   Int_t ntimes = 10000;
00083 
00084   Double_t start = step;
00085   for(Int_t ip=0;ip<n_simul_peaks;ip++){
00086     FillPeak(his,start,simul_sigma,ntimes);
00087     //    cout << step << " ; " << simul_sigma << " ; " << ntimes << endl;
00088     start+=step;
00089     UpdateDisplay(his);
00090   }
00091 
00092   Int_t ntimesnoise = (Int_t) ( ( (Double_t)his->GetNbinsX()) * his->GetMaximum() / sig_noise_ratio );
00093   //Int_t ntimesnoise = 100;
00094   
00095   gSystem->Sleep(sleepms);
00096   FillNoiseUniform(his,ntimesnoise);
00097   UpdateDisplay(his);
00098 
00099   gSystem->Sleep(sleepms);
00100   FillNoise2(his,(Int_t)( (Double_t)ntimesnoise * gRandom->Gaus(0.2,.01) ) );
00101 
00102   //  his->Sumw2();
00103 
00104   UpdateDisplay(his);
00105 
00106   return his;
00107 }

Generated on Tue Oct 16 15:40:47 2007 by  doxygen 1.5.2