00001 scanBXoff( char* Rrun = "R6104032") {
00002 gStyle->SetPalette(1,0);
00003 gStyle->SetOptStat(11);
00004
00005 char *tit[]={"bX7","bX48"};
00006
00007 int myOff[]={-1, -1};
00008 int myRms[]={-1, -1};
00009 int nd=2;
00010
00011 TString fname="iter1/"; fname+=Rrun; fname+=".hist.root";
00012
00013 TFile *fd=new TFile(fname);
00014
00015 if(!fd->IsOpen()) {
00016 printf("scanBXoff %s not exits \n",fname.Data());
00017 return;
00018 }
00019
00020 fd->ls();
00021
00022
00023
00024
00025 hI =(TH1F*) fd->Get("bXI"); assert(hI);
00026
00027
00028 TH1F *hI2=(TH1F*)hI->Clone();
00029
00030 hI2->Scale(200.);
00031 hI2->SetFillColor(kYellow);
00032 hI2->SetLineColor(kWhite);
00033
00034 TCanvas *c1=new TCanvas(Rrun,Rrun,800,700);
00035 c1->Divide(nd,3);
00036
00037
00038
00039 int k,i;
00040
00041 for(k=0;k<nd;k++) {
00042 TString tt=tit[k];
00043 TH1F *hc =new TH1F("ch-"+tt,"CHi2 dostribution vs. bXing offset for:"+tt,120,-0.5,119.5);
00044
00045 TH1F *hd=(TH1F*) fd->Get(tt);
00046 hd->Print();
00047 if(hd==0) {
00048 printf("%s file exist but %s is missing\n",fname.Data(),tt.Data());
00049 continue;
00050 }
00051 assert(hd);
00052
00053 c1->cd(0*nd+k+1); hd->Draw();
00054 hI2->Draw("same");
00055 hd->Draw("same");
00056 hd->SetFillColor(kBlue);
00057
00058
00059 myRms[k]=hd->GetRMS();
00060 if(myRms[k]<10) continue;
00061
00062 TH1F *hx=(TH1F*)hd->Clone();
00063 setNorm(hx);
00064
00065 int off= scanOff(hx, hI, hc);
00066 myOff[k]=off;
00067 printf("run=%s off=%d\n",Rrun,off);
00068 c1->cd(1*nd+k+1); hc->Draw(); gPad->SetLogy();
00069
00070
00071
00072 TH1F *hg=(TH1F*)hd->Clone();
00073 shift(hd,hg,off);
00074 c1->cd(2*nd+k+1);
00075 hg->Draw();
00076 hI2->Draw("same");
00077 hg->Draw("same");
00078 hg->SetFillColor(kBlue);
00079
00080
00081 }
00082
00083 printf("#scanBXoff %s ",Rrun);
00084
00085 for(k=0;k<nd;k++) {
00086 printf("%s: rms=%f off=%d ",tit[k], myRms[k], myOff[k]);
00087 }
00088
00089 printf("\n");
00090
00091
00092
00093 }
00094
00095
00096
00097
00098
00099 void shift(TH1F *hd,TH1F *hg, int off) {
00100
00101 hg->Reset();
00102 int i;
00103 for(i=0;i<120;i++) {
00104 int j= 1+(i+120-off)%120;
00105 hg->SetBinContent(i+1, hd->GetBinContent(j));
00106
00107 }
00108 }
00109
00110
00111
00112 int scanOff(TH1F *hx, TH1F *hp, TH1F *hc) {
00113 int off;
00114 float min=1e100;
00115 int k=-1;
00116 for(off=0;off<120;off++) {
00117 float chi2= getChi2(hx,hp,off);
00118 hc->Fill(off,chi2);
00119 if(min>chi2) { min=chi2; k=off;}
00120
00121
00122 }
00123 if(min*3.>hc->Integral()/120.) return -2;
00124
00125 return (120-k)%120;
00126 }
00127
00128
00129
00130
00131
00132
00133 float getChi2(TH1F *hx, TH1F *hp, int off) {
00134 int nb=hx->GetNbinsX();
00135 assert(nb>=120);
00136
00137 float * x=hx->GetArray();
00138 float * p=hp->GetArray();
00139
00140 float sum=0;
00141 int i;
00142 for(i=0;i<120;i++) {
00143 int j= 1+(i+off)%120;
00144 float xx= (x[j] -p[i+1])/hx->GetBinError(j);
00145 sum+=xx*xx;
00146 }
00147 return sum;
00148 }
00149
00150
00151
00152
00153 void setNorm(TH1F *h){
00154 int nb=h->GetNbinsX();
00155
00156 float * y=h->GetArray();
00157 float sum=0;
00158 int i;
00159 for(i=1;i<=nb;i++) {
00160 sum+=y[i];
00161 }
00162 float fac=(50*1+10*0.01 )/sum;
00163
00164 for(i=1;i<=120;i++) {
00165 float xx=y[i];
00166 float val=xx*fac;
00167 if(xx==0) xx=1;
00168 float err=fac*sqrt(xx);
00169 h->SetBinContent(i,val);
00170 h->SetBinError(i,err);
00171 }
00172
00173 }
00174
00175
00176
00177
00178
00179 void draw_raw2(TFile *dirL1=0){
00180
00181 assert(dirL1->IsOpen());
00182
00183 TCanvas *c1=new TCanvas("raw1","My raw1",450,500);
00184 c1->Divide(1,2);
00185
00186 char *tit[]={"bX7","bX48"};
00187 int nd=2;
00188 int k,i;
00189
00190 for(k=0;k<nd;k++) {
00191 c1->cd(1+k);
00192
00193 TH1F* h1=(TH1F*) dirL1->Get(tit[k]);
00194 h1->Draw();
00195 h1->SetLineColor(kBlue);
00196
00197 }
00198 }
00199
00200
00201