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.

919 lines
29 KiB

  1. //////////////////////////////////////////////////////////
  2. // This class has been automatically generated on
  3. // Tue Sep 12 15:40:57 2017 by ROOT version 5.34/32
  4. // from TTree t/t
  5. // found on file: ../../HIT_26-11-2016/with_timestamp/pin/PiN_run1.root
  6. //////////////////////////////////////////////////////////
  7. #ifndef analyse2_h
  8. #define analyse2_h
  9. #include <TROOT.h>
  10. #include <TChain.h>
  11. #include <TFile.h>
  12. #include <TTree.h>
  13. #include <stdio.h>
  14. #include <TH2D.h>
  15. #include <TH1D.h>
  16. #include <TProfile.h>
  17. #include <TCanvas.h>
  18. #include <TF1.h>
  19. #include <TMath.h>
  20. #include <vector>
  21. #include <utility>
  22. #include <TString.h>
  23. #include <TDirectory.h>
  24. #include <TString.h>
  25. #include <TVectorD.h>
  26. #include <TGraph.h>
  27. #include <TGraphErrors.h>
  28. #include <cmath>
  29. #include <TPaveStats.h>
  30. #include <TText.h>
  31. #include <TStyle.h>
  32. #include <TObjArray.h>
  33. #include <TLegend.h>
  34. #include <TFitResult.h>
  35. #include <gsl/gsl_statistics.h>
  36. #include <math.h>
  37. #include <gsl/gsl_errno.h>
  38. #include <gsl/gsl_fft_complex.h>
  39. using namespace std;
  40. // Header file for the classes stored in the TTree if any.
  41. // Fixed size dimensions of array or collections stored in the TTree if any.
  42. class analyse2 {
  43. public :
  44. TTree *fChain; //!pointer to the analyzed TTree or TChain
  45. Int_t fCurrent; //!current Tree number in a TChain
  46. TTree *data;
  47. TTree *newdata;
  48. // Declaration of leaf types
  49. Double_t time;
  50. Double_t event_id;
  51. Double_t sync_out;
  52. Double_t sync_in;
  53. Double_t data_ok;
  54. Double_t led_voltage;
  55. Double_t current;
  56. Double_t ch00;
  57. Double_t ch01;
  58. Double_t ch02;
  59. Double_t ch03;
  60. Double_t ch04;
  61. Double_t ch05;
  62. Double_t ch06;
  63. Double_t ch07;
  64. Double_t ch08;
  65. Double_t ch09;
  66. Double_t ch10;
  67. Double_t ch11;
  68. Double_t ch12;
  69. Double_t ch13;
  70. Double_t ch14;
  71. Double_t ch15;
  72. Double_t ch16;
  73. Double_t ch17;
  74. Double_t ch18;
  75. Double_t ch19;
  76. Double_t ch20;
  77. Double_t ch21;
  78. Double_t ch22;
  79. Double_t ch23;
  80. Double_t ch24;
  81. Double_t ch25;
  82. Double_t ch26;
  83. Double_t ch27;
  84. Double_t ch28;
  85. Double_t ch29;
  86. Double_t ch30;
  87. Double_t ch31;
  88. Double_t ch32;
  89. Double_t ch33;
  90. Double_t ch34;
  91. Double_t ch35;
  92. Double_t ch36;
  93. Double_t ch37;
  94. Double_t ch38;
  95. Double_t ch39;
  96. Double_t ch40;
  97. Double_t ch41;
  98. Double_t ch42;
  99. Double_t ch43;
  100. Double_t ch44;
  101. Double_t ch45;
  102. Double_t ch46;
  103. Double_t ch47;
  104. Double_t ch48;
  105. Double_t ch49;
  106. Double_t ch50;
  107. Double_t ch51;
  108. Double_t ch52;
  109. Double_t ch53;
  110. Double_t ch54;
  111. Double_t ch55;
  112. Double_t ch56;
  113. Double_t ch57;
  114. Double_t ch58;
  115. Double_t ch59;
  116. Double_t ch60;
  117. Double_t ch61;
  118. Double_t ch62;
  119. Double_t ch63;
  120. Double_t ic1;
  121. Double_t ic2;
  122. Double_t mw1_focusx;
  123. Double_t mw1_focusy;
  124. Double_t mw2_focusx;
  125. Double_t mw2_focusy;
  126. Double_t mw1_posx;
  127. Double_t mw1_posy;
  128. Double_t mw2_posx;
  129. Double_t mw2_posy;
  130. // List of branches
  131. TBranch *b_time; //!
  132. TBranch *b_event_id; //!
  133. TBranch *b_sync_out; //!
  134. TBranch *b_sync_in; //!
  135. TBranch *b_data_ok; //!
  136. TBranch *b_led_voltage; //!
  137. TBranch *b_current; //!
  138. TBranch *b_ch00; //!
  139. TBranch *b_ch01; //!
  140. TBranch *b_ch02; //!
  141. TBranch *b_ch03; //!
  142. TBranch *b_ch04; //!
  143. TBranch *b_ch05; //!
  144. TBranch *b_ch06; //!
  145. TBranch *b_ch07; //!
  146. TBranch *b_ch08; //!
  147. TBranch *b_ch09; //!
  148. TBranch *b_ch10; //!
  149. TBranch *b_ch11; //!
  150. TBranch *b_ch12; //!
  151. TBranch *b_ch13; //!
  152. TBranch *b_ch14; //!
  153. TBranch *b_ch15; //!
  154. TBranch *b_ch16; //!
  155. TBranch *b_ch17; //!
  156. TBranch *b_ch18; //!
  157. TBranch *b_ch19; //!
  158. TBranch *b_ch20; //!
  159. TBranch *b_ch21; //!
  160. TBranch *b_ch22; //!
  161. TBranch *b_ch23; //!
  162. TBranch *b_ch24; //!
  163. TBranch *b_ch25; //!
  164. TBranch *b_ch26; //!
  165. TBranch *b_ch27; //!
  166. TBranch *b_ch28; //!
  167. TBranch *b_ch29; //!
  168. TBranch *b_ch30; //!
  169. TBranch *b_ch31; //!
  170. TBranch *b_ch32; //!
  171. TBranch *b_ch33; //!
  172. TBranch *b_ch34; //!
  173. TBranch *b_ch35; //!
  174. TBranch *b_ch36; //!
  175. TBranch *b_ch37; //!
  176. TBranch *b_ch38; //!
  177. TBranch *b_ch39; //!
  178. TBranch *b_ch40; //!
  179. TBranch *b_ch41; //!
  180. TBranch *b_ch42; //!
  181. TBranch *b_ch43; //!
  182. TBranch *b_ch44; //!
  183. TBranch *b_ch45; //!
  184. TBranch *b_ch46; //!
  185. TBranch *b_ch47; //!
  186. TBranch *b_ch48; //!
  187. TBranch *b_ch49; //!
  188. TBranch *b_ch50; //!
  189. TBranch *b_ch51; //!
  190. TBranch *b_ch52; //!
  191. TBranch *b_ch53; //!
  192. TBranch *b_ch54; //!
  193. TBranch *b_ch55; //!
  194. TBranch *b_ch56; //!
  195. TBranch *b_ch57; //!
  196. TBranch *b_ch58; //!
  197. TBranch *b_ch59; //!
  198. TBranch *b_ch60; //!
  199. TBranch *b_ch61; //!
  200. TBranch *b_ch62; //!
  201. TBranch *b_ch63; //!
  202. TBranch *b_ic1; //!
  203. TBranch *b_ic2; //!
  204. TBranch *b_mw1_focusx; //!
  205. TBranch *b_mw1_focusy; //!
  206. TBranch *b_mw2_focusx; //!
  207. TBranch *b_mw2_focusy; //!
  208. TBranch *b_mw1_posx; //!
  209. TBranch *b_mw1_posy; //!
  210. TBranch *b_mw2_posx; //!
  211. TBranch *b_mw2_posy; //!
  212. char *file;
  213. TFile *fileIn;
  214. TFile *fileOut;
  215. TObjArray objectArray;
  216. TBranch *b_channels[64];
  217. TBranch *b_baseline[64];
  218. double channels[64];
  219. double channelamp_smooth[64];
  220. double baseline[64];
  221. double channellist[64];
  222. double errorx[64];
  223. double errory[64];
  224. TBranch *b_beamNumX_1;
  225. TBranch *b_beamPosX_1;
  226. TBranch *b_beamFocusX_1;
  227. TBranch *b_beamSignal_1;
  228. TBranch *b_beamSidebandNoise_1;
  229. TBranch *b_beamSkewX_1;
  230. TBranch *b_beamKurtX_1;
  231. TBranch *b_beamPosX_fit;
  232. TBranch *b_beamFocusX_fit;
  233. TBranch *b_beamChi2_fit;
  234. TBranch *b_beamPeakX_fit;
  235. TBranch *b_beamPosX_fit2;
  236. TBranch *b_beamFocusX_fit2;
  237. TBranch *b_beamFocus2X_fit2;
  238. TBranch *b_beamChi2_fit2;
  239. TBranch *b_beamPeakX_fit2;
  240. TBranch *b_beamPeak2X_fit2;
  241. TBranch *b_ic1_1; //!
  242. TBranch *b_ic2_1; //!
  243. TBranch *b_mw1_focusx_1; //!
  244. TBranch *b_mw1_focusy_1; //!
  245. TBranch *b_mw2_focusx_1; //!
  246. TBranch *b_mw2_focusy_1; //!
  247. TBranch *b_mw1_posx_1; //!
  248. TBranch *b_mw1_posy_1; //!
  249. TBranch *b_mw2_posx_1; //!
  250. TBranch *b_mw2_posy_1; //!
  251. TBranch *b_baseline_integral;
  252. TBranch *b_time_1; //!
  253. TBranch *b_eventid_1; //!
  254. TBranch *b_rollingavg; //!
  255. TBranch *b_beamon; //!
  256. double time_1;
  257. double ic1_1;
  258. double ic2_1;
  259. double mw1_focusx_1;
  260. double mw2_focusx_1;
  261. double mw2_posx_1;
  262. double mw1_posx_1;
  263. double mw1_focusy_1;
  264. double mw2_focusy_1;
  265. double mw2_posy_1;
  266. double mw1_posy_1;
  267. double beamNumX_1;
  268. double beamPosX_1;
  269. double beamFocusX_1;
  270. double beamSignal_1;
  271. double beamSidebandNoise_1;
  272. double beamSkewX_1;
  273. double beamKurtX_1;
  274. double baseline_integral;
  275. double chargeintegral;
  276. double timeintegral;
  277. double eventid_1;
  278. double rollingavg;
  279. int beamon;
  280. double beamPosX_fit;
  281. double beamFocusX_fit;
  282. double beamChi2_fit;
  283. double beamPeakX_fit;
  284. double beamPosX_fit2;
  285. double beamFocusX_fit2;
  286. double beamFocus2X_fit2;
  287. double beamChi2_fit2;
  288. double beamPeakX_fit2;
  289. double beamPeak2X_fit2;
  290. analyse2(TTree *tree=0);
  291. virtual ~analyse2();
  292. virtual Int_t Cut(Long64_t entry);
  293. virtual Int_t GetEntry(Long64_t entry);
  294. virtual Long64_t LoadTree(Long64_t entry);
  295. virtual void Init(TTree *tree);
  296. virtual void Loop();
  297. virtual Bool_t Notify();
  298. virtual void Show(Long64_t entry = -1);
  299. double channelWidth;
  300. 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);
  301. void Baseline(bool useFirstEvents, int nBaselineEvents, int StartEvent, bool run50hz);
  302. void Save();
  303. void Close();
  304. double * FFTsmoothing(double arr[]);
  305. // double RollingAverage(const int length, double newdata);
  306. TH2D * th2d_mw1_beamPosX;
  307. TH2D * th2d_ic1_beamSignal;
  308. TH1D * th1d_beamSignal_ic1ratio;
  309. TH2D * th2d_beamSignal_channel;
  310. TH2D * th2d_bkg_channel;
  311. TH1D * th1d_baseline;
  312. TH1D * th1d_baselinesub;
  313. TH1D * th1d_chbaseline;
  314. TH1D * th1d_chbaselinesub;
  315. TH2D * th2d_fitdiff_channel;
  316. TH2D * th2d_fit2diff_channel;
  317. TH2D * th2d_AmpFFT_channel;
  318. TH2D * th2d_PhaseFFT_channel;
  319. TFitResultPtr bkgfitResult;
  320. Double_t bkgfitPar0[65];
  321. Double_t bkgfitPar1[65];
  322. Double_t bkgfitPar2[65];
  323. Double_t bkgfitPar3[65];
  324. Double_t bkgfitPar4[65];
  325. };
  326. #endif
  327. #ifdef analyse2_cxx
  328. analyse2::analyse2(TTree *tree) : fChain(0)
  329. {
  330. // if parameter tree is not specified (or zero), connect the file
  331. // used to generate this class and read the Tree.
  332. if (tree == 0) {
  333. cerr << "no TTree found." << endl;
  334. }
  335. Init(tree);
  336. }
  337. analyse2::~analyse2()
  338. {
  339. if (!fChain) return;
  340. delete fChain->GetCurrentFile();
  341. }
  342. Int_t analyse2::GetEntry(Long64_t entry)
  343. {
  344. // Read contents of entry.
  345. if (!fChain) return 0;
  346. return fChain->GetEntry(entry);
  347. }
  348. Long64_t analyse2::LoadTree(Long64_t entry)
  349. {
  350. // Set the environment to read one entry
  351. if (!fChain) return -5;
  352. Long64_t centry = fChain->LoadTree(entry);
  353. if (centry < 0) return centry;
  354. if (fChain->GetTreeNumber() != fCurrent) {
  355. fCurrent = fChain->GetTreeNumber();
  356. Notify();
  357. }
  358. return centry;
  359. }
  360. void analyse2::Init(TTree *tree)
  361. {
  362. // The Init() function is called when the selector needs to initialize
  363. // a new tree or chain. Typically here the branch addresses and branch
  364. // pointers of the tree will be set.
  365. // It is normally not necessary to make changes to the generated
  366. // code, but the routine can be extended by the user if needed.
  367. // Init() will be called many times when running on PROOF
  368. // (once per file to be processed).
  369. // Set branch addresses and branch pointers
  370. if (!tree) return;
  371. fChain = tree;
  372. fCurrent = -1;
  373. fChain->SetMakeClass(1);
  374. fChain->SetBranchAddress("time", &time, &b_time);
  375. fChain->SetBranchAddress("event_id", &event_id, &b_event_id);
  376. fChain->SetBranchAddress("sync_out", &sync_out, &b_sync_out);
  377. fChain->SetBranchAddress("sync_in", &sync_in, &b_sync_in);
  378. fChain->SetBranchAddress("data_ok", &data_ok, &b_data_ok);
  379. fChain->SetBranchAddress("led_voltage", &led_voltage, &b_led_voltage);
  380. fChain->SetBranchAddress("current", &current, &b_current);
  381. fChain->SetBranchAddress("ch00", &ch00, &b_ch00);
  382. fChain->SetBranchAddress("ch01", &ch01, &b_ch01);
  383. fChain->SetBranchAddress("ch02", &ch02, &b_ch02);
  384. fChain->SetBranchAddress("ch03", &ch03, &b_ch03);
  385. fChain->SetBranchAddress("ch04", &ch04, &b_ch04);
  386. fChain->SetBranchAddress("ch05", &ch05, &b_ch05);
  387. fChain->SetBranchAddress("ch06", &ch06, &b_ch06);
  388. fChain->SetBranchAddress("ch07", &ch07, &b_ch07);
  389. fChain->SetBranchAddress("ch08", &ch08, &b_ch08);
  390. fChain->SetBranchAddress("ch09", &ch09, &b_ch09);
  391. fChain->SetBranchAddress("ch10", &ch10, &b_ch10);
  392. fChain->SetBranchAddress("ch11", &ch11, &b_ch11);
  393. fChain->SetBranchAddress("ch12", &ch12, &b_ch12);
  394. fChain->SetBranchAddress("ch13", &ch13, &b_ch13);
  395. fChain->SetBranchAddress("ch14", &ch14, &b_ch14);
  396. fChain->SetBranchAddress("ch15", &ch15, &b_ch15);
  397. fChain->SetBranchAddress("ch16", &ch16, &b_ch16);
  398. fChain->SetBranchAddress("ch17", &ch17, &b_ch17);
  399. fChain->SetBranchAddress("ch18", &ch18, &b_ch18);
  400. fChain->SetBranchAddress("ch19", &ch19, &b_ch19);
  401. fChain->SetBranchAddress("ch20", &ch20, &b_ch20);
  402. fChain->SetBranchAddress("ch21", &ch21, &b_ch21);
  403. fChain->SetBranchAddress("ch22", &ch22, &b_ch22);
  404. fChain->SetBranchAddress("ch23", &ch23, &b_ch23);
  405. fChain->SetBranchAddress("ch24", &ch24, &b_ch24);
  406. fChain->SetBranchAddress("ch25", &ch25, &b_ch25);
  407. fChain->SetBranchAddress("ch26", &ch26, &b_ch26);
  408. fChain->SetBranchAddress("ch27", &ch27, &b_ch27);
  409. fChain->SetBranchAddress("ch28", &ch28, &b_ch28);
  410. fChain->SetBranchAddress("ch29", &ch29, &b_ch29);
  411. fChain->SetBranchAddress("ch30", &ch30, &b_ch30);
  412. fChain->SetBranchAddress("ch31", &ch31, &b_ch31);
  413. fChain->SetBranchAddress("ch32", &ch32, &b_ch32);
  414. fChain->SetBranchAddress("ch33", &ch33, &b_ch33);
  415. fChain->SetBranchAddress("ch34", &ch34, &b_ch34);
  416. fChain->SetBranchAddress("ch35", &ch35, &b_ch35);
  417. fChain->SetBranchAddress("ch36", &ch36, &b_ch36);
  418. fChain->SetBranchAddress("ch37", &ch37, &b_ch37);
  419. fChain->SetBranchAddress("ch38", &ch38, &b_ch38);
  420. fChain->SetBranchAddress("ch39", &ch39, &b_ch39);
  421. fChain->SetBranchAddress("ch40", &ch40, &b_ch40);
  422. fChain->SetBranchAddress("ch41", &ch41, &b_ch41);
  423. fChain->SetBranchAddress("ch42", &ch42, &b_ch42);
  424. fChain->SetBranchAddress("ch43", &ch43, &b_ch43);
  425. fChain->SetBranchAddress("ch44", &ch44, &b_ch44);
  426. fChain->SetBranchAddress("ch45", &ch45, &b_ch45);
  427. fChain->SetBranchAddress("ch46", &ch46, &b_ch46);
  428. fChain->SetBranchAddress("ch47", &ch47, &b_ch47);
  429. fChain->SetBranchAddress("ch48", &ch48, &b_ch48);
  430. fChain->SetBranchAddress("ch49", &ch49, &b_ch49);
  431. fChain->SetBranchAddress("ch50", &ch50, &b_ch50);
  432. fChain->SetBranchAddress("ch51", &ch51, &b_ch51);
  433. fChain->SetBranchAddress("ch52", &ch52, &b_ch52);
  434. fChain->SetBranchAddress("ch53", &ch53, &b_ch53);
  435. fChain->SetBranchAddress("ch54", &ch54, &b_ch54);
  436. fChain->SetBranchAddress("ch55", &ch55, &b_ch55);
  437. fChain->SetBranchAddress("ch56", &ch56, &b_ch56);
  438. fChain->SetBranchAddress("ch57", &ch57, &b_ch57);
  439. fChain->SetBranchAddress("ch58", &ch58, &b_ch58);
  440. fChain->SetBranchAddress("ch59", &ch59, &b_ch59);
  441. fChain->SetBranchAddress("ch60", &ch60, &b_ch60);
  442. fChain->SetBranchAddress("ch61", &ch61, &b_ch61);
  443. fChain->SetBranchAddress("ch62", &ch62, &b_ch62);
  444. fChain->SetBranchAddress("ch63", &ch63, &b_ch63);
  445. fChain->SetBranchAddress("ic1", &ic1, &b_ic1);
  446. fChain->SetBranchAddress("ic2", &ic2, &b_ic2);
  447. fChain->SetBranchAddress("mw1_focusx", &mw1_focusx, &b_mw1_focusx);
  448. fChain->SetBranchAddress("mw1_focusy", &mw1_focusy, &b_mw1_focusy);
  449. fChain->SetBranchAddress("mw2_focusx", &mw2_focusx, &b_mw2_focusx);
  450. fChain->SetBranchAddress("mw2_focusy", &mw2_focusy, &b_mw2_focusy);
  451. fChain->SetBranchAddress("mw1_posx", &mw1_posx, &b_mw1_posx);
  452. fChain->SetBranchAddress("mw1_posy", &mw1_posy, &b_mw1_posy);
  453. fChain->SetBranchAddress("mw2_posx", &mw2_posx, &b_mw2_posx);
  454. fChain->SetBranchAddress("mw2_posy", &mw2_posy, &b_mw2_posy);
  455. // v->ResizeTo(10);
  456. Notify();
  457. }
  458. Bool_t analyse2::Notify()
  459. {
  460. // The Notify() function is called when a new file is opened. This
  461. // can be either for a new TTree in a TChain or when when a new TTree
  462. // is started when using PROOF. It is normally not necessary to make changes
  463. // to the generated code, but the routine can be extended by the
  464. // user if needed. The return value is currently not used.
  465. return kTRUE;
  466. }
  467. void analyse2::Show(Long64_t entry)
  468. {
  469. // Print contents of entry.
  470. // If entry is not specified, print current entry
  471. if (!fChain) return;
  472. fChain->Show(entry);
  473. }
  474. Int_t analyse2::Cut(Long64_t entry)
  475. {
  476. // This function may be called from Loop.
  477. // returns 1 if entry is accepted.
  478. // returns -1 otherwise.
  479. return 1;
  480. }
  481. void analyse2::Initialize(const char *dirname, const char *filename, int baselineEvents, int prelimEvents, double beamLevel, bool firstFibermat, double readOutFrequency, int integrationTime)
  482. {
  483. TString fn = TString(filename);
  484. TString dn = TString(dirname);
  485. channelWidth = 0.8;
  486. TString inName = dn + "pin/" + fn;
  487. fileIn = new TFile(inName, "READ");
  488. fileIn->GetObject("t", fChain);
  489. fChain->Print();
  490. Init(fChain);
  491. TString outname = filename;
  492. outname.Insert(0, "SAVE_");
  493. outname.Insert(0, dirname);
  494. fileOut = new TFile(outname.Data(), "RECREATE");
  495. newdata = new TTree("newdata", "newdata");
  496. for (int n = 0; n < 64; n++)
  497. {
  498. baseline[n] = 0.;
  499. channellist[n] = double(n);
  500. errorx[n] = 0.0;
  501. errory[n] = 7.0;
  502. char bname[20];
  503. sprintf(bname, "ch%02d", n);
  504. b_channels[n] = fChain->GetBranch(bname);
  505. b_channels[n]->SetAddress(&channels[n]);
  506. }
  507. b_eventid_1 = newdata->Branch("eventid_1", &eventid_1);
  508. b_beamNumX_1 = newdata->Branch("beamNumX_1", &beamNumX_1);
  509. b_beamPosX_1 = newdata->Branch("beamPosX_1", &beamPosX_1);
  510. b_beamFocusX_1 = newdata->Branch("beamFocusX_1", &beamFocusX_1);
  511. b_beamSignal_1 = newdata->Branch("beamSignal_1", &beamSignal_1);
  512. b_beamSidebandNoise_1 = newdata->Branch("beamSidebandNoise_1", &beamSidebandNoise_1);
  513. b_beamSkewX_1 = newdata->Branch("beamSkewX_1", &beamSkewX_1);
  514. b_beamKurtX_1 = newdata->Branch("beamKurtX_1", &beamKurtX_1);
  515. b_beamPosX_fit = newdata->Branch("beamPosX_fit", &beamPosX_fit);
  516. b_beamFocusX_fit = newdata->Branch("beamFocusX_fit", &beamFocusX_fit);
  517. b_beamChi2_fit = newdata->Branch("beamChi2_fit", &beamChi2_fit);
  518. b_beamPeakX_fit = newdata->Branch("beamPeakX_fit", &beamPeakX_fit);
  519. b_beamPosX_fit2 = newdata->Branch("beamPosX_fit2", &beamPosX_fit2);
  520. b_beamFocusX_fit2 = newdata->Branch("beamFocusX_fit2", &beamFocusX_fit2);
  521. b_beamFocus2X_fit2 = newdata->Branch("beamFocus2X_fit2", &beamFocus2X_fit2);
  522. b_beamChi2_fit2 = newdata->Branch("beamChi2_fit2", &beamChi2_fit2);
  523. b_beamPeakX_fit2 = newdata->Branch("beamPeakX_fit2", &beamPeakX_fit2);
  524. b_beamPeak2X_fit2 = newdata->Branch("beamPeak2X_fit2", &beamPeak2X_fit2);
  525. b_ic1_1 = newdata->Branch("ic1_1", &ic1_1);
  526. b_ic2_1 = newdata->Branch("ic2_1", &ic2_1);
  527. b_mw1_focusx_1 = newdata->Branch("mw1_focusx_1", &mw1_focusx_1);
  528. b_mw1_posx_1 = newdata->Branch("mw1_posx_1", &mw1_posx_1);
  529. b_mw2_focusx_1 = newdata->Branch("mw2_focusx_1", &mw2_focusx_1);
  530. b_mw2_posx_1 = newdata->Branch("mw2_posx_1", &mw2_posx_1);
  531. b_mw1_focusy_1 = newdata->Branch("mw1_focusy_1", &mw1_focusy_1);
  532. b_mw1_posy_1 = newdata->Branch("mw1_posy_1", &mw1_posy_1);
  533. b_mw2_focusy_1 = newdata->Branch("mw2_focusy_1", &mw2_focusy_1);
  534. b_mw2_posy_1 = newdata->Branch("mw2_posy_1", &mw2_posy_1);
  535. b_rollingavg = newdata->Branch("rollingavg", &rollingavg);
  536. b_beamon = newdata->Branch("beamon", &beamon);
  537. b_time_1 = newdata->Branch("time_1", &time_1);
  538. //histograms;
  539. th2d_mw1_beamPosX = new TH2D("th2d_mw1_beamPosX","th2d_mw1_beamPosX",1000,0,64,1000,0,60);
  540. th2d_ic1_beamSignal = new TH2D("th2d_ic1_beamSignal","th2d_ic1_beamSignal",500,0,500,500,0,100000);
  541. th1d_beamSignal_ic1ratio = new TH1D("th1d_beamSignal_ic1ratio","th1d_beamSignal_ic1ratio",500,0,500.);
  542. th2d_beamSignal_channel = new TH2D("th2d_beamSignal_channel","th2d_beamSignal_channel",65,-0.5,64.5,5100,-1000,10000);
  543. th2d_fitdiff_channel = new TH2D("th2d_fitdiff_channel","th2d_fitdiff_channel",65,-0.5,64.5,5100,-10000,1000);
  544. th2d_fit2diff_channel = new TH2D("th2d_fit2diff_channel","th2d_fit2diff_channel",65,-0.5,64.5,5100,-10000,1000);
  545. th2d_bkg_channel = new TH2D("th2d_bkg_channel","th2d_bkg_channel",65,-0.5,64.5,500,-500,500);
  546. th1d_baseline = new TH1D("th1d_baseline","th1d_baseline",500,-1500,1500.);
  547. th1d_baselinesub = new TH1D("th1d_baselinesub","th1d_baselinesub",500,-1500,1500.);
  548. th1d_chbaseline = new TH1D("th1d_chbaseline","th1d_chbaseline",2100,-100,1000.);
  549. th1d_chbaselinesub = new TH1D("th1d_chbaselinesub","th1d_chbaselinesub",200,-100,100.);
  550. th2d_AmpFFT_channel = new TH2D("th2d_AmpFFT_channel","th2d_AmpFFT_channel",65,-0.5,64.5,500,0,1000);
  551. th2d_PhaseFFT_channel = new TH2D("th2d_PhaseFFT_channel","th2d_PhaseFFT_channel",65,-0.5,64.5,100,-2,2);
  552. }
  553. void analyse2::Baseline(bool useFirstEvents, const int nBaselineEvents, int startevent, bool run50hz)
  554. {
  555. Long64_t nevents = fChain->GetEntriesFast();
  556. baseline_integral = 0.;
  557. // use the first/last nBaselineEvents events to calculate the average/baseline
  558. cout << "Start background subtraction event:" << startevent << endl;
  559. const int numbaseline = 3500;
  560. Double_t channelamp[numbaseline];
  561. Double_t basetime[numbaseline] = {0.};
  562. TGraph * g1;
  563. TF1 * fitfunc = new TF1("fitfunc","[0]+[1]*TMath::Cos(x*[2]*(2*3.14159)+[3])");
  564. fitfunc->SetParameters( 0.0, 10.0, 0.05, 0.0 );
  565. fitfunc->FixParameter(2,0.05);
  566. fChain->GetEvent(1);
  567. Double_t basetimeoffsetzero = time;
  568. fChain->GetEvent(startevent+numbaseline);
  569. Double_t basetimeoffset = time-basetimeoffsetzero;
  570. fChain->GetEvent(startevent);
  571. Double_t basetimeoffsetstart = time-basetimeoffsetzero;
  572. Int_t counter = 0;
  573. char bname[20];
  574. for (int ch = 0; ch < 64; ch++)
  575. {
  576. baseline[ch] =0.;
  577. }
  578. if (!useFirstEvents)
  579. { // last
  580. for (int i = nevents- numbaseline; i < nevents; i++)
  581. {
  582. fChain->GetEvent(i);
  583. for (int ch = 0; ch < 64; ch++)
  584. {
  585. if (channels[ch]>-100) baseline[ch] += channels[ch] / double(numbaseline);
  586. if (channels[ch]>-100) baseline_integral +=baseline[ch];
  587. th1d_chbaseline->Fill(channels[ch]);
  588. }
  589. }
  590. // cout << "baseline int: " << baseline_integral << endl;
  591. }
  592. else
  593. {
  594. for (int i = startevent ; i <= startevent+ numbaseline; i++)
  595. {
  596. fChain->GetEvent(i);
  597. for (int ch = 0; ch < 64; ch++)
  598. {
  599. if (channels[ch]>-100) baseline[ch] += channels[ch] / double(numbaseline);
  600. if (channels[ch]>-100) baseline_integral +=baseline[ch];
  601. th1d_chbaseline->Fill(channels[ch]);
  602. }
  603. // cout << "baseline int: " << baseline_integral << endl;
  604. }
  605. }
  606. if (run50hz){
  607. ///fit a 50Hz background noise to each channel
  608. for (int ch = 0; ch < 64; ch++)
  609. {
  610. counter = 0;
  611. for (int i = startevent ; i < startevent+numbaseline; i++)
  612. {
  613. fChain->GetEvent(i);
  614. // for (int ch = 0; ch < 64; ch++)
  615. //{
  616. channelamp[counter]= channels[ch]- baseline[ch];
  617. //}
  618. basetime[counter] = time-basetimeoffsetzero;
  619. counter++;
  620. }
  621. g1 = new TGraph(numbaseline,basetime,channelamp);
  622. ///////finished here. 1) need to fit the baseline to a Cosine function 2) return 3 parameters per channel. 50 Hz is fixed.
  623. bkgfitResult = g1->Fit(fitfunc,"QSR","",basetimeoffsetstart,basetimeoffset );
  624. bkgfitPar0[ch] = bkgfitResult->Parameter(0);
  625. bkgfitPar1[ch] = bkgfitResult->Parameter(1);
  626. bkgfitPar2[ch] = bkgfitResult->Parameter(2);
  627. bkgfitPar3[ch] = bkgfitResult->Parameter(3);
  628. sprintf(bname, "bkg_ch%02d_graph", ch);
  629. g1->SetTitle(bname);
  630. g1->SetName(bname);
  631. //fitResult->Print("V");
  632. // bkgfitResult->Write();
  633. g1->Write();
  634. } //end for (int ch = ...
  635. for (int ch = 0; ch < 64; ch++)
  636. {
  637. counter = 0;
  638. fitfunc->SetParameters( bkgfitPar0[ch], bkgfitPar1[ch], bkgfitPar2[ch], bkgfitPar3[ch]);
  639. for (Long64_t i = startevent ; i <startevent+ numbaseline; i++)
  640. {
  641. fChain->GetEvent(i);
  642. channelamp[counter] = channels[ch]- baseline[ch]- fitfunc->Eval(time-basetimeoffsetzero);
  643. th1d_chbaselinesub->Fill(channelamp[counter]);
  644. counter++;
  645. th2d_bkg_channel->Fill(ch,channelamp[counter]);
  646. }
  647. }
  648. Double_t channelampsum[numbaseline] = {0.};
  649. counter = 0;
  650. ///fit a 50Hz background noise to the sum of all channels
  651. for (Long64_t i = startevent ; i <startevent+ numbaseline; i++)
  652. {
  653. fChain->GetEvent(i);
  654. channelampsum[counter]=0.;
  655. for (int ch = 0; ch < 64; ch++){
  656. if (channels[ch]>-100 && baseline[ch]>-100) channelampsum[counter] += channels[ch] - baseline[ch]; //integrate the signal
  657. }
  658. basetime[counter] = (time-basetimeoffsetzero);
  659. counter++;
  660. }
  661. TGraph * g2 = new TGraph(counter,basetime,channelampsum);
  662. TF1 * fitfunc2 = new TF1("fitfunc2","[0]+[1]*TMath::Cos(x*[2]*(2*3.14159)+[3])");
  663. fitfunc2->SetParameters( 5120.0, 450.0, 0.05, 0.0 );
  664. // fitfunc2->SetParLimits(1,0.,1000.);
  665. fitfunc2->FixParameter(2,0.05);
  666. TFitResultPtr sumbkgfitResult = g2->Fit(fitfunc2,"QRS","", basetimeoffsetstart, basetimeoffset);
  667. g2->SetName("commonmodenoise"); g2->SetTitle("commonmodenoise");
  668. g2->Write();
  669. sumbkgfitResult->Write();
  670. fitfunc2->Write();
  671. bkgfitPar0[64] = fitfunc2->GetParameter(0);
  672. bkgfitPar1[64] = fitfunc2->GetParameter(1);
  673. bkgfitPar2[64] = fitfunc2->GetParameter(2);
  674. bkgfitPar3[64] = fitfunc2->GetParameter(3);
  675. counter=0;
  676. for (Long64_t i = startevent ; i <startevent+ numbaseline; i++)
  677. {
  678. fChain->GetEvent(i);
  679. channelampsum[counter] -= fitfunc2->Eval(time-basetimeoffsetzero);
  680. th1d_baselinesub->Fill(channelampsum[counter]);
  681. counter++;
  682. }
  683. th1d_baseline->Write();
  684. th1d_baselinesub->Write();
  685. th1d_chbaseline->Write();
  686. th1d_chbaselinesub->Write();
  687. TGraph * g3 = new TGraph(counter,basetime,channelampsum);
  688. g3->SetName("commonmodenoisesub"); g2->SetTitle("commonmodenoisesub");
  689. g3->Write();
  690. }
  691. }
  692. void analyse2::Save()
  693. {
  694. //fileIn->Close();
  695. //fileOut->cd();
  696. newdata->AutoSave();
  697. }
  698. void analyse2::Close()
  699. {
  700. fileIn->Close();
  701. fileOut->Close();
  702. if(fChain) delete fChain->GetCurrentFile();
  703. }
  704. #define REAL(z,i) ((z)[2*(i)])
  705. #define IMAG(z,i) ((z)[2*(i)+1])
  706. double * analyse2::FFTsmoothing(double arr[])
  707. {
  708. int i; double data[2*64];
  709. double channel[64];
  710. double RealInput[64];
  711. double ImagInput[64];
  712. double RealFFT[64];
  713. double ImagFFT[64];
  714. double AmpFFT[64];
  715. double PhaseFFT[64];
  716. double RealOutput[64];
  717. double ImagOutput[64];
  718. double avgnoiseAmpFFT = 0.;
  719. for (i = 0; i < 64; i++)
  720. {
  721. REAL(data,i) = arr[i]; IMAG(data,i) = 0.0;
  722. }
  723. // REAL(data,0) = 1.0;
  724. // for (i = 1; i <= 10; i++)
  725. // {
  726. // REAL(data,i) 1.0;
  727. // }
  728. for (i = 0; i < 64; i++)
  729. {
  730. channel[i] = i;
  731. RealInput[i] = REAL(data,i);
  732. ImagInput[i] = IMAG(data,i);
  733. // printf ("%d %e %e\n", i, REAL(data,i), IMAG(data,i));
  734. }
  735. // printf ("\n");
  736. // TGraph * g1_input_real = new TGraph(64,channel,RealInput);
  737. // TGraph * g1_input_imag = new TGraph(64,channel,ImagInput);
  738. // g1_input_real->SetName("input_real"); g1_input_real->Write();
  739. //g1_input_imag->SetName("input_imag"); g1_input_imag->Write();
  740. //apply Fourier transform
  741. gsl_fft_complex_radix2_forward (data, 1, 64);
  742. for (i = 0; i < 64; i++)
  743. {
  744. // printf ("%d %e %e\n", i, REAL(data,i)/sqrt(64), IMAG(data,i)/sqrt(64));
  745. RealFFT[i] = REAL(data,i)/sqrt(64);
  746. ImagFFT[i] = IMAG(data,i)/sqrt(64);
  747. AmpFFT[i] = sqrt(RealFFT[i]*RealFFT[i] + ImagFFT[i]*ImagFFT[i]);
  748. PhaseFFT[i] = atan(ImagFFT[i]/RealFFT[i]);
  749. if (beamon){
  750. th2d_AmpFFT_channel->Fill(i,AmpFFT[i]);
  751. th2d_PhaseFFT_channel->Fill(i,PhaseFFT[i]);
  752. }
  753. }
  754. // printf ("\n");
  755. //find the average noise frequency amplitude
  756. for (i = 22; i < 32; i++){
  757. avgnoiseAmpFFT +=AmpFFT[i]/10;
  758. }
  759. //find where the signal frequency amplitude falls below noise;
  760. int cutchannel = 31;
  761. for (i = 0; i < 32; i++)
  762. {
  763. if (AmpFFT[i]<avgnoiseAmpFFT) {
  764. cutchannel = i;
  765. break;
  766. }
  767. }
  768. // TGraph * g1_FFT_real = new TGraph(64,channel,RealFFT);
  769. // TGraph * g1_FFT_imag = new TGraph(64,channel,ImagFFT);
  770. //g1_FFT_real->SetName("FFT_real"); g1_FFT_real->Write();
  771. // g1_FFT_imag->SetName("FFT_imag");g1_FFT_imag->Write();
  772. // TGraph * g1_FFT_amp = new TGraph(64,channel,AmpFFT);
  773. // TGraph * g1_FFT_phase = new TGraph(64,channel,PhaseFFT);
  774. // g1_FFT_amp->SetName("FFT_amp"); g1_FFT_amp->Write();
  775. // g1_FFT_phase->SetName("FFT_phase");g1_FFT_phase->Write();
  776. //suppress channels that are more likely noise; the signal has a lower frequency than noise;
  777. for (i = cutchannel+2; i < 32; i++)
  778. {
  779. REAL(data,i) = 0.;
  780. IMAG(data,i) = 0.;
  781. REAL(data,64-i) = 0.;
  782. IMAG(data,64-i) = 0.;
  783. }
  784. //apply inverse Fourier transform
  785. gsl_fft_complex_radix2_inverse (data, 1, 64);
  786. for (i = 0; i < 64; i++)
  787. {
  788. // printf ("%d %e %e\n", i, REAL(data,i), IMAG(data,i));
  789. RealOutput[i] = REAL(data,i);
  790. ImagOutput[i] = IMAG(data,i);
  791. arr[i] = RealOutput[i];
  792. }
  793. //printf ("\n");
  794. // TGraph * g1_output_real = new TGraph(64,channel,RealOutput);
  795. // TGraph * g1_output_imag = new TGraph(64,channel,ImagOutput);
  796. // g1_output_real->SetName("output_real");g1_output_real->Write();
  797. // g1_output_imag->SetName("output_imag");g1_output_imag->Write();
  798. return arr;
  799. }
  800. #endif // #ifdef analyse2_cxx