data analysis scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2017 lines
54 KiB

  1. #include <TFile.h>
  2. #include <TTree.h>
  3. #include <stdio.h>
  4. #include <TH2D.h>
  5. #include <TH1D.h>
  6. #include <TProfile.h>
  7. #include <TCanvas.h>
  8. #include <TF1.h>
  9. #include <TMath.h>
  10. #include <vector>
  11. #include <utility>
  12. #include <TString.h>
  13. #include <TDirectory.h>
  14. #include <TString.h>
  15. #include <TVectorD.h>
  16. #include <TGraph.h>
  17. #include <TGraphErrors.h>
  18. #include <cmath>
  19. #include <TPaveStats.h>
  20. #include <TText.h>
  21. #include <TROOT.h>
  22. #include <TStyle.h>
  23. #include <TObjArray.h>
  24. #include <TLegend.h>
  25. using namespace std;
  26. double globalUpperBound = 63;
  27. double globalLowerBound = 1;
  28. double measurementsPerSecond = 3000;
  29. int Fit_Fail_Counts = 0;
  30. int signalScale = 1;
  31. int alignint = 1;
  32. int alignintIc = 1;
  33. double discardUpperThreshold = 0.01;
  34. double discardLowerThreshold = -0.015;
  35. Double_t fitfunc(Double_t *x, Double_t *par)
  36. {
  37. Double_t arg1 = 0;
  38. if (par[2] != 0)
  39. arg1 = (x[0] - par[1]) / par[2];
  40. Double_t fitval1 = par[0] * TMath::Exp(-0.5 * arg1 * arg1);
  41. Double_t arg2 = 0;
  42. if (par[4] != 0)
  43. arg2 = (x[0] - par[1]) / par[4];
  44. Double_t fitval2 = par[3] * TMath::Exp(-0.5 * arg2 * arg2);
  45. Double_t fitsum = par[5] + fitval1 + fitval2;
  46. return fitsum;
  47. }
  48. class Beammon
  49. {
  50. private:
  51. TTree *data;
  52. TTree *newdata;
  53. TBranch *b_time;
  54. TBranch *b_ic1;
  55. TBranch *b_ic2;
  56. TBranch *b_mw1_focusx;
  57. TBranch *b_mw1_focusy;
  58. TBranch *b_mw2_focusx;
  59. TBranch *b_mw2_focusy;
  60. TBranch *b_mw1_posx;
  61. TBranch *b_mw1_posy;
  62. TBranch *b_mw2_posx;
  63. TBranch *b_mw2_posy;
  64. TBranch *b_channels[64];
  65. TBranch *bb_ic1;
  66. TBranch *bb_ic2;
  67. TBranch *bb_mw1_focusx;
  68. TBranch *bb_mw2_focusx;
  69. TBranch *bb_mw1_posx;
  70. TBranch *bb_mw2_posx;
  71. TBranch *b_beamPosX_1;
  72. TBranch *b_beamFocusX_1;
  73. TBranch *b_beamSignal_1;
  74. TBranch *b_beamPosX_2;
  75. TBranch *b_beamFocusX_2;
  76. TBranch *b_beamSignal_2;
  77. TBranch *b_beamOn;
  78. double beamPosX_1;
  79. double beamFocusX_1;
  80. double beamSignal_1;
  81. double beamPosX_2;
  82. double beamFocusX_2;
  83. double beamSignal_2;
  84. double beamOn;
  85. double sigma_pos = 0.1;
  86. char *file;
  87. TFile *fileIn;
  88. TFile *fileOut;
  89. TObjArray objectArray;
  90. bool isSipm;
  91. bool firstMat;
  92. double channelWidth;
  93. double shift;
  94. long nevents;
  95. int nBaselineEvents;
  96. int nPrelimEvents;
  97. int highestChannelBin;
  98. int highestTimeBin;
  99. double beamOnAdcVal;
  100. double referenceIntegral;
  101. double beamOnLevel;
  102. double prelimMean;
  103. double prelimSigma;
  104. double highVal;
  105. double readOutFreq;
  106. int intTime;
  107. double chi2ndf;
  108. double fwhm;
  109. double mean_ave;
  110. double sigma_ave;
  111. double signalToParticles = 1; // multiply signal by this amount, so that mean of singal (photodiode) and icChamber distribution are the same
  112. double signalDistScale = 1; // icChamber signal distribution has more entries than signal (photodiode) one, so increase bin content in signal dist. by 1*this factor, to achieve the same integral
  113. double time;
  114. int startTime;
  115. int stopTime;
  116. int timesteps;
  117. double intSignal;
  118. int beamOnTime;
  119. double baselineIntegral;
  120. double ic1;
  121. double ic2;
  122. double mw1_focusx;
  123. double mw1_focusy;
  124. double mw2_focusx;
  125. double mw2_focusy;
  126. double mw2_posx;
  127. double mw2_posy;
  128. double mw1_posx;
  129. double mw1_posy;
  130. double icIntegral;
  131. double completeIntegral;
  132. double ic1_1;
  133. double ic2_1;
  134. double mw1_focusx_1;
  135. double mw2_focusx_1;
  136. double mw2_posx_1;
  137. double mw1_posx_1;
  138. vector<pair<int, int> > spillTimes;
  139. double channels[64];
  140. double baseline[64];
  141. double channelErrors[64];
  142. double current;
  143. TGraph *gChannelErrors;
  144. TH1D *meanHistOn;
  145. TH1D *widthHistOn;
  146. TH2D *timePosHist;
  147. TH1D *prelimBeam;
  148. TH2D *timeCenters;
  149. TH2D *timeCenters_MWPC;
  150. TH2D *timeWidths;
  151. TH2D *timeWidths_MWPC;
  152. TH1D *signalDist;
  153. TH1D *signalNoiseDist;
  154. TH1D *signalNoiseChannel;
  155. TH2D *simpleView;
  156. TH1D *timeIntSignal;
  157. TH1D *timeIntSignal_MWPC;
  158. TH1D *beamPosHist;
  159. TH1D *beamWidthHist;
  160. TH1D *icSignalDist;
  161. TGraphErrors *beamPosTimeGraph;
  162. TGraphErrors *signalTimeGraph;
  163. TGraphErrors *sigIcScatterGraph;
  164. TGraphErrors *mwPosScatterGraph;
  165. TH2D *sigIcScatterPlot[20];
  166. TH2D *sigIcScatterPlot_ic2;
  167. TH2D *mwPosScatterPlot[20];
  168. TH1D *ic1Hist;
  169. TH1D *ic2Hist;
  170. TH1D *signalComp;
  171. TH1D *positionComp;
  172. TH1D *widthComp;
  173. TH1D *mw1_focusxHist;
  174. TH1D *mw1_focusyHist;
  175. TH1D *mw2_focusxHist;
  176. TH1D *mw2_focusyHist;
  177. TH1D *mw1_posxHist;
  178. TH1D *mw1_posyHist;
  179. TH1D *mw2_posxHist;
  180. TH1D *mw2_posyHist;
  181. TH1D *channelSig[64];
  182. TH1D *chisquareDist;
  183. TH1D *mwPosX;
  184. TH1D *mwFocusX;
  185. TH1D *beamPosX;
  186. TH1D *beamFocusX;
  187. TH1D *sigHist;
  188. TH1D *sigHist_weighted;
  189. TH1D *fibreHist;
  190. TH1D *fibreHist_weighted;
  191. TH1D *mwpcHist;
  192. TH2D *Center_Signal;
  193. TH2D *Diff_timeCenters;
  194. TH2D *Diff_Signal;
  195. TH2D *Diff_ic1;
  196. TH1D *Diff_hist;
  197. TH2D *deltaSig_time;
  198. TH2D *deltaSig_Sig;
  199. TH1D *SlicesAvg;
  200. TH2D *SignalErrHist;
  201. void calcBaseline(bool useFirstEvents);
  202. void calcBaselineIntegral();
  203. void prelimProfile();
  204. TH1D *AverageHist(int dir, int timeStart = -1, int timeStop = -1);
  205. TH1D *TimeAverage(int timeStart, int timeStop);
  206. TH1D *TimeIntSignal(int timeStart = -1, int timeStop = -1);
  207. bool isAboveThreshold(TH1D *slice);
  208. bool isAboveThreshold(double val);
  209. bool isAboveThreshold(TGraphErrors *slice);
  210. TH1D *calcTimeCenters(int start, int stop);
  211. double calcBeamTime();
  212. // Tom Add
  213. public:
  214. Beammon();
  215. ~Beammon();
  216. void Initialize(const char *dirname, const char *filename, int baselineEvents = 1000, int prelimEvents = 10000, double beamLevel = 0.1, bool firstFibermat = true, double readOutFrequency = 1000., int integrationTime = 100);
  217. TH1D *GetPrelimProfile();
  218. void ProcessData();
  219. TH1D *GetChannelAvg(bool beamOn = false);
  220. TH1D *GetChannelSigDist(int channel);
  221. TH1D *GetTimeAvg();
  222. TH1D *GetTimeAvg(int nSpill);
  223. TH1D *GetTimeAvg(int nSpill, int nMeasurements);
  224. TH2D *GetTimeAvgScatter();
  225. TH2D *GetTimeAvgScatter(int nSpill, int nMeasurements);
  226. TH1D *GetChannelSlice(int time = -1, int offset = 0);
  227. TH1D *GetChannelSlice2(int time = -1, int offset = 0);
  228. TGraphErrors *GetChannelGraph(int time = -1, int offset = 0);
  229. TH1D *GetChannelGraphHist(int time, int offset);
  230. TH1D *GetTimeSlice(int channel = -1, int startTime = -1, int stopTime = -1, bool beamOn = false);
  231. TCanvas *GetTimeSliceCanvas(int channel = -1);
  232. TCanvas *GetSignalNoiseChannel(int channel = -1);
  233. TH1D *GetSignalOverTime();
  234. void GetTimeCenters();
  235. void GetTimeWidths();
  236. void PrintSpillTimes();
  237. pair<int, int> GetSpillTimes(int nSpill);
  238. TH2D *GetSpillCenters(int nSpill);
  239. TH1D *GetSpillSlice(int nSpill);
  240. TH1D *GetSignalDistribution();
  241. TCanvas *GetSignalDistributionCanvas();
  242. TH2D *GetSimpleView();
  243. TH1D *GetBaselineHist();
  244. TH1D *icSignalComp();
  245. double GetIntSignal();
  246. int GetBeamOnTime();
  247. double GetSignalStd();
  248. TCanvas *signalOverTime2d();
  249. TCanvas *FitSnapshot(int offset);
  250. TCanvas *FitAverage();
  251. TCanvas *PrintBaseline();
  252. TCanvas *PrintChannelNoise(int channel);
  253. TCanvas *PrintProfile(int offset);
  254. TCanvas *IcFitSlicesY();
  255. TCanvas *PrintSigIcGraph();
  256. TH1D *GetMwPosComp();
  257. void Save();
  258. void Close();
  259. TCanvas *GetSig_e_ic_Hist();
  260. TCanvas *GetSig_e_ic_Hist_Weighted();
  261. int Fit_Fail_Number = 200;
  262. int event_count = 0;
  263. };
  264. Beammon::Beammon() {}
  265. Beammon::~Beammon() {}
  266. void Beammon::Initialize(const char *dirname, const char *filename, int baselineEvents, int prelimEvents, double beamLevel, bool firstFibermat, double readOutFrequency, int integrationTime)
  267. {
  268. signalToParticles = 7937 / 1.443e4; // FOR RUN 26 only
  269. signalDistScale = 1.; //44585./26225;// FOR RUN 26 ONLY
  270. intTime = integrationTime;
  271. readOutFreq = readOutFrequency;
  272. firstMat = firstFibermat;
  273. highestChannelBin = 20;
  274. highestTimeBin = 0;
  275. icIntegral = 0;
  276. completeIntegral = 0;
  277. TString fn = TString(filename);
  278. TString dn = TString(dirname);
  279. if (fn.Contains("sipm"))
  280. {
  281. channelWidth = 0.25;
  282. isSipm = true;
  283. }
  284. else
  285. {
  286. channelWidth = 0.8;
  287. isSipm = false;
  288. }
  289. TString inName = dn + "pin/" + fn;
  290. fileIn = new TFile(inName, "UPDATE");
  291. fileIn->GetObject("t", data);
  292. TString outname = filename;
  293. if (firstMat)
  294. outname.Insert(0, "SAVE_");
  295. else
  296. outname.Insert(0, "SAVE_mat2_");
  297. outname.Insert(0, dirname);
  298. data->SetBranchStatus("ch*", 0);
  299. fileOut = new TFile(outname.Data(), "RECREATE");
  300. newdata = new TTree("newdata", "newdata");
  301. data->SetBranchStatus("ch*", 1);
  302. b_time = data->GetBranch("time");
  303. b_time->SetAddress(&time);
  304. for (int n = 0; n < 64; n++)
  305. {
  306. baseline[n] = 0.;
  307. char bname[20];
  308. if (firstMat)
  309. {
  310. sprintf(bname, "ch%02d", n);
  311. b_channels[n] = data->GetBranch(bname);
  312. b_channels[n]->SetAddress(&channels[n]);
  313. }
  314. else
  315. {
  316. sprintf(bname, "ch%d", n + 64);
  317. printf("%s\n", bname);
  318. b_channels[n] = data->GetBranch(bname);
  319. b_channels[n]->SetAddress(&channels[63 - n]);
  320. }
  321. }
  322. b_ic1 = data->GetBranch("ic1");
  323. b_ic1->SetAddress(&ic1);
  324. b_ic2 = data->GetBranch("ic2");
  325. b_ic2->SetAddress(&ic2);
  326. b_mw1_focusx = data->GetBranch("mw1_focusx");
  327. b_mw1_focusx->SetAddress(&mw1_focusx);
  328. b_mw1_focusy = data->GetBranch("mw1_focusy");
  329. b_mw1_focusy->SetAddress(&mw1_focusy);
  330. b_mw2_focusx = data->GetBranch("mw2_focusx");
  331. b_mw2_focusx->SetAddress(&mw2_focusx);
  332. b_mw2_focusy = data->GetBranch("mw2_focusy");
  333. b_mw2_focusy->SetAddress(&mw2_focusy);
  334. b_mw1_posx = data->GetBranch("mw1_posx");
  335. b_mw1_posx->SetAddress(&mw1_posx);
  336. b_mw1_posy = data->GetBranch("mw1_posy");
  337. b_mw1_posy->SetAddress(&mw1_posy);
  338. b_mw2_posx = data->GetBranch("mw2_posx");
  339. b_mw2_posx->SetAddress(&mw2_posx);
  340. b_mw2_posy = data->GetBranch("mw2_posy");
  341. b_mw2_posy->SetAddress(&mw2_posy);
  342. nevents = data->GetEntries();
  343. startTime = 0;
  344. stopTime = nevents - 1;
  345. nBaselineEvents = baselineEvents;
  346. nPrelimEvents = prelimEvents;
  347. beamOnLevel = beamLevel;
  348. beamOnAdcVal = 0;
  349. beamOnTime = 0;
  350. calcBaseline(false); //baseline from end of events
  351. calcBaselineIntegral();
  352. ProcessData();
  353. }
  354. void Beammon::Save()
  355. {
  356. fileIn->Close();
  357. fileOut->cd();
  358. for (int i = 0; i < 5; i++){
  359. PrintProfile(i)->Write();
  360. PrintProfile(i)->Delete();
  361. }
  362. FitAverage()->Write();
  363. FitAverage()->Delete();;
  364. PrintBaseline()->Write();
  365. PrintBaseline()->Delete();//added
  366. //for(int i=0;i<64;i++)
  367. // PrintChannelNoise(i)->Write();
  368. GetSignalOverTime()->Write();
  369. GetSignalOverTime()->Delete();
  370. for (int i = 0; i < 5; i++){
  371. GetChannelSlice2(-1, i)->Write();
  372. GetChannelSlice2(-1, i)->Delete();
  373. }
  374. //fileIn->Close();
  375. GetTimeSliceCanvas(-1)->Write();
  376. timePosHist->ProjectionX()->Write();
  377. GetTimeCenters();
  378. GetSignalDistributionCanvas()->Write();
  379. GetSignalDistribution()->Write();
  380. GetSignalNoiseChannel()->Write();
  381. chisquareDist->Write();
  382. sigIcScatterGraph->Write();
  383. mwPosScatterGraph->Write();
  384. for (int i = 6; i < 15; i++)
  385. {
  386. sigIcScatterPlot[i]->Write();
  387. mwPosScatterPlot[i]->Write();
  388. }
  389. mwPosScatterPlot[11]->Write();
  390. sigIcScatterPlot[11]->Write();
  391. mw1_focusxHist->Write();
  392. mw1_posxHist->Write();
  393. mwPosX->Write();
  394. mwFocusX->Write();
  395. beamPosX->Write();
  396. beamFocusX->Write();
  397. TVectorD v(6);
  398. v[0] = GetIntSignal();
  399. v[1] = GetBeamOnTime();
  400. v[2] = signalDist->GetStdDev();
  401. v[3] = icIntegral;
  402. v[4] = completeIntegral;
  403. v[5] = fwhm;
  404. v.Write("beaminfo");
  405. beamPosTimeGraph->Write();
  406. signalTimeGraph->Write();
  407. gChannelErrors->Write();
  408. ic1Hist->Write();
  409. ic2Hist->Write();
  410. sigHist->Write();
  411. fibreHist->Write();
  412. mwpcHist->Write();
  413. fibreHist_weighted->Write();
  414. sigHist_weighted->Write();
  415. GetSig_e_ic_Hist()->Write();
  416. GetSig_e_ic_Hist_Weighted()->Write();
  417. signalComp->Write();
  418. GetMwPosComp()->Write();
  419. widthComp->Write();
  420. icSignalComp()->Write();
  421. signalOverTime2d();
  422. IcFitSlicesY();
  423. PrintSigIcGraph()->Write();
  424. //GetTimeCenters();
  425. newdata->AutoSave();
  426. Close();
  427. // v.Delete();
  428. }
  429. void Beammon::calcBaseline(bool useFirstEvents)
  430. {
  431. // use the first/last nBaselineEvents events to calculate the average/baseline
  432. if (!useFirstEvents)
  433. { // last
  434. for (int i = nevents - nBaselineEvents; i < nevents; i++)
  435. {
  436. data->GetEvent(i);
  437. for (int ch = 0; ch < 64; ch++)
  438. {
  439. baseline[ch] += channels[ch] / nBaselineEvents;
  440. }
  441. }
  442. }
  443. else
  444. {
  445. for (int i = 1; i <= nBaselineEvents; i++)
  446. {
  447. data->GetEvent(i);
  448. for (int ch = 0; ch < 64; ch++)
  449. {
  450. baseline[ch] += channels[ch] / nBaselineEvents;
  451. }
  452. }
  453. }
  454. }
  455. void Beammon::calcBaselineIntegral()
  456. {
  457. /*baselineIntegral = 0;
  458. for(int i=1; i <= nBaselineEvents;i++){
  459. data->GetEvent(i);
  460. double currentIntegral = 0;
  461. for(int ch=0;ch<64;ch++){
  462. currentIntegral += channels[ch] - baseline[ch];
  463. }
  464. if(currentIntegral > baselineIntegral)
  465. baselineIntegral = currentIntegral;
  466. }
  467. printf("baseline int: %f \n",baselineIntegral);*/
  468. for (int i = 1; i <= nBaselineEvents; i++)
  469. {
  470. data->GetEvent(i);
  471. for (int ch = 0; ch < 64; ch++)
  472. channelSig[ch]->Fill(channels[ch] - baseline[ch]);
  473. }
  474. for (int k = 0; k < 64; k++)
  475. {
  476. double stdDev = channelSig[k]->GetStdDev();
  477. channelErrors[k] = stdDev;
  478. }
  479. }
  480. void Beammon::ProcessData()
  481. {
  482. bool beamOn = false;
  483. bool previousBeamStatus = false;
  484. int lastSpillOn = 0;
  485. int lastSpillOff = 0;
  486. int minSpillTime = 3000; // in ms
  487. int histChannel;
  488. int highestSigVal = 0;
  489. for (int i = 1; i < nevents; i++)
  490. {
  491. if (i % 10000 == 00)
  492. printf("Event: %d / %ld \n", i, nevents);
  493. // cout << "Event: " << i << endl;
  494. data->GetEvent(i);
  495. double maxSignal = channels[highestChannelBin] - baseline[highestChannelBin];
  496. /*
  497. for(int j=0;j<64;j++){
  498. double val = channels[j] - baseline[j];
  499. if (val > maxSignal)
  500. maxSignal = val;
  501. }*/
  502. if (isAboveThreshold(maxSignal) && (i > nBaselineEvents))
  503. {
  504. beamOn = true;
  505. if ((i - lastSpillOff) > minSpillTime && !previousBeamStatus)
  506. {
  507. if (lastSpillOff != 0)
  508. spillTimes.push_back(make_pair(lastSpillOn, lastSpillOff));
  509. lastSpillOn = i;
  510. }
  511. }
  512. else
  513. {
  514. beamOn = false;
  515. if (previousBeamStatus)
  516. lastSpillOff = i;
  517. }
  518. previousBeamStatus = beamOn;
  519. int sigVal = 0;
  520. for (int j = 0; j < 64; j++)
  521. {
  522. int chan;
  523. if (isSipm)
  524. {
  525. if (j % 2 == 0)
  526. histChannel = 64 - (j + 1);
  527. else
  528. histChannel = 64 - (j - 1);
  529. }
  530. else
  531. histChannel = j + 1;
  532. double chVal = channels[j] - baseline[j];
  533. sigVal += chVal;
  534. timePosHist->SetBinContent(histChannel, i, chVal);
  535. completeIntegral += chVal;
  536. }
  537. if (sigVal > highestSigVal)
  538. {
  539. highestSigVal = sigVal;
  540. highestTimeBin = i;
  541. }
  542. // cout << ic1 << " " << mw1_focusx << " " << mw1_focusy << endl;
  543. // IONISATION CHAMBERS
  544. double chargeToParts = 1. / 3.2886706587037837e-15 * 1e-9 * 312e-6;
  545. // if (!(ic1 != ic1))
  546. // ic1Hist->SetBinContent(i, ic1 * chargeToParts);
  547. // if (!(ic2 != ic2))
  548. // ic2Hist->SetBinContent(i, ic2 * chargeToParts);
  549. spillTimes.push_back(make_pair(lastSpillOn, lastSpillOff));
  550. TH1D *tmp = GetChannelSlice(-1);
  551. tmp->Fit("gaus");
  552. TF1 *fit = tmp->GetFunction("gaus");
  553. referenceIntegral = fit->GetParameter(0); // this is the constant in front of the gaus!! The real integral is constant*sigma*sqrt(2*pi), but because sigma is the same in one measurement it doesn't matter!
  554. signalDist = new TH1D("signalDist", "signal distribution", 200, 0., referenceIntegral * 2.51 * fit->GetParameter(2));
  555. signalNoiseDist = new TH1D("signalNoiseDist", "", 200, 0., referenceIntegral * 2.51 * fit->GetParameter(2));
  556. signalNoiseChannel = new TH1D("signalNoiseChannel", "", 200, 0., tmp->GetMaximum());
  557. calcBeamTime();
  558. printf("HighestBin: %d \n", highestTimeBin);
  559. }
  560. TH1D *Beammon::GetPrelimProfile()
  561. {
  562. if (!prelimBeam)
  563. {
  564. printf("WARNING: preliminary profile not yet generated");
  565. }
  566. return prelimBeam;
  567. }
  568. TH1D *Beammon::TimeAverage(int timeStart, int timeStop)
  569. {
  570. if (timeStart == -1)
  571. timeStart = startTime;
  572. if (timeStop == -1)
  573. timeStop = stopTime;
  574. int count = 0;
  575. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  576. TH1D *tmp;
  577. printf("\n\n start: %d, stop: %d \n\n", timeStart, timeStop);
  578. for (int i = timeStart; i <= timeStop; i++)
  579. {
  580. tmp = GetChannelSlice(i, 0);
  581. if (isAboveThreshold(tmp))
  582. {
  583. res->Add(tmp, 1.);
  584. count++;
  585. }
  586. delete tmp;
  587. }
  588. if (count)
  589. {
  590. for (int n = 1; n <= 64; n++)
  591. {
  592. res->SetBinContent(n, res->GetBinContent(n) / count);
  593. }
  594. return res;
  595. }
  596. else
  597. return 0;
  598. }
  599. TH1D *Beammon::TimeIntSignal(int timeStart, int timeStop)
  600. {
  601. if (timeStart == -1)
  602. timeStart = startTime;
  603. if (timeStop == -1)
  604. timeStop = stopTime;
  605. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  606. TH1D *tmp;
  607. for (int i = timeStart; i < timeStop; i++)
  608. {
  609. tmp = GetChannelSlice(i, 0);
  610. if (isAboveThreshold(tmp))
  611. {
  612. res->Add(tmp, 1.);
  613. beamOnTime++;
  614. }
  615. delete tmp;
  616. }
  617. return res;
  618. }
  619. TH1D *Beammon::AverageHist(int dir, int timeStart, int timeStop)
  620. { // dir = 0 => x direction, 1 => y
  621. TH2D *hist = timePosHist;
  622. int nbins1, nbins2;
  623. double binStart, binStop;
  624. int firstBin, secondBin;
  625. if (!dir)
  626. {
  627. // Timeaverage
  628. if ((timeStart != -1) && (timeStop != -1))
  629. {
  630. firstBin = timeStart; //hist->GetYaxis()->FindBin((timeStart-startTime)/readOutFreq);
  631. secondBin = timeStop; //hist->GetYaxis()->FindBin((timeStop-startTime)/readOutFreq);
  632. nbins1 = hist->GetNbinsX();
  633. nbins2 = secondBin - firstBin;
  634. binStart = hist->GetXaxis()->GetBinCenter(1);
  635. binStop = hist->GetXaxis()->GetBinCenter(nbins1);
  636. }
  637. else
  638. {
  639. nbins1 = hist->GetNbinsX();
  640. nbins2 = hist->GetNbinsY();
  641. binStart = hist->GetXaxis()->GetBinCenter(1);
  642. binStop = hist->GetXaxis()->GetBinCenter(nbins1);
  643. firstBin = 1;
  644. secondBin = nbins2;
  645. }
  646. }
  647. else
  648. {
  649. nbins1 = hist->GetNbinsY();
  650. nbins2 = hist->GetNbinsX();
  651. binStart = hist->GetYaxis()->GetBinCenter(1);
  652. binStop = hist->GetYaxis()->GetBinCenter(nbins1);
  653. firstBin = 1;
  654. secondBin = hist->GetNbinsX();
  655. }
  656. TH1D *retHist = new TH1D("average", "average", nbins1, binStart, binStop);
  657. for (int i = 1; i <= nbins1; i++)
  658. {
  659. int count = 0;
  660. double avg = 0;
  661. for (int j = firstBin; j <= secondBin; j++)
  662. {
  663. if (!dir)
  664. {
  665. /*bool beam = beamOnVector[j];
  666. if(beam==false)
  667. continue;
  668. //else
  669. //printf("TRUE");*/
  670. avg += hist->GetBinContent(i, j);
  671. }
  672. else
  673. avg += hist->GetBinContent(j, i);
  674. count++;
  675. }
  676. if (count > 0)
  677. avg /= count;
  678. retHist->SetBinContent(i, avg);
  679. }
  680. return retHist;
  681. }
  682. TH1D *Beammon::GetTimeAvg()
  683. {
  684. int n = spillTimes.size();
  685. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  686. for (int i = 0; i < n; i++)
  687. {
  688. TH1D *tmp = TimeAverage(spillTimes[i].first, spillTimes[i].second);
  689. if (!tmp)
  690. printf("\n\n timeAvgHist is Null! \n\n");
  691. if (tmp)
  692. res->Add(tmp, 1. / n);
  693. }
  694. res->SetTitle("average beam profile");
  695. return res;
  696. }
  697. TH1D *Beammon::GetTimeAvg(int nSpill)
  698. {
  699. if (nSpill >= spillTimes.size())
  700. return 0;
  701. TH1D *res = TimeAverage(spillTimes[nSpill].first, spillTimes[nSpill].second);
  702. res->SetTitle("average beam profile");
  703. return res;
  704. }
  705. TH1D *Beammon::GetTimeAvg(int nSpill, int nMeasurements)
  706. {
  707. if (nSpill >= spillTimes.size())
  708. return 0;
  709. TH1D *res = TimeAverage(spillTimes[nSpill].second - nMeasurements, spillTimes[nSpill].second);
  710. TString title = TString::Format("average beam profile, %d ms, spill %d", nMeasurements, nSpill);
  711. TString name = TString::Format("avg%dmsspill%d", nMeasurements, nSpill);
  712. res->SetTitle(title.Data());
  713. res->SetName(name.Data());
  714. return res;
  715. }
  716. TH2D *Beammon::GetTimeAvgScatter(int nSpill, int nMeasurements)
  717. {
  718. int tStart = spillTimes[nSpill].first;
  719. if (nMeasurements == -1)
  720. tStart = spillTimes[nSpill].second - nMeasurements;
  721. TH2D *res = new TH2D("tavgScatter", "tavgScatter", 64, 0.5, 64.5, 400, 0., highVal);
  722. for (int i = spillTimes[nSpill].first; i < spillTimes[nSpill].second; i++)
  723. {
  724. TH1D *tmp = GetChannelSlice(i, 0);
  725. if (!isAboveThreshold(tmp))
  726. continue;
  727. for (int bin = 1; bin <= 64; bin++)
  728. {
  729. res->Fill(bin, tmp->GetBinContent(bin));
  730. }
  731. delete tmp;
  732. }
  733. TString title = TString::Format("beam profile, %d ms, spill %d", nMeasurements, nSpill);
  734. res->SetTitle(title.Data());
  735. return res;
  736. }
  737. TH2D *Beammon::GetTimeAvgScatter()
  738. {
  739. TH2D *res = new TH2D("tavgScatter", "tavgScatter", 64, 0.5, 64.5, 400, 0., highVal);
  740. int n = spillTimes.size();
  741. for (int i = 0; i < n; i++)
  742. {
  743. res->Add(GetTimeAvgScatter(i, -1), 1. / n);
  744. }
  745. return res;
  746. }
  747. TH1D *Beammon::GetChannelSigDist(int channel)
  748. {
  749. TString sName = TString("signal distribution channel ") + TString(channel);
  750. double maxAmp = 0;
  751. for (int n = 0; n < spillTimes.size(); n++)
  752. {
  753. TH1D *tmp = GetSpillSlice(n);
  754. if (tmp->GetMaximum() > maxAmp)
  755. maxAmp = tmp->GetMaximum();
  756. delete tmp;
  757. }
  758. TH1D *res = new TH1D(sName.Data(), sName.Data(), 200, 0., maxAmp);
  759. for (int n = 0; n < spillTimes.size(); n++)
  760. {
  761. TH1D *tmp = GetSpillSlice(n);
  762. for (int bin = 1; bin <= tmp->GetNbinsX(); bin++)
  763. {
  764. res->Fill(tmp->GetBinContent(bin));
  765. //printf("bin: %d, amp: %f \n", bin, tmp->GetBinContent(bin));
  766. }
  767. delete tmp;
  768. }
  769. return res;
  770. }
  771. TH1D *Beammon::GetChannelAvg(bool beamOn)
  772. {
  773. return AverageHist(1, -1, -1);
  774. }
  775. TH1D *Beammon::GetTimeSlice(int channel, int timeStart, int timeStop, bool beamOn)
  776. {
  777. if (timeStart == -1)
  778. timeStart = startTime;
  779. if (timeStop == -1)
  780. timeStop = stopTime;
  781. int nbins = timeStop - timeStart;
  782. TH1D *tmp2 = new TH1D("ts", "timeslice", nbins, (timeStart - startTime) / readOutFreq, (timeStop - startTime) / readOutFreq);
  783. if (channel == -1)
  784. channel = highestChannelBin;
  785. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  786. int offset = tmp->GetXaxis()->FindBin(timeStart);
  787. for (int bin = 1; bin <= nbins; bin++)
  788. {
  789. tmp2->SetBinContent(bin, tmp->GetBinContent(bin + offset));
  790. }
  791. TString title = TString::Format("time projection of channel %d", channel);
  792. tmp2->SetTitle(title.Data());
  793. return tmp2;
  794. }
  795. TCanvas *Beammon::GetTimeSliceCanvas(int channel)
  796. {
  797. TCanvas *c1 = new TCanvas();
  798. c1->SetName("timeslice");
  799. int nbins = stopTime - startTime;
  800. TH1D *tmp2 = new TH1D("timeslice", "", nbins, 0., (stopTime - startTime) / readOutFreq);
  801. if (channel == -1)
  802. channel = highestChannelBin;
  803. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  804. for (int bin = 1; bin <= nbins; bin++)
  805. {
  806. tmp2->SetBinContent(bin, tmp->GetBinContent(bin));
  807. }
  808. tmp2->GetXaxis()->SetTitle("times [s]");
  809. TString label;
  810. label.Form("adc counts / %d #mus", intTime);
  811. tmp2->GetYaxis()->SetTitle(label.Data());
  812. tmp2->Draw("P");
  813. return c1;
  814. }
  815. TCanvas *Beammon::GetSignalNoiseChannel(int channel)
  816. {
  817. TCanvas *c1 = new TCanvas();
  818. c1->SetName("signalNoiseChannel");
  819. int nbins = stopTime - startTime;
  820. if (channel == -1)
  821. channel = highestChannelBin;
  822. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  823. for (int bin = 1; bin <= nbins; bin++)
  824. {
  825. signalNoiseChannel->Fill(tmp->GetBinContent(bin));
  826. }
  827. signalNoiseChannel->GetYaxis()->SetTitle("entries");
  828. TString label;
  829. label.Form("adc counts / %d #mus", intTime);
  830. signalNoiseChannel->GetXaxis()->SetTitle(label.Data());
  831. signalNoiseChannel->Draw();
  832. return c1;
  833. }
  834. TH1D *Beammon::GetChannelSlice(int time, int offset)
  835. {
  836. if (time == -1)
  837. time = highestTimeBin + offset;
  838. //printf("time: %d\n", time);
  839. TH1D *res = timePosHist->ProjectionX("", time, time); //timePosHist->ProjectionX("",timePosHist->GetYaxis()->FindBin(time+offset),timePosHist->GetYaxis()->FindBin(time+offset));
  840. //res->SetError(channelErrors);
  841. res->SetTitle("single measurement (1ms)");
  842. res->SetName("Channel_" + (TString)time);
  843. res->Fit("gaus", "Q0");
  844. double mean = res->GetFunction("gaus")->GetParameter(1);
  845. double sigma = res->GetFunction("gaus")->GetParameter(2);
  846. return res;
  847. }
  848. TH1D *Beammon::GetChannelSlice2(int time, int offset)
  849. {
  850. if (time == -1)
  851. time = highestTimeBin + offset;
  852. //printf("time: %d\n", time);
  853. TH1D *res = timePosHist->ProjectionX("", time, time); //timePosHist->ProjectionX("",timePosHist->GetYaxis()->FindBin(time+offset),timePosHist->GetYaxis()->FindBin(time+offset));
  854. //res->SetError(channelErrors);
  855. res->SetTitle("single measurement (1ms)");
  856. res->SetName("Channel_" + (TString)time);
  857. res->Fit("gaus", "Q0");
  858. double mean = res->GetFunction("gaus")->GetParameter(1);
  859. mean_ave += mean;
  860. double sigma = res->GetFunction("gaus")->GetParameter(2);
  861. sigma_ave += sigma;
  862. return res;
  863. }
  864. TGraphErrors *Beammon::GetChannelGraph(int time, int offset)
  865. {
  866. if (time == -1)
  867. time = highestTimeBin + offset;
  868. double xErr = 0.8 / sqrt(12);
  869. TH1D *res = timePosHist->ProjectionX("", time, time);
  870. TGraphErrors *graph = new TGraphErrors(64);
  871. for (int ch = 1; ch <= 64; ch++)
  872. {
  873. graph->SetPoint(ch, ch, res->GetBinContent(ch));
  874. // graph->SetPointError(ch,xErr,channelErrors[ch-1]);//sqrt(channelErrors[ch-1]*channelErrors[ch-1]+res->GetBinContent(ch)));
  875. graph->SetPointError(ch, xErr, channelErrors[ch - 1]);
  876. }
  877. delete res;
  878. return graph;
  879. }
  880. TH1D *Beammon::GetChannelGraphHist(int time, int offset)
  881. {
  882. if (time == -1)
  883. time = highestTimeBin + offset;
  884. double xErr = 0.8 / sqrt(12);
  885. TH1D *res = timePosHist->ProjectionX("", time, time);
  886. return res;
  887. }
  888. TH1D *Beammon::GetSignalOverTime()
  889. {
  890. TH1D *tmp = timePosHist->ProjectionY("", 1, 64);
  891. tmp->SetTitle("signal over time");
  892. tmp->SetName("signal over time");
  893. return tmp;
  894. }
  895. void Beammon::PrintSpillTimes()
  896. {
  897. printf("%lu spills recorded: \n", spillTimes.size());
  898. // cout << spillTimes.size() << "spills recorded: " << endl;
  899. for (int n = 0; n < spillTimes.size(); n++)
  900. printf("%02d: %f - %f \n", n, (spillTimes[n].first - startTime) / readOutFreq, (spillTimes[n].second - startTime) / readOutFreq);
  901. }
  902. TH1D *Beammon::calcTimeCenters(int start, int stop)
  903. {
  904. int tStart = start;
  905. int tStop = stop;
  906. if (start == -1)
  907. tStart = startTime;
  908. if (stop == -1)
  909. tStop = stopTime;
  910. int nbins = (tStop - tStart);
  911. double mean = 0;
  912. double sigma = 0;
  913. double constant;
  914. TH1D *res = new TH1D("timeIntSignal_tmp", "time integrated signal", 64, 0.5, 64.5);
  915. TCanvas *tmpcanvas = new TCanvas();
  916. int offset = tStart;
  917. TF1 *fit = new TF1("gaus_L", "gaus+[3]", 1, 64);
  918. TF1 *fit2 = new TF1("gaus_2", "gaus", 1, 64);
  919. fit2->SetParameter(0, 2000);
  920. fit2->SetParameter(1, mean_ave);
  921. fit2->SetParameter(2, sigma_ave);
  922. for (int bin = tStart; bin <= tStop; bin++)
  923. {
  924. TGraphErrors *tmp = GetChannelGraph(bin, 0);
  925. TH1D *tmp2 = GetChannelGraphHist(bin, 0);
  926. tmp->Fit("gaus_2", "Q0", "", mean_ave - 3 * sigma_ave, mean_ave + 3 * sigma_ave);
  927. TF1 *func = tmp->GetFunction("gaus_2");
  928. if (tmp2->GetMean() * 0.8 > 0)
  929. fibreHist_weighted->SetBinContent(bin + 1, tmp2->GetMean() * 0.8);
  930. else
  931. fibreHist_weighted->SetBinContent(bin + 1, 0);
  932. if (tmp2->GetEntries() > 0)
  933. sigHist_weighted->SetBinContent(bin - 1, tmp2->GetEntries() * signalToParticles);
  934. else
  935. sigHist_weighted->SetBinContent(bin - 1, 0);
  936. if (func)
  937. {
  938. if (!isAboveThreshold(tmp))
  939. continue;
  940. constant = func->GetParameter(0);
  941. mean = func->GetParameter(1) * channelWidth;
  942. sigma = func->GetParameter(2) * channelWidth;
  943. if (mean <= 0 || mean > 64)
  944. continue;
  945. /*if (constant > 1e5 || mean > (mean_ave * channelWidth + 0.2) || mean < (mean_ave * channelWidth - 0.2))
  946. {
  947. fit->SetParameter(0, 1000);
  948. fit->SetParameter(1, mean_ave);
  949. fit->SetParameter(2, sigma_ave);
  950. fit->SetParameter(3, 0.1);
  951. fit->SetParLimits(0, 100, 2e5);
  952. fit->SetParLimits(1, 1, 64);
  953. fit->SetParLimits(2, 1, 10);
  954. fit->SetParLimits(3, -50, 50);
  955. tmp->Fit("gaus", "Q0", "", mean_ave - 3 * sigma_ave, mean_ave + 3 * sigma_ave);
  956. func = tmp->GetFunction("gaus");
  957. constant = func->GetParameter(0);
  958. mean = func->GetParameter(1) * channelWidth;
  959. sigma = func->GetParameter(2) * channelWidth;
  960. }*/
  961. if (abs(constant) > 1e5)
  962. {
  963. Fit_Fail_Number = Fit_Fail_Number - 1;
  964. if (Fit_Fail_Number > 0)
  965. {
  966. tmpcanvas->cd();
  967. tmp->Draw("AP");
  968. func->Draw("SAME");
  969. tmpcanvas->Modified();
  970. tmpcanvas->Write();
  971. }
  972. continue;
  973. }
  974. //Fill the error hidto
  975. double erry, tmpx;
  976. bool discard = false;
  977. int nPoint = 0;
  978. for (int chn = 1; chn < 65; chn++)
  979. {
  980. tmp->GetPoint(nPoint, tmpx, erry);
  981. SignalErrHist->Fill((double)tmpx, (erry - func->Eval(tmpx)) / (constant * sigma));
  982. if ((erry - func->Eval(tmpx)) / (constant * sigma) > discardUpperThreshold || (erry - func->Eval(tmpx)) / (constant * sigma) < discardLowerThreshold)
  983. {
  984. tmp->RemovePoint(nPoint);
  985. nPoint--;
  986. discard = true;
  987. }
  988. nPoint++;
  989. }
  990. double signal = sigma * 2.51 * constant * signalToParticles;
  991. //Fill the new tree
  992. event_count++;
  993. //data->GetEntry(bin);
  994. beamPosX_1 = mean;
  995. beamFocusX_1 = 2.35 * sigma;
  996. beamSignal_1 = signal;
  997. ic1_1 = ic1Hist->GetBinContent(bin - alignintIc);
  998. ic2_1 = ic2Hist->GetBinContent(bin - alignintIc);
  999. mw1_focusx_1 = mw1_focusxHist->GetBinContent(bin - alignint);
  1000. mw1_posx_1 = mw1_posxHist->GetBinContent(bin - alignint);
  1001. mw2_focusx_1 = mw2_focusxHist->GetBinContent(bin - alignint);
  1002. mw2_posx_1 = mw2_posxHist->GetBinContent(bin - alignint);
  1003. beamOn = 1;
  1004. newdata->Fill();
  1005. if (event_count % 5000 == 0)
  1006. {
  1007. printf("Signal events: %d ", event_count);
  1008. printf("Event number: %d\n", bin);
  1009. }
  1010. if (signal < 0 || signal != signal)
  1011. continue;
  1012. //Fill the Histograms
  1013. beamPosX->Fill(mean);
  1014. fibreHist->Fill(bin, mean);
  1015. mwpcHist->Fill(bin, mw1_posxHist->GetBinContent(bin - alignint));
  1016. beamFocusX->Fill(2.35 * sigma);
  1017. beamPosTimeGraph->SetPoint(bin, bin, mean);
  1018. beamPosTimeGraph->SetPointError(bin, 0., func->GetParError(1));
  1019. timeCenters->Fill((bin) / readOutFreq, mean);
  1020. timeWidths->Fill((bin) / readOutFreq, sigma * 2.35);
  1021. timeCenters_MWPC->Fill((bin) / readOutFreq, mw1_posxHist->GetBinContent(bin - alignint));
  1022. timeWidths_MWPC->Fill((bin) / readOutFreq, mw1_focusxHist->GetBinContent(bin - alignint));
  1023. Diff_timeCenters->Fill((bin) / readOutFreq, mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1024. if (mw1_posxHist->GetBinContent(bin - alignint) < 20)
  1025. {
  1026. mwPosScatterGraph->SetPoint(mwPosScatterGraph->GetN(), mw1_posxHist->GetBinContent(bin - alignint), mean);
  1027. //positionComp->Fill(mean - mw1_posxHist->GetBinContent(bin-4) +111.5415 - 32.02);
  1028. for (int i = 0; i < 20; i++)
  1029. mwPosScatterPlot[i]->Fill(mw1_posxHist->GetBinContent(bin - i + 10), mean);
  1030. }
  1031. double sigError = 2.51 * sqrt(sigma * sigma * func->GetParError(0) * func->GetParError(0) + constant * constant * func->GetParError(2) * func->GetParError(2)) * signalToParticles;
  1032. if (signal > 1e5 || sigError > signal)
  1033. {
  1034. //signal = 0;
  1035. sigError = 0;
  1036. }
  1037. if (signal)
  1038. {
  1039. signalDist->Fill(signal);
  1040. if (ic1Hist->GetBinContent(bin - alignintIc))
  1041. {
  1042. sigHist->Fill(bin - alignintIc - 1, signal);
  1043. signalComp->Fill((signal - ic1Hist->GetBinContent(bin - alignintIc)) / signal);
  1044. double x, y, totalSignal = 0, signalError = 0;
  1045. for (int k = 0; k < 63; k++)
  1046. {
  1047. tmp->GetPoint(k, x, y);
  1048. signalError += tmp->GetErrorY(k) * tmp->GetErrorY(k);
  1049. totalSignal += y * signalToParticles;
  1050. }
  1051. signalError = sqrt(signalError) * signalToParticles;
  1052. sigIcScatterGraph->SetPoint(sigIcScatterGraph->GetN(), ic1Hist->GetBinContent(bin - alignintIc), totalSignal);
  1053. sigIcScatterGraph->SetPointError(sigIcScatterGraph->GetN(), 0, signalError);
  1054. for (int i = 0; i < 20; i++)
  1055. sigIcScatterPlot[i]->Fill(ic1Hist->GetBinContent(bin - i + 10), signal);
  1056. Center_Signal->Fill(signal, mean);
  1057. // sigIcScatterPlot[14]->Fill(ic1Hist->GetBinContent(bin - 14 + 10), signal);
  1058. sigIcScatterPlot_ic2->Fill(ic2Hist->GetBinContent(bin - 14 + 10), signal);
  1059. }
  1060. }
  1061. else
  1062. {
  1063. //ic1Hist->SetBinContent(bin, 0);
  1064. }
  1065. widthComp->Fill(sigma * 2.51 - mw1_focusxHist->GetBinContent(bin - alignint));
  1066. //Center_Signal->Fill(totalSignal, mean);
  1067. Diff_hist->Fill(mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1068. Diff_Signal->Fill(signal, mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1069. Diff_ic1->Fill(ic1Hist->GetBinContent(bin - alignintIc), mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1070. icIntegral += ic1Hist->GetBinContent(bin);
  1071. signalTimeGraph->SetPoint(bin, bin, signal);
  1072. signalTimeGraph->SetPointError(bin, 0., sigError);
  1073. beamOnTime++;
  1074. double c2 = func->GetChisquare() / func->GetNDF();
  1075. if (c2 < 100)
  1076. chisquareDist->Fill(c2);
  1077. }
  1078. delete func;
  1079. delete tmp;
  1080. delete tmp2;
  1081. }
  1082. delete tmpcanvas;
  1083. return res;
  1084. }
  1085. void Beammon::GetTimeCenters()
  1086. {
  1087. TH1D *tmp_TimeCenter;
  1088. double mean_MWPC = mwPosX->GetMean();
  1089. shift = mean_ave * 0.8 / 5 - mwPosX->GetMean();
  1090. double max_ic1 = ic1Hist->GetMaximum();
  1091. beamOnTime = 0;
  1092. int nbins = stopTime - startTime;
  1093. mean_ave = prelimBeam->GetMean();
  1094. sigma_ave = prelimBeam->GetStdDev();
  1095. printf("Mean: %f \n", mean_ave);
  1096. printf("Sigma: %f \n", sigma_ave);
  1097. timeCenters = new TH2D("timeCenters", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, (0.8 * mean_ave - 6. * sigma_pos), (0.8 * mean_ave + 6 * sigma_pos));
  1098. timeWidths = new TH2D("timeWidths", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, 0.4 * sigma_ave * 2.35, 0.95 * sigma_ave * 2.35);
  1099. timeIntSignal = new TH1D("timeIntSignal", "time integrated signal", 64, 0.5, 64.5);
  1100. timeCenters_MWPC = new TH2D("timeCenters_MWPC", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, mean_MWPC - 5 * sigma_pos, mean_MWPC + 5 * sigma_pos);
  1101. timeWidths_MWPC = new TH2D("timeWidths_MWPC", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, 1.5, mw1_focusxHist->GetMaximum() * 1.05);
  1102. timeIntSignal_MWPC = new TH1D("timeIntSignal_MWPC", "time integrated signal", 64, 0.5, 64.5);
  1103. for (int j = 0; j < 20; j++)
  1104. {
  1105. TString name;
  1106. name.Form("sigIcScatterPlot%d", j);
  1107. sigIcScatterPlot[j] = new TH2D(name.Data(), name.Data(), 200, 0, -1, 200, 0, -1);
  1108. TString name2;
  1109. name2.Form("mwPosScatterPlot%d", j);
  1110. mwPosScatterPlot[j] = new TH2D(name2.Data(), name2.Data(), 200, mean_MWPC - 0.1 * sigma_ave, mean_MWPC + 0.1 * sigma_ave, 200, 0.8 * (mean_ave - 0.15 * sigma_ave), 0.8 * (mean_ave + 0.15 * sigma_ave));
  1111. }
  1112. sigIcScatterPlot_ic2 = new TH2D("sigIcScatterPlot_ic2", "sigIcScatterPlot_ic2", 200, 0, -1, 200, 0, -1);
  1113. Center_Signal = new TH2D("Center_Signal", "", 200, 0., max_ic1 - 5000, 200, (0.8 * mean_ave - 5 * sigma_pos), (0.8 * mean_ave + 5 * sigma_pos));
  1114. Diff_timeCenters = new TH2D("Diff_time", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, -6 * sigma_pos, 6 * sigma_pos);
  1115. Diff_hist = new TH1D("Diff_hist", "l", 200, -2, 2.);
  1116. Diff_Signal = new TH2D("Diff_Signal", "", 200, 0., max_ic1 - 5000, 200, -6 * sigma_pos, 6 * sigma_pos);
  1117. Diff_ic1 = new TH2D("Diff_ic1", "", 200, 0., max_ic1 - 5000, 200, -6 * sigma_pos, 6 * sigma_pos);
  1118. b_beamPosX_1 = newdata->Branch("beamPosX_1", &beamPosX_1);
  1119. b_beamFocusX_1 = newdata->Branch("beamFocusX_1", &beamFocusX_1);
  1120. b_beamSignal_1 = newdata->Branch("beamSignal_1", &beamSignal_1);
  1121. bb_ic1 = newdata->Branch("ic1_1", &ic1_1);
  1122. bb_ic2 = newdata->Branch("ic2_1", &ic2_1);
  1123. bb_mw1_focusx = newdata->Branch("mw1_focusx_1", &mw1_focusx_1);
  1124. bb_mw1_posx = newdata->Branch("mw1_posx_1", &mw1_posx_1);
  1125. bb_mw2_focusx = newdata->Branch("mw2_focusx_1", &mw2_focusx_1);
  1126. bb_mw2_posx = newdata->Branch("mw2_posx_1", &mw2_posx_1);
  1127. b_beamOn = newdata->Branch("beamOn", &beamOn);
  1128. newdata->SetBranchStatus("*", 1);
  1129. for (int i = 0; i < spillTimes.size(); i++)
  1130. {
  1131. tmp_TimeCenter = calcTimeCenters(spillTimes[i].first + 200, spillTimes[i].second);
  1132. timeIntSignal->Add(tmp_TimeCenter, 1.);
  1133. delete tmp_TimeCenter;
  1134. beamPosX_1 = 0;
  1135. beamFocusX_1 = 0;
  1136. beamSignal_1 = 0;
  1137. ic1_1 = 0;
  1138. mw1_focusx_1 = 0;
  1139. mw1_posx_1 = 0;
  1140. beamOn = 0;
  1141. newdata->Fill();
  1142. }
  1143. SignalErrHist->Write();
  1144. printf("Fit_Fail_Counts=%d\n", Fit_Fail_Counts);
  1145. //Draw timeCenter
  1146. timeCenters->GetXaxis()->SetTitle("time [s]");
  1147. TString label;
  1148. label.Form("mean position[mm]");
  1149. timeCenters->GetYaxis()->SetTitle(label.Data());
  1150. timeCenters->Write();
  1151. //Draw timeCenter_MWPC
  1152. timeCenters_MWPC->GetXaxis()->SetTitle("time [s]");
  1153. TString label2;
  1154. label2.Form("mean position[mm]");
  1155. timeCenters_MWPC->GetYaxis()->SetTitle(label.Data());
  1156. timeCenters_MWPC->Write();
  1157. //Draw timeWidths
  1158. timeWidths->GetXaxis()->SetTitle("time [s]");
  1159. TString label12;
  1160. label12.Form("width fiber[mm]");
  1161. timeWidths->GetYaxis()->SetTitle(label12.Data());
  1162. timeWidths->Write();
  1163. //Draw timeWidths_MWPC
  1164. timeWidths_MWPC->GetXaxis()->SetTitle("time [s]");
  1165. TString label13;
  1166. label13.Form("width MWPC[mm]");
  1167. timeWidths_MWPC->GetYaxis()->SetTitle(label12.Data());
  1168. timeWidths_MWPC->Write();
  1169. Center_Signal->GetXaxis()->SetTitle("Signal [particles]");
  1170. TString label123;
  1171. label123.Form("Position_Fiber[mm]");
  1172. Center_Signal->GetYaxis()->SetTitle(label123.Data());
  1173. Center_Signal->Write();
  1174. Center_Signal->ProfileX()->Write();
  1175. //Draw Diff_timeCenters
  1176. Diff_timeCenters->GetXaxis()->SetTitle("time [s]");
  1177. TString label3;
  1178. label3.Form("mean position diff[mm]");
  1179. Diff_timeCenters->GetYaxis()->SetTitle(label.Data());
  1180. Diff_timeCenters->Write();
  1181. //Draw Diff_Signal
  1182. Diff_Signal->GetXaxis()->SetTitle("signal");
  1183. TString label4;
  1184. label4.Form("mean position diff[mm]");
  1185. Diff_Signal->GetYaxis()->SetTitle(label.Data());
  1186. Diff_Signal->Write();
  1187. //Draw Diff_Signal
  1188. Diff_ic1->GetXaxis()->SetTitle("ic1");
  1189. TString label5;
  1190. label5.Form("mean position diff[mm]");
  1191. Diff_ic1->GetYaxis()->SetTitle(label.Data());
  1192. Diff_ic1->Write();
  1193. TObjArray aSlices, bSlices;
  1194. Diff_Signal->FitSlicesY(0, 0, -1, 5, "QNR", &aSlices);
  1195. for (int i = 0; i < 4; i++)
  1196. {
  1197. aSlices[i]->Write();
  1198. }
  1199. Diff_ic1->FitSlicesY(0, 0, -1, 5, "QNR", &bSlices);
  1200. for (int i = 0; i < 4; i++)
  1201. {
  1202. bSlices[i]->Write();
  1203. }
  1204. mean_ave = beamPosX->GetMean();
  1205. sigma_pos = beamPosX->GetStdDev();
  1206. sigma_ave = beamFocusX->GetMean();
  1207. return;
  1208. }
  1209. pair<int, int> Beammon::GetSpillTimes(int nSpill)
  1210. {
  1211. return spillTimes[nSpill];
  1212. }
  1213. TH2D *Beammon::GetSpillCenters(int nSpill)
  1214. {
  1215. //return calcTimeCenters(spillTimes[nSpill].first, spillTimes[nSpill].second);
  1216. }
  1217. TH1D *Beammon::GetSpillSlice(int nSpill)
  1218. {
  1219. return GetTimeSlice(-1, spillTimes[nSpill].first - 500, spillTimes[nSpill].second + 500, true);
  1220. }
  1221. TH1D *Beammon::GetSignalDistribution()
  1222. {
  1223. /*int n = signalDist->GetNbinsX();
  1224. for(int i = 1; i<= n; i++){
  1225. signalDist->SetBinContent(i,signalDist->GetBinContent(i)*signalDistScale);
  1226. }*/
  1227. signalDist->Scale(signalDistScale);
  1228. return signalDist;
  1229. }
  1230. TCanvas *Beammon::GetSignalDistributionCanvas()
  1231. {
  1232. TCanvas *c1 = new TCanvas();
  1233. c1->SetName("signalDistributionCanvas");
  1234. signalDist->Draw();
  1235. signalDist->SetTitle("");
  1236. signalDist->GetYaxis()->SetTitle("entries");
  1237. TString label;
  1238. label.Form("integrated signal [adc counts / %d #mus]", intTime);
  1239. signalDist->GetXaxis()->SetTitle(label.Data());
  1240. //c1->SaveAs("dummy");
  1241. return c1;
  1242. }
  1243. TH2D *Beammon::GetSimpleView()
  1244. {
  1245. return timePosHist;
  1246. }
  1247. TH1D *Beammon::GetBaselineHist()
  1248. {
  1249. TH1D *tmp = new TH1D("baseline", "baseline", 64, 0.5, 64.5);
  1250. for (int n = 1; n <= 64; n++)
  1251. {
  1252. tmp->SetBinContent(n, baseline[n - 1]);
  1253. }
  1254. return tmp;
  1255. }
  1256. void Beammon::Close()
  1257. {
  1258. delete timePosHist;
  1259. fileOut->Close();
  1260. }
  1261. double Beammon::GetIntSignal()
  1262. {
  1263. return intSignal;
  1264. }
  1265. int Beammon::GetBeamOnTime()
  1266. {
  1267. return beamOnTime;
  1268. }
  1269. double Beammon::GetSignalStd()
  1270. {
  1271. return signalDist->GetStdDev(1);
  1272. }
  1273. bool Beammon::isAboveThreshold(TGraphErrors *slice)
  1274. {
  1275. double channel, signal;
  1276. slice->GetPoint(highestChannelBin, channel, signal);
  1277. if (signal > beamOnAdcVal)
  1278. return true;
  1279. else
  1280. return false;
  1281. }
  1282. bool Beammon::isAboveThreshold(TH1D *slice)
  1283. {
  1284. double val = slice->GetBinContent(highestChannelBin);
  1285. if (val > beamOnAdcVal)
  1286. return true;
  1287. else
  1288. return false;
  1289. }
  1290. bool Beammon::isAboveThreshold(double val)
  1291. {
  1292. // old version using the gaussian fit to the measurement
  1293. if (val > beamOnAdcVal)
  1294. return true;
  1295. else
  1296. return false;
  1297. /*
  1298. if(val > baselineIntegral * 1.5 && val < baselineIntegral *100)
  1299. return true;
  1300. else
  1301. return false;*/
  1302. }
  1303. TH1D *Beammon::icSignalComp()
  1304. {
  1305. icSignalDist = new TH1D("icSignal", "icSignal", 200, 0., ic1Hist->GetMaximum());
  1306. TH1D *icSignalDist2 = new TH1D("icSignal2", "icSignal2", 200, 0., ic2Hist->GetMaximum());
  1307. for (int i = 0; i < ic1Hist->GetNbinsX(); i++)
  1308. {
  1309. if (ic1Hist->GetBinContent(i) != 0)
  1310. icSignalDist->Fill(ic1Hist->GetBinContent(i));
  1311. if (ic2Hist->GetBinContent(i) != 0)
  1312. icSignalDist2->Fill(ic2Hist->GetBinContent(i));
  1313. }
  1314. icSignalDist2->Write();
  1315. return icSignalDist;
  1316. }
  1317. TCanvas *Beammon::signalOverTime2d()
  1318. {
  1319. TCanvas *c1 = new TCanvas();
  1320. c1->SetName("signalOverTime2d");
  1321. TH1D *tmp = GetSignalOverTime();
  1322. int binsX = tmp->GetNbinsX();
  1323. int max = tmp->GetMaximum();
  1324. int min = tmp->GetMinimum();
  1325. TH2D *res = new TH2D("sigTime2d", "", binsX / 200, 0., binsX / readOutFreq, 100, min, max);
  1326. for (int k = 0; k < binsX; k++)
  1327. {
  1328. res->Fill(k / readOutFreq, tmp->GetBinContent(k));
  1329. }
  1330. res->Draw("colz");
  1331. res->GetXaxis()->SetTitle("time [s]");
  1332. TString label;
  1333. label.Form("adc counts / %d #mus", intTime);
  1334. res->GetYaxis()->SetTitle(label.Data());
  1335. //c1->SaveAs("dummy");
  1336. res->Write();
  1337. return c1;
  1338. }
  1339. double Beammon::calcBeamTime()
  1340. {
  1341. beamOnTime = 0;
  1342. for (int i = 0; i < spillTimes.size(); i++)
  1343. {
  1344. beamOnTime += spillTimes[i].second - spillTimes[i].first;
  1345. }
  1346. return beamOnTime;
  1347. }
  1348. TCanvas *Beammon::PrintProfile(int offset)
  1349. {
  1350. TCanvas *c1 = new TCanvas();
  1351. c1->cd();
  1352. //TH1D *res = GetChannelSlice(-1,offset);
  1353. TGraphErrors *res = new TGraphErrors(64);
  1354. TGraphErrors *tmp = GetChannelGraph(-1, offset);
  1355. double x, y, errx, erry;
  1356. for (int ch = 0; ch < 63; ch++)
  1357. {
  1358. tmp->GetPoint(ch, x, y);
  1359. x *= 0.8;
  1360. errx = 0;
  1361. erry = tmp->GetErrorY(ch);
  1362. res->SetPoint(ch, x, y);
  1363. res->SetPointError(ch, errx, erry);
  1364. }
  1365. res->Fit("gaus", "Q0");
  1366. double stepsize = 0.01;
  1367. double lim = res->GetFunction("gaus")->GetMaximum() / 2;
  1368. double val, x1, x2, pos = 0;
  1369. bool below = true;
  1370. while (pos <= 64)
  1371. {
  1372. val = res->GetFunction("gaus")->Eval(pos);
  1373. if (below && val >= lim)
  1374. {
  1375. x1 = pos;
  1376. below = false;
  1377. }
  1378. else if (below == false && val <= lim)
  1379. {
  1380. x2 = pos;
  1381. break;
  1382. }
  1383. pos += stepsize;
  1384. }
  1385. fwhm = (x2 - x1) * channelWidth;
  1386. res->Draw("AP");
  1387. TString sName;
  1388. sName.Form("Profile%d", offset);
  1389. c1->SetName(sName.Data());
  1390. res->SetTitle("");
  1391. res->GetXaxis()->SetTitle("position [mm]");
  1392. TString label;
  1393. label.Form("adc counts / %d #mus", intTime);
  1394. res->GetYaxis()->SetTitle(label.Data());
  1395. res->GetYaxis()->SetTitleOffset(1.4);
  1396. TString str;
  1397. str.Form("fwhm = %.3f mm", fwhm);
  1398. TText *t = new TText(34, 900, str.Data());
  1399. t->Draw("SAME");
  1400. delete t;
  1401. delete res;
  1402. c1->Update();
  1403. c1->Modified();
  1404. //c1->SaveAs(sName.Data());
  1405. return c1;
  1406. }
  1407. TCanvas *Beammon::FitSnapshot(int offset)
  1408. {
  1409. //gSyle->SetOptFit(1111);
  1410. TCanvas *c1 = new TCanvas();
  1411. c1->cd();
  1412. TGraphErrors *res = GetChannelGraph(-1, offset);
  1413. TString sName;
  1414. sName.Form("Snapshot%d", offset);
  1415. c1->SetName(sName.Data());
  1416. res->SetTitle("");
  1417. res->GetXaxis()->SetTitle("channel");
  1418. TString label;
  1419. label.Form("adc counts / %d #mus", intTime);
  1420. res->GetYaxis()->SetTitle(label.Data());
  1421. res->GetYaxis()->SetTitleOffset(1.4);
  1422. //res->Fit("gaus");
  1423. res->Fit("gaus");
  1424. double mean = res->GetFunction("gaus")->GetParameter(1);
  1425. double sigma = res->GetFunction("gaus")->GetParameter(2);
  1426. double constant = res->GetFunction("gaus")->GetParameter(0);
  1427. TF1 *func = new TF1("doublegaus", fitfunc, 0, 64, 6); // t
  1428. func->SetNpx(1000);
  1429. func->SetParameter(0, constant);
  1430. func->SetParameter(1, mean);
  1431. func->SetParameter(2, sigma);
  1432. func->SetParameter(3, constant);
  1433. func->SetParameter(4, sigma);
  1434. func->SetParameter(5, 0.);
  1435. func->SetParLimits(1, 0., 64.);
  1436. func->SetParLimits(2, 1., 20.);
  1437. func->SetParLimits(4, 5., 100.);
  1438. //func->SetParameter(5,0.);
  1439. res->Fit("doublegaus");
  1440. res->Draw("AP");
  1441. c1->SaveAs(sName.Data());
  1442. return c1;
  1443. }
  1444. TCanvas *Beammon::FitAverage()
  1445. {
  1446. TCanvas *c1 = new TCanvas();
  1447. c1->cd();
  1448. int n = spillTimes.size();
  1449. double yValues[64];
  1450. double yErrors[64];
  1451. double xValues[64];
  1452. double xErrors[64];
  1453. int count = 0;
  1454. double x, y, errY;
  1455. TGraphErrors *tmp;
  1456. yValues[0] = 0;
  1457. for (int i = 0; i < n; i++)
  1458. {
  1459. int start = spillTimes[i].first;
  1460. int stop = spillTimes[i].second;
  1461. for (int k = start; k <= stop; k++)
  1462. {
  1463. count++;
  1464. tmp = GetChannelGraph(k, 0);
  1465. for (int bin = 1; bin <= 64; bin++)
  1466. {
  1467. errY = tmp->GetErrorY(bin);
  1468. tmp->GetPoint(bin, x, y);
  1469. if (!(y != y || errY != errY))
  1470. {
  1471. yValues[bin - 1] += y;
  1472. yErrors[bin - 1] += errY * errY;
  1473. }
  1474. else
  1475. {
  1476. yValues[bin - 1] += 0;
  1477. yErrors[bin - 1] += 0;
  1478. }
  1479. }
  1480. delete tmp;
  1481. }
  1482. }
  1483. for (int bin = 0; bin < 64; bin++)
  1484. {
  1485. xValues[bin] = bin;
  1486. xErrors[bin] = 1. / sqrt(12);
  1487. yValues[bin] *= 1. / count;
  1488. yErrors[bin] = sqrt(yErrors[bin]) / count;
  1489. }
  1490. /*for (int i = 0; i < 64; i++)
  1491. {
  1492. printf("channelErrors[%d]: %f ", i, channelErrors[i]);
  1493. printf("yValue[%d]: %f ", i, yValues[i]);
  1494. printf("yError[%d]: %f \n", i, yErrors[i]);
  1495. }*/
  1496. TGraph *res = new TGraph(64, xValues, yValues); //, xErrors, yErrors);
  1497. c1->SetName("avgGraph");
  1498. res->SetTitle("");
  1499. res->GetXaxis()->SetTitle("channel");
  1500. TString label;
  1501. label.Form("adc counts / %d #mus", intTime);
  1502. res->GetYaxis()->SetTitle(label.Data());
  1503. res->GetYaxis()->SetTitleOffset(1.4);
  1504. res->Fit("gaus", "Q0");
  1505. double mean = res->GetFunction("gaus")->GetParameter(1);
  1506. //mean_ave = mean;
  1507. double sigma = res->GetFunction("gaus")->GetParameter(2);
  1508. //sigma_ave = sigma;
  1509. double constant = res->GetFunction("gaus")->GetParameter(0);
  1510. //res->Fit("gaus","","",mean-1.5*sigma,mean+1.5*sigma);
  1511. TF1 *func = new TF1("doublegaus", fitfunc, 0, 64, 6); // t
  1512. func->SetNpx(1000);
  1513. func->SetParameter(0, constant);
  1514. func->SetParameter(1, mean);
  1515. func->SetParameter(2, sigma);
  1516. func->SetParameter(3, constant);
  1517. func->SetParameter(4, sigma);
  1518. func->SetParameter(5, 0.);
  1519. func->SetParLimits(1, 0., 64.);
  1520. func->SetParLimits(2, 1., 20.);
  1521. func->SetParLimits(4, 5., 100.);
  1522. //func->SetParameter(5,0.);
  1523. res->Fit("doublegaus");
  1524. res->Draw("AP");
  1525. //c1->SaveAs("avgFit");
  1526. // find fwhm
  1527. double stepsize = 0.01;
  1528. double lim = res->GetFunction("doublegaus")->GetMaximum() / 2;
  1529. double val, x1, x2, pos = 0;
  1530. bool below = true;
  1531. while (pos <= 64)
  1532. {
  1533. val = func->Eval(pos);
  1534. if (below && val >= lim)
  1535. {
  1536. x1 = pos;
  1537. below = false;
  1538. }
  1539. else if (below == false && val <= lim)
  1540. {
  1541. x2 = pos;
  1542. break;
  1543. }
  1544. pos += stepsize;
  1545. }
  1546. fwhm = (x2 - x1) * channelWidth;
  1547. return c1;
  1548. }
  1549. TCanvas *Beammon::PrintChannelNoise(int channel)
  1550. {
  1551. TCanvas *c1 = new TCanvas();
  1552. c1->cd();
  1553. TString name;
  1554. name.Form("channelNoise%d", channel);
  1555. TString label;
  1556. label.Form("adc counts / %d #mus", intTime);
  1557. c1->SetName(name.Data());
  1558. channelSig[channel]->SetTitle("");
  1559. channelSig[channel]->SetName(name.Data());
  1560. channelSig[channel]->GetXaxis()->SetTitle(label.Data());
  1561. channelSig[channel]->GetYaxis()->SetTitle("entries");
  1562. channelSig[channel]->Draw();
  1563. gStyle->SetOptStat("mr");
  1564. gPad->Modified();
  1565. //c1->SaveAs("blub");
  1566. return c1;
  1567. }
  1568. TCanvas *Beammon::IcFitSlicesY()
  1569. {
  1570. TCanvas *c1 = new TCanvas();
  1571. c1->cd();
  1572. c1->SetName("icFitSlicesY");
  1573. TH1D *tmp = timePosHist->ProjectionY("", 1, 64);
  1574. TH1D *means = (TH1D *)sigIcScatterPlot[10 + alignintIc]->ProfileY(); //(TH1D*)objectArray[1];
  1575. means->SetLineColor(kBlack);
  1576. //sigIcScatterPlot->Draw();
  1577. means->Draw("E");
  1578. means->GetXaxis()->SetRangeUser(0., means->GetXaxis()->GetXmax());
  1579. means->SetMinimum(0.);
  1580. means->SetTitle("");
  1581. //means->SetName("icFitSlicesY");
  1582. TString label1, label2;
  1583. label1.Form("mean photodiode signal [particles / %d #mus]", intTime);
  1584. label2.Form("ionisation chamber signal [particles / %d #mus]", intTime);
  1585. means->GetXaxis()->SetTitle(label2.Data());
  1586. means->GetYaxis()->SetTitle(label1.Data());
  1587. means->Fit("pol1");
  1588. //sigIcScatterPlot->Draw();
  1589. gPad->Modified();
  1590. //c1->SaveAs("blub");
  1591. means->Write();
  1592. return c1;
  1593. }
  1594. TCanvas *Beammon::PrintSigIcGraph()
  1595. {
  1596. TCanvas *c1 = new TCanvas();
  1597. c1->cd();
  1598. TString name;
  1599. name.Form("sigIcScatterGraph");
  1600. TString label, label2;
  1601. label.Form("adc counts / %d #mus", intTime);
  1602. label2.Form("whatever / %d #mus", intTime);
  1603. c1->SetName(name.Data());
  1604. sigIcScatterGraph->SetTitle("");
  1605. sigIcScatterGraph->SetName(name.Data());
  1606. sigIcScatterGraph->GetXaxis()->SetTitle(label2.Data());
  1607. sigIcScatterGraph->GetYaxis()->SetTitle(label.Data());
  1608. sigIcScatterGraph->Fit("pol1", "W");
  1609. sigIcScatterGraph->Draw("AP");
  1610. gPad->Modified();
  1611. //c1->SaveAs("blub");
  1612. return c1;
  1613. }
  1614. TH1D *Beammon::GetMwPosComp()
  1615. {
  1616. double bin, mean;
  1617. double shift = beamPosX->GetMean() - mwPosX->GetMean();
  1618. //printf("beam: %f \n",beamPosTimeGraph->GetMean(2));
  1619. int n = beamPosTimeGraph->GetN();
  1620. for (int i = 0; i < n; i++)
  1621. {
  1622. beamPosTimeGraph->GetPoint(i, bin, mean);
  1623. if (!mean)
  1624. continue;
  1625. positionComp->Fill(mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1626. }
  1627. return positionComp;
  1628. }
  1629. //
  1630. ///
  1631. /////////////////////////////////////////////////////////////////////////////
  1632. TCanvas *Beammon::GetSig_e_ic_Hist()
  1633. {
  1634. shift = beamPosX->GetMean() - mwPosX->GetMean();
  1635. printf("shift = %f", shift);
  1636. TCanvas *c1 = new TCanvas();
  1637. c1->cd();
  1638. c1->SetName("SignalComp_vs_Time");
  1639. Double_t norm = ic1Hist->Integral();
  1640. ic1Hist->Scale(1. / norm);
  1641. ic1Hist->SetLineColor(kRed);
  1642. ic1Hist->Draw("HIST L");
  1643. Double_t norm0 = sigHist->Integral();
  1644. Double_t scaleSig = norm / norm0;
  1645. sigHist->Scale(1. / norm0);
  1646. sigHist->Draw("HIST SAME L");
  1647. TLegend * legend= new TLegend(0.1, 0.7, 0.48, 0.9);
  1648. legend->AddEntry(ic1Hist, "ic", "l");
  1649. legend->AddEntry(sigHist, "Fibre", "l");
  1650. legend->Draw();
  1651. TH1D *compsig = new TH1D("compsig", "compsig", 200, -0.5, 0.5);
  1652. for (int i = 0; i < nevents; i++)
  1653. {
  1654. compsig->Fill((ic1Hist->GetBinContent(i) - sigHist->GetBinContent(i)) * 2 / (ic1Hist->GetBinContent(i) + sigHist->GetBinContent(i)));
  1655. }
  1656. compsig->Write();
  1657. TCanvas *c2 = new TCanvas();
  1658. c2->cd();
  1659. c2->SetName("PositionComp_vs_Time");
  1660. TH1D *fibreHistNew = new TH1D("fibreHistNew", "fibreHistNew", nevents, 0., nevents - 1);
  1661. for (int i = 0; i < nevents; i++)
  1662. {
  1663. if (fibreHist->GetBinContent(i) != 0)
  1664. {
  1665. fibreHistNew->SetBinContent(i, fibreHist->GetBinContent(i) - shift);
  1666. }
  1667. else
  1668. fibreHistNew->SetBinContent(i, 0);
  1669. }
  1670. fibreHistNew->Draw("HIST L");
  1671. mwpcHist->SetLineColor(kRed);
  1672. mwpcHist->SetLineColor(kRed);
  1673. mwpcHist->Draw("HIST SAME L");
  1674. TLegend * legend2 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1675. legend2->AddEntry(mwpcHist, "MWPC", "l");
  1676. legend2->AddEntry(fibreHistNew, "Fibre", "l");
  1677. legend2->Draw();
  1678. c2->Write();
  1679. return c1;
  1680. }
  1681. TCanvas *Beammon::GetSig_e_ic_Hist_Weighted()
  1682. {
  1683. TCanvas *c1 = new TCanvas();
  1684. c1->cd();
  1685. c1->SetName("SignalComp_vs_Time_all");
  1686. Double_t norm = ic1Hist->Integral();
  1687. ic1Hist->Scale(1. / norm);
  1688. ic1Hist->SetLineColor(kRed);
  1689. ic1Hist->SetLineWidth(2);
  1690. ic1Hist->Draw("HIST L");
  1691. Double_t norm0 = sigHist_weighted->Integral();
  1692. sigHist_weighted->Scale(1. / norm0);
  1693. sigHist_weighted->Draw("HIST SAME L");
  1694. Double_t norm1 = sigHist->Integral();
  1695. sigHist->Scale(1. / norm1);
  1696. sigHist->SetLineColor(kBlue);
  1697. sigHist->Draw("HIST SAME L");
  1698. TLegend * legend= new TLegend(0.1, 0.7, 0.48, 0.9);
  1699. legend->AddEntry(ic1Hist, "ic", "l");
  1700. legend->AddEntry(sigHist, "Fibre_fit", "l");
  1701. legend->AddEntry(sigHist_weighted, "Fibre_sum", "l");
  1702. legend->Draw();
  1703. c1->Write();
  1704. TH1D *compsig_weighted = new TH1D("compsig_weighted", "compsig", 200, -0.5, 0.5);
  1705. for (int i = 0; i < nevents; i++)
  1706. {
  1707. compsig_weighted->Fill((ic1Hist->GetBinContent(i) - sigHist_weighted->GetBinContent(i)) * 2 / (ic1Hist->GetBinContent(i) + sigHist_weighted->GetBinContent(i)));
  1708. }
  1709. compsig_weighted->SetLineColor(kRed);
  1710. compsig_weighted->Write();
  1711. TH1D *compsig = new TH1D("compsig", "compsig", 200, -0.5, 0.5);
  1712. for (int i = 0; i < nevents; i++)
  1713. {
  1714. compsig->Fill((ic1Hist->GetBinContent(i) - sigHist->GetBinContent(i)) * 2 / (ic1Hist->GetBinContent(i) + sigHist->GetBinContent(i)));
  1715. }
  1716. c1->cd();
  1717. c1->SetName("CompSig_all");
  1718. compsig->Draw();
  1719. compsig_weighted->Draw("SAME");
  1720. TLegend * legend2 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1721. legend2->AddEntry(compsig_weighted, "Weighted", "l");
  1722. legend2->AddEntry(compsig, "Fit", "l");
  1723. legend2->Draw();
  1724. c1->Write();
  1725. TCanvas *c2 = new TCanvas();
  1726. c2->cd();
  1727. c2->SetName("PositionComp_vs_Time_all");
  1728. TH1D *fibreHistNew_weighted = new TH1D("fibreHistNew_weighted", "fibreHistNew_weighted", nevents, 0., nevents - 1);
  1729. for (int i = 0; i < nevents; i++)
  1730. {
  1731. if (fibreHist_weighted->GetBinContent(i) != 0)
  1732. {
  1733. fibreHistNew_weighted->SetBinContent(i, fibreHist_weighted->GetBinContent(i) - shift);
  1734. }
  1735. else
  1736. fibreHistNew_weighted->SetBinContent(i, 0);
  1737. }
  1738. TH1D *fibreHistNew = new TH1D("fibreHistNew", "fibreHistNew", nevents, 0., nevents - 1);
  1739. for (int i = 0; i < nevents; i++)
  1740. {
  1741. if (fibreHist->GetBinContent(i) != 0)
  1742. {
  1743. fibreHistNew->SetBinContent(i, fibreHist->GetBinContent(i) - shift);
  1744. }
  1745. else
  1746. fibreHistNew->SetBinContent(i, 0);
  1747. }
  1748. fibreHistNew->Draw("HIST L");
  1749. fibreHistNew->SetLineColor(kBlue);
  1750. fibreHistNew_weighted->Draw("HIST L SAME");
  1751. mwpcHist->SetLineColor(kRed);
  1752. mwpcHist->SetLineWidth(2);
  1753. mwpcHist->Draw("HIST SAME L");
  1754. TLegend * legend3 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1755. legend3->AddEntry(mwpcHist, "MWPC", "l");
  1756. legend3->AddEntry(fibreHistNew, "Fibre_fit", "l");
  1757. legend3->AddEntry(fibreHistNew_weighted, "Fibre_Weighted", "l");
  1758. legend3->Draw();
  1759. c2->Write();
  1760. c2->cd();
  1761. c2->Clear();
  1762. c2->SetName("PosComp_All");
  1763. TH1D *comppos_weighted = new TH1D("comppos_weighted", "comppos", 200, -0.4, 0.4);
  1764. for (int i = 0; i < nevents; i++)
  1765. {
  1766. comppos_weighted->Fill((mwpcHist->GetBinContent(i) - fibreHist_weighted->GetBinContent(i) + shift));
  1767. }
  1768. comppos_weighted->SetLineColor(kRed);
  1769. comppos_weighted->Write();
  1770. TH1D *comppos = new TH1D("comppos", "comppos", 200, -0.4, 0.4);
  1771. for (int i = 0; i < nevents; i++)
  1772. {
  1773. comppos->Fill((mwpcHist->GetBinContent(i) - fibreHist->GetBinContent(i) + shift));
  1774. }
  1775. comppos->Draw();
  1776. comppos_weighted->Draw("SAME");
  1777. TLegend * legend4 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1778. legend4->AddEntry(comppos_weighted, "Weighted", "l");
  1779. legend4->AddEntry(comppos, "Fit", "l");
  1780. legend4->Draw();
  1781. return c2;
  1782. }