PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/methodComp.cpp

https://gitlab.com/weinshec/tes_performance
C++ | 144 lines | 103 code | 26 blank | 15 comment | 5 complexity | ba85053f88d670a8bbda34cc975ae3d0 MD5 | raw file
  1. /**
  2. * methodComp.cpp - Compare PHPI method with the peak fitting method.
  3. *
  4. * author: Christoph Weinsheimer
  5. * email: christoph.weinsheimer@desy.de
  6. */
  7. #include <iostream>
  8. #include <fast/utils.h>
  9. #include <fast/processor.h>
  10. #include <fast/pulsefct.h>
  11. #include "TFile.h"
  12. #include "TMath.h"
  13. #include "TTree.h"
  14. #include "version.h"
  15. using namespace fast;
  16. using ConfigParser = fast::utils::ConfigParser;
  17. int main(int argc, char* argv[]) {
  18. common::printInfo(argc, argv);
  19. utils::printLibVersion();
  20. utils::Logger::instance()->setLevel(utils::LogLevel::INFO);
  21. if (argc != 4) {
  22. std::cout << "Syntax: methodComp <input> <config> <output>" << std::endl;
  23. return 1;
  24. }
  25. // Parse configuration
  26. auto config = ConfigParser::parseJson(argv[2]);
  27. const Channel ch = Channel::ChA;
  28. // Read input file
  29. TFile infile(argv[1], "READ");
  30. std::shared_ptr<TTree> inTree((TTree*) infile.Get("alazar_data"));
  31. // Create output file
  32. TFile outfile(argv[3], "RECREATE");
  33. auto outTree = std::make_shared<TTree>("processed_data", "processed_data");
  34. auto plotDir = outfile.mkdir("plots");
  35. plotDir->cd();
  36. // Create peakfinding unit
  37. std::shared_ptr<PeakFindingUnit> pfu = nullptr;
  38. double threshold = config["threshold"]["value"];
  39. auto noise_ref = ConfigParser::parseJson(
  40. utils::expandEnv(config["threshold"]["reference"]));
  41. std::stringstream param_key;
  42. param_key << std::fixed << std::setprecision(2);
  43. switch (config["method"].get<unsigned int>()) {
  44. case PeakFindingUnit::DERIV:
  45. param_key << config["params"]["cutOff"].get<float>();
  46. threshold *= noise_ref["deriv"][param_key.str()]["sigma"].get<double>();
  47. pfu = std::make_shared<DerivProcessor>(
  48. "L2", ch, config["params"]["cutOff"], threshold
  49. );
  50. break;
  51. case PeakFindingUnit::XCORR:
  52. param_key << config["params"]["tMinFactor"].get<float>();
  53. threshold *= noise_ref["xcorr"][param_key.str()]["sigma"].get<double>();
  54. pfu = std::make_shared<XCorrProcessor>(
  55. "L2", ch, PulseShape::PHOTON_55mV(), config["params"]["tMinFactor"],
  56. threshold
  57. );
  58. break;
  59. }
  60. pfu->setTree(outTree);
  61. // Create Processors
  62. PulseFitProcessor pu_fit("fit", ch, 0.005, pfu);
  63. pu_fit.setTree(outTree);
  64. PHPIProcessor pu_phpi("phpi", ch, true);
  65. pu_phpi.setTree(outTree);
  66. // Create TreeRecord for truth record part
  67. struct TruthRecord : public TreeRecord {
  68. double truth_amplitude;
  69. double truth_tRise;
  70. double truth_tFall;
  71. double truth_tOffset;
  72. BranchMap branchmap{
  73. {"truth_amplitude_0", &truth_amplitude},
  74. {"truth_tRise_0", &truth_tRise},
  75. {"truth_tFall_0", &truth_tFall},
  76. {"truth_tOffset_0", &truth_tOffset},
  77. };
  78. };
  79. // Create TreeReader
  80. TreeReader<TruthRecord> truthReader(inTree);
  81. TreeReader<AlazarRecord> treeReader(inTree);
  82. // Create additional branches
  83. double fit_integral;
  84. double fit_dAmplitude, fit_dIntegral, phpi_dAmplitude, phpi_dIntegral;
  85. outTree->Branch("fit_integral", &fit_integral);
  86. outTree->Branch("fit_dAmplitude", &fit_dAmplitude);
  87. outTree->Branch("fit_dIntegral", &fit_dIntegral);
  88. outTree->Branch("phpi_dAmplitude", &phpi_dAmplitude);
  89. outTree->Branch("phpi_dIntegral", &phpi_dIntegral);
  90. // Event loop
  91. for (size_t i = 0; i < treeReader.size(); ++i) {
  92. auto event = treeReader.getEntry(i);
  93. auto truth = truthReader.getEntry(i);
  94. auto fitRecord = pu_fit(event);
  95. auto phpiRecord = pu_phpi(event);
  96. fit_dAmplitude = (fitRecord.amplitude[0] - truth.truth_amplitude)
  97. / truth.truth_amplitude;
  98. phpi_dAmplitude = (phpiRecord.ph - truth.truth_amplitude)
  99. / truth.truth_amplitude;
  100. auto truth_integral = pulsefct::pulse_integral(truth.truth_amplitude,
  101. truth.truth_tRise,
  102. truth.truth_tFall);
  103. fit_integral = pulsefct::pulse_integral(fitRecord.amplitude[0],
  104. fitRecord.tRise[0],
  105. fitRecord.tFall[0]);
  106. fit_dIntegral = (fit_integral - truth_integral) / truth_integral;
  107. phpi_dIntegral = (phpiRecord.pi - truth_integral) / truth_integral;
  108. if (!fitRecord.fitResult->IsValid()) {
  109. utils::LogWarning("methodComp")
  110. << "Fit failed! Event " + std::to_string(i);
  111. fitRecord.graph->Write(utils::add_suffix("failed", i).c_str());
  112. }
  113. outTree->Fill();
  114. }
  115. outfile.cd();
  116. outTree->Write();
  117. return 0;
  118. }