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.

2163 lines
60 KiB

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