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.

2164 lines
60 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. timePosHist = new TH2D("timePosHist", "beam position", 64, 0.5, 64.5, nevents, 0, nevents - 1); //display in seconds (y-axis), but with bins of 100ms
  346. SignalErrHist = new TH2D("SignalErrHist", "SignalErrHist", 64, 0.5, 64.5, 200, 0, -1);
  347. chisquareDist = new TH1D("chiqsuareDist", "chiqsuareDist", 100, -1, -1);
  348. prelimBeam = new TH1D("prelimBeam", "preliminary beam profile", 64, 0.5, 64.5);
  349. beamPosHist = new TH1D("beamPosHist", "mean positions", readOutFreq, 0., 64. * channelWidth);
  350. beamWidthHist = new TH1D("beamWidthHist", "beam width", 500, 0., 50.);
  351. beamPosTimeGraph = new TGraphErrors(nevents);
  352. beamPosTimeGraph->SetName("beamPosTimeGraph");
  353. signalTimeGraph = new TGraphErrors(nevents);
  354. signalTimeGraph->SetName("signalTimeGraph");
  355. sigIcScatterGraph = new TGraphErrors();
  356. mwPosScatterGraph = new TGraphErrors();
  357. //sigIcScatterPlot = new TH2D("sigIcScatterPlot", "", 100, 0, -1, 100, 0, -1);
  358. signalComp = new TH1D("signalComp", "signalComp", 200., -1., 1.);
  359. positionComp = new TH1D("positionComp", "positionComp", 200., -3., 3.);
  360. widthComp = new TH1D("widthComp", "widthComp", 200., 0., 3.5);
  361. ic1Hist = new TH1D("ic1Hist", "ic1Hist", nevents, 0., nevents - 1);
  362. ic2Hist = new TH1D("ic2Hist", "ic2Hist", nevents, 0., nevents - 1);
  363. sigHist = new TH1D("sigHist", "sigHist", nevents, 0., nevents - 1);
  364. sigHist_weighted = new TH1D("sigHist_weighted", "sigHist_weighted", nevents, 0., nevents - 1);
  365. fibreHist = new TH1D("fibreHist", "fibreHist", nevents, 0., nevents - 1);
  366. fibreHist_weighted = new TH1D("fibreHist_weighted", "fibreHist_weighted", nevents, 0., nevents - 1);
  367. mwpcHist = new TH1D("mwpcHist", "mwpcHist", nevents, 0., nevents - 1);
  368. mw1_focusxHist = new TH1D("mw1_focusxHist", "mw1_focusxHist", nevents, 0, nevents - 1);
  369. mw1_focusyHist = new TH1D("mw1_focusyHist", "mw1_focusyHist", nevents, 0, nevents - 1);
  370. mw2_focusxHist = new TH1D("mw2_focusxHist", "mw2_focusxHist", nevents, 0, nevents - 1);
  371. mw2_focusyHist = new TH1D("mw2_focusyHist", "mw2_focusyHist", nevents, 0, nevents - 1);
  372. mw1_posxHist = new TH1D("mw1_posxHist", "mw1_posxHist", nevents, 0, nevents - 1);
  373. mw1_posyHist = new TH1D("mw1_posyHist", "mw1_posyHist", nevents, 0, nevents - 1);
  374. mw2_posxHist = new TH1D("mw2_posxHist", "mw2_posxHist", nevents, 0, nevents - 1);
  375. mw2_posyHist = new TH1D("mw2_posyHist", "mw2_posyHist", nevents, 0, nevents - 1);
  376. mwPosX = new TH1D("mwPosX", "", 200, 0, 20);
  377. mwFocusX = new TH1D("mwFocusX", "", 200, 2, 3.5);
  378. beamPosX = new TH1D("beamPosX", "", 200, 20, 30);
  379. beamFocusX = new TH1D("beamFocusX", "", 200, 0, 20);
  380. nBaselineEvents = baselineEvents;
  381. nPrelimEvents = prelimEvents;
  382. beamOnLevel = beamLevel;
  383. beamOnAdcVal = 0;
  384. beamOnTime = 0;
  385. if (!isSipm)
  386. {
  387. if (fn.Contains("int1"))
  388. calcBaseline(false);
  389. else
  390. calcBaseline(true);
  391. }
  392. prelimProfile();
  393. calcBaselineIntegral();
  394. ProcessData();
  395. }
  396. void Beammon::Save()
  397. {
  398. fileIn->Close();
  399. fileOut->cd();
  400. for (int i = 0; i < 5; i++)
  401. PrintProfile(i)->Write();
  402. FitAverage()->Write();
  403. PrintBaseline()->Write();
  404. //for(int i=0;i<64;i++)
  405. // PrintChannelNoise(i)->Write();
  406. GetSignalOverTime()->Write();
  407. for (int i = 0; i < 5; i++)
  408. GetChannelSlice2(-1, i)->Write();
  409. //fileIn->Close();
  410. GetTimeSliceCanvas(-1)->Write();
  411. timePosHist->ProjectionX()->Write();
  412. GetTimeCenters();
  413. GetSignalDistributionCanvas()->Write();
  414. GetSignalDistribution()->Write();
  415. GetSignalNoiseChannel()->Write();
  416. chisquareDist->Write();
  417. sigIcScatterGraph->Write();
  418. mwPosScatterGraph->Write();
  419. for (int i = 6; i < 15; i++)
  420. {
  421. sigIcScatterPlot[i]->Write();
  422. mwPosScatterPlot[i]->Write();
  423. }
  424. mwPosScatterPlot[11]->Write();
  425. sigIcScatterPlot[11]->Write();
  426. mw1_focusxHist->Write();
  427. mw1_posxHist->Write();
  428. mwPosX->Write();
  429. mwFocusX->Write();
  430. beamPosX->Write();
  431. beamFocusX->Write();
  432. TVectorD v(6);
  433. v[0] = GetIntSignal();
  434. v[1] = GetBeamOnTime();
  435. v[2] = signalDist->GetStdDev();
  436. v[3] = icIntegral;
  437. v[4] = completeIntegral;
  438. v[5] = fwhm;
  439. v.Write("beaminfo");
  440. beamPosTimeGraph->Write();
  441. signalTimeGraph->Write();
  442. gChannelErrors->Write();
  443. ic1Hist->Write();
  444. ic2Hist->Write();
  445. sigHist->Write();
  446. fibreHist->Write();
  447. mwpcHist->Write();
  448. fibreHist_weighted->Write();
  449. sigHist_weighted->Write();
  450. GetSig_e_ic_Hist()->Write();
  451. GetSig_e_ic_Hist_Weighted()->Write();
  452. signalComp->Write();
  453. GetMwPosComp()->Write();
  454. widthComp->Write();
  455. icSignalComp()->Write();
  456. signalOverTime2d();
  457. IcFitSlicesY();
  458. PrintSigIcGraph()->Write();
  459. //GetTimeCenters();
  460. newdata->AutoSave();
  461. Close();
  462. // v.Delete();
  463. }
  464. void Beammon::calcBaseline(bool useFirstEvents)
  465. {
  466. // use the first/last nBaselineEvents events to calculate the average/baseline
  467. if (!useFirstEvents)
  468. { // last
  469. for (int i = nevents - nBaselineEvents; i < nevents; i++)
  470. {
  471. data->GetEvent(i);
  472. for (int ch = 0; ch < 64; ch++)
  473. {
  474. baseline[ch] += channels[ch] / nBaselineEvents;
  475. }
  476. }
  477. }
  478. else
  479. {
  480. for (int i = 1; i <= nBaselineEvents; i++)
  481. {
  482. data->GetEvent(i);
  483. for (int ch = 0; ch < 64; ch++)
  484. {
  485. baseline[ch] += channels[ch] / nBaselineEvents;
  486. }
  487. }
  488. }
  489. }
  490. TCanvas *Beammon::PrintBaseline()
  491. {
  492. TCanvas *c1 = new TCanvas();
  493. c1->cd();
  494. TGraphErrors *g = new TGraphErrors(64);
  495. for (int k = 0; k < 64; k++)
  496. {
  497. double stdDev = channelSig[k]->GetStdDev();
  498. channelErrors[k] = stdDev;
  499. g->SetPoint(k, k, baseline[k]);
  500. g->SetPointError(k, 0., channelErrors[k]);
  501. }
  502. g->SetTitle("");
  503. g->SetName("baseline");
  504. g->GetXaxis()->SetTitle("channel");
  505. TString label;
  506. label.Form("adc counts / %d #mus", intTime);
  507. g->GetYaxis()->SetTitle(label.Data());
  508. g->Draw("AP");
  509. c1->SetName("baseline");
  510. delete g;//added
  511. return c1;
  512. }
  513. void Beammon::calcBaselineIntegral()
  514. {
  515. /*baselineIntegral = 0;
  516. for(int i=1; i <= nBaselineEvents;i++){
  517. data->GetEvent(i);
  518. double currentIntegral = 0;
  519. for(int ch=0;ch<64;ch++){
  520. currentIntegral += channels[ch] - baseline[ch];
  521. }
  522. if(currentIntegral > baselineIntegral)
  523. baselineIntegral = currentIntegral;
  524. }
  525. printf("baseline int: %f \n",baselineIntegral);*/
  526. double highVal = 0;
  527. for (int i = 1; i <= nBaselineEvents; i++)
  528. {
  529. data->GetEvent(i);
  530. double val = channels[highestChannelBin] - baseline[highestChannelBin];
  531. if (val > highVal)
  532. highVal = val;
  533. }
  534. beamOnAdcVal = beamOnLevel * highVal;
  535. int maxNoise = floor(highVal);
  536. for (int j = 0; j < 64; j++)
  537. {
  538. TString name;
  539. name.Form("channelSig%d", j);
  540. channelSig[j] = new TH1D(name.Data(), name.Data(), maxNoise * 2, -maxNoise, maxNoise);
  541. }
  542. for (int i = 1; i <= nBaselineEvents; i++)
  543. {
  544. data->GetEvent(i);
  545. for (int ch = 0; ch < 64; ch++)
  546. channelSig[ch]->Fill(channels[ch] - baseline[ch]);
  547. }
  548. gChannelErrors = new TGraph(64);
  549. for (int k = 0; k < 64; k++)
  550. {
  551. double stdDev = channelSig[k]->GetStdDev();
  552. channelErrors[k] = stdDev;
  553. gChannelErrors->SetPoint(k + 1, k + 1, stdDev);
  554. }
  555. }
  556. void Beammon::prelimProfile()
  557. {
  558. highVal = 0;
  559. int histChannel = 0;
  560. double highVal_tmp[64];
  561. for (int i = 1; i < nevents - 1000; i += 1)
  562. { //nevents/nPrelimEvents){
  563. data->GetEvent(i);
  564. for (int ch = 0; ch < 64; ch++)
  565. {
  566. double val = channels[ch] - baseline[ch];
  567. /* if (val > highVal_tmp[ch])
  568. {
  569. highVal_tmp[ch] = val;
  570. }*/
  571. if (isSipm)
  572. histChannel = 64 - ch;
  573. else
  574. histChannel = ch + 1;
  575. prelimBeam->SetBinContent(histChannel, prelimBeam->GetBinContent(histChannel) + val / nPrelimEvents); //nPrelimEvents);
  576. }
  577. }
  578. TF1 *fit = new TF1("fitfunc", "gaus", 1, 64);
  579. fit->SetParameter(1, prelimBeam->GetMean());
  580. // printf("Mean: %f \n", prelimBeam->GetMean());
  581. // printf("Sigma: %f \n", prelimBeam->GetStdDev());
  582. // cout << prelimBeam->GetStdDev() << endl;
  583. fit->SetParameter(2, prelimBeam->GetStdDev());
  584. prelimBeam->Fit("fitfunc", "Q");
  585. prelimMean = fit->GetParameter(1);
  586. prelimSigma = fit->GetParameter(2);
  587. highestChannelBin = floor(prelimMean);
  588. if (highestChannelBin < 0)
  589. highestChannelBin = 0;
  590. /*
  591. highVal = 0;
  592. for (int i = 0; i < 64; i++)
  593. {
  594. if ( highVal_tmp[i] > highVal)
  595. {
  596. highVal = highVal_tmp[i];
  597. }
  598. }*/
  599. // beamOnAdcVal = beamOnLevel * highVal;
  600. delete fit;
  601. printf("\ncenter channel: %d \n", highestChannelBin);
  602. }
  603. void Beammon::ProcessData()
  604. {
  605. bool beamOn = false;
  606. bool previousBeamStatus = false;
  607. int lastSpillOn = 0;
  608. int lastSpillOff = 0;
  609. int minSpillTime = 3000; // in ms
  610. int histChannel;
  611. int highestSigVal = 0;
  612. for (int i = 1; i < nevents; i++)
  613. {
  614. if (i % 10000 == 00)
  615. printf("Event: %d / %ld \n", i, nevents);
  616. // cout << "Event: " << i << endl;
  617. data->GetEvent(i);
  618. double maxSignal = channels[highestChannelBin] - baseline[highestChannelBin];
  619. /*
  620. for(int j=0;j<64;j++){
  621. double val = channels[j] - baseline[j];
  622. if (val > maxSignal)
  623. maxSignal = val;
  624. }*/
  625. if (isAboveThreshold(maxSignal) && (i > nBaselineEvents))
  626. {
  627. beamOn = true;
  628. if ((i - lastSpillOff) > minSpillTime && !previousBeamStatus)
  629. {
  630. if (lastSpillOff != 0)
  631. spillTimes.push_back(make_pair(lastSpillOn, lastSpillOff));
  632. lastSpillOn = i;
  633. }
  634. }
  635. else
  636. {
  637. beamOn = false;
  638. if (previousBeamStatus)
  639. lastSpillOff = i;
  640. }
  641. previousBeamStatus = beamOn;
  642. int sigVal = 0;
  643. for (int j = 0; j < 64; j++)
  644. {
  645. int chan;
  646. if (isSipm)
  647. {
  648. if (j % 2 == 0)
  649. histChannel = 64 - (j + 1);
  650. else
  651. histChannel = 64 - (j - 1);
  652. }
  653. else
  654. histChannel = j + 1;
  655. double chVal = channels[j] - baseline[j];
  656. sigVal += chVal;
  657. timePosHist->SetBinContent(histChannel, i, chVal);
  658. completeIntegral += chVal;
  659. }
  660. if (sigVal > highestSigVal)
  661. {
  662. highestSigVal = sigVal;
  663. highestTimeBin = i;
  664. }
  665. // IONISATION CHAMBERS
  666. double chargeToParts = 1. / 3.2886706587037837e-15 * 1e-9 * 312e-6;
  667. if (!(ic1 != ic1))
  668. ic1Hist->SetBinContent(i, ic1 * chargeToParts);
  669. if (!(ic2 != ic2))
  670. ic2Hist->SetBinContent(i, ic2 * chargeToParts);
  671. // MW CHAMBERS
  672. if (!(mw1_focusx != mw1_focusx))
  673. mw1_focusxHist->SetBinContent(i, mw1_focusx);
  674. mw1_focusyHist->SetBinContent(i, mw1_focusy);
  675. mw2_focusxHist->SetBinContent(i, mw2_focusx);
  676. mw2_focusyHist->SetBinContent(i, mw2_focusy);
  677. if (!(mw1_posx != mw1_posx))
  678. mw1_posxHist->SetBinContent(i, mw1_posx);
  679. mw1_posyHist->SetBinContent(i, mw1_posy);
  680. mw2_posxHist->SetBinContent(i, mw2_posx);
  681. mw2_posyHist->SetBinContent(i, mw2_posy);
  682. if (mw1_posx < 100)
  683. {
  684. mwPosX->Fill(mw1_posx);
  685. mwFocusX->Fill(mw1_focusx);
  686. }
  687. }
  688. spillTimes.push_back(make_pair(lastSpillOn, lastSpillOff));
  689. TH1D *tmp = GetChannelSlice(-1);
  690. tmp->Fit("gaus");
  691. TF1 *fit = tmp->GetFunction("gaus");
  692. 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!
  693. signalDist = new TH1D("signalDist", "signal distribution", 200, 0., referenceIntegral * 2.51 * fit->GetParameter(2));
  694. signalNoiseDist = new TH1D("signalNoiseDist", "", 200, 0., referenceIntegral * 2.51 * fit->GetParameter(2));
  695. signalNoiseChannel = new TH1D("signalNoiseChannel", "", 200, 0., tmp->GetMaximum());
  696. calcBeamTime();
  697. printf("HighestBin: %d \n", highestTimeBin);
  698. }
  699. TH1D *Beammon::GetPrelimProfile()
  700. {
  701. if (!prelimBeam)
  702. {
  703. printf("WARNING: preliminary profile not yet generated");
  704. }
  705. return prelimBeam;
  706. }
  707. TH1D *Beammon::TimeAverage(int timeStart, int timeStop)
  708. {
  709. if (timeStart == -1)
  710. timeStart = startTime;
  711. if (timeStop == -1)
  712. timeStop = stopTime;
  713. int count = 0;
  714. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  715. TH1D *tmp;
  716. printf("\n\n start: %d, stop: %d \n\n", timeStart, timeStop);
  717. for (int i = timeStart; i <= timeStop; i++)
  718. {
  719. tmp = GetChannelSlice(i, 0);
  720. if (isAboveThreshold(tmp))
  721. {
  722. res->Add(tmp, 1.);
  723. count++;
  724. }
  725. delete tmp;
  726. }
  727. if (count)
  728. {
  729. for (int n = 1; n <= 64; n++)
  730. {
  731. res->SetBinContent(n, res->GetBinContent(n) / count);
  732. }
  733. return res;
  734. }
  735. else
  736. return 0;
  737. }
  738. TH1D *Beammon::TimeIntSignal(int timeStart, int timeStop)
  739. {
  740. if (timeStart == -1)
  741. timeStart = startTime;
  742. if (timeStop == -1)
  743. timeStop = stopTime;
  744. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  745. TH1D *tmp;
  746. for (int i = timeStart; i < timeStop; i++)
  747. {
  748. tmp = GetChannelSlice(i, 0);
  749. if (isAboveThreshold(tmp))
  750. {
  751. res->Add(tmp, 1.);
  752. beamOnTime++;
  753. }
  754. delete tmp;
  755. }
  756. return res;
  757. }
  758. TH1D *Beammon::AverageHist(int dir, int timeStart, int timeStop)
  759. { // dir = 0 => x direction, 1 => y
  760. TH2D *hist = timePosHist;
  761. int nbins1, nbins2;
  762. double binStart, binStop;
  763. int firstBin, secondBin;
  764. if (!dir)
  765. {
  766. // Timeaverage
  767. if ((timeStart != -1) && (timeStop != -1))
  768. {
  769. firstBin = timeStart; //hist->GetYaxis()->FindBin((timeStart-startTime)/readOutFreq);
  770. secondBin = timeStop; //hist->GetYaxis()->FindBin((timeStop-startTime)/readOutFreq);
  771. nbins1 = hist->GetNbinsX();
  772. nbins2 = secondBin - firstBin;
  773. binStart = hist->GetXaxis()->GetBinCenter(1);
  774. binStop = hist->GetXaxis()->GetBinCenter(nbins1);
  775. }
  776. else
  777. {
  778. nbins1 = hist->GetNbinsX();
  779. nbins2 = hist->GetNbinsY();
  780. binStart = hist->GetXaxis()->GetBinCenter(1);
  781. binStop = hist->GetXaxis()->GetBinCenter(nbins1);
  782. firstBin = 1;
  783. secondBin = nbins2;
  784. }
  785. }
  786. else
  787. {
  788. nbins1 = hist->GetNbinsY();
  789. nbins2 = hist->GetNbinsX();
  790. binStart = hist->GetYaxis()->GetBinCenter(1);
  791. binStop = hist->GetYaxis()->GetBinCenter(nbins1);
  792. firstBin = 1;
  793. secondBin = hist->GetNbinsX();
  794. }
  795. TH1D *retHist = new TH1D("average", "average", nbins1, binStart, binStop);
  796. for (int i = 1; i <= nbins1; i++)
  797. {
  798. int count = 0;
  799. double avg = 0;
  800. for (int j = firstBin; j <= secondBin; j++)
  801. {
  802. if (!dir)
  803. {
  804. /*bool beam = beamOnVector[j];
  805. if(beam==false)
  806. continue;
  807. //else
  808. //printf("TRUE");*/
  809. avg += hist->GetBinContent(i, j);
  810. }
  811. else
  812. avg += hist->GetBinContent(j, i);
  813. count++;
  814. }
  815. if (count > 0)
  816. avg /= count;
  817. retHist->SetBinContent(i, avg);
  818. }
  819. return retHist;
  820. }
  821. TH1D *Beammon::GetTimeAvg()
  822. {
  823. int n = spillTimes.size();
  824. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  825. for (int i = 0; i < n; i++)
  826. {
  827. TH1D *tmp = TimeAverage(spillTimes[i].first, spillTimes[i].second);
  828. if (!tmp)
  829. printf("\n\n timeAvgHist is Null! \n\n");
  830. if (tmp)
  831. res->Add(tmp, 1. / n);
  832. }
  833. res->SetTitle("average beam profile");
  834. return res;
  835. }
  836. TH1D *Beammon::GetTimeAvg(int nSpill)
  837. {
  838. if (nSpill >= spillTimes.size())
  839. return 0;
  840. TH1D *res = TimeAverage(spillTimes[nSpill].first, spillTimes[nSpill].second);
  841. res->SetTitle("average beam profile");
  842. return res;
  843. }
  844. TH1D *Beammon::GetTimeAvg(int nSpill, int nMeasurements)
  845. {
  846. if (nSpill >= spillTimes.size())
  847. return 0;
  848. TH1D *res = TimeAverage(spillTimes[nSpill].second - nMeasurements, spillTimes[nSpill].second);
  849. TString title = TString::Format("average beam profile, %d ms, spill %d", nMeasurements, nSpill);
  850. TString name = TString::Format("avg%dmsspill%d", nMeasurements, nSpill);
  851. res->SetTitle(title.Data());
  852. res->SetName(name.Data());
  853. return res;
  854. }
  855. TH2D *Beammon::GetTimeAvgScatter(int nSpill, int nMeasurements)
  856. {
  857. int tStart = spillTimes[nSpill].first;
  858. if (nMeasurements == -1)
  859. tStart = spillTimes[nSpill].second - nMeasurements;
  860. TH2D *res = new TH2D("tavgScatter", "tavgScatter", 64, 0.5, 64.5, 400, 0., highVal);
  861. for (int i = spillTimes[nSpill].first; i < spillTimes[nSpill].second; i++)
  862. {
  863. TH1D *tmp = GetChannelSlice(i, 0);
  864. if (!isAboveThreshold(tmp))
  865. continue;
  866. for (int bin = 1; bin <= 64; bin++)
  867. {
  868. res->Fill(bin, tmp->GetBinContent(bin));
  869. }
  870. delete tmp;
  871. }
  872. TString title = TString::Format("beam profile, %d ms, spill %d", nMeasurements, nSpill);
  873. res->SetTitle(title.Data());
  874. return res;
  875. }
  876. TH2D *Beammon::GetTimeAvgScatter()
  877. {
  878. TH2D *res = new TH2D("tavgScatter", "tavgScatter", 64, 0.5, 64.5, 400, 0., highVal);
  879. int n = spillTimes.size();
  880. for (int i = 0; i < n; i++)
  881. {
  882. res->Add(GetTimeAvgScatter(i, -1), 1. / n);
  883. }
  884. return res;
  885. }
  886. TH1D *Beammon::GetChannelSigDist(int channel)
  887. {
  888. TString sName = TString("signal distribution channel ") + TString(channel);
  889. double maxAmp = 0;
  890. for (int n = 0; n < spillTimes.size(); n++)
  891. {
  892. TH1D *tmp = GetSpillSlice(n);
  893. if (tmp->GetMaximum() > maxAmp)
  894. maxAmp = tmp->GetMaximum();
  895. delete tmp;
  896. }
  897. TH1D *res = new TH1D(sName.Data(), sName.Data(), 200, 0., maxAmp);
  898. for (int n = 0; n < spillTimes.size(); n++)
  899. {
  900. TH1D *tmp = GetSpillSlice(n);
  901. for (int bin = 1; bin <= tmp->GetNbinsX(); bin++)
  902. {
  903. res->Fill(tmp->GetBinContent(bin));
  904. //printf("bin: %d, amp: %f \n", bin, tmp->GetBinContent(bin));
  905. }
  906. delete tmp;
  907. }
  908. return res;
  909. }
  910. TH1D *Beammon::GetChannelAvg(bool beamOn)
  911. {
  912. return AverageHist(1, -1, -1);
  913. }
  914. TH1D *Beammon::GetTimeSlice(int channel, int timeStart, int timeStop, bool beamOn)
  915. {
  916. if (timeStart == -1)
  917. timeStart = startTime;
  918. if (timeStop == -1)
  919. timeStop = stopTime;
  920. int nbins = timeStop - timeStart;
  921. TH1D *tmp2 = new TH1D("ts", "timeslice", nbins, (timeStart - startTime) / readOutFreq, (timeStop - startTime) / readOutFreq);
  922. if (channel == -1)
  923. channel = highestChannelBin;
  924. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  925. int offset = tmp->GetXaxis()->FindBin(timeStart);
  926. for (int bin = 1; bin <= nbins; bin++)
  927. {
  928. tmp2->SetBinContent(bin, tmp->GetBinContent(bin + offset));
  929. }
  930. TString title = TString::Format("time projection of channel %d", channel);
  931. tmp2->SetTitle(title.Data());
  932. return tmp2;
  933. }
  934. TCanvas *Beammon::GetTimeSliceCanvas(int channel)
  935. {
  936. TCanvas *c1 = new TCanvas();
  937. c1->SetName("timeslice");
  938. int nbins = stopTime - startTime;
  939. TH1D *tmp2 = new TH1D("timeslice", "", nbins, 0., (stopTime - startTime) / readOutFreq);
  940. if (channel == -1)
  941. channel = highestChannelBin;
  942. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  943. for (int bin = 1; bin <= nbins; bin++)
  944. {
  945. tmp2->SetBinContent(bin, tmp->GetBinContent(bin));
  946. }
  947. tmp2->GetXaxis()->SetTitle("times [s]");
  948. TString label;
  949. label.Form("adc counts / %d #mus", intTime);
  950. tmp2->GetYaxis()->SetTitle(label.Data());
  951. tmp2->Draw("P");
  952. return c1;
  953. }
  954. TCanvas *Beammon::GetSignalNoiseChannel(int channel)
  955. {
  956. TCanvas *c1 = new TCanvas();
  957. c1->SetName("signalNoiseChannel");
  958. int nbins = stopTime - startTime;
  959. if (channel == -1)
  960. channel = highestChannelBin;
  961. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  962. for (int bin = 1; bin <= nbins; bin++)
  963. {
  964. signalNoiseChannel->Fill(tmp->GetBinContent(bin));
  965. }
  966. signalNoiseChannel->GetYaxis()->SetTitle("entries");
  967. TString label;
  968. label.Form("adc counts / %d #mus", intTime);
  969. signalNoiseChannel->GetXaxis()->SetTitle(label.Data());
  970. signalNoiseChannel->Draw();
  971. return c1;
  972. }
  973. TH1D *Beammon::GetChannelSlice(int time, int offset)
  974. {
  975. if (time == -1)
  976. time = highestTimeBin + offset;
  977. //printf("time: %d\n", time);
  978. TH1D *res = timePosHist->ProjectionX("", time, time); //timePosHist->ProjectionX("",timePosHist->GetYaxis()->FindBin(time+offset),timePosHist->GetYaxis()->FindBin(time+offset));
  979. //res->SetError(channelErrors);
  980. res->SetTitle("single measurement (1ms)");
  981. res->SetName("Channel_" + (TString)time);
  982. res->Fit("gaus", "Q0");
  983. double mean = res->GetFunction("gaus")->GetParameter(1);
  984. double sigma = res->GetFunction("gaus")->GetParameter(2);
  985. return res;
  986. }
  987. TH1D *Beammon::GetChannelSlice2(int time, int offset)
  988. {
  989. if (time == -1)
  990. time = highestTimeBin + offset;
  991. //printf("time: %d\n", time);
  992. TH1D *res = timePosHist->ProjectionX("", time, time); //timePosHist->ProjectionX("",timePosHist->GetYaxis()->FindBin(time+offset),timePosHist->GetYaxis()->FindBin(time+offset));
  993. //res->SetError(channelErrors);
  994. res->SetTitle("single measurement (1ms)");
  995. res->SetName("Channel_" + (TString)time);
  996. res->Fit("gaus", "Q0");
  997. double mean = res->GetFunction("gaus")->GetParameter(1);
  998. mean_ave += mean;
  999. double sigma = res->GetFunction("gaus")->GetParameter(2);
  1000. sigma_ave += sigma;
  1001. return res;
  1002. }
  1003. TGraphErrors *Beammon::GetChannelGraph(int time, int offset)
  1004. {
  1005. if (time == -1)
  1006. time = highestTimeBin + offset;
  1007. double xErr = 0.8 / sqrt(12);
  1008. TH1D *res = timePosHist->ProjectionX("", time, time);
  1009. TGraphErrors *graph = new TGraphErrors(64);
  1010. for (int ch = 1; ch <= 64; ch++)
  1011. {
  1012. graph->SetPoint(ch, ch, res->GetBinContent(ch));
  1013. // graph->SetPointError(ch,xErr,channelErrors[ch-1]);//sqrt(channelErrors[ch-1]*channelErrors[ch-1]+res->GetBinContent(ch)));
  1014. graph->SetPointError(ch, xErr, channelErrors[ch - 1]);
  1015. }
  1016. delete res;
  1017. return graph;
  1018. }
  1019. TH1D *Beammon::GetChannelGraphHist(int time, int offset)
  1020. {
  1021. if (time == -1)
  1022. time = highestTimeBin + offset;
  1023. double xErr = 0.8 / sqrt(12);
  1024. TH1D *res = timePosHist->ProjectionX("", time, time);
  1025. return res;
  1026. }
  1027. TH1D *Beammon::GetSignalOverTime()
  1028. {
  1029. TH1D *tmp = timePosHist->ProjectionY("", 1, 64);
  1030. tmp->SetTitle("signal over time");
  1031. tmp->SetName("signal over time");
  1032. return tmp;
  1033. }
  1034. void Beammon::PrintSpillTimes()
  1035. {
  1036. printf("%lu spills recorded: \n", spillTimes.size());
  1037. // cout << spillTimes.size() << "spills recorded: " << endl;
  1038. for (int n = 0; n < spillTimes.size(); n++)
  1039. printf("%02d: %f - %f \n", n, (spillTimes[n].first - startTime) / readOutFreq, (spillTimes[n].second - startTime) / readOutFreq);
  1040. }
  1041. TH1D *Beammon::calcTimeCenters(int start, int stop)
  1042. {
  1043. int tStart = start;
  1044. int tStop = stop;
  1045. if (start == -1)
  1046. tStart = startTime;
  1047. if (stop == -1)
  1048. tStop = stopTime;
  1049. int nbins = (tStop - tStart);
  1050. double mean = 0;
  1051. double sigma = 0;
  1052. double constant;
  1053. TH1D *res = new TH1D("timeIntSignal_tmp", "time integrated signal", 64, 0.5, 64.5);
  1054. TCanvas *tmpcanvas = new TCanvas();
  1055. int offset = tStart;
  1056. TF1 *fit = new TF1("gaus_L", "gaus+[3]", 1, 64);
  1057. TF1 *fit2 = new TF1("gaus_2", "gaus", 1, 64);
  1058. fit2->SetParameter(0, 2000);
  1059. fit2->SetParameter(1, mean_ave);
  1060. fit2->SetParameter(2, sigma_ave);
  1061. for (int bin = tStart; bin <= tStop; bin++)
  1062. {
  1063. TGraphErrors *tmp = GetChannelGraph(bin, 0);
  1064. TH1D *tmp2 = GetChannelGraphHist(bin, 0);
  1065. tmp->Fit("gaus_2", "Q0", "", mean_ave - 3 * sigma_ave, mean_ave + 3 * sigma_ave);
  1066. TF1 *func = tmp->GetFunction("gaus_2");
  1067. if (tmp2->GetMean() * 0.8 > 0)
  1068. fibreHist_weighted->SetBinContent(bin + 1, tmp2->GetMean() * 0.8);
  1069. else
  1070. fibreHist_weighted->SetBinContent(bin + 1, 0);
  1071. if (tmp2->GetEntries() > 0)
  1072. sigHist_weighted->SetBinContent(bin - 1, tmp2->GetEntries() * signalToParticles);
  1073. else
  1074. sigHist_weighted->SetBinContent(bin - 1, 0);
  1075. if (func)
  1076. {
  1077. if (!isAboveThreshold(tmp))
  1078. continue;
  1079. constant = func->GetParameter(0);
  1080. mean = func->GetParameter(1) * channelWidth;
  1081. sigma = func->GetParameter(2) * channelWidth;
  1082. if (mean <= 0 || mean > 64)
  1083. continue;
  1084. /*if (constant > 1e5 || mean > (mean_ave * channelWidth + 0.2) || mean < (mean_ave * channelWidth - 0.2))
  1085. {
  1086. fit->SetParameter(0, 1000);
  1087. fit->SetParameter(1, mean_ave);
  1088. fit->SetParameter(2, sigma_ave);
  1089. fit->SetParameter(3, 0.1);
  1090. fit->SetParLimits(0, 100, 2e5);
  1091. fit->SetParLimits(1, 1, 64);
  1092. fit->SetParLimits(2, 1, 10);
  1093. fit->SetParLimits(3, -50, 50);
  1094. tmp->Fit("gaus", "Q0", "", mean_ave - 3 * sigma_ave, mean_ave + 3 * sigma_ave);
  1095. func = tmp->GetFunction("gaus");
  1096. constant = func->GetParameter(0);
  1097. mean = func->GetParameter(1) * channelWidth;
  1098. sigma = func->GetParameter(2) * channelWidth;
  1099. }*/
  1100. if (abs(constant) > 1e5)
  1101. {
  1102. Fit_Fail_Number = Fit_Fail_Number - 1;
  1103. if (Fit_Fail_Number > 0)
  1104. {
  1105. tmpcanvas->cd();
  1106. tmp->Draw("AP");
  1107. func->Draw("SAME");
  1108. tmpcanvas->Modified();
  1109. tmpcanvas->Write();
  1110. }
  1111. continue;
  1112. }
  1113. //Fill the error hidto
  1114. double erry, tmpx;
  1115. bool discard = false;
  1116. int nPoint = 0;
  1117. for (int chn = 1; chn < 65; chn++)
  1118. {
  1119. tmp->GetPoint(nPoint, tmpx, erry);
  1120. SignalErrHist->Fill((double)tmpx, (erry - func->Eval(tmpx)) / (constant * sigma));
  1121. if ((erry - func->Eval(tmpx)) / (constant * sigma) > discardUpperThreshold || (erry - func->Eval(tmpx)) / (constant * sigma) < discardLowerThreshold)
  1122. {
  1123. tmp->RemovePoint(nPoint);
  1124. nPoint--;
  1125. discard = true;
  1126. }
  1127. nPoint++;
  1128. }
  1129. double signal = sigma * 2.51 * constant * signalToParticles;
  1130. //Fill the new tree
  1131. event_count++;
  1132. //data->GetEntry(bin);
  1133. beamPosX_1 = mean;
  1134. beamFocusX_1 = 2.35 * sigma;
  1135. beamSignal_1 = signal;
  1136. ic1_1 = ic1Hist->GetBinContent(bin - alignintIc);
  1137. ic2_1 = ic2Hist->GetBinContent(bin - alignintIc);
  1138. mw1_focusx_1 = mw1_focusxHist->GetBinContent(bin - alignint);
  1139. mw1_posx_1 = mw1_posxHist->GetBinContent(bin - alignint);
  1140. mw2_focusx_1 = mw2_focusxHist->GetBinContent(bin - alignint);
  1141. mw2_posx_1 = mw2_posxHist->GetBinContent(bin - alignint);
  1142. beamOn = 1;
  1143. newdata->Fill();
  1144. if (event_count % 5000 == 0)
  1145. {
  1146. printf("Signal events: %d ", event_count);
  1147. printf("Event number: %d\n", bin);
  1148. }
  1149. if (signal < 0 || signal != signal)
  1150. continue;
  1151. //Fill the Histograms
  1152. beamPosX->Fill(mean);
  1153. fibreHist->Fill(bin, mean);
  1154. mwpcHist->Fill(bin, mw1_posxHist->GetBinContent(bin - alignint));
  1155. beamFocusX->Fill(2.35 * sigma);
  1156. beamPosTimeGraph->SetPoint(bin, bin, mean);
  1157. beamPosTimeGraph->SetPointError(bin, 0., func->GetParError(1));
  1158. timeCenters->Fill((bin) / readOutFreq, mean);
  1159. timeWidths->Fill((bin) / readOutFreq, sigma * 2.35);
  1160. timeCenters_MWPC->Fill((bin) / readOutFreq, mw1_posxHist->GetBinContent(bin - alignint));
  1161. timeWidths_MWPC->Fill((bin) / readOutFreq, mw1_focusxHist->GetBinContent(bin - alignint));
  1162. Diff_timeCenters->Fill((bin) / readOutFreq, mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1163. if (mw1_posxHist->GetBinContent(bin - alignint) < 20)
  1164. {
  1165. mwPosScatterGraph->SetPoint(mwPosScatterGraph->GetN(), mw1_posxHist->GetBinContent(bin - alignint), mean);
  1166. //positionComp->Fill(mean - mw1_posxHist->GetBinContent(bin-4) +111.5415 - 32.02);
  1167. for (int i = 0; i < 20; i++)
  1168. mwPosScatterPlot[i]->Fill(mw1_posxHist->GetBinContent(bin - i + 10), mean);
  1169. }
  1170. double sigError = 2.51 * sqrt(sigma * sigma * func->GetParError(0) * func->GetParError(0) + constant * constant * func->GetParError(2) * func->GetParError(2)) * signalToParticles;
  1171. if (signal > 1e5 || sigError > signal)
  1172. {
  1173. //signal = 0;
  1174. sigError = 0;
  1175. }
  1176. if (signal)
  1177. {
  1178. signalDist->Fill(signal);
  1179. if (ic1Hist->GetBinContent(bin - alignintIc))
  1180. {
  1181. sigHist->Fill(bin - alignintIc - 1, signal);
  1182. signalComp->Fill((signal - ic1Hist->GetBinContent(bin - alignintIc)) / signal);
  1183. double x, y, totalSignal = 0, signalError = 0;
  1184. for (int k = 0; k < 63; k++)
  1185. {
  1186. tmp->GetPoint(k, x, y);
  1187. signalError += tmp->GetErrorY(k) * tmp->GetErrorY(k);
  1188. totalSignal += y * signalToParticles;
  1189. }
  1190. signalError = sqrt(signalError) * signalToParticles;
  1191. sigIcScatterGraph->SetPoint(sigIcScatterGraph->GetN(), ic1Hist->GetBinContent(bin - alignintIc), totalSignal);
  1192. sigIcScatterGraph->SetPointError(sigIcScatterGraph->GetN(), 0, signalError);
  1193. for (int i = 0; i < 20; i++)
  1194. sigIcScatterPlot[i]->Fill(ic1Hist->GetBinContent(bin - i + 10), signal);
  1195. Center_Signal->Fill(signal, mean);
  1196. // sigIcScatterPlot[14]->Fill(ic1Hist->GetBinContent(bin - 14 + 10), signal);
  1197. sigIcScatterPlot_ic2->Fill(ic2Hist->GetBinContent(bin - 14 + 10), signal);
  1198. }
  1199. }
  1200. else
  1201. {
  1202. //ic1Hist->SetBinContent(bin, 0);
  1203. }
  1204. widthComp->Fill(sigma * 2.51 - mw1_focusxHist->GetBinContent(bin - alignint));
  1205. //Center_Signal->Fill(totalSignal, mean);
  1206. Diff_hist->Fill(mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1207. Diff_Signal->Fill(signal, mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1208. Diff_ic1->Fill(ic1Hist->GetBinContent(bin - alignintIc), mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1209. icIntegral += ic1Hist->GetBinContent(bin);
  1210. signalTimeGraph->SetPoint(bin, bin, signal);
  1211. signalTimeGraph->SetPointError(bin, 0., sigError);
  1212. beamOnTime++;
  1213. double c2 = func->GetChisquare() / func->GetNDF();
  1214. if (c2 < 100)
  1215. chisquareDist->Fill(c2);
  1216. }
  1217. delete func;
  1218. delete tmp;
  1219. delete tmp2;
  1220. }
  1221. delete tmpcanvas;
  1222. return res;
  1223. }
  1224. void Beammon::GetTimeCenters()
  1225. {
  1226. TH1D *tmp_TimeCenter;
  1227. double mean_MWPC = mwPosX->GetMean();
  1228. shift = mean_ave * 0.8 / 5 - mwPosX->GetMean();
  1229. double max_ic1 = ic1Hist->GetMaximum();
  1230. beamOnTime = 0;
  1231. int nbins = stopTime - startTime;
  1232. mean_ave = prelimBeam->GetMean();
  1233. sigma_ave = prelimBeam->GetStdDev();
  1234. printf("Mean: %f \n", mean_ave);
  1235. printf("Sigma: %f \n", sigma_ave);
  1236. 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));
  1237. timeWidths = new TH2D("timeWidths", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, 0.4 * sigma_ave * 2.35, 0.95 * sigma_ave * 2.35);
  1238. timeIntSignal = new TH1D("timeIntSignal", "time integrated signal", 64, 0.5, 64.5);
  1239. timeCenters_MWPC = new TH2D("timeCenters_MWPC", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, mean_MWPC - 5 * sigma_pos, mean_MWPC + 5 * sigma_pos);
  1240. timeWidths_MWPC = new TH2D("timeWidths_MWPC", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, 1.5, mw1_focusxHist->GetMaximum() * 1.05);
  1241. timeIntSignal_MWPC = new TH1D("timeIntSignal_MWPC", "time integrated signal", 64, 0.5, 64.5);
  1242. for (int j = 0; j < 20; j++)
  1243. {
  1244. TString name;
  1245. name.Form("sigIcScatterPlot%d", j);
  1246. sigIcScatterPlot[j] = new TH2D(name.Data(), name.Data(), 200, 0, -1, 200, 0, -1);
  1247. TString name2;
  1248. name2.Form("mwPosScatterPlot%d", j);
  1249. 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));
  1250. }
  1251. sigIcScatterPlot_ic2 = new TH2D("sigIcScatterPlot_ic2", "sigIcScatterPlot_ic2", 200, 0, -1, 200, 0, -1);
  1252. 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));
  1253. Diff_timeCenters = new TH2D("Diff_time", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, -6 * sigma_pos, 6 * sigma_pos);
  1254. Diff_hist = new TH1D("Diff_hist", "l", 200, -2, 2.);
  1255. Diff_Signal = new TH2D("Diff_Signal", "", 200, 0., max_ic1 - 5000, 200, -6 * sigma_pos, 6 * sigma_pos);
  1256. Diff_ic1 = new TH2D("Diff_ic1", "", 200, 0., max_ic1 - 5000, 200, -6 * sigma_pos, 6 * sigma_pos);
  1257. b_beamPosX_1 = newdata->Branch("beamPosX_1", &beamPosX_1);
  1258. b_beamFocusX_1 = newdata->Branch("beamFocusX_1", &beamFocusX_1);
  1259. b_beamSignal_1 = newdata->Branch("beamSignal_1", &beamSignal_1);
  1260. bb_ic1 = newdata->Branch("ic1_1", &ic1_1);
  1261. bb_ic2 = newdata->Branch("ic2_1", &ic2_1);
  1262. bb_mw1_focusx = newdata->Branch("mw1_focusx_1", &mw1_focusx_1);
  1263. bb_mw1_posx = newdata->Branch("mw1_posx_1", &mw1_posx_1);
  1264. bb_mw2_focusx = newdata->Branch("mw2_focusx_1", &mw2_focusx_1);
  1265. bb_mw2_posx = newdata->Branch("mw2_posx_1", &mw2_posx_1);
  1266. b_beamOn = newdata->Branch("beamOn", &beamOn);
  1267. newdata->SetBranchStatus("*", 1);
  1268. for (int i = 0; i < spillTimes.size(); i++)
  1269. {
  1270. tmp_TimeCenter = calcTimeCenters(spillTimes[i].first + 200, spillTimes[i].second);
  1271. timeIntSignal->Add(tmp_TimeCenter, 1.);
  1272. delete tmp_TimeCenter;
  1273. beamPosX_1 = 0;
  1274. beamFocusX_1 = 0;
  1275. beamSignal_1 = 0;
  1276. ic1_1 = 0;
  1277. mw1_focusx_1 = 0;
  1278. mw1_posx_1 = 0;
  1279. beamOn = 0;
  1280. newdata->Fill();
  1281. }
  1282. SignalErrHist->Write();
  1283. printf("Fit_Fail_Counts=%d\n", Fit_Fail_Counts);
  1284. //Draw timeCenter
  1285. timeCenters->GetXaxis()->SetTitle("time [s]");
  1286. TString label;
  1287. label.Form("mean position[mm]");
  1288. timeCenters->GetYaxis()->SetTitle(label.Data());
  1289. timeCenters->Write();
  1290. //Draw timeCenter_MWPC
  1291. timeCenters_MWPC->GetXaxis()->SetTitle("time [s]");
  1292. TString label2;
  1293. label2.Form("mean position[mm]");
  1294. timeCenters_MWPC->GetYaxis()->SetTitle(label.Data());
  1295. timeCenters_MWPC->Write();
  1296. //Draw timeWidths
  1297. timeWidths->GetXaxis()->SetTitle("time [s]");
  1298. TString label12;
  1299. label12.Form("width fiber[mm]");
  1300. timeWidths->GetYaxis()->SetTitle(label12.Data());
  1301. timeWidths->Write();
  1302. //Draw timeWidths_MWPC
  1303. timeWidths_MWPC->GetXaxis()->SetTitle("time [s]");
  1304. TString label13;
  1305. label13.Form("width MWPC[mm]");
  1306. timeWidths_MWPC->GetYaxis()->SetTitle(label12.Data());
  1307. timeWidths_MWPC->Write();
  1308. Center_Signal->GetXaxis()->SetTitle("Signal [particles]");
  1309. TString label123;
  1310. label123.Form("Position_Fiber[mm]");
  1311. Center_Signal->GetYaxis()->SetTitle(label123.Data());
  1312. Center_Signal->Write();
  1313. Center_Signal->ProfileX()->Write();
  1314. //Draw Diff_timeCenters
  1315. Diff_timeCenters->GetXaxis()->SetTitle("time [s]");
  1316. TString label3;
  1317. label3.Form("mean position diff[mm]");
  1318. Diff_timeCenters->GetYaxis()->SetTitle(label.Data());
  1319. Diff_timeCenters->Write();
  1320. //Draw Diff_Signal
  1321. Diff_Signal->GetXaxis()->SetTitle("signal");
  1322. TString label4;
  1323. label4.Form("mean position diff[mm]");
  1324. Diff_Signal->GetYaxis()->SetTitle(label.Data());
  1325. Diff_Signal->Write();
  1326. //Draw Diff_Signal
  1327. Diff_ic1->GetXaxis()->SetTitle("ic1");
  1328. TString label5;
  1329. label5.Form("mean position diff[mm]");
  1330. Diff_ic1->GetYaxis()->SetTitle(label.Data());
  1331. Diff_ic1->Write();
  1332. TObjArray aSlices, bSlices;
  1333. Diff_Signal->FitSlicesY(0, 0, -1, 5, "QNR", &aSlices);
  1334. for (int i = 0; i < 4; i++)
  1335. {
  1336. aSlices[i]->Write();
  1337. }
  1338. Diff_ic1->FitSlicesY(0, 0, -1, 5, "QNR", &bSlices);
  1339. for (int i = 0; i < 4; i++)
  1340. {
  1341. bSlices[i]->Write();
  1342. }
  1343. mean_ave = beamPosX->GetMean();
  1344. sigma_pos = beamPosX->GetStdDev();
  1345. sigma_ave = beamFocusX->GetMean();
  1346. return;
  1347. }
  1348. pair<int, int> Beammon::GetSpillTimes(int nSpill)
  1349. {
  1350. return spillTimes[nSpill];
  1351. }
  1352. TH2D *Beammon::GetSpillCenters(int nSpill)
  1353. {
  1354. //return calcTimeCenters(spillTimes[nSpill].first, spillTimes[nSpill].second);
  1355. }
  1356. TH1D *Beammon::GetSpillSlice(int nSpill)
  1357. {
  1358. return GetTimeSlice(-1, spillTimes[nSpill].first - 500, spillTimes[nSpill].second + 500, true);
  1359. }
  1360. TH1D *Beammon::GetSignalDistribution()
  1361. {
  1362. /*int n = signalDist->GetNbinsX();
  1363. for(int i = 1; i<= n; i++){
  1364. signalDist->SetBinContent(i,signalDist->GetBinContent(i)*signalDistScale);
  1365. }*/
  1366. signalDist->Scale(signalDistScale);
  1367. return signalDist;
  1368. }
  1369. TCanvas *Beammon::GetSignalDistributionCanvas()
  1370. {
  1371. TCanvas *c1 = new TCanvas();
  1372. c1->SetName("signalDistributionCanvas");
  1373. signalDist->Draw();
  1374. signalDist->SetTitle("");
  1375. signalDist->GetYaxis()->SetTitle("entries");
  1376. TString label;
  1377. label.Form("integrated signal [adc counts / %d #mus]", intTime);
  1378. signalDist->GetXaxis()->SetTitle(label.Data());
  1379. //c1->SaveAs("dummy");
  1380. return c1;
  1381. }
  1382. TH2D *Beammon::GetSimpleView()
  1383. {
  1384. return timePosHist;
  1385. }
  1386. TH1D *Beammon::GetBaselineHist()
  1387. {
  1388. TH1D *tmp = new TH1D("baseline", "baseline", 64, 0.5, 64.5);
  1389. for (int n = 1; n <= 64; n++)
  1390. {
  1391. tmp->SetBinContent(n, baseline[n - 1]);
  1392. }
  1393. return tmp;
  1394. }
  1395. void Beammon::Close()
  1396. {
  1397. delete timePosHist;
  1398. fileOut->Close();
  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(64);
  1493. TGraphErrors *tmp = GetChannelGraph(-1, offset);
  1494. double x, y, errx, erry;
  1495. for (int ch = 0; ch < 63; 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 <= 64)
  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, 64, 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., 64.);
  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[64];
  1589. double yErrors[64];
  1590. double xValues[64];
  1591. double xErrors[64];
  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 <= 64; 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. delete tmp;
  1620. }
  1621. }
  1622. for (int bin = 0; bin < 64; 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 < 64; 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(64, 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, 64, 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., 64.);
  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 <= 64)
  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, 64);
  1713. TH1D *means = (TH1D *)sigIcScatterPlot[10 + alignintIc]->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 - alignint) - 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. TLegend* 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. TLegend * 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. TLegend * 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. TLegend * 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. TLegend * 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. TLegend * 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. }