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