drawRaw.C

macros for determination the thickness distribution of SQ20 - Ivan Muzalevsky, 06/07/2018 01:54 PM

Download (24.2 KB)

 
1
void drawRaw(){
2

    
3
  TELoss fAlphaSi;
4
  fAlphaSi.SetEL(1, 2.321); // density in g/cm3
5
        fAlphaSi.AddEL(14., 28.086, 1);  //Z, mass
6
//        mSi.SetZP(1., 1.);                //protons
7
        fAlphaSi.SetZP(2., 4.);                //alphas, Z, A
8
        fAlphaSi.SetEtab(100000, 200.);        // ?, MeV calculate ranges
9
        fAlphaSi.SetDeltaEtab(300);
10

    
11

    
12
  TString cut[16];
13
  TString cut_0[16];
14
  TString cut1("");
15
  TString cut2("");
16
  TString cut3("");
17
  TString cut4("");
18
  
19
  
20
  TString hDraw,hname,hcut;
21

    
22
  TH1F *h0,*h1,*h2,*h[300];
23

    
24
  TString space(" || ");
25
  for(Int_t i =0;i<16;i++) {
26
    cut[i].Form("SQY_L[%d]>130",i);
27
    cut_0[i].Form("SQY_L[%d]<130",i);
28

    
29
    if(i==0) {
30
      cut1 = cut[0];
31
      cut3 = cut[0];
32
      cut4 = cut_0[0];
33
    }
34
    if(i<8) cut1 = cut1 + " || " + cut[i];
35

    
36
    if(i==8) cut2 = cut[8];
37
    if(i>7 && i<16) cut2 = cut2 + " || " + cut[i];
38

    
39
    cut3 = cut3 + " || " + cut[i];
40
    cut4 = cut4 + " && " + cut_0[i];
41
  }
42

    
43
  //cout << "cut1 "<< cut1.Data() << endl;
44
  //cout << "cut2 "<< cut2.Data() << endl;
45
  //cout << "cut3 "<< cut3.Data() << endl;
46
  //cout << "cut4 "<< cut4.Data() << endl;
47

    
48
  TChain *ch = new TChain("AnalysisxTree");
49
        //ch->Add("/media/analysis_nas/exp201804/calib/si_after/si-1_si-20_35cm_45deg_new1_00*.root");
50
  //ch->Add("/media/analysis_nas/exp201804/calib/si_after/si-1_si-20_35cm_60deg_00*.root");
51

    
52
  TChain *ch0 = new TChain("AnalysisxTree");
53
        ch0->Add("/media/analysis_nas/exp201804/calib/si_after/si-1_si-20_35cm_0deg_new1_*.root");
54
  
55

    
56
 // -0.4293        0.006653                0
57

    
58
/*  TCanvas *c1 = new TCanvas("c1","raw data",1800,1000);
59
  c1->Divide(3,2);
60

61
  c1->cd(1);
62
  ch->Draw("NeEvent.SQY_L[7]*0.006653-0.4293 >> hy_0(200,0.435590,8)","","",1000000,1);
63
  h[0] = (TH1F*)gPad->GetPrimitive("hy_0");
64
        h[0]->GetXaxis()->SetTitle("Edep in 1mm middle Y strip");
65
  h[0]->GetXaxis()->SetTitleColor(2);
66
  h[0]->GetXaxis()->SetTitleOffset(0.75);
67
        h[0]->GetXaxis()->SetTitleSize(0.047);
68
        h[0]->GetXaxis()->CenterTitle();
69
  c1->Update();
70

71
  c1->cd(4);
72
  ch->Draw("NeEvent.SQY_L[15]*0.006501-0.4096 >> hy_15(200,0.435590,8)","","",10000000,1);
73
  h[1] = (TH1F*)gPad->GetPrimitive("hy_15");
74
        h[1]->GetXaxis()->SetTitle("Edep in 1mm last Y strip");
75
  h[1]->GetXaxis()->SetTitleColor(2);
76
  h[1]->GetXaxis()->SetTitleOffset(0.75);
77
        h[1]->GetXaxis()->SetTitleSize(0.047);
78
        h[1]->GetXaxis()->CenterTitle();
79
  c1->Update();
80

81
  c1->cd(2);
82
  ch->Draw("NeEvent.SQX_L[15]*0.006454+0.09241 >> hx_15(200,1.383210,8)","","",10000000,1);
83
  ch->Draw("NeEvent.SQX_L[0]*0.006705-0.001022 >> hx_0(200,1.339978,8)","","same",10000000,1);
84
  h[2] = (TH1F*)gPad->GetPrimitive("hx_15");
85
        h[2]->GetXaxis()->SetTitle("Edep in 1mm middle(blue) and first/left(green) X strip");
86
  h[2]->GetXaxis()->SetTitleColor(2);
87
  h[2]->GetXaxis()->SetTitleOffset(0.75);
88
        h[2]->GetXaxis()->SetTitleSize(0.047);
89
        h[2]->GetXaxis()->CenterTitle();
90

91
  h[3] = (TH1F*)gPad->GetPrimitive("hx_0");
92
  h[3]->SetLineColor(3);
93

94
  c1->Update();
95

96
  c1->cd(5);
97
  ch->Draw("NeEvent.SQX_L[31]*0.006666+0.1119 >> hx_31(200,1.445100,8)","","",10000000,1);
98
  h[4] = (TH1F*)gPad->GetPrimitive("hx_31");
99
        h[4]->GetXaxis()->SetTitle("Edep in 1mm last/right X strip");
100
  h[4]->GetXaxis()->SetTitleColor(2);
101
  h[4]->GetXaxis()->SetTitleOffset(0.75);
102
        h[4]->GetXaxis()->SetTitleSize(0.047);
103
        h[4]->GetXaxis()->CenterTitle();
104
  c1->Update();
105

106
  c1->cd(3);
107
  ch->Draw("NeEvent.SQY_R[0] >> h20_0(1300,200,1500)","","",10000000,1);
108
  h[5] = (TH1F*)gPad->GetPrimitive("h20_0");
109
        h[5]->GetXaxis()->SetTitle("Edep of 20mkm first/left strip");
110
  h[5]->GetXaxis()->SetTitleColor(2);
111
  h[5]->GetXaxis()->SetTitleOffset(0.75);
112
        h[5]->GetXaxis()->SetTitleSize(0.047);
113
        h[5]->GetXaxis()->CenterTitle();
114

115
  c1->Update();
116

117
  c1->cd(6);
118
  ch->Draw("NeEvent.SQY_R[15] >> h20_15(1300,200,1500)","","",10000000,1);
119
  h[6] = (TH1F*)gPad->GetPrimitive("h20_15");
120
        h[6]->GetXaxis()->SetTitle("Edep in 20mkm last/right strip");
121
  h[6]->GetXaxis()->SetTitleColor(2);
122
  h[6]->GetXaxis()->SetTitleOffset(0.75);
123
        h[6]->GetXaxis()->SetTitleSize(0.047);
124
        h[6]->GetXaxis()->CenterTitle();
125
  c1->Update();
126

127

128

129
  TCanvas *c2 = new TCanvas("c2","selection of SQ20 data",1800,1000);
130
  c2->Divide(2,2);
131

132
  c2->cd(1);
133
  ch->Draw("NeEvent.SQY_R[15] >> h22_0-7(1300,200,1500)",cut1.Data(),"",10000000,1);
134
  h[7] = (TH1F*)gPad->GetPrimitive("h22_0-7");
135
        h[7]->GetXaxis()->SetTitle("Edep in first half of 20mkm last/right strip (strips 0-7 of SQY_L were fired)");
136
  h[7]->GetXaxis()->SetTitleColor(2);
137
  h[7]->GetXaxis()->SetTitleOffset(0.75);
138
        h[7]->GetXaxis()->SetTitleSize(0.047);
139
        h[7]->GetXaxis()->CenterTitle();
140
  c2->Update();
141

142
  c2->cd(2);
143
  ch->Draw("NeEvent.SQY_R[15] >> h22_8-15(1300,200,1500)",cut2.Data(),"",10000000,1);
144
  h[8] = (TH1F*)gPad->GetPrimitive("h22_8-15");
145
        h[8]->GetXaxis()->SetTitle("Edep in second half of 20mkm last/right strip (strips 8-15 of SQY_L were fired)");
146
  h[8]->GetXaxis()->SetTitleColor(2);
147
  h[8]->GetXaxis()->SetTitleOffset(0.75);
148
        h[8]->GetXaxis()->SetTitleSize(0.047);
149
        h[8]->GetXaxis()->CenterTitle();
150
  c2->Update();
151

152
  c2->cd(3);
153
  ch->Draw("NeEvent.SQY_R[15] >> h22_0-15(1300,200,1500)",cut3.Data(),"",10000000,1);
154
  h[9] = (TH1F*)gPad->GetPrimitive("h22_0-15");
155
        h[9]->GetXaxis()->SetTitle("Edep in 20mkm last/right strip (strips 0-15 of SQY_L were fired)");
156
  h[9]->GetXaxis()->SetTitleColor(2);
157
  h[9]->GetXaxis()->SetTitleOffset(0.75);
158
        h[9]->GetXaxis()->SetTitleSize(0.047);
159
        h[9]->GetXaxis()->CenterTitle();
160
  c2->Update();
161

162

163
  c2->cd(4);
164
  ch->Draw("NeEvent.SQY_R[15] >> h22_underThr(1300,200,1500)",cut4.Data(),"",10000000,1);
165
  h[10] = (TH1F*)gPad->GetPrimitive("h22_underThr");
166
        h[10]->GetXaxis()->SetTitle("Edep in 20mkm last/right strip (strips 0-15 of SQY_L were NOT fired)");
167
  h[10]->GetXaxis()->SetTitleColor(2);
168
  h[10]->GetXaxis()->SetTitleOffset(0.75);
169
        h[10]->GetXaxis()->SetTitleSize(0.047);
170
        h[10]->GetXaxis()->CenterTitle();
171
  c2->Update();
172
*/
173
/*
174
/// calibration
175
  TF1* g1 = new TF1("g1", "gaus", 1040, 1125);
176
  g1->SetParLimits(0,1.,5000.);
177
  g1->SetParLimits(1,1040.,1125.);
178
  g1->SetParLimits(2,5,100.);
179

180
  TF1* g2 = new TF1("g2", "gaus", 1200, 1270);
181
  g2->SetParLimits(0,1.,5000.);
182
  g2->SetParLimits(1,1200.,1270.);
183
  g2->SetParLimits(2,1320.,1400.);
184

185
  TF1* g3 = new TF1("g3", "gaus", 1320, 1400);
186
  g3->SetParLimits(0,1.,5000.);
187
  g3->SetParLimits(1,1320.,1400.);
188
  g3->SetParLimits(2,5,100.);
189

190
  Double_t peakPosition,peakHight,x[3],y[3],e[3],temp;
191
  // energies after passing through 0.75 mkm dl
192
  y[0] = 4.7844;
193
  y[1] = 5.4895;
194
  y[2] = 6.0024;
195

196
        TList *functions;
197
  TPolyMarker *pm;
198
  TGraph *g[50];
199
  TF1 *lin = new TF1("lin","[0]*x+[1]",0,1500);
200

201
        ofstream outcalfile;
202
        outcalfile.open("par_20_60.cal");
203
        if (!outcalfile.is_open()) {
204
                Error("drawRaw", "Output file %s was not opened", "prepar_20.cal");
205
                return 0;
206
        }//if
207

208
        ofstream outXfile;
209
        outXfile.open("positions_20_60.cal");
210
        if (!outXfile.is_open()) {
211
                Error("drawRaw", "Output file %s was not opened", "positions_20.cal");
212
                return 0;
213
        }//if
214

215

216
  Float_t dl[16];
217
  ifstream myfile;
218
  TString line;
219
  Int_t count=0;
220
  myfile.open("/home/muzalevsky/work/temp/deadLayers.txt");
221
  while (! myfile.eof() ){
222
    line.ReadLine(myfile);
223
    if(line.IsNull()) break;
224
    sscanf(line.Data(),"%g", dl+count);
225
    count++;
226
  }
227
  for(Int_t i=0;i<16;i++) {
228
    cout << dl[i]<<endl;
229
  }
230

231
  TCanvas *c4 = new TCanvas("c4","raw 20 mkm data trigger=3",1800,1000);
232
  c4->Divide(4,4);
233

234
  TCanvas *c5 = new TCanvas("c5","linfits",1800,1000);
235
  c5->Divide(4,4);
236

237

238
  for(Int_t i=0;i<16;i++) {
239
    cout << " new pad " << i+1 << endl;
240
    c4->cd(i+1);
241
    hDraw.Form("NeEvent.SQY_R[%d] >> h20_60_%d(1100,500,1600)",i,i);
242
    hname.Form("h20_60_%d",i);
243
    ch->Draw(hDraw.Data(),"NeEvent.trigger==3","",10000000,1);
244
    h[i] = (TH1F*)gPad->GetPrimitive(hname.Data());
245
    h[i]->ShowPeaks(20,"", 0.5);
246
    c4->Update();
247

248
          functions = h[i]->GetListOfFunctions();
249
          pm = (TPolyMarker*)functions->FindObject("TPolyMarker");
250
          //cout << *pm->GetY() << endl;
251
    for(Int_t j=0;j<3;j++) {
252
      x[j] = *(pm->GetX()+j);
253
      cout << x[j] << " ";
254
    } 
255
    cout <<endl<< "sorted " << endl;
256

257
    for (Int_t m = 0; m < 2; ++m){ 
258
      for (Int_t n = 0; n < 2; ++n){ 
259
        if(x[n] > x[n+1]) { 
260
            temp = x[n]; 
261
            x[n] = x[n+1]; 
262
            x[n+1] = temp; 
263
        }
264
      } 
265
    }
266
    for(Int_t j=0;j<3;j++) {
267
       cout << x[j] << " ";
268
    } 
269
    cout << endl;
270

271
    
272
    // recalculation energies using different effective dead layers for different strips
273
    cout << " shifted energies " << endl;
274
    for(Int_t j =0;j<16;j++) {
275
      for(Int_t k=0;k<3;k++) {
276
        e[k] = fAlphaSi.GetE(y[k],dl[j]);
277
        //cout << e[k] << " ";
278
      }
279
      //cout << endl;
280
    }
281

282
    g[i] = new TGraph(3,x,e);
283
    c5->cd(i+1);
284
    g[i]->SetMarkerSize(3);
285
    g[i]->Draw("AL*");
286
    g[i]->Fit("lin");
287
    c5->Update();
288
    outcalfile << setprecision(4) << lin->GetParameter(1) << "\t" << lin->GetParameter(0) << "\t" << endl;
289
    outXfile << setprecision(4) << x[0] << "\t" << x[1] << "\t" << x[2] << endl;
290

291
    //for (Int_t m = 0; m < 3; m++)  {
292
    //    cout << x[m] << " ";
293
    //}
294
    //cout << endl;
295
   
296
  }
297
*/
298

    
299
/*  cout << ch0->GetEntries() << " entr " << endl;
300
  Double_t peakPosition,peakHight,x[3],y[3],temp;
301
  y[0] = 4.7844;
302
  y[1] = 5.4895;
303
  y[2] = 6.0024;
304

305
        TList *functions;
306
  TPolyMarker *pm;
307
  TGraph *g[50];
308
  TF1 *lin = new TF1("lin","[0]*x+[1]",0,1500);
309

310
        ofstream outcalfile;
311
        outcalfile.open("prepar_20.cal");
312
        if (!outcalfile.is_open()) {
313
                Error("drawRaw", "Output file %s was not opened", "prepar_20.cal");
314
                return 0;
315
        }//if
316

317
        ofstream outXfile;
318
        outXfile.open("positions_20.cal");
319
        if (!outXfile.is_open()) {
320
                Error("drawRaw", "Output file %s was not opened", "positions_20.cal");
321
                return 0;
322
        }//if
323

324

325
  TCanvas *c4 = new TCanvas("c4","raw 20 mkm data trigger=3",1800,1000);
326
  c4->Divide(4,4);
327

328
  //TCanvas *c5 = new TCanvas("c5","linfits",1800,1000);
329
  //c5->Divide(4,4);
330
  Double_t x0;
331

332
  for(Int_t i=0;i<16;i++) {
333
    cout << " new pad " << i+1 << endl;
334
    c4->cd(i+1);
335
    hDraw.Form("NeEvent.SQY_R[%d] >> h20_%d(1400,200,1600)",i,i);
336
    hname.Form("h20_%d",i);
337
    ch0->Draw(hDraw.Data(),"NeEvent.trigger==3","",7876702,1);
338
    h[i] = (TH1F*)gPad->GetPrimitive(hname.Data());
339
    //x0 = h[i]->GetBinCenter(h[i]->GetMaximumBin());
340
    gStyle->SetStatW(0.3);                
341
    // Set width of stat-box (fraction of pad size)
342
    gStyle->SetStatH(0.4);
343

344
    c4->Update();
345

346
          //functions = h[i]->GetListOfFunctions();
347
          //pm = (TPolyMarker*)functions->FindObject("TPolyMarker");
348
          //cout << *pm->GetY() << endl;
349
    //for(Int_t j=0;j<3;j++) {
350
      //x[j] = *(pm->GetX()+j);
351
     // cout << x[j] << " ";
352
    //} 
353
    //cout <<endl<< "sorted " << endl;
354

355
    for (Int_t m = 0; m < 2; ++m){ 
356
      for (Int_t n = 0; n < 2; ++n){ 
357
        if(x[n] > x[n+1]) { 
358
            temp = x[n]; 
359
            x[n] = x[n+1]; 
360
            x[n+1] = temp; 
361
        }
362
      } 
363
    }
364
    //g[i] = new TGraph(3,x,y);
365
    //c5->cd(i+1);
366
    //g[i]->SetMarkerSize(3);
367
    //g[i]->Draw("AL*");
368
   // g[i]->Fit("lin");
369
    //c5->Update();
370
    //outcalfile << setprecision(4) << lin->GetParameter(1) << "\t" << lin->GetParameter(0) << "\t" << endl;
371
    outXfile << setprecision(4) << x0 << endl;
372

373
    //for (Int_t m = 0; m < 3; m++)  {
374
    //    cout << x[m] << " ";
375
    //}
376
    //cout << endl;
377
   
378
  }
379

380
*/
381

    
382
//thickness determination
383

    
384
/*Int_t SearchPeaks(const TH1 *hin, Double_t sigma, Option_t *option, const Int_t searchedpeaks)
385
{
386
        //Function searching peaks in inputed TH1 spectrum and selects the peaks in the histogram.
387
        //
388
        //  hin:
389
        //  sigma:
390
        //  option:
391
        //  threshold:
392
        //  searchedpeaks:
393

394
        TSpectrum sc;        //by default for 100 peaks
395
        Int_t nopeaks = sc.Search(hin, sigma, "goff", fFitPeakThreshold);
396

397
        TString opt = option;
398
        opt.ToLower();
399

400
        const Double_t tStep = 0.05;
401

402

403
        if (!nopeaks) {
404
                return 0;
405
        }
406

407
        if (opt.Contains("goff")) {
408
                return nopeaks;
409
        }
410

411
        TPolyMarker *pm = (TPolyMarker*)hin->GetListOfFunctions()->FindObject("TPolyMarker");
412
        if (pm) {
413
                hin->GetListOfFunctions()->Remove(pm);
414
                delete pm;
415
        }
416
        pm = new TPolyMarker(nopeaks, sc.GetPositionX(), sc.GetPositionY());
417
        hin->GetListOfFunctions()->Add(pm);
418
        pm->SetMarkerStyle(23);
419
        pm->SetMarkerColor(kRed);
420
        pm->SetMarkerSize(1.3);
421

422
        return nopeaks;
423
}
424

425
Int_t AculCalibration::PeaksFitting(TH1* hSpectrum, Option_t* option, Double_t sigmamin)
426
{
427

428
        if (!hSpectrum) {
429
    cout<< "raw spec was not found " << endl;
430
    return 1;
431
  }
432
        Int_t dimension = hSpectrum->GetDimension();
433
        if (dimension > 1) {
434
                Error("PeaksFitting", "Only implemented for 1-d histograms");
435
                return 1;
436
        }
437

438
        TString        opt = option;
439
        opt.ToLower();
440

441
        if (!kRaNOPEAKS) {
442
                Error("PeaksFitting", "kRaNOPEAKS is set to zero; calibration spectrum must be set");
443
                return 1;
444
        }
445

446
        const Int_t peaksNumber =        SearchPeaks(hSpectrum, sigmamin, "", kRaNOPEAKS);
447

448
        if (peaksNumber != kRaNOPEAKS) {
449
                Info("PeaksFitting", "In histogram %s was found %d peaks", hSpectrum->GetName(), peaksNumber);
450
                return 1;
451
        }
452
        //should be optional output
453
        Info("PeaksFitting", "Number of peaks in %s: %d", hSpectrum->GetName(), peaksNumber);
454

455
        //working array for peaks, there are founded in accidental order
456
        Double_t peak[peaksNumber];
457
        Double_t *peakPosition;
458
        Double_t *peakHight;
459

460
        TList *functions = hSpectrum->GetListOfFunctions();
461
        TPolyMarker *pm = (TPolyMarker*)functions->FindObject("TPolyMarker");
462

463
        peakPosition = pm->GetX();
464
        peakHight = pm->GetY();
465

466
        for (Int_t i = 0; i < peaksNumber; i++) {
467

468
                Double_t fitMin = 0;
469
                Double_t fitMax = 0;
470
                Double_t fitStep = hSpectrum->GetXaxis()->GetBinWidth(0);
471
//                cout << fitStep << endl;
472
//                cout << fLowerPeakRelativeHight << "\t" << fUpperPeakRelativeHight << endl;
473

474
                //fitting range:
475
                //shift a range of fit and search for raw boarder of peak determined by fUpperPeakRelativeHight
476
                //maximum
477
                Int_t j = 0;
478
                Double_t currentHight = peakHight[i];
479
                while ( currentHight > (peakHight[i]*fUpperPeakRelativeHight) ) {
480
                        j++;
481
                        fitMax = static_cast<Double_t>(peakPosition[i]) + j*fitStep;
482
                        currentHight = hSpectrum->GetBinContent(hSpectrum->GetXaxis()->FindBin(fitMax));
483
                }
484

485
                //minimum
486
                j = 0;
487
                currentHight = peakHight[i];
488
                while ( currentHight > (peakHight[i]*fLowerPeakRelativeHight) ) {
489
                        j++;
490
                        fitMin = static_cast<Double_t>(peakPosition[i]) - j*fitStep;
491
                        currentHight = hSpectrum->GetBinContent(hSpectrum->GetXaxis()->FindBin(fitMin));
492
                }
493

494
                //fitting
495
                if (opt.Contains("gp")) {
496
                        Info("PeaksFitting", "Option containing gp");
497
                        char fncname[20];
498
                        sprintf(fncname, "gaus_aux_%d", i);
499
                        TF1 *gausAux = new TF1(fncname, "gaus", fitMin - 10, fitMax + 10);                //pomocny gaus
500
                        hSpectrum->Fit(fncname, "0 Q", "", fitMin - 15, fitMax + 15);                                //prvotni fitovani
501

502
                        sprintf(fncname, "auto_gp_%d", i);
503
                        TF1 *fitAuto = new TF1(fncname, "gaus(0) + pol0(3)", fitMin - 15, fitMax + 15);                //fce pro automaticke fitovani
504
                        fitAuto->SetParameter(0, gausAux->GetParameter(0));                //nastavovani parametru fitovaci fce
505
                        fitAuto->SetParameter(1, gausAux->GetParameter(1));
506
                        fitAuto->SetParameter(2, gausAux->GetParameter(2));
507

508
                        hSpectrum->Fit(fncname, "0 R Q +", "", fitMin - 15, fitMax + 15); //dodelat zapis vsech fci
509
                        hSpectrum->GetFunction(fncname)->ResetBit(TF1::kNotDraw);
510
                        peak[i] = fitAuto->GetParameter(1);                        //zapis asi pozice v kanalech do pomocneho pole
511
                        if (opt.Contains("V")) {
512
                                Info("PeaksFitting", "Peak position is\t %4.2f \tresolution is \t %2.1f %%", fitAuto->GetParameter(1), 235*(fitAuto->GetParameter(2))/(fitAuto->GetParameter(1)));
513
                        }
514
                }
515
                else {
516
                        char fncname[20];
517
                        sprintf(fncname, "auto_g%d", i);
518
                        TF1 *fitAuto = new TF1(fncname, "gaus", fitMin, fitMax);                //fce pro automaticke fitovani
519
//                        cout << fitMin << "\t" << fitMax << endl;
520
//                        fitAuto->SetParameter(2, fitMax-fitMin);
521
                        fitAuto->SetLineWidth(fFitFuncLineWidth);
522
                        hSpectrum->Fit(fncname, "+ 0 R Q", "", fitMin - 1, fitMax + 1);
523
//                        hSpectrum->GetFunction(fncname)->ResetBit(TF1::kNotDraw);
524
                        hSpectrum->GetFunction(fncname)->InvertBit(TF1::kNotDraw);
525
                        peak[i] = fitAuto->GetParameter(1);                        //zapis asi pozice v kanalech do pomocneho pole
526
                        if (opt.Contains("v")) {
527
                                Info("PeaksFitting", "Peak position is\t%4.2f\tresolution is \t%2.1f %%", fitAuto->GetParameter(1), 235*(fitAuto->GetParameter(2))/(fitAuto->GetParameter(1)));
528
                        }
529
                }//else
530
                //end of fitting
531
        }//for over all analyzed peaks
532

533
        //peaks sorting
534
        Int_t j[peaksNumber];
535
        TMath::Sort(peaksNumber, peak, j, kFALSE);
536
        fPeak.Set(peaksNumber);
537
        for (Int_t i = 0; i < peaksNumber; i++) {
538
                fPeak[i] = peak[j[i]];
539
                //printf("\tPeak peak\t%f\n", fPeak[i]);
540
        }
541

542
        if (!opt.Contains("q") || opt.Contains("v")) {
543
                Info("PeaksFitting", "Control output:");
544
                for (Int_t i = 0; i < peaksNumber; i++) {
545
                        printf("\tPeak position is\t%f\n", fPeak[i]);
546
                }
547
        }
548

549
        //        provest kontrolu pomerne polohy piku,
550
        //        jestli jsou spatne, provest urcita opatreni,
551
        //        napr. zapis daneho histogramu do souboru,
552
        //        zapis do souboru s chybama, vypis na obrazovku, ...
553
        for (Int_t i = 0; i < peaksNumber; i++) {
554
                        if ( !( (((1-fPeakPositionTolerance)*(fEnergy[0]/fEnergy[i])) < (fPeak[0]/fPeak[i])) && (((1+fPeakPositionTolerance)*(fEnergy[0]/fEnergy[i])) > (fPeak[0]/fPeak[i])) ) ) {
555
                        //printf("\tPeaksFitt fEnergy\t%f\n", fEnergy[i]);                
556
                        if (fCalInformation  && opt.Contains("writebad")) {
557
                                fCalInformation->cd();
558
                                hSpectrum->Write();
559
                        }
560
                        //return 2;
561
                }
562
        }//for
563

564
        return 0;
565
}
566
*/
567

    
568

    
569
// reading input pars
570
  Float_t parYL1[16],parYL2[16];
571
  ifstream myfile4;
572
  TString line4;
573
  Int_t count=-2;
574
//  myfile4.open("/home/muzalevsky/work/exp1803/data/siCal/SQY_L.cal");
575
  myfile4.open("/home/muzalevsky/work/exp1803/data/siCal/presentPars/SQY_L.cal");
576
  while (! myfile4.eof() ){
577
    line4.ReadLine(myfile4);
578
    if(count < 0){
579
      count++;
580
      continue;
581
    }
582
    if(line4.IsNull()) break;
583
    sscanf(line4.Data(),"%g %g", parYL1+count,parYL2+count);
584
    count++;
585
  }
586

    
587
  cout << endl << " pars for YL strips" << endl;
588
  for(Int_t i=0;i<16;i++) cout << parYL1[i] << " " << parYL2[i] << endl;    
589

    
590
  // angles for DL in strips SQY_L
591
  Double_t alpha[16];
592
  for(Int_t i=0;i<16;i++) {
593
    if(i<8) alpha[i] = atan(3.625*(7.5-i)/350.);
594
    if(i>7) alpha[i] = atan(3.625*(0.5+i-8)/350.);
595
  }
596

    
597
  cout << "angles for DL in strips SQY_L [Rad]" << endl;
598
  for(Int_t i=0;i<16;i++) {
599
    cout << alpha[i] << " ";
600
  }
601
  cout << endl;
602

    
603
  // angles for SQ20 strips
604
  Double_t betta[16];
605
  for(Int_t i=0;i<16;i++) {
606
    if(i<8) betta[i] = atan(3.125*(7.5-i)/339.5);
607
    if(i>7) betta[i] = atan(3.125*(0.5+i-8)/339.5);
608
  }
609

    
610
  cout << "angles for SQ20 strips [Rad]" << endl;
611
  for(Int_t i=0;i<16;i++) {
612
    cout << betta[i] << " ";
613
  }
614
  cout << endl;
615

    
616
//
617
  TH1* hSpectrum;
618

    
619
  Int_t peaksNumber,nopeaks;
620
  Double_t peak[peaksNumber];
621
  Double_t *peakPosition;
622
  Double_t *peakHight;
623
  Double_t *peakPositionS;//sorted peaks
624
  Double_t *peakHightS;
625

    
626
  Double_t minPos,minH;
627

    
628
  TList *functions;
629
  TPolyMarker *pm;
630
  TSpectrum sc;        //by default for 100 peaks
631

    
632
        Double_t fitMin, fitMax, fitStep;
633
  TF1 *fitAuto = new TF1("fitAuto", "gaus",0,10);
634
  
635
  Double_t upFitBorder,lowFitBorder,E[2],E0[2],nCh[2],th[2],thAv;
636
  upFitBorder = 0.5; lowFitBorder = 0.5;
637

    
638
  // initial alpha energies [MeV] 
639
  E0[0] = 7.6869;
640
  E0[1] = 6.0024;
641

    
642
  Int_t m;
643

    
644
  TH2F *hTh = new TH2F("hTh","thickness distr",16,0,15,15,0,14);
645
  for(Int_t i =0;i<16;i++) {
646
    for(Int_t j=0;j<15;j++) {
647
      hTh->SetBinContent(i,j,0);
648
    }
649
  }  
650

    
651

    
652
  TCanvas *c[16];
653
  TString cName,cTitle;
654
  Int_t nPad,nCan;
655
  for(Int_t i=0;i<16;i++) {
656
    cName.Form("c%d",i);
657
    cTitle.Form("eDepostis in %d strip",i); 
658
    c[i] = new TCanvas(cName.Data(),cTitle.Data(),1800,1000);
659
    c[i]->Divide(4,4);
660
  }
661

    
662
  
663
  for(Int_t i=0;i<16;i++) { // strips in SQ20 (0-15)
664
    for(Int_t nStr =0;nStr<15;nStr++) { // strips in SQY_L (0-14)
665

    
666
      cout << "#########" << endl << "SQ20 " << i << " SQY_L " << nStr << endl;
667
      //nCan = i/16;
668
      //nPad = i - nCan*16 + 1;
669
      c[i]->cd(nStr+1);
670
   /* }  
671
  return;
672
    TCanvas *c6 = new TCanvas("c6","Edeposit distr",1800,1000);
673
    c6->Divide(4,4);
674
    for(Int_t i=0;i<4;i++) {
675
      c6->cd(i+1);*/
676

    
677
      // обнуление
678
      fitMin = 0;
679
      fitMax = 0;
680
      peakPosition = NULL;
681
      peakHight = NULL;
682
      peakPositionS = NULL;
683
      peakHightS = NULL;
684

    
685
      // заполнение гистограмм через tree->Draw()
686
      hDraw.Form("NeEvent.SQY_L[%d]>>edep%d_%d(870,130,1000)",nStr,i,nStr);
687
      hname.Form("edep%d_%d",i,nStr);
688
      hcut.Form("NeEvent.SQY_R[%d]>200",i);
689
      ch0->Draw(hDraw.Data(),hcut.Data(),"");
690
      h[i] = (TH1F*)gPad->GetPrimitive(hname.Data());
691
      
692
      hSpectrum = h[i];
693
      if (!hSpectrum) {
694
        cout<< "raw spec was not found " << endl;
695
        return 1;
696
      }    
697
      
698
      // подсчёт количества пиков
699
      nopeaks = sc.Search(hSpectrum, 8, "goff", 0.35);
700
      if (nopeaks<2) {
701
        cout << " less than 2 peaks were found " << endl;
702
        return;
703
      }
704
      cout << nopeaks << endl;
705
      //functions = hSpectrum->GetListOfFunctions();
706
      //pm = (TPolyMarker*)functions->FindObject("TPolyMarker");
707

    
708
      pm = new TPolyMarker(nopeaks, sc.GetPositionX(), sc.GetPositionY());
709
      hSpectrum->GetListOfFunctions()->Add(pm);
710
      pm->SetMarkerStyle(23);
711
      pm->SetMarkerColor(kRed);
712
      pm->SetMarkerSize(1.3);
713
      
714
      peakPosition = pm->GetX();
715
      peakHight = pm->GetY();
716

    
717
      peakPositionS = new Double_t [pm->GetN()];
718
      peakHightS = new Double_t [pm->GetN()];
719

    
720
      for(Int_t a=0;a<pm->GetN();a++) {
721
        peakPositionS[a] = peakPosition[a];
722
        peakHightS[a] = peakHight[a];
723
      }
724

    
725
      //peakPositionS = pm->GetX();
726
      //peakHightS = pm->GetY();
727

    
728
     /* cout << "positions and heights " << endl;
729
      for(Int_t a=0;a<pm->GetN();a++) {
730
        cout << peakPosition[a] << " " << peakHight[a] << endl;
731
      }*/
732

    
733
      for (Int_t b = 0; b < pm->GetN(); ++b){ 
734
        for (Int_t a = 0; a < pm->GetN()-1; ++a){ 
735
          if(peakPositionS[a] < peakPositionS[a+1]) { 
736
              minPos = peakPositionS[a]; 
737
              minH = peakHightS[a]; 
738

    
739
              peakPositionS[a] = peakPositionS[a+1]; 
740
              peakHightS[a] = peakHightS[a+1];
741

    
742
              peakPositionS[a+1] = minPos; 
743
              peakHightS[a+1] = minH;
744
          }
745
        } 
746
      }  
747

    
748

    
749

    
750

    
751
      /*cout << "sorted positions and heights " << endl;
752
      for(Int_t a=0;a<pm->GetN();a++) {
753
        cout << peakPositionS[a] << " " << peakHightS[a] << endl;
754
      }*/
755

    
756

    
757
      fitStep = hSpectrum->GetXaxis()->GetBinWidth(0);
758
      
759
      //фитирование гауссом
760
      cout << "thickness!!" << endl; 
761
      for(Int_t k = 0; k < 2; k++) {
762
        //cout << peakPosition[k] << " " << peakHight[k] << endl;
763
        //fitting range:
764
              //shift a range of fit and search for raw boarder of peak determined by fUpperPeakRelativeHight
765
              //maximum
766
              m = 0;
767
              Double_t currentHight = peakHightS[k];
768
              while ( currentHight > (peakHightS[k]*upFitBorder) ) {
769
                      m++;
770
                      fitMax = static_cast<Double_t>(peakPositionS[k]) + m*fitStep;
771
                      currentHight = hSpectrum->GetBinContent(hSpectrum->GetXaxis()->FindBin(fitMax));
772
              }
773

    
774
              //minimum
775
              m = 0;
776
              currentHight = peakHightS[k];
777
              while ( currentHight > (peakHightS[k]*lowFitBorder) ) {
778
                      m++;
779
                      fitMin = static_cast<Double_t>(peakPositionS[k]) - m*fitStep;
780
                      currentHight = hSpectrum->GetBinContent(hSpectrum->GetXaxis()->FindBin(fitMin));
781
              }
782

    
783
        //fitting
784
        fitAuto->SetRange(fitMin,fitMax);
785
        hSpectrum->Fit("fitAuto", "R+", "", fitMin, fitMax);
786
        //cout << fitAuto->GetParameter(1) << " " ;
787
        nCh[k] = fitAuto->GetParameter(1);
788
        E[k] = nCh[k]*parYL2[nStr] + parYL1[nStr];
789

    
790
        //th[2],thAv
791
        th[k] = fAlphaSi.GetR(E0[k],E[k]);
792
        cout << nCh[k] << " " << E[k]<< " " << th[k] << endl;
793
      } // nopeaks == 2
794
      
795
      thAv = (th[0] + th[1])/2.;
796
      thAv = ( thAv - 0.2 - (2.44/cos(alpha[nStr])) )/cos(betta[i]);
797
      hTh->SetBinContent(i, nStr, thAv);
798
//SetBinContent(Int_t binx, Int_t biny, Double_t content)
799
      cout  << "THICKNESS"<< endl << thAv << endl;
800
  /*
801
      peakPosition = pm->GetX();
802
      peakHight = pm->GetY();
803
      for(Int_t j=0;j<nopeaks;j++) {
804
        cout << peakPosition[j] << " " <<  peakHight[j] << endl;
805

806

807
  for(Int_t i=0;i<2;i++) { // strips in SQ20 (0-15)
808
    for(Int_t nStr = 0;nStr<3;nStr++) { // strips in SQY_L (0-14)
809

810
      }*/
811

    
812
      c[i]->Update();
813
    } // nStr
814
  }// i
815
 
816
  // outfile
817
  TFile *fw = new TFile("pdf/thick.root", "RECREATE");
818
        if (fw->IsOpen() == 0) {
819
                Error("CalculateCalibParameters", "File %s was not created and won't be processed", "pdf/thick.root");
820
                return 1;
821
        }
822
  fw->cd();
823
  hTh->Write();
824
        fw->Close();
825

    
826
  // printing canvases
827
  TString fileName;
828
  for(Int_t i=0;i<16;i++){
829
    fileName.Form("pdf/SQ20_%d.png",i);
830
    c[i]->Print(fileName.Data());
831
  } 
832

    
833
  return;
834

    
835

    
836
  
837
}