ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yangyong/combineICEB/combineCalibConstantv2.C
Revision: 1.4
Committed: Fri Aug 10 15:05:02 2012 UTC (12 years, 9 months ago) by yangyong
Content type: text/plain
Branch: MAIN
CVS Tags: V2012combv2b
Changes since 1.3: +9 -12 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 yangyong 1.2 #include "rootheader.h"
2     #include "testSelection_cluster.h"
3    
4     #include "common_functions.cc"
5     #include "foldEndcap.cc"
6    
7     TRandom3 *rgen_;
8    
9     #include "usefullcode.cc"
10    
11     float interCalib_preCalib[170][360];
12     float interCalibEndcap_preCalib[2][101][101];
13    
14     void copyConstant(float c1[170][360], float c2[170][360]){
15    
16     for(int j=0; j<170; j++){
17     for(int k=0; k<360; k++){
18     c2[j][k] = c1[j][k];
19     }
20     }
21    
22     }
23    
24     void isAtEcalBarrelModuleCracks(int ieta, int iphi, bool &etaCracks, bool &phiCracks){
25     int absieta = abs(ieta);
26    
27     etaCracks = false;
28     phiCracks = false;
29    
30     if( absieta == 25 || absieta == 26 || absieta == 45 || absieta == 46 || absieta == 65 || absieta == 66) etaCracks = true;
31     if( iphi %20 == 1 || iphi %20 == 0) phiCracks = true;
32    
33    
34     }
35    
36     float ic[50][170][360]; ///at most 20 set
37    
38     float icv1[50][170][360];
39    
40     int ndeadflagietaiphi_ic[50][170][360];
41    
42     float icwt_period[50][170][360]; //pi0&eta combined for each period
43    
44    
45    
46     bool isTestBeamSM(int iSM){
47    
48    
49    
50     //if( iSM == 1 || iSM == 2 || iSM == 11 || iSM == 15 || iSM == 21 || iSM == 24) return true;
51    
52     if( iSM == 1 || iSM == 11 || iSM == 15 || iSM == 24) return true;
53    
54    
55     else return false;
56    
57     }
58    
59    
60     #include "effSigma.C"
61    
62     #include "getCrystaldeadflag.cc"
63    
64     #include "getCalibConstants.cc"
65    
66     #include "gausfit.cc"
67    
68     float corrGR10_P_V10_over_GR09_R_V6A[170][360];
69     float corrGR_R_311_V1A_over_GR09_R_V6A[170][360];
70     float corrGR_R_311_V1A_over_GR10_P_V10[170][360];
71    
72    
73     float CphiCorrin[170][360];
74     float CBSCorrin[170][360];
75    
76     float fitWind = 3;
77     float sigmaTB = 0.55;
78     //float sigmaTB = 0.44;
79     //float sigmaTB = 0.95;
80    
81     void fitHistogram(TH1F *h1, double res[]){
82    
83     float mean = h1->GetMean();
84     float meanErr = h1->GetMeanError();
85     float rmsEff = 100 * h1->GetRMS();
86     float rmsEffErr = 100 * h1->GetRMSError();
87     float rmsGaus = 100 * h1->GetRMS();
88     float rmsGausErr = 100 * h1->GetRMSError();
89     if( h1->Integral()>50){
90     double resd[10];
91     effSigma(h1,resd);
92     rmsEff = resd[2]*100;
93     float resf[20];
94    
95     fitgauswind2(h1,fitWind,fitWind,resf);
96     //fitgauswindRefit(h1,resf);
97     rmsGaus = resf[6]*100;
98     rmsGausErr = resf[3] * 100;
99     rmsEffErr = resf[3] * 100;
100     mean = resf[0];
101     meanErr = resf[1];
102     }
103     res[0] = mean;
104     res[1] = meanErr;
105     res[2] = rmsEff;
106     res[3] = rmsEffErr;
107     res[4] = rmsGaus;
108     res[5] = rmsGausErr;
109     }
110    
111    
112    
113     float cpizv1[170][360];
114     float cpizv2[170][360];
115     float cpizv1in[170][360];
116     float cpizv2in[170][360];
117    
118     float cetav1[170][360];
119     float cetav2[170][360];
120     float cetav1in[170][360];
121     float cetav2in[170][360];
122    
123    
124     float cpizoutput[170][360];
125     float cetaoutput[170][360];
126     float cpizetaoutput[170][360];
127    
128     float cpizetaphibsoutput[170][360];
129     float cpizetaphibsoutputv1[170][360];
130    
131     float cpizetaphibsprecaliboutput[170][360];
132     float cpizetaphibsprecaliboutputFinal[170][360];
133     float cpizetaphibsprecaliboutputv1[170][360];
134    
135    
136     float cphibsoutput[170][360];
137     float cphibsoutputv1[170][360];
138    
139    
140     float cpizoutputv1[170][360];
141     float cetaoutputv1[170][360];
142     float cpizetaoutputv1[170][360];
143    
144    
145     void rescaleConstTo(float Cin[170][360], float Corr[170][360]){
146    
147     float mean = 0;
148     int nmean = 0;
149     for(int j=0; j< 170; j++){
150     for(int k=0; k< 360; k++){
151     if( Cin[j][k]> 0){
152     Cin[j][k] /= Corr[j][k];
153     nmean ++;
154     mean += Cin[j][k];
155     }
156     }
157     }
158     mean /= nmean;
159     for(int j=0; j< 170; j++){
160     for(int k=0; k< 360; k++){
161     if( Cin[j][k]> 0){
162     Cin[j][k] /= mean;
163     }
164     }
165     }
166     }
167    
168     ///Cprecaib * c1 == c_new * c2
169     ///c2 = c1 / ( c_new / cprecalib)
170    
171     void rescaleConstTov1(float Cin[170][360], float Corr[170][360]){
172    
173     float mean = 0;
174     int nmean = 0;
175     for(int j=0; j< 170; j++){
176     for(int k=0; k< 360; k++){
177     if( Cin[j][k]> 0){
178     Cin[j][k] *= Corr[j][k];
179     nmean ++;
180     mean += Cin[j][k];
181     }
182     }
183     }
184     mean /= nmean;
185     for(int j=0; j< 170; j++){
186     for(int k=0; k< 360; k++){
187     if( Cin[j][k]> 0){
188     Cin[j][k] /= mean;
189     }
190     }
191     }
192     }
193    
194    
195     ///code to combined different pi0/eta EB IC for each laserTag
196    
197    
198     void combineCalibConstantv2(){
199    
200    
201    
202 yangyong 1.3 readInterCalibConstEBSimple("interCalibEB_GR_P_V39.txt",interCalib_preCalib);
203 yangyong 1.2
204    
205    
206 yangyong 1.3 map<int, string> smScaleFiles;
207 yangyong 1.2
208    
209    
210    
211     //SM-scale files Pi0
212    
213 yangyong 1.3 smScaleFiles[0] = "../Pi0Calibration/Barrel/calibres/deriveCalibConst.dflag2.pe1.step1.iter1.root";
214     smScaleFiles[1] = "../Pi0Calibration/Barrel/calibres/deriveCalibConst.dflag3.pe2.step1.iter1.root";
215    
216     // number of IC periods
217     int nIC = 1;
218 yangyong 1.2
219    
220    
221     map<int,string> icFiles;
222    
223     //IC Pi0
224 yangyong 1.3 icFiles[0] = "../Pi0Calibration/Barrel/calibres/deriveCalibConst.dflag2.pe1.step4.iter30.txt";
225 yangyong 1.2 //IC Eta
226 yangyong 1.3 icFiles[1] = "../Pi0Calibration/Barrel/calibres/deriveCalibConst.dflag3.pe2.step4.iter30.txt";
227 yangyong 1.2
228     //crystal dead flag
229 yangyong 1.3 getCrystaldeadflagBarrel_v1("../Pi0Calibration/Barrel/crystal_deadflag_eb_dflag2.txt",ndeadflagietaiphi_ic[0]);
230     getCrystaldeadflagBarrel_v1("../Pi0Calibration/Barrel/crystal_deadflag_eb_dflag3.txt",ndeadflagietaiphi_ic[1]);
231    
232 yangyong 1.4
233    
234     vector<string> inputfileStat;
235     inputfileStat.push_back("../Pi0Calibration/Barrel/calibres/deriveCalibConst.dflag2.pe1.step3.iter11.root");
236     inputfileStat.push_back("../Pi0Calibration/Barrel/calibres/deriveCalibConst.dflag3.pe2.step3.iter11.root");
237    
238    
239    
240 yangyong 1.2 ofstream txtoutTocheck("combinedCalibConstantv2.txt");
241    
242    
243     int nConstantSet = int(icFiles.size());
244    
245     cout<<" nConstantSet " << nConstantSet <<endl;
246    
247    
248     map<int,TH1F*> hh_smscales;
249     float cc[170][360];
250    
251    
252     int nMaxICSet = 50;
253     if( nConstantSet > nMaxICSet){
254     cout<<"more than " << nMaxICSet<<" "<< nConstantSet <<endl;
255     return;
256     }
257    
258    
259    
260     float icwt[170][360];
261     float icwtv1[170][360];
262    
263    
264    
265     // float icwt1[170][360];
266     // float icwt2[170][360];
267    
268    
269 yangyong 1.3
270 yangyong 1.2 for(int n=0; n< nConstantSet; n++){
271     string filename = smScaleFiles[n];
272     TFile *ff = new TFile(filename.c_str(),"read");
273 yangyong 1.3 TH1F *hhtmp = (TH1F*)ff->Get("hh_corr_sm");
274 yangyong 1.2 hh_smscales[n] = hhtmp;
275    
276     filename = icFiles[n];
277    
278     readInterCalibConstEBSimplev1(filename.c_str(),cc);
279    
280     for(int j=0; j< 170; j++){
281     for(int k=0; k< 360; k++){
282     ic[n][j][k] = cc[j][k];
283     }
284     }
285    
286    
287     NormIetaAbsToUnitTestBeamSMOnly(ic[n]);
288    
289     if(n==0) cout<<"check ic[0][1] 1 " << ic[n][0][1]<<endl;
290    
291     SetSMScale(ic[n], hh_smscales[n]);
292    
293     if(n==0) cout<<"check ic[0][1] 2 " << ic[n][0][1]<<endl;
294    
295     NormCrystalDeadFlag_v1(ic[n],ndeadflagietaiphi_ic[n]);
296    
297     if(n==0) cout<<"check ic[0][1] 3 " << ic[n][0][1]<<endl;
298    
299    
300     copyConstant(ic[n],icv1[n]);
301    
302     /////for estimating precision
303     NormSMScaleToUnit(icv1[n]);
304     }
305    
306     //return;
307    
308    
309     TFile *fnew = new TFile("combineCalibConstantv2.root","recreate");
310    
311    
312     TH1F *hh_csm[51][36][3]; //for each SM, all/centra/outer
313    
314    
315     //|ieta|<=45 , all, removing eta phi boundaries, at eta moduaries, at phi boduaris
316     TH1F *hh_csmtb_ietaMod12[50][5];
317     for(int n=0; n< nConstantSet; n++){
318     for(int k=0; k<5; k++){
319     TString histname = TString(Form("hh_csmtb_ietaMod12_%d_%d",n,k));
320     hh_csmtb_ietaMod12[n][k] = new TH1F(histname,histname,500,0,2);
321     }
322     }
323    
324 yangyong 1.3
325 yangyong 1.2
326     TH1F *hh_c_ieta[50][170];
327     TH1F *hh_c_ietaAbs[50][85];
328    
329    
330    
331    
332     TH1F *hh_csmtb_ietaTT[50][34];
333     TH1F *hh_csmtb_ietaTTAbs[50][17];
334     TH1F *hh_csmco_ietaTTAbs[50][17];
335 yangyong 1.3
336 yangyong 1.2 TH1F *hh_csmco_ietaTT[50][34];
337    
338     TH1F *hh_csmall_ietaTTAbs[50][17];
339    
340    
341    
342     TH1F *hh_wtavg_csmtb_ietaTT[34];
343     TH1F *hh_wtavg_csmtb_ietaTTAbs[17];
344     TH1F *hh_wtavg_csmco_ietaTT[34];
345     TH1F *hh_wtavg_csmco_ietaTTAbs[17];
346    
347     TH1F *hh_wtavg_csmall_ietaTTAbs[17];
348    
349    
350     TH1F *hh_res_csmtbietaTT[50][3];
351     TH1F *hh_res_csmtbietaTTAbs[50][3];
352    
353     TH1F *hh_res_csmcoietaTT[50][3];
354     TH1F *hh_res_csmcoietaTTAbs[50][3];
355    
356     TH1F *hh_res_csmallietaTTAbs[50][3];
357    
358    
359    
360     TH1F *hh_res_wtavg_csmtbietaTT[3]; //mean/rmsEff/rmsGaus
361    
362     TH1F *hh_res_wtavg_csmtbietaTTAbs[3]; //mean/rmsEff/rmsGaus
363    
364     TH1F *hh_res_wtavg_csmallietaTTAbs[3]; //mean/rmsEff/rmsGaus
365    
366    
367     TH1F *hh_res_wtavg_csmcoietaTT[3]; //mean/rmsEff/rmsGaus
368     TH1F *hh_res_wtavg_csmcoietaTTAbs[3]; //mean/rmsEff/rmsGaus
369    
370     TH1F *hh_diff_csmtb_ietaTTAbs[50][50][17];
371     TH1F *hh_diff_csmtb_ietaTT[50][50][34];
372     TH1F *hh_diff_csmco_ietaTTAbs[50][50][17];
373     TH1F *hh_diff_csmco_ietaTT[50][50][34];
374     TH1F *hh_diff_csmall_ietaTT[50][50][34];
375     TH1F *hh_diff_csmall_ietaTTAbs[50][50][17];
376    
377     TH1F *hh_diff_csm[50][50][36][3]; //by each SM, all/central/outer
378    
379    
380     TH1F *hh_res_csm[51][3][3];
381    
382     TH2F *hh2_c[51];
383     TH2F *hh2_c_period[51];
384    
385    
386     float xbinLow[35];
387     for(int j=0; j< 35; j++){
388     float xbin = -85 + 5*j;
389     xbinLow[j] = xbin;
390     }
391     float xbinLow1[35];
392     for(int j=0; j<= 17 ; j++){
393     float xbin = 5 * j;
394     xbinLow1[j] = xbin+1;
395     }
396    
397    
398     TH1F *hh_c_ietaAbs_period[50][85];
399     TH1F *hh_c_ietaTTAbs_period[50][17];
400     TH1F *hh_c_smtbietaTTAbs_period[50][17];
401    
402    
403     for(int n=0; n< nIC+1; n++){
404     for(int k=0; k< 85; k++){
405     TString histname = TString(Form("hh_c_ietaAbs_period_%d_%d",n,k));
406     hh_c_ietaAbs_period[n][k] = new TH1F(histname,histname,500,0,2);
407     }
408     for(int k=0; k< 17; k++){
409     TString histname = TString(Form("hh_c_ietaTTAbs_period_%d_%d",n,k));
410     hh_c_ietaTTAbs_period[n][k] = new TH1F(histname,histname,500,0,2);
411     histname = TString(Form("hh_c_smtbietaTTAbs_period_%d_%d",n,k));
412     hh_c_smtbietaTTAbs_period[n][k] = new TH1F(histname,histname,500,0,2);
413     }
414     }
415    
416    
417    
418     TH1F *hh_res_cietaAbs_period[50][3];
419     TH1F *hh_res_cietaTTAbs_period[50][3];
420     TH1F *hh_res_csmtbietaTTAbs_period[50][3];
421     for(int n=0; n< nIC+1; n++){
422     for(int k=0;k<3; k++){
423     TString histname = TString(Form("hh_res_cietaAbs_period_%d_%d",n,k));
424     hh_res_cietaAbs_period[n][k] = new TH1F(histname,histname,85,1,86);
425     histname = TString(Form("hh_res_cietaTTAbs_period_%d_%d",n,k));
426     hh_res_cietaTTAbs_period[n][k] = new TH1F(histname,histname,17,xbinLow1);
427     histname = TString(Form("hh_res_csmtbietaTTAbs_period_%d_%d",n,k));
428     hh_res_csmtbietaTTAbs_period[n][k] = new TH1F(histname,histname,17,xbinLow1);
429     }
430     }
431    
432    
433    
434     for(int n=0; n< nIC; n++){
435     TString histname = TString(Form("hh2_c_period_%d",n));
436     hh2_c_period[n] = new TH2F(histname,histname,171,-85,86,360,1,361);
437    
438     }
439    
440    
441     for(int n=0; n< nConstantSet+1; n++){
442     TString histname = TString(Form("hh2_c_%d",n));
443     hh2_c[n] = new TH2F(histname,histname,171,-85,86,360,1,361);
444    
445     }
446    
447     for(int n=0; n< nConstantSet+1; n++){
448     for(int j=0;j<36;j++){
449     for(int k=0;k<3; k++){
450     TString histname = TString(Form("hh_csm_%d_%d_%d",n,j,k));
451     hh_csm[n][j][k] = new TH1F(histname,histname,500,0,2);
452     }
453     }
454    
455     for(int j=0;j<3; j++){
456     for(int k=0;k<3; k++){
457     TString histname = TString(Form("hh_res_csm_%d_%d_%d",n,j,k));
458     hh_res_csm[n][j][k] = new TH1F(histname,histname,36,1,37);
459     }
460     }
461     }
462    
463    
464    
465     for(int n=0; n< nConstantSet; n++){
466    
467    
468     for(int j=0; j<17; j++){
469     TString histname = TString(Form("hh_csmtb_ietaTTAbs_%d_%d",n,j));
470     hh_csmtb_ietaTTAbs[n][j] = new TH1F(histname,histname,500,0,2);
471     histname = TString(Form("hh_csmco_ietaTTAbs_%d_%d",n,j));
472     hh_csmco_ietaTTAbs[n][j] = new TH1F(histname,histname,500,0,2);
473    
474     histname = TString(Form("hh_csmall_ietaTTAbs_%d_%d",n,j));
475     hh_csmall_ietaTTAbs[n][j] = new TH1F(histname,histname,500,0,2);
476    
477     }
478    
479    
480     for(int j=0; j<34; j++){
481     TString histname = TString(Form("hh_csmtb_ietaTT_%d_%d",n,j));
482     hh_csmtb_ietaTT[n][j] = new TH1F(histname,histname,500,0,2);
483     histname = TString(Form("hh_csmco_ietaTT_%d_%d",n,j));
484     hh_csmco_ietaTT[n][j] = new TH1F(histname,histname,500,0,2);
485     }
486     }
487    
488     for(int j=0; j<34; j++){
489     TString histname = TString(Form("hh_wtavg_csmtb_ietaTT_%d",j));
490     hh_wtavg_csmtb_ietaTT[j] = new TH1F(histname,histname,500,0,2);
491     histname = TString(Form("hh_wtavg_csmco_ietaTT_%d",j));
492     hh_wtavg_csmco_ietaTT[j] = new TH1F(histname,histname,500,0,2);
493     }
494     for(int j=0; j<17; j++){
495     TString histname = TString(Form("hh_wtavg_csmtb_ietaTTAbs_%d",j));
496     hh_wtavg_csmtb_ietaTTAbs[j] = new TH1F(histname,histname,500,0,2);
497     histname = TString(Form("hh_wtavg_csmco_ietaTTAbs_%d",j));
498     hh_wtavg_csmco_ietaTTAbs[j] = new TH1F(histname,histname,500,0,2);
499    
500     histname = TString(Form("hh_wtavg_csmall_ietaTTAbs_%d",j));
501     hh_wtavg_csmall_ietaTTAbs[j] = new TH1F(histname,histname,500,0,2);
502    
503     }
504    
505    
506     TH1F *hh_c_deadflag[50][20];
507    
508     TH1F *hh_c_deadflag_period[50][20];
509     TH1F *hh_cc_deadflag_period[50][20];
510     TH1F *hh_cc1_deadflag_period[50][20];
511    
512     for(int j=0; j< nConstantSet; j++){
513     for(int k=0; k<20; k++){
514     TString histname = TString(Form("hh_c_deadflag_%d_%d",j,k));
515     hh_c_deadflag[j][k] = new TH1F(histname,histname,500,0,2);
516    
517     histname = TString(Form("hh_c_deadflag_period_%d_%d",j,k));
518     hh_c_deadflag_period[j][k] = new TH1F(histname,histname,500,0,2);
519    
520     histname = TString(Form("hh_cc_deadflag_period_%d_%d",j,k));
521     hh_cc_deadflag_period[j][k] = new TH1F(histname,histname,500,0,2);
522     histname = TString(Form("hh_cc1_deadflag_period_%d_%d",j,k));
523     hh_cc1_deadflag_period[j][k] = new TH1F(histname,histname,500,0,2);
524    
525     }
526     }
527    
528    
529     for(int j=0; j< nConstantSet; j++){
530     for(int k=0; k<3; k++){
531    
532     TString histname = TString(Form("hh_res_csmtbietaTT_%d_%d",j,k));
533     hh_res_csmtbietaTT[j][k] = new TH1F(histname,histname,34,xbinLow);
534     histname = TString(Form("hh_res_csmtbietaTTAbs_%d_%d",j,k));
535     hh_res_csmtbietaTTAbs[j][k] = new TH1F(histname,histname,17,xbinLow1);
536 yangyong 1.3
537    
538     histname = TString(Form("hh_res_csmallietaTTAbs_%d_%d",j,k));
539     hh_res_csmallietaTTAbs[j][k] = new TH1F(histname,histname,17,xbinLow1);
540    
541 yangyong 1.2
542     histname = TString(Form("hh_res_csmcoietaTT_%d_%d",j,k));
543     hh_res_csmcoietaTT[j][k] = new TH1F(histname,histname,34,xbinLow);
544     histname = TString(Form("hh_res_csmcoietaTTAbs_%d_%d",j,k));
545     hh_res_csmcoietaTTAbs[j][k] = new TH1F(histname,histname,17,xbinLow1);
546    
547     }
548     }
549    
550     for(int k=0; k<3; k++){
551     TString histname = TString(Form("hh_res_wtavg_csmtbietaTT_%d",k));
552     hh_res_wtavg_csmtbietaTT[k] = new TH1F(histname,histname,34,xbinLow);
553     histname = TString(Form("hh_res_wtavg_csmtbietaTTAbs_%d",k));
554     hh_res_wtavg_csmtbietaTTAbs[k] = new TH1F(histname,histname,17,xbinLow1);
555    
556     histname = TString(Form("hh_res_wtavg_csmallietaTTAbs_%d",k));
557     hh_res_wtavg_csmallietaTTAbs[k] = new TH1F(histname,histname,17,xbinLow1);
558    
559     histname = TString(Form("hh_res_wtavg_csmcoietaTT_%d",k));
560     hh_res_wtavg_csmcoietaTT[k] = new TH1F(histname,histname,34,xbinLow);
561     histname = TString(Form("hh_res_wtavg_csmcoietaTTAbs_%d",k));
562     hh_res_wtavg_csmcoietaTTAbs[k] = new TH1F(histname,histname,17,xbinLow1);
563     }
564     TH2F *hh2_diff[50][50];
565    
566    
567     TH1F *hh_res_diff_csmtbietaTTAbs[50][50][3];
568     TH1F *hh_res_diff_csmcoietaTTAbs[50][50][3];
569     TH1F *hh_res_diff_csmtbietaTT[50][50][3];
570     TH1F *hh_res_diff_csmcoietaTT[50][50][3];
571     TH1F *hh_res_diff_csmallietaTT[50][50][3];
572     TH1F *hh_res_diff_csmallietaTTAbs[50][50][3];
573    
574 yangyong 1.3
575 yangyong 1.2 for(int n=0; n< nConstantSet; n++){
576     for(int k=n+1; k< nConstantSet; k++){
577     TString histname = TString (Form("hh2_diff_%dand%d",n,k));
578     hh2_diff[n][k] = new TH2F(histname,histname,171,-85,86,360,1,361);
579    
580     for(int j=0; j<36; j++){
581     for(int m=0; m<3; m++){
582     histname = TString ( Form("hh_diff_csm_%dand%d_%d_%d",n,k,j,m));
583     hh_diff_csm[n][k][j][m] = new TH1F(histname,histname,50,-0.1,0.1);
584     }
585     }
586    
587     for(int j=0; j<17; j++){
588     histname = TString (Form("hh_diff_csmtb_ietaTTAbs_%dand%d_%d",n,k,j));
589     hh_diff_csmtb_ietaTTAbs[n][k][j] = new TH1F(histname,histname,500,-0.1,0.1);
590     histname = TString (Form("hh_diff_csmco_ietaTTAbs_%dand%d_%d",n,k,j));
591     hh_diff_csmco_ietaTTAbs[n][k][j] = new TH1F(histname,histname,500,-0.1,0.1);
592     histname = TString (Form("hh_diff_csmall_ietaTTAbs_%dand%d_%d",n,k,j));
593     hh_diff_csmall_ietaTTAbs[n][k][j] = new TH1F(histname,histname,500,-0.1,0.1);
594    
595     }
596     for(int j=0; j<34; j++){
597     histname = TString (Form("hh_diff_csmtb_ietaTT_%dand%d_%d",n,k,j));
598     hh_diff_csmtb_ietaTT[n][k][j] = new TH1F(histname,histname,500,-0.1,0.1);
599     histname = TString (Form("hh_diff_csmco_ietaTT_%dand%d_%d",n,k,j));
600     hh_diff_csmco_ietaTT[n][k][j] = new TH1F(histname,histname,500,-0.1,0.1);
601    
602     histname = TString (Form("hh_diff_csmall_ietaTT_%dand%d_%d",n,k,j));
603     hh_diff_csmall_ietaTT[n][k][j] = new TH1F(histname,histname,500,-0.1,0.1);
604    
605     }
606     for(int j=0;j<3; j++){
607     histname = TString(Form("hh_res_diff_csmtbietaTTAbs_%dand%d_%d",n,k,j));
608     hh_res_diff_csmtbietaTTAbs[n][k][j] = new TH1F(histname,histname,17,xbinLow1);
609     histname = TString(Form("hh_res_diff_csmcoietaTTAbs_%dand%d_%d",n,k,j));
610     hh_res_diff_csmcoietaTTAbs[n][k][j] = new TH1F(histname,histname,17,xbinLow1);
611     histname = TString(Form("hh_res_diff_csmtbietaTT_%dand%d_%d",n,k,j));
612     hh_res_diff_csmtbietaTT[n][k][j] = new TH1F(histname,histname,34,xbinLow);
613     histname = TString(Form("hh_res_diff_csmcoietaTT_%dand%d_%d",n,k,j));
614     hh_res_diff_csmcoietaTT[n][k][j] = new TH1F(histname,histname,34,xbinLow);
615    
616     histname = TString(Form("hh_res_diff_csmallietaTT_%dand%d_%d",n,k,j));
617     hh_res_diff_csmallietaTT[n][k][j] = new TH1F(histname,histname,34,xbinLow);
618     histname = TString(Form("hh_res_diff_csmallietaTTAbs_%dand%d_%d",n,k,j));
619     hh_res_diff_csmallietaTTAbs[n][k][j] = new TH1F(histname,histname,17,xbinLow1);
620     }
621    
622     }
623     }
624 yangyong 1.3
625 yangyong 1.2
626     TH2F *hh2_largeICdiff[50][50];
627     for(int n=0; n< nIC; n++){
628     for(int k=n+1; k< nIC; k++){
629     TString histname = TString (Form("hh2_largeICdiff_%dand%d",n,k));
630     hh2_largeICdiff[n][k] =new TH2F(histname,histname,171,-85,86,360,1,361);
631     }
632     }
633     TH2F *hh2_largeICdiff_all =new TH2F("hh2_largeICdiff_all","hh2_largeICdiff_all",171,-85,86,360,1,361);
634 yangyong 1.4
635 yangyong 1.2
636    
637 yangyong 1.3 TH1F *hh_res_ieta[50][4];////[4] means the stat error.
638 yangyong 1.2
639     for(int n=0; n< int(inputfileStat.size()); n++){
640     for(int k=0;k<5;k++){
641 yangyong 1.3 string filename = string(Form("hh_res_ieta_%d_%d",n,k));
642     hh_res_ieta[n][k] =new TH1F(filename.c_str(),filename.c_str(),171,-85,86);
643 yangyong 1.2 }
644     }
645 yangyong 1.3 TH1F *hh_statErr_ietaAbs[50];
646     for(int n=0; n< int(inputfileStat.size()); n++){
647     string filename = string(Form("hh_statErr_ietaAbs_%d",n));
648     hh_statErr_ietaAbs[n] =new TH1F(filename.c_str(),filename.c_str(),85,1,86);
649     }
650    
651 yangyong 1.2
652 yangyong 1.3 TH1F *hh_statErr_ietaAbs_period[50]; //pi0&eta combined for each period
653 yangyong 1.2 for(int j=0; j< nIC; j++){
654 yangyong 1.3 string filename = string(Form("hh_statErr_ietaAbs_period_%d",j));
655     hh_statErr_ietaAbs_period[j]=new TH1F(filename.c_str(),filename.c_str(),85,1,86);
656 yangyong 1.2 }
657    
658    
659     ///using MC-based forumula + 0.5/1 % sys.
660     // 7.4/resolution * 17 /sqrt(N) * sqrt( 1+ 1.8/sob) + 0.5/1%
661    
662    
663     for(int j=0; j< int(inputfileStat.size());j++){
664     string filename = inputfileStat[j];
665     TFile *f1 = new TFile(filename.c_str(),"read");
666     for(int k=0;k<4;k++){
667 yangyong 1.3 string histname = string (Form("hh_res_ieta_%d",k));
668 yangyong 1.2 TH1F *hhtmp = (TH1F*)f1->Get(histname.c_str());
669 yangyong 1.3 if(hhtmp==0){
670     cout<<"empty hh_res_ieta_ ! "<<endl;
671     return;
672     }
673    
674     hh_res_ieta[j][k]->Add(hhtmp);
675 yangyong 1.2 }
676     }
677    
678 yangyong 1.3
679 yangyong 1.2 for(int j=0; j< int(inputfileStat.size());j++){
680    
681 yangyong 1.3 for(int n=1; n<=85 ; n++){
682     float npiz = 0.5*(hh_res_ieta[j][1]->GetBinContent(87+n-1) + hh_res_ieta[j][1]->GetBinContent(85-(n-1)) ) /(360*2) ;
683     float sob = 0.5*(hh_res_ieta[j][3]->GetBinContent(87+n-1) + hh_res_ieta[j][3]->GetBinContent(85-(n-1)) );
684     float reso = 0.5*(hh_res_ieta[j][2]->GetBinContent(87+n-1) + hh_res_ieta[j][2]->GetBinContent(85-(n-1)) )/hh_res_ieta[j][0]->GetBinContent(87+n-1);
685    
686     if(npiz<=0){
687     cout<<"empty histogram!!" <<endl;
688     return;
689     }
690    
691 yangyong 1.2 float statErr = reso/7.4 * 17/sqrt(npiz) * sqrt( 1+ 1.8/sob);
692 yangyong 1.3 hh_statErr_ietaAbs[j]->SetBinContent(n,statErr);
693 yangyong 1.2 }
694     }
695    
696 yangyong 1.3
697 yangyong 1.2 ///the combined IC from all ICs
698     ofstream txtout("interCalibConstants.combinedPi0EtaAllPeriod.EcalBarrel.txt",ios::out);
699    
700    
701     ofstream txtout_period[50]; //pi0eta combined for each period
702     for(int j=0; j< nIC; j++){
703     string filename = string(Form("interCalibConstants.combinedPi0EtaPeriod%d.EcalBarrel.txt",j));
704     txtout_period[j].open(filename.c_str(),ios::out);
705     }
706    
707     ofstream txtout_periodv1[50]; //pi0eta combined for each period / averaged all
708     for(int j=0; j< nIC; j++){
709     string filename = string(Form("interCalibConstants.combinedPi0EtaPeriod%d.EcalBarrel.txtv1",j));
710     txtout_periodv1[j].open(filename.c_str(),ios::out);
711     }
712    
713    
714     cout<<"fill" <<endl;
715    
716 yangyong 1.3
717 yangyong 1.2
718    
719     for(int n=0; n< nConstantSet; n++){
720    
721     for(int j=0; j<170; j++){
722    
723     int ieta = j-85;
724     if( ieta >=0) ieta += 1;
725    
726     for(int k=0; k<360; k++){
727     int iphi = k;
728     if( k==0) iphi = 360;
729     int ietaTTAbs = (abs(ieta)-1)/5;
730     int iSM = (iphi-1)/20+1;
731     if( ieta<0) iSM += 18;
732     int smTB = isTestBeamSM(iSM);
733     int beta = ieta+85+1;
734     int bphi = iphi;
735     float c = icv1[n][j][k]; ///sm normalized to unit
736     if( c>0){
737    
738     int ndflag = -1;
739     ndflag = ndeadflagietaiphi_ic[n][j][k];
740    
741     if( ndflag <0){
742     cout<<"wrong dflag? " << ndflag <<" "<<n<<" "<<j<<" "<<k<<endl;
743     return;
744     }
745     hh_c_deadflag[n][ndflag]->Fill(c);
746    
747    
748     hh2_c[n]->SetBinContent(beta,bphi,ic[n][j][k]);
749    
750     int ietaflag[3] = {abs(ieta)<25,abs(ieta)>=25 && abs(ieta)<60,abs(ieta)>=60};
751     for(int jj=0;jj<3; jj++){
752     if( ietaflag[jj]==0) continue;
753     hh_csm[n][iSM-1][jj]->Fill(c);
754     }
755    
756     if( smTB){
757     hh_csmtb_ietaTTAbs[n][ietaTTAbs]->Fill(c);
758     hh_csmtb_ietaTT[n][j/5]->Fill(c);
759    
760     bool etaCracks;
761     bool phiCracks;
762     isAtEcalBarrelModuleCracks(ieta,iphi,etaCracks,phiCracks);
763     int crackFlag[5] = {1,!etaCracks && !phiCracks, etaCracks, phiCracks, etaCracks || phiCracks};
764     for(int t =0; t<5; t++){
765     if(crackFlag[t]==0) continue;
766     hh_csmtb_ietaMod12[n][t]->Fill(c);
767     }
768    
769     }else{
770     hh_csmco_ietaTTAbs[n][ietaTTAbs]->Fill(c);
771     hh_csmco_ietaTT[n][j/5]->Fill(c);
772     }
773 yangyong 1.3
774     hh_csmall_ietaTTAbs[n][ietaTTAbs]->Fill(c);
775    
776 yangyong 1.2 }else{
777     hh2_c[n]->SetBinContent(beta,bphi,-1);
778     }
779    
780     }
781    
782     }
783     }
784    
785    
786     for(int j=0; j<170; j++){
787    
788     int ieta = j-85;
789     if( ieta >=0) ieta += 1;
790    
791     for(int k=0; k<360; k++){
792     int iphi = k;
793     if( k==0) iphi = 360;
794     int ietaTTAbs = (abs(ieta)-1)/5;
795     int iSM = (iphi-1)/20+1;
796     if( ieta<0) iSM += 18;
797     int smTB = isTestBeamSM(iSM);
798     int beta = ieta+85+1;
799     int bphi = iphi;
800    
801     for(int n1 =0; n1 < nConstantSet; n1++){
802     for(int n2 =n1+1; n2 < nConstantSet; n2++){
803     if(ic[n1][j][k] >0 && ic[n2][j][k] > 0){
804    
805     float diff = ic[n1][j][k] - ic[n2][j][k];
806     float average = 0.5*(ic[n1][j][k] + ic[n2][j][k]);
807     diff /= average;
808    
809     hh2_diff[n1][n2] ->SetBinContent(beta,bphi, diff);
810    
811     if(smTB){
812     hh_diff_csmtb_ietaTTAbs[n1][n2][ietaTTAbs]->Fill(diff);
813     hh_diff_csmtb_ietaTT[n1][n2][j/5]->Fill(diff);
814     }else{
815     hh_diff_csmco_ietaTTAbs[n1][n2][ietaTTAbs]->Fill(diff);
816     hh_diff_csmco_ietaTT[n1][n2][j/5]->Fill(diff);
817     }
818     hh_diff_csmall_ietaTT[n1][n2][j/5]->Fill(diff);
819     hh_diff_csmall_ietaTTAbs[n1][n2][ietaTTAbs]->Fill(diff);
820    
821     }else{
822     hh2_diff[n1][n2] ->SetBinContent(beta,bphi,-1);
823     }
824     }
825     }
826     }
827     }
828    
829    
830    
831     cout<<"fitting "<<endl;
832     double resfit[10];
833    
834    
835    
836     for(int n1 =0; n1 < nConstantSet; n1++){
837     for(int n2 =n1+1; n2 < nConstantSet; n2++){
838    
839     for(int j=0;j<34; j++){
840     fitHistogram(hh_diff_csmtb_ietaTT[n1][n2][j],resfit);
841     for(int n=0;n<3; n++){
842     hh_res_diff_csmtbietaTT[n1][n2][n]->SetBinContent(j+1,resfit[2*n]);
843     hh_res_diff_csmtbietaTT[n1][n2][n]->SetBinError(j+1,resfit[2*n+1]);
844     }
845     fitHistogram(hh_diff_csmco_ietaTT[n1][n2][j],resfit);
846     for(int n=0;n<3; n++){
847     hh_res_diff_csmcoietaTT[n1][n2][n]->SetBinContent(j+1,resfit[2*n]);
848     hh_res_diff_csmcoietaTT[n1][n2][n]->SetBinError(j+1,resfit[2*n+1]);
849     }
850     fitHistogram(hh_diff_csmall_ietaTT[n1][n2][j],resfit);
851     for(int n=0;n<3; n++){
852     hh_res_diff_csmallietaTT[n1][n2][n]->SetBinContent(j+1,resfit[2*n]);
853     hh_res_diff_csmallietaTT[n1][n2][n]->SetBinError(j+1,resfit[2*n+1]);
854     }
855    
856     }
857     for(int j=0;j<17; j++){
858     fitHistogram(hh_diff_csmtb_ietaTTAbs[n1][n2][j],resfit);
859     for(int n=0;n<3; n++){
860     hh_res_diff_csmtbietaTTAbs[n1][n2][n]->SetBinContent(j+1,resfit[2*n]);
861     hh_res_diff_csmtbietaTTAbs[n1][n2][n]->SetBinError(j+1,resfit[2*n+1]);
862     }
863     fitHistogram(hh_diff_csmco_ietaTTAbs[n1][n2][j],resfit);
864     for(int n=0;n<3; n++){
865     hh_res_diff_csmcoietaTTAbs[n1][n2][n]->SetBinContent(j+1,resfit[2*n]);
866     hh_res_diff_csmcoietaTTAbs[n1][n2][n]->SetBinError(j+1,resfit[2*n+1]);
867     }
868     fitHistogram(hh_diff_csmall_ietaTTAbs[n1][n2][j],resfit);
869     for(int n=0;n<3; n++){
870     hh_res_diff_csmallietaTTAbs[n1][n2][n]->SetBinContent(j+1,resfit[2*n]);
871     hh_res_diff_csmallietaTTAbs[n1][n2][n]->SetBinError(j+1,resfit[2*n+1]);
872     }
873    
874     }
875    
876     }
877     }
878    
879    
880     for(int k=0;k<nConstantSet; k++){
881    
882    
883     for(int j=0;j<34; j++){
884     fitHistogram(hh_csmtb_ietaTT[k][j],resfit);
885     for(int n=0;n<3; n++){
886     hh_res_csmtbietaTT[k][n]->SetBinContent(j+1,resfit[2*n]);
887     hh_res_csmtbietaTT[k][n]->SetBinError(j+1,resfit[2*n+1]);
888     }
889     }
890     for(int j=0;j<17; j++){
891     fitHistogram(hh_csmtb_ietaTTAbs[k][j],resfit);
892     for(int n=0;n<3; n++){
893     hh_res_csmtbietaTTAbs[k][n]->SetBinContent(j+1,resfit[2*n]);
894     hh_res_csmtbietaTTAbs[k][n]->SetBinError(j+1,resfit[2*n+1]);
895     }
896     }
897    
898 yangyong 1.3 for(int j=0;j<17; j++){
899     fitHistogram(hh_csmall_ietaTTAbs[k][j],resfit);
900     for(int n=0;n<3; n++){
901     hh_res_csmallietaTTAbs[k][n]->SetBinContent(j+1,resfit[2*n]);
902     hh_res_csmallietaTTAbs[k][n]->SetBinError(j+1,resfit[2*n+1]);
903     }
904     }
905    
906    
907    
908 yangyong 1.2 for(int j=0;j<34; j++){
909     fitHistogram(hh_csmco_ietaTT[k][j],resfit);
910     for(int n=0;n<3; n++){
911     hh_res_csmcoietaTT[k][n]->SetBinContent(j+1,resfit[2*n]);
912     hh_res_csmcoietaTT[k][n]->SetBinError(j+1,resfit[2*n+1]);
913     }
914     }
915     for(int j=0;j<17; j++){
916     fitHistogram(hh_csmco_ietaTTAbs[k][j],resfit);
917     for(int n=0;n<3; n++){
918     hh_res_csmcoietaTTAbs[k][n]->SetBinContent(j+1,resfit[2*n]);
919     hh_res_csmcoietaTTAbs[k][n]->SetBinError(j+1,resfit[2*n+1]);
920     }
921     }
922    
923     }
924    
925     cout<<"combining " <<endl;
926    
927    
928     float wtSumC_period[50] = {0};
929     float wtSumS_period[50] = {0};
930    
931    
932    
933     for(int j=0; j<170; j++){
934     int ieta = j-85;
935     if( ieta >=0) ieta += 1;
936     for(int k=0; k<360; k++){
937     int iphi = k;
938     if( k==0) iphi = 360;
939     int ietaTTAbs = (abs(ieta)-1)/5;
940     int iSM = (iphi-1)/20+1;
941     if( ieta<0) iSM += 18;
942     int smTB = isTestBeamSM(iSM);
943     int beta = ieta+85+1;
944     int bphi = iphi;
945    
946    
947     float wtSumC = 0;
948     float wtSumS = 0;
949    
950     for(int n=0; n< nIC; n++){ ///for each period
951     wtSumC_period[n] = 0;
952     wtSumS_period[n] = 0;
953     }
954    
955    
956     for(int n=0; n< nConstantSet; n++){
957     float c = ic[n][j][k];
958    
959    
960    
961    
962     //float sigma = hh_res_csmtbietaTTAbs[n][2]->GetBinContent(ietaTTAbs+1);
963     //if( sigma > sigmaTB){
964     //sigma = sqrt( sigma * sigma - sigmaTB * sigmaTB);
965     //}
966     float statErr = 0;
967     float sysErr = 0.5;
968     if(abs(ieta)>=60) sysErr = 1;
969    
970     // if(n>=0 && n<=1){
971     // statErr = hh_res_diff_csmallietaTTAbs[0][1][2]->GetBinContent(ietaTTAbs+1)/sqrt(2);
972     // }else if( n>=2 && n<=3){
973     // statErr = hh_res_diff_csmallietaTTAbs[2][3][2]->GetBinContent(ietaTTAbs+1)/sqrt(2);
974     // }
975    
976    
977     //now use MC-predicted precision
978 yangyong 1.3 statErr = hh_res_ieta[n][4]->GetBinContent(87+abs(ieta)-1);
979 yangyong 1.2
980     float sigma = sqrt( statErr * statErr + sysErr * sysErr);
981    
982     //if( ieta==1) cout<<"sigma " << ieta<<" n"<< n<<" "<< sigma <<" "<< statErr <<" "<< sysErr <<endl;
983    
984     if( c > 0){
985    
986     float tmp1 = c/ ( sigma * sigma);
987     float tmp2 = 1/(sigma * sigma);
988     int nperiod = n% nIC;
989    
990     wtSumC_period[nperiod] += tmp1;
991     wtSumS_period[nperiod] += tmp2;
992    
993     if( c> 0){ //all combined
994     wtSumC += tmp1;
995     wtSumS += tmp2;
996     }
997     }
998     }
999    
1000    
1001     if( wtSumC > 0){ //combined all
1002     icwt[j][k] = wtSumC / wtSumS;
1003     hh2_c[nConstantSet]->SetBinContent(beta,bphi,icwt[j][k]);
1004     }else{
1005     icwt[j][k] = -1;
1006     hh2_c[nConstantSet]->SetBinContent(beta,bphi,-1);
1007     }
1008    
1009     for(int n=0; n< nIC; n++){ ///for each period
1010     if( wtSumC_period[n] > 0){
1011     icwt_period[n][j][k] = wtSumC_period[n] / wtSumS_period[n];
1012     hh2_c_period[n]->SetBinContent(beta,bphi,icwt_period[n][j][k]);
1013     }else{
1014     icwt_period[n][j][k] = -1;
1015     hh2_c_period[n]->SetBinContent(beta,bphi,-1);
1016     }
1017     }
1018    
1019     }
1020     }
1021    
1022    
1023    
1024     float statErr_allCombined[85] = {0} ;
1025    
1026 yangyong 1.3 for(int b=1; b<= hh_statErr_ietaAbs[0]->GetNbinsX(); b++){
1027 yangyong 1.2 float sumStatErr2 = 0;
1028     for(int n=0; n< nConstantSet; n++){
1029 yangyong 1.3 float statErr = hh_statErr_ietaAbs[n]->GetBinContent(b);
1030 yangyong 1.2 sumStatErr2 += 1./ ( statErr * statErr );
1031     }
1032     float statErr = sqrt( 1./ sumStatErr2 );
1033     statErr_allCombined[b-1] = statErr;
1034    
1035 yangyong 1.3 cout<<" statErr_allCombined " << b <<" "<< statErr_allCombined[b-1]*100 << " % "<<endl;
1036 yangyong 1.2
1037     }
1038    
1039    
1040     ///Stat error for each period
1041     for(int j=0; j< nIC; j++){
1042 yangyong 1.3 for(int b=1; b<= hh_statErr_ietaAbs[0]->GetNbinsX();b++){
1043     float statErrPi0 = hh_statErr_ietaAbs[j]->GetBinContent(b);
1044     float statErrEta = hh_statErr_ietaAbs[j+nIC]->GetBinContent(b);
1045 yangyong 1.2 float statErr = 1/(statErrPi0*statErrPi0) + 1./(statErrEta*statErrEta);
1046     statErr = sqrt(1/statErr);
1047 yangyong 1.3 hh_statErr_ietaAbs_period[j]->SetBinContent(b,statErr);
1048 yangyong 1.2 }
1049     }
1050    
1051    
1052     txtoutTocheck <<"checkme " <<endl;
1053    
1054     for(int j=0; j<170; j++){
1055    
1056     int ieta = j-85;
1057     if( ieta >=0) ieta += 1;
1058    
1059 yangyong 1.3
1060     int absieta = abs(ieta);
1061    
1062 yangyong 1.2 for(int k=0; k<360; k++){
1063     int iphi = k;
1064     if( k==0) iphi = 360;
1065     int beta = ieta+85+1;
1066     int bphi = iphi;
1067     int ietaTTAbs = (abs(ieta)-1)/5;
1068    
1069     bool largeIC = false;
1070    
1071     for(int n=0; n< nIC; n++){ ///for each period
1072     if( icwt_period[n][j][k] > 0 && (icwt_period[n][j][k] >1.2 || icwt_period[n][j][k] < 0.8)){
1073     largeIC = true;
1074     }
1075     }
1076     if(largeIC){
1077     txtoutTocheck<<"largeIC "<< j<<" "<<k <<" "<<ieta <<" "<<iphi<<" " ;
1078     for(int n1 =0; n1 < nIC ; n1++){
1079     txtoutTocheck<<icwt_period[n1][j][k]<<" ";
1080     }
1081     txtoutTocheck<<endl;
1082     }
1083    
1084     bool checkICdiff = false;
1085     for(int n1=0; n1< nIC; n1++){ ///for each period
1086     for(int n2=n1+1;n2< nIC; n2++){ ///for each period
1087     if( icwt_period[n1][j][k]>0 && icwt_period[n2][j][k]>0){
1088     float reldiff = fabs( icwt_period[n1][j][k] - icwt_period[n2][j][k] )/ ( 0.5* (icwt_period[n1][j][k] + icwt_period[n2][j][k]));
1089    
1090 yangyong 1.3 float statErr1 = hh_statErr_ietaAbs_period[n1]->GetBinContent(absieta)/100;
1091     float statErr2 = hh_statErr_ietaAbs_period[n2]->GetBinContent(absieta)/100;
1092 yangyong 1.2
1093     ////assuming same sys.
1094     float diff_statErr = sqrt( statErr1*statErr1 + statErr2*statErr2);
1095    
1096     checkICdiff = false;
1097     // if( abs(ieta)<60 && reldiff >0.02){
1098     // checkICdiff = true;
1099     // }
1100     // if( abs(ieta)>=60 && reldiff >0.1){
1101     // checkICdiff = true;
1102     // }
1103     checkICdiff = reldiff > 3 * diff_statErr ;
1104     if(checkICdiff){
1105     hh2_largeICdiff[n1][n2]->SetBinContent(beta,bphi,1);
1106     }else{
1107     hh2_largeICdiff[n1][n2]->SetBinContent(beta,bphi,-1);
1108     }
1109    
1110     }else{
1111     hh2_largeICdiff[n1][n2]->SetBinContent(beta,bphi,-1);
1112     }
1113     }
1114     }
1115     if(checkICdiff){
1116     hh2_largeICdiff_all->SetBinContent(beta,bphi,1);
1117     txtoutTocheck<<"checkICdiff "<< j<<" "<<k <<" "<<ieta <<" "<<iphi<<" " ;
1118     for(int n1 =0; n1 < nIC ; n1++){
1119     txtoutTocheck<<icwt_period[n1][j][k]<<" ";
1120     }
1121     txtoutTocheck<<endl;
1122     }else{
1123     hh2_largeICdiff_all->SetBinContent(beta,bphi,-1);
1124     }
1125    
1126     }
1127     }
1128    
1129    
1130    
1131    
1132     scaleMeanToUnit(icwt);
1133     ///for estimating precision of combined IC
1134     copyConstant(icwt,icwtv1);
1135     NormSMScaleToUnit(icwtv1);
1136    
1137    
1138     for(int n=0; n< nIC; n++){ ///for each period
1139     scaleMeanToUnit(icwt_period[n]);
1140     }
1141    
1142    
1143     for(int j=0; j<170; j++){
1144     int ieta = j-85;
1145     if( ieta >=0) ieta += 1;
1146     for(int k=0; k<360; k++){
1147     int iphi = k;
1148     if( k==0) iphi = 360;
1149     int ietaTTAbs = (abs(ieta)-1)/5;
1150     int iSM = (iphi-1)/20+1;
1151     if( ieta<0) iSM += 18;
1152     int smTB = isTestBeamSM(iSM);
1153    
1154    
1155     float sysErr = 0.5;
1156     if(abs(ieta)>=60) sysErr = 1;
1157    
1158     float icErr = sysErr;
1159     float statErr = statErr_allCombined[ietaTTAbs];
1160     icErr = sqrt(sysErr *sysErr + statErr * statErr);
1161     icErr /=100;
1162    
1163    
1164     if( icwt[j][k] > 0){
1165     txtout<<ieta<<" "<<iphi<<" "<< icwt[j][k]*interCalib_preCalib[j][k]<<" "<< icErr* icwt[j][k]*interCalib_preCalib[j][k]<<endl;
1166     //txtout<<ieta<<" "<<iphi<<" "<< icwt[j][k]*interCalib_preCalib[j][k]<<endl;
1167     }else{
1168     txtout<<ieta<<" "<<iphi<<" "<< -1 <<" "<< 999 <<endl;
1169     //txtout<<ieta<<" "<<iphi<<" "<< interCalib_preCalib[j][k]<<endl;
1170    
1171     }
1172    
1173     float c = icwtv1[j][k]; ///sm normalized to unit
1174    
1175    
1176     int absieta = abs(ieta);
1177    
1178     for(int n=0; n< nIC+1; n++){ ///for each period and all combined
1179    
1180     if(n< nIC){
1181     if( icwt_period[n][j][k] > 0){
1182     hh_c_ietaAbs_period[n][absieta-1]->Fill(icwt_period[n][j][k]);
1183     hh_c_ietaTTAbs_period[n][ietaTTAbs]->Fill(icwt_period[n][j][k]);
1184    
1185     if( smTB){
1186     hh_c_smtbietaTTAbs_period[n][ietaTTAbs]->Fill(icwt_period[n][j][k]);
1187     }
1188     int deadflag = ndeadflagietaiphi_ic[n][j][k];
1189     if( deadflag<0){
1190     cout<<"wrong deadlfag !!! n " << n <<" "<<endl;
1191     exit(1);
1192     }
1193     hh_c_deadflag_period[n][deadflag]->Fill(icwt_period[n][j][k]);
1194 yangyong 1.3
1195 yangyong 1.2 if(deadflag>0){
1196     hh_c_deadflag_period[n][19]->Fill(icwt_period[n][j][k]);
1197     hh_cc_deadflag_period[n][19]->Fill( interCalib_preCalib[j][k] * icwt_period[n][j][k]);
1198     }
1199    
1200     }
1201     }else{
1202     if( icwt[j][k] > 0){
1203     hh_c_ietaAbs_period[n][absieta-1]->Fill(icwt[j][k]);
1204     hh_c_ietaTTAbs_period[n][ietaTTAbs]->Fill(icwt[j][k]);
1205    
1206     int deadflag = ndeadflagietaiphi_ic[0][j][k];
1207     if( deadflag<0){
1208     cout<<"wrong deadlfag !!! comb " << n <<" "<<endl;
1209     exit(1);
1210     }
1211     hh_c_deadflag_period[n][deadflag]->Fill(icwt[j][k]);
1212     if(deadflag>0){
1213     hh_c_deadflag_period[n][19]->Fill(icwt[j][k]);
1214     }
1215    
1216     if( smTB){
1217     hh_c_smtbietaTTAbs_period[n][ietaTTAbs]->Fill(icwt[j][k]);
1218     }
1219    
1220     }
1221     }
1222     }
1223    
1224    
1225     for(int n=0; n< nIC; n++){ ///for each period
1226 yangyong 1.3 statErr = hh_statErr_ietaAbs_period[n]->GetBinContent(absieta);
1227 yangyong 1.2 icErr = sqrt( statErr * statErr + sysErr * sysErr);
1228     icErr /= 100;
1229    
1230     if( icwt_period[n][j][k] > 0){
1231     txtout_period[n]<<ieta<<" "<<iphi<<" "<< icwt_period[n][j][k]*interCalib_preCalib[j][k]<<" "<< icErr*icwt_period[n][j][k]*interCalib_preCalib[j][k] <<endl;
1232     //txtout_period[n]<<ieta<<" "<<iphi<<" "<< icwt_period[n][j][k]*interCalib_preCalib[j][k]<<endl;
1233     }else{
1234     txtout_period[n]<<ieta<<" "<<iphi<<" "<< -1<<" "<< 999 <<endl;
1235     //txtout_period[n]<<ieta<<" "<<iphi<<" "<< interCalib_preCalib[j][k]<<" "<< 999 <<endl;
1236    
1237     }
1238     }
1239    
1240    
1241    
1242     if( c>0){
1243    
1244     int ietaflag[3] = {abs(ieta)<25,abs(ieta)>=25 && abs(ieta)<60,abs(ieta)>=60};
1245     for(int jj=0;jj<3; jj++){
1246     if( ietaflag[jj]==0) continue;
1247     hh_csm[nConstantSet][iSM-1][jj]->Fill(c);
1248     }
1249    
1250     if( smTB){
1251     hh_wtavg_csmtb_ietaTT[j/5]->Fill(c);
1252     hh_wtavg_csmtb_ietaTTAbs[ietaTTAbs]->Fill(c);
1253     }else{
1254     hh_wtavg_csmco_ietaTT[j/5]->Fill(c);
1255     hh_wtavg_csmco_ietaTTAbs[ietaTTAbs]->Fill(c);
1256     }
1257    
1258     ///all sm
1259     hh_wtavg_csmall_ietaTTAbs[ietaTTAbs]->Fill(c);
1260    
1261     }
1262    
1263     }
1264     }
1265    
1266     cout <<" fitting combined " <<endl;
1267    
1268    
1269     for(int n=0; n< nConstantSet+1; n++){
1270     for(int j=0; j<36; j++){
1271    
1272     for(int k=0;k<3;k++){
1273     fitHistogram(hh_csm[n][j][k],resfit);
1274     for(int jj=0;jj<3; jj++){
1275     hh_res_csm[n][k][jj]->SetBinContent(j+1,resfit[2*jj]);
1276     hh_res_csm[n][k][jj]->SetBinError(j+1,resfit[2*jj+1]);
1277     }
1278     }
1279     }
1280     }
1281    
1282    
1283    
1284     for(int n1=0; n1< nIC+1; n1++){ ///for each period and all combined
1285     for(int j=0; j< 85; j++){
1286     fitHistogram(hh_c_ietaAbs_period[n1][j],resfit);
1287     for(int n=0;n<3; n++){
1288     hh_res_cietaAbs_period[n1][n]->SetBinContent(j+1,resfit[2*n]);
1289     hh_res_cietaAbs_period[n1][n]->SetBinError(j+1,resfit[2*n+1]);
1290     }
1291     }
1292     for(int j=0; j< 17; j++){
1293     fitHistogram(hh_c_ietaTTAbs_period[n1][j],resfit);
1294     for(int n=0;n<3; n++){
1295     hh_res_cietaTTAbs_period[n1][n]->SetBinContent(j+1,resfit[2*n]);
1296     hh_res_cietaTTAbs_period[n1][n]->SetBinError(j+1,resfit[2*n+1]);
1297     }
1298     fitHistogram(hh_c_smtbietaTTAbs_period[n1][j],resfit);
1299     for(int n=0;n<3; n++){
1300     hh_res_csmtbietaTTAbs_period[n1][n]->SetBinContent(j+1,resfit[2*n]);
1301     hh_res_csmtbietaTTAbs_period[n1][n]->SetBinError(j+1,resfit[2*n+1]);
1302     }
1303     }
1304     }
1305    
1306    
1307     for(int j=0;j<34; j++){
1308     fitHistogram(hh_wtavg_csmtb_ietaTT[j],resfit);
1309     for(int n=0;n<3; n++){
1310     hh_res_wtavg_csmtbietaTT[n]->SetBinContent(j+1,resfit[2*n]);
1311     hh_res_wtavg_csmtbietaTT[n]->SetBinError(j+1,resfit[2*n+1]);
1312     }
1313     fitHistogram(hh_wtavg_csmco_ietaTT[j],resfit);
1314     for(int n=0;n<3; n++){
1315     hh_res_wtavg_csmcoietaTT[n]->SetBinContent(j+1,resfit[2*n]);
1316     hh_res_wtavg_csmcoietaTT[n]->SetBinError(j+1,resfit[2*n+1]);
1317     }
1318     }
1319     for(int j=0;j<17; j++){
1320     fitHistogram(hh_wtavg_csmtb_ietaTTAbs[j],resfit);
1321     for(int n=0;n<3; n++){
1322     hh_res_wtavg_csmtbietaTTAbs[n]->SetBinContent(j+1,resfit[2*n]);
1323     hh_res_wtavg_csmtbietaTTAbs[n]->SetBinError(j+1,resfit[2*n+1]);
1324     }
1325    
1326    
1327     fitHistogram(hh_wtavg_csmall_ietaTTAbs[j],resfit);
1328     for(int n=0;n<3; n++){
1329     hh_res_wtavg_csmallietaTTAbs[n]->SetBinContent(j+1,resfit[2*n]);
1330     hh_res_wtavg_csmallietaTTAbs[n]->SetBinError(j+1,resfit[2*n+1]);
1331     }
1332    
1333     fitHistogram(hh_wtavg_csmco_ietaTTAbs[j],resfit);
1334     for(int n=0;n<3; n++){
1335     hh_res_wtavg_csmcoietaTTAbs[n]->SetBinContent(j+1,resfit[2*n]);
1336     hh_res_wtavg_csmcoietaTTAbs[n]->SetBinError(j+1,resfit[2*n+1]);
1337     }
1338     }
1339    
1340    
1341     fnew->Write();
1342     fnew->Close();
1343    
1344     }