ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtTPCXSManager.cxx
Go to the documentation of this file.
1 #include "AtTPCXSManager.h"
2 
3 #include <TH2.h>
4 
5 #include <fstream> // IWYU pragma: keep
6 #include <iostream>
7 #include <sstream> // IWYU pragma: keep
8 
9 // Allow us use std::make_unique using a protected constructor this struct
10 // is only defined in this translation unit (.cpp file)
11 namespace {
12 struct concrete_AtTPCXSManager : public AtTPCXSManager {
13 };
14 } // namespace
15 
16 std::unique_ptr<AtTPCXSManager> AtTPCXSManager::fInstance = nullptr;
17 
19 {
20  if (fInstance == nullptr)
21  fInstance = std::make_unique<concrete_AtTPCXSManager>();
22  return fInstance.get();
23 }
24 
25 Bool_t AtTPCXSManager::SetExcitationFunction(std::string filename)
26 {
27  fExFunctionFile = filename;
28  std::ifstream file;
29  file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
30 
31  try {
32  file.open(fExFunctionFile);
33 
34  Int_t nLines = 0;
35  Int_t nPoints = 0;
36 
37  Float_t Ebinsize = 0;
38  Float_t Abinsize = 0;
39  Float_t ERangeUp = 0;
40  Float_t ARangeUp = 0;
41  Float_t ERangeDown = 0;
42  Float_t ARangeDown = 0;
43  Float_t XSTot = 0;
44 
45  // read a single line
46  std::string line;
47  std::getline(file, line);
48  std::cout << line << "\n";
49  std::getline(file, line);
50  std::istringstream isheader(line);
51  isheader >> Ebinsize >> Abinsize >> ERangeUp >> ERangeDown >> ARangeUp >> ARangeDown >> XSTot;
52  std::cout << " Energy bin size : " << Ebinsize << " - Angular bin size : " << Abinsize
53  << " - Energy range Up : " << ERangeUp << " - Energy range down :" << ERangeDown
54  << " - Angular range Up : " << ARangeUp << " - Angular range Down : " << ARangeDown
55  << " - Total cross section : " << XSTot << "\n";
56 
57  auto nEbins = static_cast<Int_t>(((ERangeUp + 0.000001) - ERangeDown) / Ebinsize);
58  auto nAbins = static_cast<Int_t>(((ARangeUp + 0.000001) - ARangeDown) / Abinsize);
59 
60  std::cout << " Number of energy - angle bins : " << nEbins << " - " << nAbins << "\n";
61 
62  fExFunction = std::make_shared<TH2F>("fExFunction", "fExFunction", nEbins + 1, ERangeDown - Ebinsize / 2.0,
63  ERangeUp + Ebinsize / 2.0, nAbins + 1, ARangeDown - Abinsize / 2.0,
64  ARangeUp + Abinsize / 2.0);
65 
66  while (!file.eof()) {
67 
68  std::getline(file, line);
69 
70  Float_t ecm, acm, xs, ph; // Energy, cross section, placeholder
71 
72  // read with default seperator (space) seperated elements
73  std::istringstream isxs(line);
74 
75  isxs >> ecm >> xs >> acm >> ph;
76 
77  std::cout << " Energy cm (MeV) : " << ecm << " - Cross section (mb/sr or mb) : " << xs
78  << " - Angle cm (deg) : " << acm << " - Place holder : " << ph << "\n";
79 
80  Int_t bin = fExFunction->Fill(ecm, acm, xs);
81  std::cout << " Bin fill " << bin << "\n";
82 
83  ++nPoints;
84  }
85 
86  file.close();
87 
88  std::cout << "Successfully read " << nPoints << " points in " << nLines << " lines!" << std::endl;
89  kIsExFunction = kTRUE;
90  return true;
91  } catch (...) {
92  // std::cout <<cGREEN<< "Something bad happened while reading "<<mapPath<<"!" <<cNORMAL<< std::endl;
93  }
94 
95  return false;
96 }
97 
ClassImp
ClassImp(AtFindVertex)
AtTPCXSManager.h
AtTPCXSManager::Instance
static AtTPCXSManager * Instance()
Definition: AtTPCXSManager.cxx:18
AtTPCXSManager
Definition: AtTPCXSManager.h:20
AtTPCXSManager::SetExcitationFunction
bool SetExcitationFunction(std::string filename)
Definition: AtTPCXSManager.cxx:25