Angular analysis of B+->K*+(K+pi0)mumu
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.

179 lines
6.0 KiB

  1. //Renata Kopecna
  2. #ifndef MVACLASS_HPP
  3. #define MVACLASS_HPP
  4. #include "GlobalFunctions.hh"
  5. ////////////////////////////////////
  6. ///
  7. /// MVA CONFIG-CLASS
  8. ///
  9. /// Use the txt files to keep an easy overview of the MVA variables
  10. ///
  11. ////////////////////////////////////
  12. using namespace std;
  13. //define MVA configurations:
  14. struct MVAconfiguration{
  15. //If SplitYears is false, the MVA training will process the the selected Run (1: 2011+2012 2: 2015+2016+2017+2018 or 12: 2011-2018)
  16. bool SplitYears = false;
  17. Int_t Run = 1;
  18. std::vector<Int_t> years = {2011};
  19. Int_t KShortDecaysInVelo = 1;
  20. bool SplitInQ2Range = SplitInQ2;
  21. bool UseLowQ2Range = true;
  22. bool gammaTM = false; //T: only true gammas, F: include random gamma
  23. string customTMbranch = "TMed";
  24. //to name the output files differently
  25. Int_t nConfiguration = 0;
  26. } MVAconfig;
  27. struct MVA_def{
  28. string ReaderName; //Name of the MVA reader
  29. string LaTeXName; //Displayed name in the plots
  30. string Unit; //Unit for the branch
  31. int NoBr; //Number of used branches for the reader
  32. string Formula; //formula: Reader = f(Branches)
  33. char DataType; //D: Double_t, F: Float_t, I: Int_t,...
  34. };
  35. class MVA_variables{
  36. private:
  37. int NoVariables;
  38. int NoBranches;
  39. public:
  40. vector<MVA_def> AllVariables;
  41. vector <string> AllBranches;
  42. MVA_variables(){
  43. NoVariables = 0;
  44. AllVariables.clear();
  45. } //empty constructor
  46. MVA_variables(std::string DL); //default constructor
  47. ~MVA_variables(); //destuctor
  48. void print();
  49. vector <string> GetAllReaderNames();
  50. int NumberOfVariables(){
  51. return NoVariables;
  52. }
  53. int NumberOfBranches(){
  54. return NoBranches;
  55. }
  56. vector <string> GetAllBranches(){
  57. return AllBranches;
  58. }
  59. };
  60. MVA_variables::MVA_variables(std::string DL){
  61. std:: ifstream file;
  62. std:: string line = "";
  63. MVA_def tmp;
  64. std::string StrTmp;
  65. std::string filename = thePath+"/TMVA_variables_" + TheDecay + DL + ".txt";
  66. file.open(filename); //open file with the MVA variables stored; different files for different decays!
  67. if (file.is_open()) cout << "[INFO]\t\tInput MVA variables are being read from file "<< filename << "." <<endl;
  68. else{
  69. cout << "[ERROR]\t\tInput MVA variables file " << filename << " failed to open." << endl;
  70. return;
  71. }
  72. /*
  73. else { //in case of using this in the CodeForTests folder
  74. filename = "../TMVA_variables_" + TheDecay + DL + ".txt";
  75. file.open(filename); //open file with the MVA variables stored; different files for different decays!
  76. if (file.is_open()) cout << "[INFO]\t\tInput MVA variables are being read from file "<< filename << "." <<endl;
  77. else{
  78. cout << "[ERROR]\t\tInput MVA variables file " << filename << " failed to open." << endl;
  79. return;
  80. }
  81. }
  82. */
  83. //list of variables suffix which get an '_DTF' appendix
  84. const int pp = 9;
  85. std::string P[pp] = {"_PX", "_PY", "_PZ", "_PT", "_PE", "_ETA", "_M", "_MERR", "_ID"};
  86. getline(file, line); //skipping first line with info about the file
  87. while(1){ //loop over lines until you find the end of the file
  88. getline(file, line);
  89. if (line == "###") break;
  90. if(file.eof()) break;
  91. std::istringstream istr(line); //save from file to the vector
  92. istr >> tmp.ReaderName;
  93. istr >> tmp.LaTeXName;
  94. istr >> tmp.Unit;
  95. if (tmp.Unit == "0") tmp.Unit = "";
  96. istr >> tmp.DataType;
  97. istr >> tmp.NoBr;
  98. istr >> tmp.Formula;
  99. for (int n = 0; n < tmp.NoBr; n++){
  100. istr >> StrTmp;
  101. AllBranches.push_back(StrTmp);
  102. }
  103. //DTF modifications:
  104. if(UseDTF){
  105. for(int p = 0; p < pp; p++){ //loop over suffix //ReaderNames
  106. size_t pos = tmp.ReaderName.find(P[p].c_str());
  107. if(pos!=std::string::npos){
  108. coutDebug("[DTF]\t\tReplacing ReaderName '" + tmp.ReaderName);
  109. }
  110. while(pos!=std::string::npos){
  111. tmp.ReaderName.replace(pos,P[p].length(),(P[p]+"_DTF").c_str());
  112. pos = tmp.ReaderName.find(P[p].c_str(), pos+1);
  113. if(pos==std::string::npos)coutDebug("' with '" + tmp.ReaderName + "'.");
  114. }
  115. }
  116. }
  117. AllVariables.push_back(tmp); //Now I assume user is not an idiot!
  118. };
  119. file.close();
  120. if(UseDTF){
  121. for(int p = 0; p < pp; p++){ //loop over suffix //BranchNames
  122. for (unsigned int n = 0; n < AllBranches.size(); n++){
  123. size_t pos = AllBranches.at(n).find(P[p].c_str());
  124. if(pos!=std::string::npos) coutDebug("[DTF]\t\tReplacing variable '" + AllBranches.at(n));
  125. while(pos!=std::string::npos){
  126. AllBranches.at(n).replace(pos,P[p].length(),(P[p]+"_DTF").c_str());
  127. pos = AllBranches.at(n).find(P[p].c_str(), pos+1);
  128. if(pos==std::string::npos)coutDebug("' with '" + AllBranches.at(n) + "'.");
  129. }
  130. }
  131. }
  132. }
  133. NoVariables = AllVariables.size();
  134. NoBranches = AllBranches.size();
  135. return;
  136. }
  137. MVA_variables::~MVA_variables(){
  138. AllVariables.empty();
  139. NoVariables = 0;
  140. }
  141. void MVA_variables::print(){
  142. coutInfo("Using " + to_string(NoVariables) + " variables.");
  143. coutInfo("BranchName \t\t LaTeXName \t\t Unit \t\t DataType");
  144. for (vector<MVA_def>::iterator tracksIter1 = AllVariables.begin(); tracksIter1 !=AllVariables.end();++tracksIter1){
  145. coutInfo((*tracksIter1).ReaderName + "\t\t" + (*tracksIter1).LaTeXName + "\t\t" + (*tracksIter1).Unit + "\t\t" + (*tracksIter1).DataType);
  146. }
  147. }
  148. vector <string> MVA_variables::GetAllReaderNames(){
  149. vector<string> tmp;
  150. for (vector<MVA_def>::iterator tracksIter1 = AllVariables.begin(); tracksIter1 !=AllVariables.end();++tracksIter1){
  151. tmp.push_back((*tracksIter1).ReaderName);
  152. }
  153. return tmp;
  154. }
  155. #endif // MVACLASS_HPP