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.

2170 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. PrintProfile(i)->Delete();
  403. }
  404. FitAverage()->Write();
  405. FitAverage()->Delete();;
  406. PrintBaseline()->Write();
  407. PrintBaseline()->Delete();//added
  408. //for(int i=0;i<64;i++)
  409. // PrintChannelNoise(i)->Write();
  410. GetSignalOverTime()->Write();
  411. GetSignalOverTime()->Delete();
  412. for (int i = 0; i < 5; i++){
  413. GetChannelSlice2(-1, i)->Write();
  414. GetChannelSlice2(-1, i)->Delete();
  415. }
  416. //fileIn->Close();
  417. GetTimeSliceCanvas(-1)->Write();
  418. timePosHist->ProjectionX()->Write();
  419. GetTimeCenters();
  420. GetSignalDistributionCanvas()->Write();
  421. GetSignalDistribution()->Write();
  422. GetSignalNoiseChannel()->Write();
  423. chisquareDist->Write();
  424. sigIcScatterGraph->Write();
  425. mwPosScatterGraph->Write();
  426. for (int i = 6; i < 15; i++)
  427. {
  428. sigIcScatterPlot[i]->Write();
  429. mwPosScatterPlot[i]->Write();
  430. }
  431. mwPosScatterPlot[11]->Write();
  432. sigIcScatterPlot[11]->Write();
  433. mw1_focusxHist->Write();
  434. mw1_posxHist->Write();
  435. mwPosX->Write();
  436. mwFocusX->Write();
  437. beamPosX->Write();
  438. beamFocusX->Write();
  439. TVectorD v(6);
  440. v[0] = GetIntSignal();
  441. v[1] = GetBeamOnTime();
  442. v[2] = signalDist->GetStdDev();
  443. v[3] = icIntegral;
  444. v[4] = completeIntegral;
  445. v[5] = fwhm;
  446. v.Write("beaminfo");
  447. beamPosTimeGraph->Write();
  448. signalTimeGraph->Write();
  449. gChannelErrors->Write();
  450. ic1Hist->Write();
  451. ic2Hist->Write();
  452. sigHist->Write();
  453. fibreHist->Write();
  454. mwpcHist->Write();
  455. fibreHist_weighted->Write();
  456. sigHist_weighted->Write();
  457. GetSig_e_ic_Hist()->Write();
  458. GetSig_e_ic_Hist_Weighted()->Write();
  459. signalComp->Write();
  460. GetMwPosComp()->Write();
  461. widthComp->Write();
  462. icSignalComp()->Write();
  463. signalOverTime2d();
  464. IcFitSlicesY();
  465. PrintSigIcGraph()->Write();
  466. //GetTimeCenters();
  467. newdata->AutoSave();
  468. Close();
  469. // v.Delete();
  470. }
  471. void Beammon::calcBaseline(bool useFirstEvents)
  472. {
  473. // use the first/last nBaselineEvents events to calculate the average/baseline
  474. if (!useFirstEvents)
  475. { // last
  476. for (int i = nevents - nBaselineEvents; i < nevents; i++)
  477. {
  478. data->GetEvent(i);
  479. for (int ch = 0; ch < 64; ch++)
  480. {
  481. baseline[ch] += channels[ch] / nBaselineEvents;
  482. }
  483. }
  484. }
  485. else
  486. {
  487. for (int i = 1; i <= nBaselineEvents; i++)
  488. {
  489. data->GetEvent(i);
  490. for (int ch = 0; ch < 64; ch++)
  491. {
  492. baseline[ch] += channels[ch] / nBaselineEvents;
  493. }
  494. }
  495. }
  496. }
  497. TCanvas *Beammon::PrintBaseline()
  498. {
  499. TCanvas *c1 = new TCanvas();
  500. c1->cd();
  501. TGraphErrors *g = new TGraphErrors(64);
  502. for (int k = 0; k < 64; k++)
  503. {
  504. double stdDev = channelSig[k]->GetStdDev();
  505. channelErrors[k] = stdDev;
  506. g->SetPoint(k, k, baseline[k]);
  507. g->SetPointError(k, 0., channelErrors[k]);
  508. }
  509. g->SetTitle("");
  510. g->SetName("baseline");
  511. g->GetXaxis()->SetTitle("channel");
  512. TString label;
  513. label.Form("adc counts / %d #mus", intTime);
  514. g->GetYaxis()->SetTitle(label.Data());
  515. g->Draw("AP");
  516. c1->SetName("baseline");
  517. //delete g;//added
  518. return c1;
  519. }
  520. void Beammon::calcBaselineIntegral()
  521. {
  522. /*baselineIntegral = 0;
  523. for(int i=1; i <= nBaselineEvents;i++){
  524. data->GetEvent(i);
  525. double currentIntegral = 0;
  526. for(int ch=0;ch<64;ch++){
  527. currentIntegral += channels[ch] - baseline[ch];
  528. }
  529. if(currentIntegral > baselineIntegral)
  530. baselineIntegral = currentIntegral;
  531. }
  532. printf("baseline int: %f \n",baselineIntegral);*/
  533. double highVal = 0;
  534. for (int i = 1; i <= nBaselineEvents; i++)
  535. {
  536. data->GetEvent(i);
  537. double val = channels[highestChannelBin] - baseline[highestChannelBin];
  538. if (val > highVal)
  539. highVal = val;
  540. }
  541. beamOnAdcVal = beamOnLevel * highVal;
  542. int maxNoise = floor(highVal);
  543. for (int j = 0; j < 64; j++)
  544. {
  545. TString name;
  546. name.Form("channelSig%d", j);
  547. channelSig[j] = new TH1D(name.Data(), name.Data(), maxNoise * 2, -maxNoise, maxNoise);
  548. }
  549. for (int i = 1; i <= nBaselineEvents; i++)
  550. {
  551. data->GetEvent(i);
  552. for (int ch = 0; ch < 64; ch++)
  553. channelSig[ch]->Fill(channels[ch] - baseline[ch]);
  554. }
  555. gChannelErrors = new TGraph(64);
  556. for (int k = 0; k < 64; k++)
  557. {
  558. double stdDev = channelSig[k]->GetStdDev();
  559. channelErrors[k] = stdDev;
  560. gChannelErrors->SetPoint(k + 1, k + 1, stdDev);
  561. }
  562. }
  563. void Beammon::prelimProfile()
  564. {
  565. highVal = 0;
  566. int histChannel = 0;
  567. double highVal_tmp[64];
  568. for (int i = 1; i < nevents - 1000; i += 1)
  569. { //nevents/nPrelimEvents){
  570. data->GetEvent(i);
  571. for (int ch = 0; ch < 64; ch++)
  572. {
  573. double val = channels[ch] - baseline[ch];
  574. /* if (val > highVal_tmp[ch])
  575. {
  576. highVal_tmp[ch] = val;
  577. }*/
  578. if (isSipm)
  579. histChannel = 64 - ch;
  580. else
  581. histChannel = ch + 1;
  582. prelimBeam->SetBinContent(histChannel, prelimBeam->GetBinContent(histChannel) + val / nPrelimEvents); //nPrelimEvents);
  583. }
  584. }
  585. TF1 *fit = new TF1("fitfunc", "gaus", 1, 64);
  586. fit->SetParameter(1, prelimBeam->GetMean());
  587. // printf("Mean: %f \n", prelimBeam->GetMean());
  588. // printf("Sigma: %f \n", prelimBeam->GetStdDev());
  589. // cout << prelimBeam->GetStdDev() << endl;
  590. fit->SetParameter(2, prelimBeam->GetStdDev());
  591. prelimBeam->Fit("fitfunc", "Q");
  592. prelimMean = fit->GetParameter(1);
  593. prelimSigma = fit->GetParameter(2);
  594. highestChannelBin = floor(prelimMean);
  595. if (highestChannelBin < 0)
  596. highestChannelBin = 0;
  597. /*
  598. highVal = 0;
  599. for (int i = 0; i < 64; i++)
  600. {
  601. if ( highVal_tmp[i] > highVal)
  602. {
  603. highVal = highVal_tmp[i];
  604. }
  605. }*/
  606. // beamOnAdcVal = beamOnLevel * highVal;
  607. delete fit;
  608. printf("\ncenter channel: %d \n", highestChannelBin);
  609. }
  610. void Beammon::ProcessData()
  611. {
  612. bool beamOn = false;
  613. bool previousBeamStatus = false;
  614. int lastSpillOn = 0;
  615. int lastSpillOff = 0;
  616. int minSpillTime = 3000; // in ms
  617. int histChannel;
  618. int highestSigVal = 0;
  619. for (int i = 1; i < nevents; i++)
  620. {
  621. if (i % 10000 == 00)
  622. printf("Event: %d / %ld \n", i, nevents);
  623. // cout << "Event: " << i << endl;
  624. data->GetEvent(i);
  625. double maxSignal = channels[highestChannelBin] - baseline[highestChannelBin];
  626. /*
  627. for(int j=0;j<64;j++){
  628. double val = channels[j] - baseline[j];
  629. if (val > maxSignal)
  630. maxSignal = val;
  631. }*/
  632. if (isAboveThreshold(maxSignal) && (i > nBaselineEvents))
  633. {
  634. beamOn = true;
  635. if ((i - lastSpillOff) > minSpillTime && !previousBeamStatus)
  636. {
  637. if (lastSpillOff != 0)
  638. spillTimes.push_back(make_pair(lastSpillOn, lastSpillOff));
  639. lastSpillOn = i;
  640. }
  641. }
  642. else
  643. {
  644. beamOn = false;
  645. if (previousBeamStatus)
  646. lastSpillOff = i;
  647. }
  648. previousBeamStatus = beamOn;
  649. int sigVal = 0;
  650. for (int j = 0; j < 64; j++)
  651. {
  652. int chan;
  653. if (isSipm)
  654. {
  655. if (j % 2 == 0)
  656. histChannel = 64 - (j + 1);
  657. else
  658. histChannel = 64 - (j - 1);
  659. }
  660. else
  661. histChannel = j + 1;
  662. double chVal = channels[j] - baseline[j];
  663. sigVal += chVal;
  664. timePosHist->SetBinContent(histChannel, i, chVal);
  665. completeIntegral += chVal;
  666. }
  667. if (sigVal > highestSigVal)
  668. {
  669. highestSigVal = sigVal;
  670. highestTimeBin = i;
  671. }
  672. // cout << ic1 << " " << mw1_focusx << " " << mw1_focusy << endl;
  673. // IONISATION CHAMBERS
  674. double chargeToParts = 1. / 3.2886706587037837e-15 * 1e-9 * 312e-6;
  675. if (!(ic1 != ic1))
  676. ic1Hist->SetBinContent(i, ic1 * chargeToParts);
  677. if (!(ic2 != ic2))
  678. ic2Hist->SetBinContent(i, ic2 * chargeToParts);
  679. // MW CHAMBERS
  680. if (!(mw1_focusx != mw1_focusx))
  681. mw1_focusxHist->SetBinContent(i, mw1_focusx);
  682. mw1_focusyHist->SetBinContent(i, mw1_focusy);
  683. mw2_focusxHist->SetBinContent(i, mw2_focusx);
  684. mw2_focusyHist->SetBinContent(i, mw2_focusy);
  685. if (!(mw1_posx != mw1_posx))
  686. mw1_posxHist->SetBinContent(i, mw1_posx);
  687. mw1_posyHist->SetBinContent(i, mw1_posy);
  688. mw2_posxHist->SetBinContent(i, mw2_posx);
  689. mw2_posyHist->SetBinContent(i, mw2_posy);
  690. if (mw1_posx < 100)
  691. {
  692. mwPosX->Fill(mw1_posx);
  693. mwFocusX->Fill(mw1_focusx);
  694. }
  695. }
  696. spillTimes.push_back(make_pair(lastSpillOn, lastSpillOff));
  697. TH1D *tmp = GetChannelSlice(-1);
  698. tmp->Fit("gaus");
  699. TF1 *fit = tmp->GetFunction("gaus");
  700. 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!
  701. signalDist = new TH1D("signalDist", "signal distribution", 200, 0., referenceIntegral * 2.51 * fit->GetParameter(2));
  702. signalNoiseDist = new TH1D("signalNoiseDist", "", 200, 0., referenceIntegral * 2.51 * fit->GetParameter(2));
  703. signalNoiseChannel = new TH1D("signalNoiseChannel", "", 200, 0., tmp->GetMaximum());
  704. calcBeamTime();
  705. printf("HighestBin: %d \n", highestTimeBin);
  706. }
  707. TH1D *Beammon::GetPrelimProfile()
  708. {
  709. if (!prelimBeam)
  710. {
  711. printf("WARNING: preliminary profile not yet generated");
  712. }
  713. return prelimBeam;
  714. }
  715. TH1D *Beammon::TimeAverage(int timeStart, int timeStop)
  716. {
  717. if (timeStart == -1)
  718. timeStart = startTime;
  719. if (timeStop == -1)
  720. timeStop = stopTime;
  721. int count = 0;
  722. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  723. TH1D *tmp;
  724. printf("\n\n start: %d, stop: %d \n\n", timeStart, timeStop);
  725. for (int i = timeStart; i <= timeStop; i++)
  726. {
  727. tmp = GetChannelSlice(i, 0);
  728. if (isAboveThreshold(tmp))
  729. {
  730. res->Add(tmp, 1.);
  731. count++;
  732. }
  733. delete tmp;
  734. }
  735. if (count)
  736. {
  737. for (int n = 1; n <= 64; n++)
  738. {
  739. res->SetBinContent(n, res->GetBinContent(n) / count);
  740. }
  741. return res;
  742. }
  743. else
  744. return 0;
  745. }
  746. TH1D *Beammon::TimeIntSignal(int timeStart, int timeStop)
  747. {
  748. if (timeStart == -1)
  749. timeStart = startTime;
  750. if (timeStop == -1)
  751. timeStop = stopTime;
  752. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  753. TH1D *tmp;
  754. for (int i = timeStart; i < timeStop; i++)
  755. {
  756. tmp = GetChannelSlice(i, 0);
  757. if (isAboveThreshold(tmp))
  758. {
  759. res->Add(tmp, 1.);
  760. beamOnTime++;
  761. }
  762. delete tmp;
  763. }
  764. return res;
  765. }
  766. TH1D *Beammon::AverageHist(int dir, int timeStart, int timeStop)
  767. { // dir = 0 => x direction, 1 => y
  768. TH2D *hist = timePosHist;
  769. int nbins1, nbins2;
  770. double binStart, binStop;
  771. int firstBin, secondBin;
  772. if (!dir)
  773. {
  774. // Timeaverage
  775. if ((timeStart != -1) && (timeStop != -1))
  776. {
  777. firstBin = timeStart; //hist->GetYaxis()->FindBin((timeStart-startTime)/readOutFreq);
  778. secondBin = timeStop; //hist->GetYaxis()->FindBin((timeStop-startTime)/readOutFreq);
  779. nbins1 = hist->GetNbinsX();
  780. nbins2 = secondBin - firstBin;
  781. binStart = hist->GetXaxis()->GetBinCenter(1);
  782. binStop = hist->GetXaxis()->GetBinCenter(nbins1);
  783. }
  784. else
  785. {
  786. nbins1 = hist->GetNbinsX();
  787. nbins2 = hist->GetNbinsY();
  788. binStart = hist->GetXaxis()->GetBinCenter(1);
  789. binStop = hist->GetXaxis()->GetBinCenter(nbins1);
  790. firstBin = 1;
  791. secondBin = nbins2;
  792. }
  793. }
  794. else
  795. {
  796. nbins1 = hist->GetNbinsY();
  797. nbins2 = hist->GetNbinsX();
  798. binStart = hist->GetYaxis()->GetBinCenter(1);
  799. binStop = hist->GetYaxis()->GetBinCenter(nbins1);
  800. firstBin = 1;
  801. secondBin = hist->GetNbinsX();
  802. }
  803. TH1D *retHist = new TH1D("average", "average", nbins1, binStart, binStop);
  804. for (int i = 1; i <= nbins1; i++)
  805. {
  806. int count = 0;
  807. double avg = 0;
  808. for (int j = firstBin; j <= secondBin; j++)
  809. {
  810. if (!dir)
  811. {
  812. /*bool beam = beamOnVector[j];
  813. if(beam==false)
  814. continue;
  815. //else
  816. //printf("TRUE");*/
  817. avg += hist->GetBinContent(i, j);
  818. }
  819. else
  820. avg += hist->GetBinContent(j, i);
  821. count++;
  822. }
  823. if (count > 0)
  824. avg /= count;
  825. retHist->SetBinContent(i, avg);
  826. }
  827. return retHist;
  828. }
  829. TH1D *Beammon::GetTimeAvg()
  830. {
  831. int n = spillTimes.size();
  832. TH1D *res = new TH1D("timeAverage", "time average", 64, 0.5, 64.5);
  833. for (int i = 0; i < n; i++)
  834. {
  835. TH1D *tmp = TimeAverage(spillTimes[i].first, spillTimes[i].second);
  836. if (!tmp)
  837. printf("\n\n timeAvgHist is Null! \n\n");
  838. if (tmp)
  839. res->Add(tmp, 1. / n);
  840. }
  841. res->SetTitle("average beam profile");
  842. return res;
  843. }
  844. TH1D *Beammon::GetTimeAvg(int nSpill)
  845. {
  846. if (nSpill >= spillTimes.size())
  847. return 0;
  848. TH1D *res = TimeAverage(spillTimes[nSpill].first, spillTimes[nSpill].second);
  849. res->SetTitle("average beam profile");
  850. return res;
  851. }
  852. TH1D *Beammon::GetTimeAvg(int nSpill, int nMeasurements)
  853. {
  854. if (nSpill >= spillTimes.size())
  855. return 0;
  856. TH1D *res = TimeAverage(spillTimes[nSpill].second - nMeasurements, spillTimes[nSpill].second);
  857. TString title = TString::Format("average beam profile, %d ms, spill %d", nMeasurements, nSpill);
  858. TString name = TString::Format("avg%dmsspill%d", nMeasurements, nSpill);
  859. res->SetTitle(title.Data());
  860. res->SetName(name.Data());
  861. return res;
  862. }
  863. TH2D *Beammon::GetTimeAvgScatter(int nSpill, int nMeasurements)
  864. {
  865. int tStart = spillTimes[nSpill].first;
  866. if (nMeasurements == -1)
  867. tStart = spillTimes[nSpill].second - nMeasurements;
  868. TH2D *res = new TH2D("tavgScatter", "tavgScatter", 64, 0.5, 64.5, 400, 0., highVal);
  869. for (int i = spillTimes[nSpill].first; i < spillTimes[nSpill].second; i++)
  870. {
  871. TH1D *tmp = GetChannelSlice(i, 0);
  872. if (!isAboveThreshold(tmp))
  873. continue;
  874. for (int bin = 1; bin <= 64; bin++)
  875. {
  876. res->Fill(bin, tmp->GetBinContent(bin));
  877. }
  878. delete tmp;
  879. }
  880. TString title = TString::Format("beam profile, %d ms, spill %d", nMeasurements, nSpill);
  881. res->SetTitle(title.Data());
  882. return res;
  883. }
  884. TH2D *Beammon::GetTimeAvgScatter()
  885. {
  886. TH2D *res = new TH2D("tavgScatter", "tavgScatter", 64, 0.5, 64.5, 400, 0., highVal);
  887. int n = spillTimes.size();
  888. for (int i = 0; i < n; i++)
  889. {
  890. res->Add(GetTimeAvgScatter(i, -1), 1. / n);
  891. }
  892. return res;
  893. }
  894. TH1D *Beammon::GetChannelSigDist(int channel)
  895. {
  896. TString sName = TString("signal distribution channel ") + TString(channel);
  897. double maxAmp = 0;
  898. for (int n = 0; n < spillTimes.size(); n++)
  899. {
  900. TH1D *tmp = GetSpillSlice(n);
  901. if (tmp->GetMaximum() > maxAmp)
  902. maxAmp = tmp->GetMaximum();
  903. delete tmp;
  904. }
  905. TH1D *res = new TH1D(sName.Data(), sName.Data(), 200, 0., maxAmp);
  906. for (int n = 0; n < spillTimes.size(); n++)
  907. {
  908. TH1D *tmp = GetSpillSlice(n);
  909. for (int bin = 1; bin <= tmp->GetNbinsX(); bin++)
  910. {
  911. res->Fill(tmp->GetBinContent(bin));
  912. //printf("bin: %d, amp: %f \n", bin, tmp->GetBinContent(bin));
  913. }
  914. delete tmp;
  915. }
  916. return res;
  917. }
  918. TH1D *Beammon::GetChannelAvg(bool beamOn)
  919. {
  920. return AverageHist(1, -1, -1);
  921. }
  922. TH1D *Beammon::GetTimeSlice(int channel, int timeStart, int timeStop, bool beamOn)
  923. {
  924. if (timeStart == -1)
  925. timeStart = startTime;
  926. if (timeStop == -1)
  927. timeStop = stopTime;
  928. int nbins = timeStop - timeStart;
  929. TH1D *tmp2 = new TH1D("ts", "timeslice", nbins, (timeStart - startTime) / readOutFreq, (timeStop - startTime) / readOutFreq);
  930. if (channel == -1)
  931. channel = highestChannelBin;
  932. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  933. int offset = tmp->GetXaxis()->FindBin(timeStart);
  934. for (int bin = 1; bin <= nbins; bin++)
  935. {
  936. tmp2->SetBinContent(bin, tmp->GetBinContent(bin + offset));
  937. }
  938. TString title = TString::Format("time projection of channel %d", channel);
  939. tmp2->SetTitle(title.Data());
  940. return tmp2;
  941. }
  942. TCanvas *Beammon::GetTimeSliceCanvas(int channel)
  943. {
  944. TCanvas *c1 = new TCanvas();
  945. c1->SetName("timeslice");
  946. int nbins = stopTime - startTime;
  947. TH1D *tmp2 = new TH1D("timeslice", "", nbins, 0., (stopTime - startTime) / readOutFreq);
  948. if (channel == -1)
  949. channel = highestChannelBin;
  950. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  951. for (int bin = 1; bin <= nbins; bin++)
  952. {
  953. tmp2->SetBinContent(bin, tmp->GetBinContent(bin));
  954. }
  955. tmp2->GetXaxis()->SetTitle("times [s]");
  956. TString label;
  957. label.Form("adc counts / %d #mus", intTime);
  958. tmp2->GetYaxis()->SetTitle(label.Data());
  959. tmp2->Draw("P");
  960. return c1;
  961. }
  962. TCanvas *Beammon::GetSignalNoiseChannel(int channel)
  963. {
  964. TCanvas *c1 = new TCanvas();
  965. c1->SetName("signalNoiseChannel");
  966. int nbins = stopTime - startTime;
  967. if (channel == -1)
  968. channel = highestChannelBin;
  969. TH1D *tmp = timePosHist->ProjectionY("", channel, channel);
  970. for (int bin = 1; bin <= nbins; bin++)
  971. {
  972. signalNoiseChannel->Fill(tmp->GetBinContent(bin));
  973. }
  974. signalNoiseChannel->GetYaxis()->SetTitle("entries");
  975. TString label;
  976. label.Form("adc counts / %d #mus", intTime);
  977. signalNoiseChannel->GetXaxis()->SetTitle(label.Data());
  978. signalNoiseChannel->Draw();
  979. return c1;
  980. }
  981. TH1D *Beammon::GetChannelSlice(int time, int offset)
  982. {
  983. if (time == -1)
  984. time = highestTimeBin + offset;
  985. //printf("time: %d\n", time);
  986. TH1D *res = timePosHist->ProjectionX("", time, time); //timePosHist->ProjectionX("",timePosHist->GetYaxis()->FindBin(time+offset),timePosHist->GetYaxis()->FindBin(time+offset));
  987. //res->SetError(channelErrors);
  988. res->SetTitle("single measurement (1ms)");
  989. res->SetName("Channel_" + (TString)time);
  990. res->Fit("gaus", "Q0");
  991. double mean = res->GetFunction("gaus")->GetParameter(1);
  992. double sigma = res->GetFunction("gaus")->GetParameter(2);
  993. return res;
  994. }
  995. TH1D *Beammon::GetChannelSlice2(int time, int offset)
  996. {
  997. if (time == -1)
  998. time = highestTimeBin + offset;
  999. //printf("time: %d\n", time);
  1000. TH1D *res = timePosHist->ProjectionX("", time, time); //timePosHist->ProjectionX("",timePosHist->GetYaxis()->FindBin(time+offset),timePosHist->GetYaxis()->FindBin(time+offset));
  1001. //res->SetError(channelErrors);
  1002. res->SetTitle("single measurement (1ms)");
  1003. res->SetName("Channel_" + (TString)time);
  1004. res->Fit("gaus", "Q0");
  1005. double mean = res->GetFunction("gaus")->GetParameter(1);
  1006. mean_ave += mean;
  1007. double sigma = res->GetFunction("gaus")->GetParameter(2);
  1008. sigma_ave += sigma;
  1009. return res;
  1010. }
  1011. TGraphErrors *Beammon::GetChannelGraph(int time, int offset)
  1012. {
  1013. if (time == -1)
  1014. time = highestTimeBin + offset;
  1015. double xErr = 0.8 / sqrt(12);
  1016. TH1D *res = timePosHist->ProjectionX("", time, time);
  1017. TGraphErrors *graph = new TGraphErrors(64);
  1018. for (int ch = 1; ch <= 64; ch++)
  1019. {
  1020. graph->SetPoint(ch, ch, res->GetBinContent(ch));
  1021. // graph->SetPointError(ch,xErr,channelErrors[ch-1]);//sqrt(channelErrors[ch-1]*channelErrors[ch-1]+res->GetBinContent(ch)));
  1022. graph->SetPointError(ch, xErr, channelErrors[ch - 1]);
  1023. }
  1024. delete res;
  1025. return graph;
  1026. }
  1027. TH1D *Beammon::GetChannelGraphHist(int time, int offset)
  1028. {
  1029. if (time == -1)
  1030. time = highestTimeBin + offset;
  1031. double xErr = 0.8 / sqrt(12);
  1032. TH1D *res = timePosHist->ProjectionX("", time, time);
  1033. return res;
  1034. }
  1035. TH1D *Beammon::GetSignalOverTime()
  1036. {
  1037. TH1D *tmp = timePosHist->ProjectionY("", 1, 64);
  1038. tmp->SetTitle("signal over time");
  1039. tmp->SetName("signal over time");
  1040. return tmp;
  1041. }
  1042. void Beammon::PrintSpillTimes()
  1043. {
  1044. printf("%lu spills recorded: \n", spillTimes.size());
  1045. // cout << spillTimes.size() << "spills recorded: " << endl;
  1046. for (int n = 0; n < spillTimes.size(); n++)
  1047. printf("%02d: %f - %f \n", n, (spillTimes[n].first - startTime) / readOutFreq, (spillTimes[n].second - startTime) / readOutFreq);
  1048. }
  1049. TH1D *Beammon::calcTimeCenters(int start, int stop)
  1050. {
  1051. int tStart = start;
  1052. int tStop = stop;
  1053. if (start == -1)
  1054. tStart = startTime;
  1055. if (stop == -1)
  1056. tStop = stopTime;
  1057. int nbins = (tStop - tStart);
  1058. double mean = 0;
  1059. double sigma = 0;
  1060. double constant;
  1061. TH1D *res = new TH1D("timeIntSignal_tmp", "time integrated signal", 64, 0.5, 64.5);
  1062. TCanvas *tmpcanvas = new TCanvas();
  1063. int offset = tStart;
  1064. TF1 *fit = new TF1("gaus_L", "gaus+[3]", 1, 64);
  1065. TF1 *fit2 = new TF1("gaus_2", "gaus", 1, 64);
  1066. fit2->SetParameter(0, 2000);
  1067. fit2->SetParameter(1, mean_ave);
  1068. fit2->SetParameter(2, sigma_ave);
  1069. for (int bin = tStart; bin <= tStop; bin++)
  1070. {
  1071. TGraphErrors *tmp = GetChannelGraph(bin, 0);
  1072. TH1D *tmp2 = GetChannelGraphHist(bin, 0);
  1073. tmp->Fit("gaus_2", "Q0", "", mean_ave - 3 * sigma_ave, mean_ave + 3 * sigma_ave);
  1074. TF1 *func = tmp->GetFunction("gaus_2");
  1075. if (tmp2->GetMean() * 0.8 > 0)
  1076. fibreHist_weighted->SetBinContent(bin + 1, tmp2->GetMean() * 0.8);
  1077. else
  1078. fibreHist_weighted->SetBinContent(bin + 1, 0);
  1079. if (tmp2->GetEntries() > 0)
  1080. sigHist_weighted->SetBinContent(bin - 1, tmp2->GetEntries() * signalToParticles);
  1081. else
  1082. sigHist_weighted->SetBinContent(bin - 1, 0);
  1083. if (func)
  1084. {
  1085. if (!isAboveThreshold(tmp))
  1086. continue;
  1087. constant = func->GetParameter(0);
  1088. mean = func->GetParameter(1) * channelWidth;
  1089. sigma = func->GetParameter(2) * channelWidth;
  1090. if (mean <= 0 || mean > 64)
  1091. continue;
  1092. /*if (constant > 1e5 || mean > (mean_ave * channelWidth + 0.2) || mean < (mean_ave * channelWidth - 0.2))
  1093. {
  1094. fit->SetParameter(0, 1000);
  1095. fit->SetParameter(1, mean_ave);
  1096. fit->SetParameter(2, sigma_ave);
  1097. fit->SetParameter(3, 0.1);
  1098. fit->SetParLimits(0, 100, 2e5);
  1099. fit->SetParLimits(1, 1, 64);
  1100. fit->SetParLimits(2, 1, 10);
  1101. fit->SetParLimits(3, -50, 50);
  1102. tmp->Fit("gaus", "Q0", "", mean_ave - 3 * sigma_ave, mean_ave + 3 * sigma_ave);
  1103. func = tmp->GetFunction("gaus");
  1104. constant = func->GetParameter(0);
  1105. mean = func->GetParameter(1) * channelWidth;
  1106. sigma = func->GetParameter(2) * channelWidth;
  1107. }*/
  1108. if (abs(constant) > 1e5)
  1109. {
  1110. Fit_Fail_Number = Fit_Fail_Number - 1;
  1111. if (Fit_Fail_Number > 0)
  1112. {
  1113. tmpcanvas->cd();
  1114. tmp->Draw("AP");
  1115. func->Draw("SAME");
  1116. tmpcanvas->Modified();
  1117. tmpcanvas->Write();
  1118. }
  1119. continue;
  1120. }
  1121. //Fill the error hidto
  1122. double erry, tmpx;
  1123. bool discard = false;
  1124. int nPoint = 0;
  1125. for (int chn = 1; chn < 65; chn++)
  1126. {
  1127. tmp->GetPoint(nPoint, tmpx, erry);
  1128. SignalErrHist->Fill((double)tmpx, (erry - func->Eval(tmpx)) / (constant * sigma));
  1129. if ((erry - func->Eval(tmpx)) / (constant * sigma) > discardUpperThreshold || (erry - func->Eval(tmpx)) / (constant * sigma) < discardLowerThreshold)
  1130. {
  1131. tmp->RemovePoint(nPoint);
  1132. nPoint--;
  1133. discard = true;
  1134. }
  1135. nPoint++;
  1136. }
  1137. double signal = sigma * 2.51 * constant * signalToParticles;
  1138. //Fill the new tree
  1139. event_count++;
  1140. //data->GetEntry(bin);
  1141. beamPosX_1 = mean;
  1142. beamFocusX_1 = 2.35 * sigma;
  1143. beamSignal_1 = signal;
  1144. ic1_1 = ic1Hist->GetBinContent(bin - alignintIc);
  1145. ic2_1 = ic2Hist->GetBinContent(bin - alignintIc);
  1146. mw1_focusx_1 = mw1_focusxHist->GetBinContent(bin - alignint);
  1147. mw1_posx_1 = mw1_posxHist->GetBinContent(bin - alignint);
  1148. mw2_focusx_1 = mw2_focusxHist->GetBinContent(bin - alignint);
  1149. mw2_posx_1 = mw2_posxHist->GetBinContent(bin - alignint);
  1150. beamOn = 1;
  1151. newdata->Fill();
  1152. if (event_count % 5000 == 0)
  1153. {
  1154. printf("Signal events: %d ", event_count);
  1155. printf("Event number: %d\n", bin);
  1156. }
  1157. if (signal < 0 || signal != signal)
  1158. continue;
  1159. //Fill the Histograms
  1160. beamPosX->Fill(mean);
  1161. fibreHist->Fill(bin, mean);
  1162. mwpcHist->Fill(bin, mw1_posxHist->GetBinContent(bin - alignint));
  1163. beamFocusX->Fill(2.35 * sigma);
  1164. beamPosTimeGraph->SetPoint(bin, bin, mean);
  1165. beamPosTimeGraph->SetPointError(bin, 0., func->GetParError(1));
  1166. timeCenters->Fill((bin) / readOutFreq, mean);
  1167. timeWidths->Fill((bin) / readOutFreq, sigma * 2.35);
  1168. timeCenters_MWPC->Fill((bin) / readOutFreq, mw1_posxHist->GetBinContent(bin - alignint));
  1169. timeWidths_MWPC->Fill((bin) / readOutFreq, mw1_focusxHist->GetBinContent(bin - alignint));
  1170. Diff_timeCenters->Fill((bin) / readOutFreq, mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1171. if (mw1_posxHist->GetBinContent(bin - alignint) < 20)
  1172. {
  1173. mwPosScatterGraph->SetPoint(mwPosScatterGraph->GetN(), mw1_posxHist->GetBinContent(bin - alignint), mean);
  1174. //positionComp->Fill(mean - mw1_posxHist->GetBinContent(bin-4) +111.5415 - 32.02);
  1175. for (int i = 0; i < 20; i++)
  1176. mwPosScatterPlot[i]->Fill(mw1_posxHist->GetBinContent(bin - i + 10), mean);
  1177. }
  1178. double sigError = 2.51 * sqrt(sigma * sigma * func->GetParError(0) * func->GetParError(0) + constant * constant * func->GetParError(2) * func->GetParError(2)) * signalToParticles;
  1179. if (signal > 1e5 || sigError > signal)
  1180. {
  1181. //signal = 0;
  1182. sigError = 0;
  1183. }
  1184. if (signal)
  1185. {
  1186. signalDist->Fill(signal);
  1187. if (ic1Hist->GetBinContent(bin - alignintIc))
  1188. {
  1189. sigHist->Fill(bin - alignintIc - 1, signal);
  1190. signalComp->Fill((signal - ic1Hist->GetBinContent(bin - alignintIc)) / signal);
  1191. double x, y, totalSignal = 0, signalError = 0;
  1192. for (int k = 0; k < 63; k++)
  1193. {
  1194. tmp->GetPoint(k, x, y);
  1195. signalError += tmp->GetErrorY(k) * tmp->GetErrorY(k);
  1196. totalSignal += y * signalToParticles;
  1197. }
  1198. signalError = sqrt(signalError) * signalToParticles;
  1199. sigIcScatterGraph->SetPoint(sigIcScatterGraph->GetN(), ic1Hist->GetBinContent(bin - alignintIc), totalSignal);
  1200. sigIcScatterGraph->SetPointError(sigIcScatterGraph->GetN(), 0, signalError);
  1201. for (int i = 0; i < 20; i++)
  1202. sigIcScatterPlot[i]->Fill(ic1Hist->GetBinContent(bin - i + 10), signal);
  1203. Center_Signal->Fill(signal, mean);
  1204. // sigIcScatterPlot[14]->Fill(ic1Hist->GetBinContent(bin - 14 + 10), signal);
  1205. sigIcScatterPlot_ic2->Fill(ic2Hist->GetBinContent(bin - 14 + 10), signal);
  1206. }
  1207. }
  1208. else
  1209. {
  1210. //ic1Hist->SetBinContent(bin, 0);
  1211. }
  1212. widthComp->Fill(sigma * 2.51 - mw1_focusxHist->GetBinContent(bin - alignint));
  1213. //Center_Signal->Fill(totalSignal, mean);
  1214. Diff_hist->Fill(mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1215. Diff_Signal->Fill(signal, mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1216. Diff_ic1->Fill(ic1Hist->GetBinContent(bin - alignintIc), mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1217. icIntegral += ic1Hist->GetBinContent(bin);
  1218. signalTimeGraph->SetPoint(bin, bin, signal);
  1219. signalTimeGraph->SetPointError(bin, 0., sigError);
  1220. beamOnTime++;
  1221. double c2 = func->GetChisquare() / func->GetNDF();
  1222. if (c2 < 100)
  1223. chisquareDist->Fill(c2);
  1224. }
  1225. delete func;
  1226. delete tmp;
  1227. delete tmp2;
  1228. }
  1229. delete tmpcanvas;
  1230. return res;
  1231. }
  1232. void Beammon::GetTimeCenters()
  1233. {
  1234. TH1D *tmp_TimeCenter;
  1235. double mean_MWPC = mwPosX->GetMean();
  1236. shift = mean_ave * 0.8 / 5 - mwPosX->GetMean();
  1237. double max_ic1 = ic1Hist->GetMaximum();
  1238. beamOnTime = 0;
  1239. int nbins = stopTime - startTime;
  1240. mean_ave = prelimBeam->GetMean();
  1241. sigma_ave = prelimBeam->GetStdDev();
  1242. printf("Mean: %f \n", mean_ave);
  1243. printf("Sigma: %f \n", sigma_ave);
  1244. 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));
  1245. timeWidths = new TH2D("timeWidths", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, 0.4 * sigma_ave * 2.35, 0.95 * sigma_ave * 2.35);
  1246. timeIntSignal = new TH1D("timeIntSignal", "time integrated signal", 64, 0.5, 64.5);
  1247. timeCenters_MWPC = new TH2D("timeCenters_MWPC", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, mean_MWPC - 5 * sigma_pos, mean_MWPC + 5 * sigma_pos);
  1248. timeWidths_MWPC = new TH2D("timeWidths_MWPC", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, 1.5, mw1_focusxHist->GetMaximum() * 1.05);
  1249. timeIntSignal_MWPC = new TH1D("timeIntSignal_MWPC", "time integrated signal", 64, 0.5, 64.5);
  1250. for (int j = 0; j < 20; j++)
  1251. {
  1252. TString name;
  1253. name.Form("sigIcScatterPlot%d", j);
  1254. sigIcScatterPlot[j] = new TH2D(name.Data(), name.Data(), 200, 0, -1, 200, 0, -1);
  1255. TString name2;
  1256. name2.Form("mwPosScatterPlot%d", j);
  1257. 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));
  1258. }
  1259. sigIcScatterPlot_ic2 = new TH2D("sigIcScatterPlot_ic2", "sigIcScatterPlot_ic2", 200, 0, -1, 200, 0, -1);
  1260. 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));
  1261. Diff_timeCenters = new TH2D("Diff_time", "", nbins / 100, 0., (stopTime - startTime) / readOutFreq, 200, -6 * sigma_pos, 6 * sigma_pos);
  1262. Diff_hist = new TH1D("Diff_hist", "l", 200, -2, 2.);
  1263. Diff_Signal = new TH2D("Diff_Signal", "", 200, 0., max_ic1 - 5000, 200, -6 * sigma_pos, 6 * sigma_pos);
  1264. Diff_ic1 = new TH2D("Diff_ic1", "", 200, 0., max_ic1 - 5000, 200, -6 * sigma_pos, 6 * sigma_pos);
  1265. b_beamPosX_1 = newdata->Branch("beamPosX_1", &beamPosX_1);
  1266. b_beamFocusX_1 = newdata->Branch("beamFocusX_1", &beamFocusX_1);
  1267. b_beamSignal_1 = newdata->Branch("beamSignal_1", &beamSignal_1);
  1268. bb_ic1 = newdata->Branch("ic1_1", &ic1_1);
  1269. bb_ic2 = newdata->Branch("ic2_1", &ic2_1);
  1270. bb_mw1_focusx = newdata->Branch("mw1_focusx_1", &mw1_focusx_1);
  1271. bb_mw1_posx = newdata->Branch("mw1_posx_1", &mw1_posx_1);
  1272. bb_mw2_focusx = newdata->Branch("mw2_focusx_1", &mw2_focusx_1);
  1273. bb_mw2_posx = newdata->Branch("mw2_posx_1", &mw2_posx_1);
  1274. b_beamOn = newdata->Branch("beamOn", &beamOn);
  1275. newdata->SetBranchStatus("*", 1);
  1276. for (int i = 0; i < spillTimes.size(); i++)
  1277. {
  1278. tmp_TimeCenter = calcTimeCenters(spillTimes[i].first + 200, spillTimes[i].second);
  1279. timeIntSignal->Add(tmp_TimeCenter, 1.);
  1280. delete tmp_TimeCenter;
  1281. beamPosX_1 = 0;
  1282. beamFocusX_1 = 0;
  1283. beamSignal_1 = 0;
  1284. ic1_1 = 0;
  1285. mw1_focusx_1 = 0;
  1286. mw1_posx_1 = 0;
  1287. beamOn = 0;
  1288. newdata->Fill();
  1289. }
  1290. SignalErrHist->Write();
  1291. printf("Fit_Fail_Counts=%d\n", Fit_Fail_Counts);
  1292. //Draw timeCenter
  1293. timeCenters->GetXaxis()->SetTitle("time [s]");
  1294. TString label;
  1295. label.Form("mean position[mm]");
  1296. timeCenters->GetYaxis()->SetTitle(label.Data());
  1297. timeCenters->Write();
  1298. //Draw timeCenter_MWPC
  1299. timeCenters_MWPC->GetXaxis()->SetTitle("time [s]");
  1300. TString label2;
  1301. label2.Form("mean position[mm]");
  1302. timeCenters_MWPC->GetYaxis()->SetTitle(label.Data());
  1303. timeCenters_MWPC->Write();
  1304. //Draw timeWidths
  1305. timeWidths->GetXaxis()->SetTitle("time [s]");
  1306. TString label12;
  1307. label12.Form("width fiber[mm]");
  1308. timeWidths->GetYaxis()->SetTitle(label12.Data());
  1309. timeWidths->Write();
  1310. //Draw timeWidths_MWPC
  1311. timeWidths_MWPC->GetXaxis()->SetTitle("time [s]");
  1312. TString label13;
  1313. label13.Form("width MWPC[mm]");
  1314. timeWidths_MWPC->GetYaxis()->SetTitle(label12.Data());
  1315. timeWidths_MWPC->Write();
  1316. Center_Signal->GetXaxis()->SetTitle("Signal [particles]");
  1317. TString label123;
  1318. label123.Form("Position_Fiber[mm]");
  1319. Center_Signal->GetYaxis()->SetTitle(label123.Data());
  1320. Center_Signal->Write();
  1321. Center_Signal->ProfileX()->Write();
  1322. //Draw Diff_timeCenters
  1323. Diff_timeCenters->GetXaxis()->SetTitle("time [s]");
  1324. TString label3;
  1325. label3.Form("mean position diff[mm]");
  1326. Diff_timeCenters->GetYaxis()->SetTitle(label.Data());
  1327. Diff_timeCenters->Write();
  1328. //Draw Diff_Signal
  1329. Diff_Signal->GetXaxis()->SetTitle("signal");
  1330. TString label4;
  1331. label4.Form("mean position diff[mm]");
  1332. Diff_Signal->GetYaxis()->SetTitle(label.Data());
  1333. Diff_Signal->Write();
  1334. //Draw Diff_Signal
  1335. Diff_ic1->GetXaxis()->SetTitle("ic1");
  1336. TString label5;
  1337. label5.Form("mean position diff[mm]");
  1338. Diff_ic1->GetYaxis()->SetTitle(label.Data());
  1339. Diff_ic1->Write();
  1340. TObjArray aSlices, bSlices;
  1341. Diff_Signal->FitSlicesY(0, 0, -1, 5, "QNR", &aSlices);
  1342. for (int i = 0; i < 4; i++)
  1343. {
  1344. aSlices[i]->Write();
  1345. }
  1346. Diff_ic1->FitSlicesY(0, 0, -1, 5, "QNR", &bSlices);
  1347. for (int i = 0; i < 4; i++)
  1348. {
  1349. bSlices[i]->Write();
  1350. }
  1351. mean_ave = beamPosX->GetMean();
  1352. sigma_pos = beamPosX->GetStdDev();
  1353. sigma_ave = beamFocusX->GetMean();
  1354. return;
  1355. }
  1356. pair<int, int> Beammon::GetSpillTimes(int nSpill)
  1357. {
  1358. return spillTimes[nSpill];
  1359. }
  1360. TH2D *Beammon::GetSpillCenters(int nSpill)
  1361. {
  1362. //return calcTimeCenters(spillTimes[nSpill].first, spillTimes[nSpill].second);
  1363. }
  1364. TH1D *Beammon::GetSpillSlice(int nSpill)
  1365. {
  1366. return GetTimeSlice(-1, spillTimes[nSpill].first - 500, spillTimes[nSpill].second + 500, true);
  1367. }
  1368. TH1D *Beammon::GetSignalDistribution()
  1369. {
  1370. /*int n = signalDist->GetNbinsX();
  1371. for(int i = 1; i<= n; i++){
  1372. signalDist->SetBinContent(i,signalDist->GetBinContent(i)*signalDistScale);
  1373. }*/
  1374. signalDist->Scale(signalDistScale);
  1375. return signalDist;
  1376. }
  1377. TCanvas *Beammon::GetSignalDistributionCanvas()
  1378. {
  1379. TCanvas *c1 = new TCanvas();
  1380. c1->SetName("signalDistributionCanvas");
  1381. signalDist->Draw();
  1382. signalDist->SetTitle("");
  1383. signalDist->GetYaxis()->SetTitle("entries");
  1384. TString label;
  1385. label.Form("integrated signal [adc counts / %d #mus]", intTime);
  1386. signalDist->GetXaxis()->SetTitle(label.Data());
  1387. //c1->SaveAs("dummy");
  1388. return c1;
  1389. }
  1390. TH2D *Beammon::GetSimpleView()
  1391. {
  1392. return timePosHist;
  1393. }
  1394. TH1D *Beammon::GetBaselineHist()
  1395. {
  1396. TH1D *tmp = new TH1D("baseline", "baseline", 64, 0.5, 64.5);
  1397. for (int n = 1; n <= 64; n++)
  1398. {
  1399. tmp->SetBinContent(n, baseline[n - 1]);
  1400. }
  1401. return tmp;
  1402. }
  1403. void Beammon::Close()
  1404. {
  1405. delete timePosHist;
  1406. fileOut->Close();
  1407. }
  1408. double Beammon::GetIntSignal()
  1409. {
  1410. return intSignal;
  1411. }
  1412. int Beammon::GetBeamOnTime()
  1413. {
  1414. return beamOnTime;
  1415. }
  1416. double Beammon::GetSignalStd()
  1417. {
  1418. return signalDist->GetStdDev(1);
  1419. }
  1420. bool Beammon::isAboveThreshold(TGraphErrors *slice)
  1421. {
  1422. double channel, signal;
  1423. slice->GetPoint(highestChannelBin, channel, signal);
  1424. if (signal > beamOnAdcVal)
  1425. return true;
  1426. else
  1427. return false;
  1428. }
  1429. bool Beammon::isAboveThreshold(TH1D *slice)
  1430. {
  1431. double val = slice->GetBinContent(highestChannelBin);
  1432. if (val > beamOnAdcVal)
  1433. return true;
  1434. else
  1435. return false;
  1436. }
  1437. bool Beammon::isAboveThreshold(double val)
  1438. {
  1439. // old version using the gaussian fit to the measurement
  1440. if (val > beamOnAdcVal)
  1441. return true;
  1442. else
  1443. return false;
  1444. /*
  1445. if(val > baselineIntegral * 1.5 && val < baselineIntegral *100)
  1446. return true;
  1447. else
  1448. return false;*/
  1449. }
  1450. TH1D *Beammon::icSignalComp()
  1451. {
  1452. icSignalDist = new TH1D("icSignal", "icSignal", 200, 0., ic1Hist->GetMaximum());
  1453. TH1D *icSignalDist2 = new TH1D("icSignal2", "icSignal2", 200, 0., ic2Hist->GetMaximum());
  1454. for (int i = 0; i < ic1Hist->GetNbinsX(); i++)
  1455. {
  1456. if (ic1Hist->GetBinContent(i) != 0)
  1457. icSignalDist->Fill(ic1Hist->GetBinContent(i));
  1458. if (ic2Hist->GetBinContent(i) != 0)
  1459. icSignalDist2->Fill(ic2Hist->GetBinContent(i));
  1460. }
  1461. icSignalDist2->Write();
  1462. return icSignalDist;
  1463. }
  1464. TCanvas *Beammon::signalOverTime2d()
  1465. {
  1466. TCanvas *c1 = new TCanvas();
  1467. c1->SetName("signalOverTime2d");
  1468. TH1D *tmp = GetSignalOverTime();
  1469. int binsX = tmp->GetNbinsX();
  1470. int max = tmp->GetMaximum();
  1471. int min = tmp->GetMinimum();
  1472. TH2D *res = new TH2D("sigTime2d", "", binsX / 200, 0., binsX / readOutFreq, 100, min, max);
  1473. for (int k = 0; k < binsX; k++)
  1474. {
  1475. res->Fill(k / readOutFreq, tmp->GetBinContent(k));
  1476. }
  1477. res->Draw("colz");
  1478. res->GetXaxis()->SetTitle("time [s]");
  1479. TString label;
  1480. label.Form("adc counts / %d #mus", intTime);
  1481. res->GetYaxis()->SetTitle(label.Data());
  1482. //c1->SaveAs("dummy");
  1483. res->Write();
  1484. return c1;
  1485. }
  1486. double Beammon::calcBeamTime()
  1487. {
  1488. beamOnTime = 0;
  1489. for (int i = 0; i < spillTimes.size(); i++)
  1490. {
  1491. beamOnTime += spillTimes[i].second - spillTimes[i].first;
  1492. }
  1493. return beamOnTime;
  1494. }
  1495. TCanvas *Beammon::PrintProfile(int offset)
  1496. {
  1497. TCanvas *c1 = new TCanvas();
  1498. c1->cd();
  1499. //TH1D *res = GetChannelSlice(-1,offset);
  1500. TGraphErrors *res = new TGraphErrors(64);
  1501. TGraphErrors *tmp = GetChannelGraph(-1, offset);
  1502. double x, y, errx, erry;
  1503. for (int ch = 0; ch < 63; ch++)
  1504. {
  1505. tmp->GetPoint(ch, x, y);
  1506. x *= 0.8;
  1507. errx = 0;
  1508. erry = tmp->GetErrorY(ch);
  1509. res->SetPoint(ch, x, y);
  1510. res->SetPointError(ch, errx, erry);
  1511. }
  1512. res->Fit("gaus", "Q0");
  1513. double stepsize = 0.01;
  1514. double lim = res->GetFunction("gaus")->GetMaximum() / 2;
  1515. double val, x1, x2, pos = 0;
  1516. bool below = true;
  1517. while (pos <= 64)
  1518. {
  1519. val = res->GetFunction("gaus")->Eval(pos);
  1520. if (below && val >= lim)
  1521. {
  1522. x1 = pos;
  1523. below = false;
  1524. }
  1525. else if (below == false && val <= lim)
  1526. {
  1527. x2 = pos;
  1528. break;
  1529. }
  1530. pos += stepsize;
  1531. }
  1532. fwhm = (x2 - x1) * channelWidth;
  1533. res->Draw("AP");
  1534. TString sName;
  1535. sName.Form("Profile%d", offset);
  1536. c1->SetName(sName.Data());
  1537. res->SetTitle("");
  1538. res->GetXaxis()->SetTitle("position [mm]");
  1539. TString label;
  1540. label.Form("adc counts / %d #mus", intTime);
  1541. res->GetYaxis()->SetTitle(label.Data());
  1542. res->GetYaxis()->SetTitleOffset(1.4);
  1543. TString str;
  1544. str.Form("fwhm = %.3f mm", fwhm);
  1545. TText *t = new TText(34, 900, str.Data());
  1546. t->Draw("SAME");
  1547. delete t;
  1548. delete res;
  1549. c1->Update();
  1550. c1->Modified();
  1551. //c1->SaveAs(sName.Data());
  1552. return c1;
  1553. }
  1554. TCanvas *Beammon::FitSnapshot(int offset)
  1555. {
  1556. //gSyle->SetOptFit(1111);
  1557. TCanvas *c1 = new TCanvas();
  1558. c1->cd();
  1559. TGraphErrors *res = GetChannelGraph(-1, offset);
  1560. TString sName;
  1561. sName.Form("Snapshot%d", offset);
  1562. c1->SetName(sName.Data());
  1563. res->SetTitle("");
  1564. res->GetXaxis()->SetTitle("channel");
  1565. TString label;
  1566. label.Form("adc counts / %d #mus", intTime);
  1567. res->GetYaxis()->SetTitle(label.Data());
  1568. res->GetYaxis()->SetTitleOffset(1.4);
  1569. //res->Fit("gaus");
  1570. res->Fit("gaus");
  1571. double mean = res->GetFunction("gaus")->GetParameter(1);
  1572. double sigma = res->GetFunction("gaus")->GetParameter(2);
  1573. double constant = res->GetFunction("gaus")->GetParameter(0);
  1574. TF1 *func = new TF1("doublegaus", fitfunc, 0, 64, 6); // t
  1575. func->SetNpx(1000);
  1576. func->SetParameter(0, constant);
  1577. func->SetParameter(1, mean);
  1578. func->SetParameter(2, sigma);
  1579. func->SetParameter(3, constant);
  1580. func->SetParameter(4, sigma);
  1581. func->SetParameter(5, 0.);
  1582. func->SetParLimits(1, 0., 64.);
  1583. func->SetParLimits(2, 1., 20.);
  1584. func->SetParLimits(4, 5., 100.);
  1585. //func->SetParameter(5,0.);
  1586. res->Fit("doublegaus");
  1587. res->Draw("AP");
  1588. c1->SaveAs(sName.Data());
  1589. return c1;
  1590. }
  1591. TCanvas *Beammon::FitAverage()
  1592. {
  1593. TCanvas *c1 = new TCanvas();
  1594. c1->cd();
  1595. int n = spillTimes.size();
  1596. double yValues[64];
  1597. double yErrors[64];
  1598. double xValues[64];
  1599. double xErrors[64];
  1600. int count = 0;
  1601. double x, y, errY;
  1602. TGraphErrors *tmp;
  1603. yValues[0] = 0;
  1604. for (int i = 0; i < n; i++)
  1605. {
  1606. int start = spillTimes[i].first;
  1607. int stop = spillTimes[i].second;
  1608. for (int k = start; k <= stop; k++)
  1609. {
  1610. count++;
  1611. tmp = GetChannelGraph(k, 0);
  1612. for (int bin = 1; bin <= 64; bin++)
  1613. {
  1614. errY = tmp->GetErrorY(bin);
  1615. tmp->GetPoint(bin, x, y);
  1616. if (!(y != y || errY != errY))
  1617. {
  1618. yValues[bin - 1] += y;
  1619. yErrors[bin - 1] += errY * errY;
  1620. }
  1621. else
  1622. {
  1623. yValues[bin - 1] += 0;
  1624. yErrors[bin - 1] += 0;
  1625. }
  1626. }
  1627. delete tmp;
  1628. }
  1629. }
  1630. for (int bin = 0; bin < 64; bin++)
  1631. {
  1632. xValues[bin] = bin;
  1633. xErrors[bin] = 1. / sqrt(12);
  1634. yValues[bin] *= 1. / count;
  1635. yErrors[bin] = sqrt(yErrors[bin]) / count;
  1636. }
  1637. /*for (int i = 0; i < 64; i++)
  1638. {
  1639. printf("channelErrors[%d]: %f ", i, channelErrors[i]);
  1640. printf("yValue[%d]: %f ", i, yValues[i]);
  1641. printf("yError[%d]: %f \n", i, yErrors[i]);
  1642. }*/
  1643. TGraph *res = new TGraph(64, xValues, yValues); //, xErrors, yErrors);
  1644. c1->SetName("avgGraph");
  1645. res->SetTitle("");
  1646. res->GetXaxis()->SetTitle("channel");
  1647. TString label;
  1648. label.Form("adc counts / %d #mus", intTime);
  1649. res->GetYaxis()->SetTitle(label.Data());
  1650. res->GetYaxis()->SetTitleOffset(1.4);
  1651. res->Fit("gaus", "Q0");
  1652. double mean = res->GetFunction("gaus")->GetParameter(1);
  1653. //mean_ave = mean;
  1654. double sigma = res->GetFunction("gaus")->GetParameter(2);
  1655. //sigma_ave = sigma;
  1656. double constant = res->GetFunction("gaus")->GetParameter(0);
  1657. //res->Fit("gaus","","",mean-1.5*sigma,mean+1.5*sigma);
  1658. TF1 *func = new TF1("doublegaus", fitfunc, 0, 64, 6); // t
  1659. func->SetNpx(1000);
  1660. func->SetParameter(0, constant);
  1661. func->SetParameter(1, mean);
  1662. func->SetParameter(2, sigma);
  1663. func->SetParameter(3, constant);
  1664. func->SetParameter(4, sigma);
  1665. func->SetParameter(5, 0.);
  1666. func->SetParLimits(1, 0., 64.);
  1667. func->SetParLimits(2, 1., 20.);
  1668. func->SetParLimits(4, 5., 100.);
  1669. //func->SetParameter(5,0.);
  1670. res->Fit("doublegaus");
  1671. res->Draw("AP");
  1672. //c1->SaveAs("avgFit");
  1673. // find fwhm
  1674. double stepsize = 0.01;
  1675. double lim = res->GetFunction("doublegaus")->GetMaximum() / 2;
  1676. double val, x1, x2, pos = 0;
  1677. bool below = true;
  1678. while (pos <= 64)
  1679. {
  1680. val = func->Eval(pos);
  1681. if (below && val >= lim)
  1682. {
  1683. x1 = pos;
  1684. below = false;
  1685. }
  1686. else if (below == false && val <= lim)
  1687. {
  1688. x2 = pos;
  1689. break;
  1690. }
  1691. pos += stepsize;
  1692. }
  1693. fwhm = (x2 - x1) * channelWidth;
  1694. return c1;
  1695. }
  1696. TCanvas *Beammon::PrintChannelNoise(int channel)
  1697. {
  1698. TCanvas *c1 = new TCanvas();
  1699. c1->cd();
  1700. TString name;
  1701. name.Form("channelNoise%d", channel);
  1702. TString label;
  1703. label.Form("adc counts / %d #mus", intTime);
  1704. c1->SetName(name.Data());
  1705. channelSig[channel]->SetTitle("");
  1706. channelSig[channel]->SetName(name.Data());
  1707. channelSig[channel]->GetXaxis()->SetTitle(label.Data());
  1708. channelSig[channel]->GetYaxis()->SetTitle("entries");
  1709. channelSig[channel]->Draw();
  1710. gStyle->SetOptStat("mr");
  1711. gPad->Modified();
  1712. //c1->SaveAs("blub");
  1713. return c1;
  1714. }
  1715. TCanvas *Beammon::IcFitSlicesY()
  1716. {
  1717. TCanvas *c1 = new TCanvas();
  1718. c1->cd();
  1719. c1->SetName("icFitSlicesY");
  1720. TH1D *tmp = timePosHist->ProjectionY("", 1, 64);
  1721. TH1D *means = (TH1D *)sigIcScatterPlot[10 + alignintIc]->ProfileY(); //(TH1D*)objectArray[1];
  1722. means->SetLineColor(kBlack);
  1723. //sigIcScatterPlot->Draw();
  1724. means->Draw("E");
  1725. means->GetXaxis()->SetRangeUser(0., means->GetXaxis()->GetXmax());
  1726. means->SetMinimum(0.);
  1727. means->SetTitle("");
  1728. //means->SetName("icFitSlicesY");
  1729. TString label1, label2;
  1730. label1.Form("mean photodiode signal [particles / %d #mus]", intTime);
  1731. label2.Form("ionisation chamber signal [particles / %d #mus]", intTime);
  1732. means->GetXaxis()->SetTitle(label2.Data());
  1733. means->GetYaxis()->SetTitle(label1.Data());
  1734. means->Fit("pol1");
  1735. //sigIcScatterPlot->Draw();
  1736. gPad->Modified();
  1737. //c1->SaveAs("blub");
  1738. means->Write();
  1739. return c1;
  1740. }
  1741. TCanvas *Beammon::PrintSigIcGraph()
  1742. {
  1743. TCanvas *c1 = new TCanvas();
  1744. c1->cd();
  1745. TString name;
  1746. name.Form("sigIcScatterGraph");
  1747. TString label, label2;
  1748. label.Form("adc counts / %d #mus", intTime);
  1749. label2.Form("whatever / %d #mus", intTime);
  1750. c1->SetName(name.Data());
  1751. sigIcScatterGraph->SetTitle("");
  1752. sigIcScatterGraph->SetName(name.Data());
  1753. sigIcScatterGraph->GetXaxis()->SetTitle(label2.Data());
  1754. sigIcScatterGraph->GetYaxis()->SetTitle(label.Data());
  1755. sigIcScatterGraph->Fit("pol1", "W");
  1756. sigIcScatterGraph->Draw("AP");
  1757. gPad->Modified();
  1758. //c1->SaveAs("blub");
  1759. return c1;
  1760. }
  1761. TH1D *Beammon::GetMwPosComp()
  1762. {
  1763. double bin, mean;
  1764. double shift = beamPosX->GetMean() - mwPosX->GetMean();
  1765. //printf("beam: %f \n",beamPosTimeGraph->GetMean(2));
  1766. int n = beamPosTimeGraph->GetN();
  1767. for (int i = 0; i < n; i++)
  1768. {
  1769. beamPosTimeGraph->GetPoint(i, bin, mean);
  1770. if (!mean)
  1771. continue;
  1772. positionComp->Fill(mean - mw1_posxHist->GetBinContent(bin - alignint) - shift);
  1773. }
  1774. return positionComp;
  1775. }
  1776. //
  1777. ///
  1778. /////////////////////////////////////////////////////////////////////////////
  1779. TCanvas *Beammon::GetSig_e_ic_Hist()
  1780. {
  1781. shift = beamPosX->GetMean() - mwPosX->GetMean();
  1782. printf("shift = %f", shift);
  1783. TCanvas *c1 = new TCanvas();
  1784. c1->cd();
  1785. c1->SetName("SignalComp_vs_Time");
  1786. Double_t norm = ic1Hist->Integral();
  1787. ic1Hist->Scale(1. / norm);
  1788. ic1Hist->SetLineColor(kRed);
  1789. ic1Hist->Draw("HIST L");
  1790. Double_t norm0 = sigHist->Integral();
  1791. Double_t scaleSig = norm / norm0;
  1792. sigHist->Scale(1. / norm0);
  1793. sigHist->Draw("HIST SAME L");
  1794. TLegend * legend= new TLegend(0.1, 0.7, 0.48, 0.9);
  1795. legend->AddEntry(ic1Hist, "ic", "l");
  1796. legend->AddEntry(sigHist, "Fibre", "l");
  1797. legend->Draw();
  1798. TH1D *compsig = new TH1D("compsig", "compsig", 200, -0.5, 0.5);
  1799. for (int i = 0; i < nevents; i++)
  1800. {
  1801. compsig->Fill((ic1Hist->GetBinContent(i) - sigHist->GetBinContent(i)) * 2 / (ic1Hist->GetBinContent(i) + sigHist->GetBinContent(i)));
  1802. }
  1803. compsig->Write();
  1804. TCanvas *c2 = new TCanvas();
  1805. c2->cd();
  1806. c2->SetName("PositionComp_vs_Time");
  1807. TH1D *fibreHistNew = new TH1D("fibreHistNew", "fibreHistNew", nevents, 0., nevents - 1);
  1808. for (int i = 0; i < nevents; i++)
  1809. {
  1810. if (fibreHist->GetBinContent(i) != 0)
  1811. {
  1812. fibreHistNew->SetBinContent(i, fibreHist->GetBinContent(i) - shift);
  1813. }
  1814. else
  1815. fibreHistNew->SetBinContent(i, 0);
  1816. }
  1817. fibreHistNew->Draw("HIST L");
  1818. mwpcHist->SetLineColor(kRed);
  1819. mwpcHist->SetLineColor(kRed);
  1820. mwpcHist->Draw("HIST SAME L");
  1821. TLegend * legend2 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1822. legend2->AddEntry(mwpcHist, "MWPC", "l");
  1823. legend2->AddEntry(fibreHistNew, "Fibre", "l");
  1824. legend2->Draw();
  1825. c2->Write();
  1826. return c1;
  1827. }
  1828. TCanvas *Beammon::GetSig_e_ic_Hist_Weighted()
  1829. {
  1830. TCanvas *c1 = new TCanvas();
  1831. c1->cd();
  1832. c1->SetName("SignalComp_vs_Time_all");
  1833. Double_t norm = ic1Hist->Integral();
  1834. ic1Hist->Scale(1. / norm);
  1835. ic1Hist->SetLineColor(kRed);
  1836. ic1Hist->SetLineWidth(2);
  1837. ic1Hist->Draw("HIST L");
  1838. Double_t norm0 = sigHist_weighted->Integral();
  1839. sigHist_weighted->Scale(1. / norm0);
  1840. sigHist_weighted->Draw("HIST SAME L");
  1841. Double_t norm1 = sigHist->Integral();
  1842. sigHist->Scale(1. / norm1);
  1843. sigHist->SetLineColor(kBlue);
  1844. sigHist->Draw("HIST SAME L");
  1845. TLegend * legend= new TLegend(0.1, 0.7, 0.48, 0.9);
  1846. legend->AddEntry(ic1Hist, "ic", "l");
  1847. legend->AddEntry(sigHist, "Fibre_fit", "l");
  1848. legend->AddEntry(sigHist_weighted, "Fibre_sum", "l");
  1849. legend->Draw();
  1850. c1->Write();
  1851. TH1D *compsig_weighted = new TH1D("compsig_weighted", "compsig", 200, -0.5, 0.5);
  1852. for (int i = 0; i < nevents; i++)
  1853. {
  1854. compsig_weighted->Fill((ic1Hist->GetBinContent(i) - sigHist_weighted->GetBinContent(i)) * 2 / (ic1Hist->GetBinContent(i) + sigHist_weighted->GetBinContent(i)));
  1855. }
  1856. compsig_weighted->SetLineColor(kRed);
  1857. compsig_weighted->Write();
  1858. TH1D *compsig = new TH1D("compsig", "compsig", 200, -0.5, 0.5);
  1859. for (int i = 0; i < nevents; i++)
  1860. {
  1861. compsig->Fill((ic1Hist->GetBinContent(i) - sigHist->GetBinContent(i)) * 2 / (ic1Hist->GetBinContent(i) + sigHist->GetBinContent(i)));
  1862. }
  1863. c1->cd();
  1864. c1->SetName("CompSig_all");
  1865. compsig->Draw();
  1866. compsig_weighted->Draw("SAME");
  1867. TLegend * legend2 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1868. legend2->AddEntry(compsig_weighted, "Weighted", "l");
  1869. legend2->AddEntry(compsig, "Fit", "l");
  1870. legend2->Draw();
  1871. c1->Write();
  1872. TCanvas *c2 = new TCanvas();
  1873. c2->cd();
  1874. c2->SetName("PositionComp_vs_Time_all");
  1875. TH1D *fibreHistNew_weighted = new TH1D("fibreHistNew_weighted", "fibreHistNew_weighted", nevents, 0., nevents - 1);
  1876. for (int i = 0; i < nevents; i++)
  1877. {
  1878. if (fibreHist_weighted->GetBinContent(i) != 0)
  1879. {
  1880. fibreHistNew_weighted->SetBinContent(i, fibreHist_weighted->GetBinContent(i) - shift);
  1881. }
  1882. else
  1883. fibreHistNew_weighted->SetBinContent(i, 0);
  1884. }
  1885. TH1D *fibreHistNew = new TH1D("fibreHistNew", "fibreHistNew", nevents, 0., nevents - 1);
  1886. for (int i = 0; i < nevents; i++)
  1887. {
  1888. if (fibreHist->GetBinContent(i) != 0)
  1889. {
  1890. fibreHistNew->SetBinContent(i, fibreHist->GetBinContent(i) - shift);
  1891. }
  1892. else
  1893. fibreHistNew->SetBinContent(i, 0);
  1894. }
  1895. fibreHistNew->Draw("HIST L");
  1896. fibreHistNew->SetLineColor(kBlue);
  1897. fibreHistNew_weighted->Draw("HIST L SAME");
  1898. mwpcHist->SetLineColor(kRed);
  1899. mwpcHist->SetLineWidth(2);
  1900. mwpcHist->Draw("HIST SAME L");
  1901. TLegend * legend3 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1902. legend3->AddEntry(mwpcHist, "MWPC", "l");
  1903. legend3->AddEntry(fibreHistNew, "Fibre_fit", "l");
  1904. legend3->AddEntry(fibreHistNew_weighted, "Fibre_Weighted", "l");
  1905. legend3->Draw();
  1906. c2->Write();
  1907. c2->cd();
  1908. c2->Clear();
  1909. c2->SetName("PosComp_All");
  1910. TH1D *comppos_weighted = new TH1D("comppos_weighted", "comppos", 200, -0.4, 0.4);
  1911. for (int i = 0; i < nevents; i++)
  1912. {
  1913. comppos_weighted->Fill((mwpcHist->GetBinContent(i) - fibreHist_weighted->GetBinContent(i) + shift));
  1914. }
  1915. comppos_weighted->SetLineColor(kRed);
  1916. comppos_weighted->Write();
  1917. TH1D *comppos = new TH1D("comppos", "comppos", 200, -0.4, 0.4);
  1918. for (int i = 0; i < nevents; i++)
  1919. {
  1920. comppos->Fill((mwpcHist->GetBinContent(i) - fibreHist->GetBinContent(i) + shift));
  1921. }
  1922. comppos->Draw();
  1923. comppos_weighted->Draw("SAME");
  1924. TLegend * legend4 = new TLegend(0.1, 0.7, 0.48, 0.9);
  1925. legend4->AddEntry(comppos_weighted, "Weighted", "l");
  1926. legend4->AddEntry(comppos, "Fit", "l");
  1927. legend4->Draw();
  1928. return c2;
  1929. }