From 204c342e1c0aa4dd8c89446e924cf60f18cf3826 Mon Sep 17 00:00:00 2001
From: Adrien Matta <matta@lpccaen.in2p3.fr>
Date: Wed, 5 May 2021 16:17:36 +0200
Subject: [PATCH] * Adding Project base configuration

---
 NPLib/Core/NPInputParser.cxx         | 17 +++++------
 NPLib/Core/NPInputParser.h           |  2 ++
 NPLib/Core/NPOptionManager.cxx       | 43 ++++++++++++++++++++++++++--
 NPLib/Core/NPOptionManager.h         | 13 +++++++--
 NPLib/Core/RootOutput.cxx            | 21 ++++++++++++--
 NPLib/Physics/NPEnergyLoss.cxx       |  4 +--
 NPSimulation/Core/MaterialManager.cc |  8 ++++--
 7 files changed, 88 insertions(+), 20 deletions(-)

diff --git a/NPLib/Core/NPInputParser.cxx b/NPLib/Core/NPInputParser.cxx
index 7962e3e80..0acf99a0d 100644
--- a/NPLib/Core/NPInputParser.cxx
+++ b/NPLib/Core/NPInputParser.cxx
@@ -86,6 +86,7 @@ std::string NPL::InputBlock::ExtractValue(std::string line,std::string separator
 void NPL::InputBlock::AddLine(std::string line){
   m_Token.push_back(ToLower(StripSpaces(ExtractToken(line))));
   m_Value.push_back(StripSpaces(ExtractValue(line)));
+  m_Lines.push_back(line); 
 }
 ////////////////////////////////////////////////////////////////////////////////
 void NPL::InputBlock::Dump(){
@@ -128,7 +129,7 @@ std::string NPL::InputBlock::GetValue(std::string Token){
 }
 ////////////////////////////////////////////////////////////////////////////////
 double NPL::InputBlock::GetDouble(std::string Token,std::string default_unit){
-  int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+  int verbose = 1; //NPOptionManager::getInstance()->GetVerboseLevel();
   std::stringstream iss(GetValue(Token));
   double val;
   std::string unit;
@@ -150,7 +151,7 @@ double NPL::InputBlock::GetDouble(std::string Token,std::string default_unit){
 }
 ////////////////////////////////////////////////////////////////////////////////
 int NPL::InputBlock::GetInt(std::string Token){
-  int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+  int verbose = 1;//NPOptionManager::getInstance()->GetVerboseLevel();
   std::stringstream iss(GetValue(Token));
   int val;
   iss >> val ;
@@ -164,7 +165,7 @@ int NPL::InputBlock::GetInt(std::string Token){
 }
 ////////////////////////////////////////////////////////////////////////////////
 std::string NPL::InputBlock::GetString(std::string Token){
-  int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+  int verbose = 1;//NPOptionManager::getInstance()->GetVerboseLevel();
   if(verbose)
     printf(" %s: %s\n",Token.c_str(),GetValue(Token).c_str());
 //    std::cout << " " << Token << ": " << GetValue(Token) << std::endl; 
@@ -173,7 +174,7 @@ std::string NPL::InputBlock::GetString(std::string Token){
 }
 ////////////////////////////////////////////////////////////////////////////////
 TVector3 NPL::InputBlock::GetTVector3(std::string Token,std::string  default_unit){
-  int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+  int verbose = 1; //NPOptionManager::getInstance()->GetVerboseLevel();
   std::stringstream iss(GetValue(Token));
 
   double x,y,z;
@@ -205,7 +206,7 @@ TVector3 NPL::InputBlock::GetTVector3(std::string Token,std::string  default_uni
 }
 ////////////////////////////////////////////////////////////////////////////////
 std::vector<std::string> NPL::InputBlock::GetVectorString(std::string Token){
-  int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+  int verbose = 1 ; //NPOptionManager::getInstance()->GetVerboseLevel();
 
   std::stringstream iss(GetValue(Token));
 
@@ -231,7 +232,7 @@ std::vector<std::string> NPL::InputBlock::GetVectorString(std::string Token){
 
 ////////////////////////////////////////////////////////////////////////////////
 std::vector<double> NPL::InputBlock::GetVectorDouble(std::string Token,std::string  default_unit){
-  int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+  int verbose = 1 ; // NPOptionManager::getInstance()->GetVerboseLevel();
 
   std::stringstream iss(GetValue(Token));
 
@@ -271,7 +272,7 @@ std::vector<double> NPL::InputBlock::GetVectorDouble(std::string Token,std::stri
 
 ////////////////////////////////////////////////////////////////////////////////
 std::vector<int> NPL::InputBlock::GetVectorInt(std::string Token){
-  int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+  int verbose = 1;// NPOptionManager::getInstance()->GetVerboseLevel();
 
   std::stringstream iss(GetValue(Token));
   std::vector<int> val;
@@ -375,7 +376,7 @@ void NPL::InputParser::TreatAliases(){
       NPL::SendErrorAndExit("NPL::InputParser", "Alias block syntax incorrect");
     }
     
-    int verbose = NPOptionManager::getInstance()->GetVerboseLevel();
+    int verbose = 1; //NPOptionManager::getInstance()->GetVerboseLevel();
 
     if(verbose)
       std::cout << "Using Alias : @" << alias[i]->GetMainValue() << std::endl;
diff --git a/NPLib/Core/NPInputParser.h b/NPLib/Core/NPInputParser.h
index 0791fe5c7..2c58e7d48 100644
--- a/NPLib/Core/NPInputParser.h
+++ b/NPLib/Core/NPInputParser.h
@@ -53,6 +53,7 @@ namespace NPL{
       std::string m_MainValue;
       std::vector<std::string> m_Token;
       std::vector<std::string> m_Value; 
+      std::vector<std::string> m_Lines; 
 
     public:
       void AddLine(std::string line);  
@@ -83,6 +84,7 @@ namespace NPL{
       std::vector<int> GetVectorInt(std::string Token);
       std::vector<std::string> GetVectorString(std::string Token);
       std::vector<NPL::InputBlock*> GetSubBlock(std::string Token);
+      std::vector<std::string> GetLines(){return m_Lines;};
 
     public:
       void Dump();
diff --git a/NPLib/Core/NPOptionManager.cxx b/NPLib/Core/NPOptionManager.cxx
index df51eca26..eb1cdef77 100644
--- a/NPLib/Core/NPOptionManager.cxx
+++ b/NPLib/Core/NPOptionManager.cxx
@@ -22,6 +22,7 @@
 
 #include "NPOptionManager.h"
 #include "NPLibVersion.h"
+#include "NPInputParser.h"
 #include <fstream>
 #include <sstream>
 #include <cstdlib>
@@ -44,15 +45,51 @@ NPOptionManager* NPOptionManager::getInstance(std::string arg){
   return instance ;
 
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void NPOptionManager::ReadProjectConfigFile(){
+ // check if the file exist
+ std::ifstream ProjectFile;
+ ProjectFile.open("./project.config");
+
+ if(ProjectFile.is_open()){
+    std::cout << "///// Loading Project Configuration: " << std::endl;
+    ProjectFile.close();
+    NPL::InputParser parser("./project.config");
+    std::vector<NPL::InputBlock*> blocks = parser.GetAllBlocksWithToken("Project");
+    unsigned int size = blocks.size();
+    for(unsigned int i = 0 ; i < size ; i++){
+
+      if(blocks[i]->HasToken("AnalysisOutput"))
+        m_AnalysisOutputPath = blocks[i]->GetString("AnalysisOutput"); 
+
+      if(blocks[i]->HasToken("SimulationOutput"))
+        m_SimulationOutputPath = blocks[i]->GetString("SimulationOutput"); 
+      
+      if(blocks[i]->HasToken("EnergyLoss"))
+        m_EnergyLossPath = blocks[i]->GetString("EnergyLoss"); 
+    }
+ } 
+
+
+ // else use the standard config
+ else{
+  std::string Path = getenv("NPTOOL");
+  m_AnalysisOutputPath=Path+"/Outputs/Analysis/";
+  m_SimulationOutputPath=Path+"/Outputs/Simulation/";
+  m_EnergyLossPath=Path+"/Inputs/EnergyLoss/";
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
 void NPOptionManager::ReadTheInputArgument(int argc, char** argv){
   if(argc==1)
     DisplayHelp();
 
-
   // Default Setting
   fDefaultReactionFileName    = "defaultReaction.reaction";
   fDefaultDetectorFileName    = "defaultDetector.detector";
-  fDefaultOutputFileName      = "myResult.root";
+  fDefaultOutputFileName      = "SimulatedTree.root";
   fDefaultOutputTreeName      = "NPTool_Tree";
   fDefaultRunToReadFileName   = "defaultRunToTreat.txt";
   fDefaultCalibrationFileName = "defaultCalibration.txt";
@@ -211,6 +248,8 @@ void NPOptionManager::DisplayVersion(){
 }
 ////////////////////////////////////////////////////////////////////////////////
 NPOptionManager::NPOptionManager(int argc, char** argv){
+  // Start by reading the project configuration
+  ReadProjectConfigFile();
   ReadTheInputArgument(argc,argv);
 }
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/NPLib/Core/NPOptionManager.h b/NPLib/Core/NPOptionManager.h
index a628d8fe0..a793188b7 100644
--- a/NPLib/Core/NPOptionManager.h
+++ b/NPLib/Core/NPOptionManager.h
@@ -60,7 +60,9 @@ class NPOptionManager{
   private:
     // Read the input argument
     void ReadTheInputArgument(int argc = 0, char** argv = NULL);
-      
+    // Look for and Read the project config file if existing
+    void ReadProjectConfigFile();
+
    private:
       // The static instance of the NPOptionManager class:
       static NPOptionManager* instance;
@@ -111,13 +113,16 @@ class NPOptionManager{
       int    GetRandomSeed()               {return fRandomSeed;}
       std::string GetSharedLibExtension()       {return fSharedLibExtension;}     
       std::string GetLastFile();                 
-      
+      std::string GetAnalysisOutputPath(){return m_AnalysisOutputPath;};
+      std::string GetSimulationOutputPath(){return m_SimulationOutputPath;};
+      std::string GetEnergyLossPath(){return m_EnergyLossPath;};
       // Setters
       void SetReactionFile(const std::string& name)  {fReactionFileName = name;CheckEventGenerator();}
       void SetDetectorFile(const std::string& name)  {fDetectorFileName = name;CheckDetectorConfiguration();}
       void SetRunToReadFile(const std::string& name) {fRunToReadFileName = name;}
       void SetVerboseLevel(int VerboseLevel)         {fVerboseLevel = VerboseLevel;}
  
+
    public: // user definition
       bool HasDefinition(std::string def) {return(fDefinition.find(def)!=fDefinition.end());}
 
@@ -158,6 +163,10 @@ class NPOptionManager{
       std::string fG4MacroPath; // Path to a geant4 macro to execute at start of nps
       bool fG4BatchMode; // Execute geant4 in batch mode, running the given macro
       std::set<std::string> fDefinition; // a set of user defined definition 
+      std::string m_AnalysisOutputPath;// output path of analysed tree
+      std::string m_SimulationOutputPath;// output path of simulated tree
+      std::string m_EnergyLossPath;// input/output path of energy loss table
+
 };
 
 #endif
diff --git a/NPLib/Core/RootOutput.cxx b/NPLib/Core/RootOutput.cxx
index a733a01dd..80400e84f 100644
--- a/NPLib/Core/RootOutput.cxx
+++ b/NPLib/Core/RootOutput.cxx
@@ -53,12 +53,27 @@ void RootOutput::Destroy(){
 RootOutput::RootOutput(std::string fileNameBase, std::string treeNameBase){
   TDirectory* currentPath= gDirectory;
 
-  // The file extension is added to the file name:
-  std::string GlobalPath = getenv("NPTOOL");
+  bool analysis=false;
+  bool simulation=false;
+  if(fileNameBase.find("Analysis/")!=std::string::npos){
+    analysis = true;
+    fileNameBase.erase(0,8);
+  }
+  else if(fileNameBase.find("Simulation/")!=std::string::npos){
+    simulation= true;
+    fileNameBase.erase(0,10);
+  }
 
   // The ROOT file is created
   if(!NPOptionManager::getInstance()->GetPROOF()){
-    std::string fileName = GlobalPath + "/Outputs/";
+    std::string fileName;
+    if(analysis)
+      fileName = NPOptionManager::getInstance()->GetAnalysisOutputPath();
+    else if(simulation)
+      fileName = NPOptionManager::getInstance()->GetSimulationOutputPath();
+    else
+      fileName="./";
+
     if (fileNameBase.find("root")!=std::string::npos) fileName += fileNameBase;
     else fileName += fileNameBase + ".root";
 
diff --git a/NPLib/Physics/NPEnergyLoss.cxx b/NPLib/Physics/NPEnergyLoss.cxx
index 123665cc8..3039c2ae4 100644
--- a/NPLib/Physics/NPEnergyLoss.cxx
+++ b/NPLib/Physics/NPEnergyLoss.cxx
@@ -39,6 +39,7 @@
 using namespace std;
 
 #include "NPEnergyLoss.h"
+#include "NPOptionManager.h"
 #include "TAxis.h"
 
 //   NPL
@@ -66,8 +67,7 @@ EnergyLoss::EnergyLoss(string Path , string Source, int NumberOfSlice=100 ,  int
   fNumberOfSlice = NumberOfSlice ; 
   fNumberOfMass  = NumberOfMass  ;
 
-  string globalPath = getenv("NPTOOL");
-  string StandardPath = globalPath + "/Inputs/EnergyLoss/" + Path;
+  string StandardPath = NPOptionManager::getInstance()->GetEnergyLossPath()+"/"+Path;
 
   cout << endl;
   cout << "/////////// Energy loss ///////////" << endl ;
diff --git a/NPSimulation/Core/MaterialManager.cc b/NPSimulation/Core/MaterialManager.cc
index c5e90a8aa..554581bf1 100644
--- a/NPSimulation/Core/MaterialManager.cc
+++ b/NPSimulation/Core/MaterialManager.cc
@@ -20,6 +20,8 @@
  * Comment:                                                                  *
  *                                                                           *
  *****************************************************************************/
+//NPL
+#include "NPOptionManager.h"
 
 // NPS
 #include "MaterialManager.hh"
@@ -1209,12 +1211,12 @@ void MaterialManager::WriteDEDXTable(G4ParticleDefinition* Particle,
     return;
   for (it = m_Material.begin(); it != m_Material.end(); it++) {
     //   Opening hte output file
-    G4String GlobalPath = getenv("NPTOOL");
+    G4String GlobalPath =NPOptionManager::getInstance()->GetEnergyLossPath();
     G4String Name       = it->second->GetName();
+
     // Remove NPS name
     Name.erase(0, 4);
-    G4String Path = GlobalPath + "/Inputs/EnergyLoss/"
-                    + Particle->GetParticleName() + "_" + Name + ".G4table";
+    G4String Path = GlobalPath +"/"+ Particle->GetParticleName() + "_" + Name + ".G4table";
 
     ofstream File;
     File.open(Path);
-- 
GitLab