From a22d4c8a2f17c4362f9b66cacc4ea45835719752 Mon Sep 17 00:00:00 2001
From: adrien matta <matta@lpccaen.in2p3.fr>
Date: Tue, 17 Jul 2018 10:21:31 +0200
Subject: [PATCH] * Progress on Online

---
 NPLib/CanvasList.txt                 |  9 +++++
 NPLib/Core/NPCore.cxx                | 21 ++++++++++
 NPLib/Core/NPCore.h                  |  1 +
 NPLib/Core/NPSpectraServer.cxx       | 40 +++++++++----------
 NPLib/Core/NPSpectraServer.h         |  6 +--
 NPLib/Core/NPVDetector.cxx           | 17 --------
 NPLib/Core/NPVDetector.h             |  2 +-
 NPLib/Online/NPOnlineGUI.cxx         | 58 ++++++++++++++++++++++------
 NPLib/Online/NPOnlineGUI.h           |  8 ++--
 NPLib/Online/NPSpectraClient.cxx     | 48 ++++++++++++++---------
 NPLib/Online/NPSpectraClient.h       | 10 ++---
 NPLib/Utility/CMakeLists.txt         |  3 ++
 NPSimulation/Core/EventAction.cc     |  2 +-
 NPSimulation/Core/MaterialManager.cc |  4 +-
 NPSimulation/Core/RunAction.cc       |  3 +-
 NPSimulation/Process/BeamReaction.cc |  6 +++
 NPSimulation/Process/BeamReaction.hh |  1 +
 Projects/MUGAST/Analysis.cxx         |  8 ++--
 Projects/MUGAST/ShowResults.C        | 51 ++++++++++++++++--------
 19 files changed, 189 insertions(+), 109 deletions(-)

diff --git a/NPLib/CanvasList.txt b/NPLib/CanvasList.txt
index 69f992086..144fbf067 100644
--- a/NPLib/CanvasList.txt
+++ b/NPLib/CanvasList.txt
@@ -13,4 +13,13 @@ Canvas
  Histo= MM@Telescope_STRX_RAW_MULT MM@Telescope_STRY_RAW_MULT
  
 
+Canvas
+  Path= test
+  Divide= 2 2 
+  Histo= h1 h2
+
+Canvas
+  Path= pipo
+  Divide= 2 2 
+  Histo= h2 h1
 
diff --git a/NPLib/Core/NPCore.cxx b/NPLib/Core/NPCore.cxx
index 082c0794b..04fda36c4 100644
--- a/NPLib/Core/NPCore.cxx
+++ b/NPLib/Core/NPCore.cxx
@@ -21,6 +21,7 @@
  *****************************************************************************/
 #include "NPCore.h"
 #include<iostream>
+#include<string>
 ////////////////////////////////////////////////////////////////////////////////
 void NPL::SendWarning(std::string Class, std::string Warning){
   std::cerr << "\033[5;34m";
@@ -44,3 +45,23 @@ void NPL::SendErrorAndExit(std::string Class , std::string Error){
   std::cerr << std::endl;
   exit(1); 
 }
+////////////////////////////////////////////////////////////////////////////////
+namespace NPL{
+  static std::string itoa_array[10000];
+
+  class itoa_proxy{
+    public:
+      itoa_proxy(){
+      char buffer[]="10000";
+        for(int i = 0 ; i < 10000 ; i++){
+          sprintf(buffer,"%d",i);
+          itoa_array[i] = buffer;
+        }
+      }
+  };
+  static itoa_proxy itoa_p ;
+}
+
+std::string NPL::itoa(const int& i){
+  return NPL::itoa_array[i];
+}
diff --git a/NPLib/Core/NPCore.h b/NPLib/Core/NPCore.h
index 181dd18d0..9f14ffadd 100644
--- a/NPLib/Core/NPCore.h
+++ b/NPLib/Core/NPCore.h
@@ -24,6 +24,7 @@
 
 #include<string>
 namespace NPL{
+  std::string itoa(const int&); 
   void SendWarning(std::string Class, std::string Warning);
   void SendInformation(std::string Class, std::string Information);
   void SendErrorAndExit(std::string Class,std::string Error);
diff --git a/NPLib/Core/NPSpectraServer.cxx b/NPLib/Core/NPSpectraServer.cxx
index ce23eff8e..940c81391 100644
--- a/NPLib/Core/NPSpectraServer.cxx
+++ b/NPLib/Core/NPSpectraServer.cxx
@@ -66,7 +66,7 @@ void NPL::SpectraServer::CheckRequest(){
   if(m_Server && m_Monitor){
     TSocket* s ;
     m_Monitor->ResetInterrupt();
-    if((s=m_Monitor->Select(100))!=(TSocket*)-1){
+    if((s=m_Monitor->Select(1))!=(TSocket*)-1){
       HandleSocket(s);
       }
   }
@@ -103,47 +103,43 @@ void NPL::SpectraServer::HandleSocket(TSocket* s){
 
     // send requested object back
     static TMessage answer(kMESS_OBJECT);
+    answer.SetCompressionLevel(1);
     answer.Reset();
-    
+    TObject* h =NULL;
    if (!strcmp(request, "RequestSpectra")){
+     std::cout << "Prepare" << std::endl;
       answer.WriteObject(m_Spectra);
+      std::cout << "Compress" << std::endl;
+      answer.Compress();
+      std::cout << "Send " << std::endl;
       s->Send(answer);
-      m_Delta[s].Clear();
+      std::cout << "done" << std::endl;
     }
-    
-    else if (!strcmp(request, "RequestDelta")){
-      answer.WriteObject(&m_Delta[s]);
-      s->Send(answer);
-      m_Delta[s].Clear();
+  
+    else if (h=m_Spectra->FindObject(request)){
+     answer.WriteObject(h);
+     s->Send(answer);
     }
-    
+
     else if (!strcmp(request, "RequestClear")){
      // TO DO 
     }
-    else // answer nothing
+    else{ // answer nothing
+      std::cout << "Fail to respond to request : " << request << std::endl; 
       s->Send(answer);
+    }
   }
 }
 ////////////////////////////////////////////////////////////////////////////////
-void NPL::SpectraServer::FillSpectra(std::string name,double valx){
+void NPL::SpectraServer::FillSpectra(const std::string& name,const double& valx){
   // Fill the local histo
   ((TH1*) m_Spectra->FindObject(name.c_str()))->Fill(valx);
-  
-  // Fill the Delta for each Socket
-  std::map<TSocket*,NPL::DeltaSpectra >::iterator it;
-  for(it = m_Delta.begin(); it!=m_Delta.end() ; it++){
-    it->second.Fill(name,valx);
-  }
 }
 ////////////////////////////////////////////////////////////////////////////////
-void NPL::SpectraServer::FillSpectra(std::string name,double valx,double valy){
+void NPL::SpectraServer::FillSpectra(const std::string& name,const double& valx,const double& valy){
   // Fill the local histo
   ((TH2*) m_Spectra->FindObject(name.c_str()))->Fill(valx,valy);
   
-  std::map<TSocket*,NPL::DeltaSpectra >::iterator it;
-  for(it = m_Delta.begin(); it!=m_Delta.end() ; it++){
-    it->second.Fill(name,valx,valy);  
-  }
 }
 ////////////////////////////////////////////////////////////////////////////////
 void NPL::SpectraServer::AddSpectra(TH1* h){
diff --git a/NPLib/Core/NPSpectraServer.h b/NPLib/Core/NPSpectraServer.h
index 168a3ac4b..7c13d4526 100644
--- a/NPLib/Core/NPSpectraServer.h
+++ b/NPLib/Core/NPSpectraServer.h
@@ -49,16 +49,14 @@ namespace NPL{
     public:
       void HandleSocket(TSocket* s);
       void AddSpectra(TH1* h);
-      //void AddSpectra(TH2* h);
-      void FillSpectra(std::string name,double valx);
-      void FillSpectra(std::string name,double valx, double valy);
+      void FillSpectra(const std::string& name,const double& valx);
+      void FillSpectra(const std::string& name,const double& valx, const double& valy);
       void CheckRequest();
 
     private:
       bool m_stop;
       TServerSocket* m_Server;     
       TMonitor* m_Monitor;     
-      std::map<TSocket*,NPL::DeltaSpectra > m_Delta;
       TList* m_Sockets;
       TList* m_Spectra;
   };
diff --git a/NPLib/Core/NPVDetector.cxx b/NPLib/Core/NPVDetector.cxx
index ea559d625..1304148c4 100644
--- a/NPLib/Core/NPVDetector.cxx
+++ b/NPLib/Core/NPVDetector.cxx
@@ -39,22 +39,5 @@ VDetector::~VDetector(){
 
 ////////////////////////////////////////////////////////////////////////////////
 namespace NPL{
-  static string itoa_array[10000];
-
-  class itoa_proxy{
-    public:
-      itoa_proxy(){
-      char buffer[]="10000";
-        for(int i = 0 ; i < 10000 ; i++){
-          sprintf(buffer,"%d",i);
-          itoa_array[i] = buffer;
-        }
-      }
-  };
-  static itoa_proxy itoa_p ;
-}
-
-std::string NPL::itoa(const int& i){
-  return NPL::itoa_array[i];
 }
 
diff --git a/NPLib/Core/NPVDetector.h b/NPLib/Core/NPVDetector.h
index 81ddd7018..415950273 100644
--- a/NPLib/Core/NPVDetector.h
+++ b/NPLib/Core/NPVDetector.h
@@ -36,9 +36,9 @@
 
 // NPL
 #include "NPInputParser.h"
+#include "NPCore.h"
 
 namespace NPL {
-  std::string itoa(const int&); 
 
   class VDetector{
     public:
diff --git a/NPLib/Online/NPOnlineGUI.cxx b/NPLib/Online/NPOnlineGUI.cxx
index cbf86f706..7cfae0a2c 100644
--- a/NPLib/Online/NPOnlineGUI.cxx
+++ b/NPLib/Online/NPOnlineGUI.cxx
@@ -46,6 +46,7 @@
 #include "TGComboBox.h"
 #include "TASImage.h"
 #include "TH2.h"
+#include "NPCore.h"
 ClassImp(NPL::OnlineGUI);
 ////////////////////////////////////////////////////////////////////////////////
 void NPL::ExecuteMacro(string name){
@@ -53,7 +54,7 @@ void NPL::ExecuteMacro(string name){
   static struct dirent *ent;
   static string path; 
   path = "./online_macros/";
-  name += ".C";
+  name += ".cxx";
   if ((dir = opendir (path.c_str())) != NULL) {
     while ((ent = readdir (dir)) != NULL) {
       if(ent->d_name==name)
@@ -665,7 +666,7 @@ void NPL::OnlineGUI::MakeGui(){
 
 
   // Center //
-  m_EmbeddedCanvas = new TRootEmbeddedCanvas("Display",m_Center,700,490,!kSunkenFrame);  
+  m_EmbeddedCanvas = new TRootEmbeddedCanvas("Display",m_Center,10000,10000,!kSunkenFrame);  
   m_EmbeddedCanvas->SetAutoFit(true);
   m_Center->AddFrame(m_EmbeddedCanvas,new TGLayoutHints(kLHintsLeft | kLHintsBottom | kLHintsExpandX | kLHintsExpandY));
   TCanvas* c = NULL;
@@ -739,12 +740,29 @@ void NPL::OnlineGUI::Connect(){
 
 ////////////////////////////////////////////////////////////////////////////////
 void NPL::OnlineGUI::Update(){
-  if(m_Client->Update()){
-    // Do some stuff with the histo
-  }
+    TCanvas* c = m_EmbeddedCanvas->GetCanvas();
+    int current = gPad->GetNumber();
+    TList* first = c->GetListOfPrimitives();
+    int size= first->GetSize();
+    for(unsigned int i =  0 ; i < size ;i++){
+      if(first->At(i)->InheritsFrom(TPad::Class())){
+      dynamic_cast<TPad*>(first->At(i))->cd();
+      TList* list = gPad->GetListOfPrimitives();
+      int Hsize = list->GetSize();
+      for(int h = 0 ; h < Hsize ; h++){
+        TObject* obj = list->At(h);
+        if(obj->InheritsFrom(TH1::Class())){
+          m_Client->Update(obj->GetName());
+          obj->Paint();
+          ExecuteMacro(obj->GetName());
+          gPad->Update();
+        }
+      }
+     }
+    }
+    c->cd(current);
+    c->Update();
 
-  else
-    m_Connect->SetState(kButtonUp);  
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -786,7 +804,7 @@ NPL::CanvasList::CanvasList(TGMainFrame* main, TGCanvas* parent,TRootEmbeddedCan
   m_ListTree->Connect("DoubleClicked(TGListTreeItem*,Int_t)","NPL::CanvasList",this,"OnDoubleClick(TGListTreeItem*,Int_t)");
   m_Main = main;
   m_EmbeddedCanvas = canvas;
-  //LoadCanvasList(Spectra);
+  m_OldCurrent     = "_void_";
 }
 ////////////////////////////////////////////////////////////////////////////////
 NPL::CanvasList::~CanvasList(){
@@ -801,8 +819,8 @@ void NPL::CanvasList::OnDoubleClick(TGListTreeItem* item, Int_t btn){
     m_EmbeddedCanvas->GetContainer()->Resize(0,0);
     m_EmbeddedCanvas->GetContainer()->Resize(size);
     c->SetHighLightColor(kAzure+7); // color of active pad
+    c->Draw();
     c->Update();
-    ExecuteMacro(c->GetName());
     c->cd(1);
   }
 }
@@ -812,12 +830,20 @@ void NPL::CanvasList::AddItem(TCanvas* c,TGListTreeItem* parent){
   item->SetPictures(m_popen, m_pclose);
   if(parent)
     parent->SetPictures(m_pfolder,m_pfolder);
-  string path = c->GetName();
   m_Canvas[c->GetName()]=c;
 }
 ////////////////////////////////////////////////////////////////////////////////
 void NPL::CanvasList::Clear(){
+  m_OldCurrent=m_EmbeddedCanvas->GetCanvas()->GetName();
+  
+  std::map<std::string,TCanvas*>::iterator it ;
+  // delete all old canvas
+  for(it=m_Canvas.begin(); it!=m_Canvas.end();it++){
+    delete it->second;
+    }
   m_Canvas.clear();
+
+  // Clear the list tree
   TGListTreeItem* item =  m_ListTree->GetFirstItem() ;
   while(item){
     m_ListTree->DeleteItem(item);
@@ -839,7 +865,7 @@ void NPL::CanvasList::SetStatusText(const char* txt, int pi){
 }
 ////////////////////////////////////////////////////////////////////////////////
 void NPL::CanvasList::EventInfo(int event,int px,int py,TObject* selected){
-    const char *text0, *text1, *text3;
+  const char *text0, *text1, *text3;
   char text2[50];
   text0 = selected->GetTitle();
   SetStatusText(text0,0);
@@ -857,6 +883,7 @@ void NPL::CanvasList::EventInfo(int event,int px,int py,TObject* selected){
 void NPL::CanvasList::LoadCanvasList(TList* Spectra){
   if(!Spectra)
     return;
+  Clear();
   NPL::InputParser parser("CanvasList.txt",false);
   vector<NPL::InputBlock*> blocks = parser.GetAllBlocksWithToken("Canvas");
   vector<std::string> token = {"Path","Divide","Histo"};
@@ -870,16 +897,23 @@ void NPL::CanvasList::LoadCanvasList(TList* Spectra){
       TCanvas* c = new TCanvas(name.c_str(), 5000,5000,0);
       c->Divide(divide[0],divide[1]);
 
-
       unsigned int size = histo.size();
       for(unsigned int h = 0 ; h < size ; h++){
         c->cd(h+1);
+        string padname=name+"_"+NPL::itoa(h);
+        gPad->SetName(padname.c_str());
         TH1* hist = (TH1*) Spectra->FindObject(histo[h].c_str());
         if(hist){
           hist->UseCurrentStyle();
           hist->Draw("colz");
+          ExecuteMacro(hist->GetName());
         }
       }
+      if(m_EmbeddedCanvas && m_OldCurrent == c->GetName()){
+        m_EmbeddedCanvas->AdoptCanvas(c);
+      }
+
+
       TGListTreeItem*  item  =  NULL;
       TGListTreeItem*  pitem =  NULL;
 
diff --git a/NPLib/Online/NPOnlineGUI.h b/NPLib/Online/NPOnlineGUI.h
index d47f2eadd..0a0b09a11 100644
--- a/NPLib/Online/NPOnlineGUI.h
+++ b/NPLib/Online/NPOnlineGUI.h
@@ -41,10 +41,9 @@
 #include "NPSpectraClient.h"
 #include "NPElog.h"
 #include<map>
-using namespace std;
 
 namespace NPL{
-   void ExecuteMacro(string name);
+   void ExecuteMacro(std::string name);
   
   class CanvasList {
     RQ_OBJECT("CanvasList")
@@ -52,15 +51,16 @@ namespace NPL{
       TGMainFrame* m_Main;
       TGListTree* m_ListTree;
       TRootEmbeddedCanvas* m_EmbeddedCanvas; 
+      std::string          m_OldCurrent;
       TGTab* m_Tab;
-      map<string,TCanvas*> m_Canvas;
+      std::map<std::string,TCanvas*> m_Canvas;
       const TGPicture* m_popen;     
       const TGPicture* m_pclose;   
       const TGPicture* m_pfolder;   
 
       Pixel_t m_BgColor;
       Pixel_t m_FgColor;
-      vector<TGStatusBar*> m_StatusBar;
+      std::vector<TGStatusBar*> m_StatusBar;
 
     public:
       CanvasList(TGMainFrame* main, TGCanvas* parent, TRootEmbeddedCanvas* canvas, TList*);
diff --git a/NPLib/Online/NPSpectraClient.cxx b/NPLib/Online/NPSpectraClient.cxx
index 63860db50..473b99a18 100644
--- a/NPLib/Online/NPSpectraClient.cxx
+++ b/NPLib/Online/NPSpectraClient.cxx
@@ -24,11 +24,11 @@
 #include<iostream>
 #include<sstream>
 #include"NPCore.h"
+#include"TH2.h"
 ////////////////////////////////////////////////////////////////////////////////
 NPL::SpectraClient::SpectraClient(){
   m_Sock =NULL;
   m_Spectra=NULL; 
-  m_Delta = NULL;
   m_Address = "localhost";
   m_Port = 9092;
 }
@@ -36,7 +36,6 @@ NPL::SpectraClient::SpectraClient(){
 NPL::SpectraClient::SpectraClient(std::string address, int port){
   m_Sock =NULL;
   m_Spectra=NULL; 
-  m_Delta = NULL;
   m_Address = address;
   m_Port = port;
 }
@@ -52,8 +51,6 @@ NPL::SpectraClient::~SpectraClient(){
     delete m_Spectra;
     m_Spectra=NULL;
   }
-  if(m_Delta)
-    delete m_Delta;
 }
 ////////////////////////////////////////////////////////////////////////////////
 bool NPL::SpectraClient::Connect(){
@@ -112,7 +109,8 @@ bool NPL::SpectraClient::Sync(){
       delete m_Spectra;
       m_Spectra = NULL;
     }
-    
+   
+    //message->Uncompress();
     m_Spectra = (TList*) message->ReadObject(message->GetClass());
     if(m_Spectra){
       NPL::SendInformation("NPL::SpectraClient","Successful sync of spectra list");
@@ -126,14 +124,14 @@ bool NPL::SpectraClient::Sync(){
   }
 
   else{
-    NPL::SendInformation("NPL::SpectraClient","Server return empty sepctra list");
+    NPL::SendInformation("NPL::SpectraClient","Server returned an empty message");
     return false;
   }
 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-bool NPL::SpectraClient::Update(){
+bool NPL::SpectraClient::Update(std::string name){
   if(!m_Sock || !(m_Sock->IsValid())){
     if(m_Sock){
       m_Sock->Close("force");
@@ -146,7 +144,7 @@ bool NPL::SpectraClient::Update(){
   }
 
   TMessage* message=NULL;
-  m_Sock->Send("RequestDelta");
+  m_Sock->Send(name.c_str());
 
   if(m_Sock->Recv(message)<=0){
     if(m_Sock){
@@ -161,16 +159,12 @@ bool NPL::SpectraClient::Update(){
   }
 
   if(message){
-    if(m_Delta)
-      delete m_Delta;
-     
-    m_Delta = (NPL::DeltaSpectra*) message->ReadObject(message->GetClass());
-   
-    if(m_Delta && m_Spectra){
-      m_Delta->UpdateLocalSpectra(m_Spectra);
-      }
-    else
-      NPL::SendWarning("NPL::SpectraClient","Local Spectra or received Delta are NULL");
+    // Get the current spectra
+    TH1* h = (TH1*) m_Spectra->FindObject(name.c_str());
+    // Get the new one
+    TH1* n = (TH1*) message->ReadObject(message->GetClass());
+    UpdateTH1(h,n); 
+    delete n;  
     return true;
   }
   
@@ -184,4 +178,22 @@ bool NPL::SpectraClient::Update(){
 TList* NPL::SpectraClient::GetSpectra(){
   return m_Spectra;
 }
+////////////////////////////////////////////////////////////////////////////////
+void NPL::SpectraClient::UpdateTH1(TH1* Old , TH1* New){
+  // Save the Ranges on the different axis
+  double minX = Old->GetXaxis()->GetBinLowEdge(Old->GetXaxis()->GetFirst());
+  double maxX = Old->GetXaxis()->GetBinUpEdge(Old->GetXaxis()->GetLast());
+  double minY = Old->GetYaxis()->GetBinLowEdge(Old->GetYaxis()->GetFirst());
+  double maxY = Old->GetYaxis()->GetBinUpEdge(Old->GetYaxis()->GetLast());
+
+  // Put new stuff in old object
+  New->Copy(*Old) ;    
+  // Reset the axis range
+  Old->SetAxisRange(minX,maxX);
+  if(Old->GetDimension()==2)
+    Old->SetAxisRange(minY,maxY,"Y");
+   
+  // Refresh style
+  Old->UseCurrentStyle();      
+ }
 
diff --git a/NPLib/Online/NPSpectraClient.h b/NPLib/Online/NPSpectraClient.h
index e80dd5c7d..fb67e65df 100644
--- a/NPLib/Online/NPSpectraClient.h
+++ b/NPLib/Online/NPSpectraClient.h
@@ -27,8 +27,7 @@
 #include "TMessage.h"
 #include "TList.h"
 #include "TH1.h"
-// NPTOOL
-#include "NPDeltaSpectra.h"
+#include "string"
 
 namespace NPL{
   class SpectraClient{
@@ -42,9 +41,9 @@ namespace NPL{
       bool Connect();
       // Get the full copy of Spectrum (erase local one first)
       bool Sync();
-      // Get the Delta since last update
-      bool Update();
-
+      // Update a single spectra
+      bool Update(std::string name);
+      void UpdateTH1(TH1* Old, TH1* New );
     private: // The sochet use for connection
       TSocket* m_Sock;
       std::string m_Address;
@@ -52,7 +51,6 @@ namespace NPL{
 
     private: // The spectrum list
       TList* m_Spectra; 
-      NPL::DeltaSpectra* m_Delta;
     public: // GUI Interface
       TList* GetSpectra();
  
diff --git a/NPLib/Utility/CMakeLists.txt b/NPLib/Utility/CMakeLists.txt
index 4e62770d5..6f3b543c7 100644
--- a/NPLib/Utility/CMakeLists.txt
+++ b/NPLib/Utility/CMakeLists.txt
@@ -4,5 +4,8 @@ add_executable(npanalysis npanalysis.cxx)
 target_link_libraries(npanalysis ${ROOT_LIBRARIES} -lThread -lGraf -lHist -lMatrix NPCore) 
 add_executable(nponline nponline.cxx)
 target_link_libraries(nponline ${ROOT_LIBRARIES} NPCore NPOnline) 
+add_executable(npspectra_test npspectra_test.cxx)
+target_link_libraries(npspectra_test ${ROOT_LIBRARIES} NPCore NPOnline) 
+
 install(PROGRAMS nptool-installer npanalysis nponline DESTINATION ${CMAKE_BINARY_OUTPUT_DIRECTORY})
 install(SCRIPT ../scripts/post_install.cmake ${DETLIST})
diff --git a/NPSimulation/Core/EventAction.cc b/NPSimulation/Core/EventAction.cc
index d3dc69a06..165bdc116 100644
--- a/NPSimulation/Core/EventAction.cc
+++ b/NPSimulation/Core/EventAction.cc
@@ -33,7 +33,6 @@
 #include "ParticleStack.hh"
 
 #include<iostream>
-using namespace std;
 
 EventAction* EventAction::m_EventAction=0;
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
@@ -59,6 +58,7 @@ void EventAction::BeginOfEventAction(const G4Event* event){
 
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 void EventAction::EndOfEventAction(const G4Event* event){
+    m_detector->ReadAllSensitive(event) ;
     m_detector->ReadAllSensitive(event) ;
     static TTree* tree =  RootOutput::getInstance()->GetTree();
     tree->Fill();
diff --git a/NPSimulation/Core/MaterialManager.cc b/NPSimulation/Core/MaterialManager.cc
index f2f18c46e..703c3dff5 100644
--- a/NPSimulation/Core/MaterialManager.cc
+++ b/NPSimulation/Core/MaterialManager.cc
@@ -849,6 +849,8 @@ G4Material* MaterialManager::GetGasFromLibrary(string Name, double Pressure, dou
 //   Generate a DEDX file table using the material used in the geometry
 void MaterialManager::WriteDEDXTable(G4ParticleDefinition* Particle ,G4double Emin,G4double Emax){
     map<string,G4Material*>::iterator it;
+    if(Particle->GetPDGCharge()==0)
+      return;
     for(it = m_Material.begin() ; it != m_Material.end() ; it++){
         //   Opening hte output file
         G4String GlobalPath = getenv("NPTOOL");
@@ -892,7 +894,7 @@ void MaterialManager::WriteDEDXTable(std::set<string> Particle ,G4double Emin,G4
   std::set<string>::iterator it;
   for(it=Particle.begin(); it!=Particle.end() ; it++){
      G4ParticleDefinition* p = G4ParticleTable::GetParticleTable()->FindParticle((*it));
-      WriteDEDXTable(p,Emin,Emax);
+     WriteDEDXTable(p,Emin,Emax);
     }
 }
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
diff --git a/NPSimulation/Core/RunAction.cc b/NPSimulation/Core/RunAction.cc
index b634a2e36..b68b214be 100644
--- a/NPSimulation/Core/RunAction.cc
+++ b/NPSimulation/Core/RunAction.cc
@@ -60,10 +60,8 @@ void RunAction::BeginOfRunAction(const G4Run* aRun){
 void RunAction::EndOfRunAction(const G4Run* aRun){
     // Pass a line for nicer presentation when chainning event generator
     cout << endl;
-    
     // Force the tree to be saved at the end of the run
     RootOutput::getInstance()->GetTree()->AutoSave("Overwrite SaveSelf");
-
     // Write DEDX Tables
     std::set<string> Particles;
     const std::vector<const G4Event*>* events = aRun->GetEventVector();
@@ -74,5 +72,6 @@ void RunAction::EndOfRunAction(const G4Run* aRun){
       for(unsigned int i = 0 ; i < size ; i++)
         Particles.insert( (*traj)[i]->GetParticleName());
     }
+    
     MaterialManager::getInstance()->WriteDEDXTable(Particles,0,10*GeV); 
 }
diff --git a/NPSimulation/Process/BeamReaction.cc b/NPSimulation/Process/BeamReaction.cc
index 27ef4e8db..d38dae754 100644
--- a/NPSimulation/Process/BeamReaction.cc
+++ b/NPSimulation/Process/BeamReaction.cc
@@ -37,6 +37,7 @@ NPS::BeamReaction::BeamReaction(G4String modelName,G4Region* envelope) :
   ReadConfiguration();
   m_PreviousEnergy=0 ;
   m_PreviousLength=0 ;
+  m_active = 0;
   }
 
 
@@ -54,6 +55,8 @@ void NPS::BeamReaction::ReadConfiguration(){
  NPL::InputParser input(NPOptionManager::getInstance()->GetReactionFile());
  m_Reaction.ReadConfigurationFile(input);
  m_BeamName=NPL::ChangeNameToG4Standard(m_Reaction.GetNucleus1().GetName());
+ if(m_Reaction.GetNucleus3().GetName()!="")
+   m_active = true;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -67,6 +70,9 @@ G4bool NPS::BeamReaction::IsApplicable( const G4ParticleDefinition& particleType
 
 ////////////////////////////////////////////////////////////////////////////////
 G4bool NPS::BeamReaction::ModelTrigger(const G4FastTrack& fastTrack) {
+  if(!m_active)
+    return false;
+
   static bool shoot = false;
   static double rand = 0;
   const G4Track* PrimaryTrack = fastTrack.GetPrimaryTrack();   
diff --git a/NPSimulation/Process/BeamReaction.hh b/NPSimulation/Process/BeamReaction.hh
index 3ec04679e..aa42d4423 100644
--- a/NPSimulation/Process/BeamReaction.hh
+++ b/NPSimulation/Process/BeamReaction.hh
@@ -46,6 +46,7 @@ namespace NPS{
       string m_BeamName;
       double m_PreviousEnergy;
       double m_PreviousLength;
+      bool   m_active;// is the process active
   };
 }
 
diff --git a/Projects/MUGAST/Analysis.cxx b/Projects/MUGAST/Analysis.cxx
index 57e731e6e..e9d626c18 100644
--- a/Projects/MUGAST/Analysis.cxx
+++ b/Projects/MUGAST/Analysis.cxx
@@ -47,18 +47,16 @@ void Analysis::Init() {
   // get reaction information
   myReaction.ReadConfigurationFile(NPOptionManager::getInstance()->GetReactionFile());
   OriginalBeamEnergy = myReaction.GetBeamEnergy();
-
   // target thickness
   TargetThickness = m_DetectorManager->GetTargetThickness();
   string TargetMaterial = m_DetectorManager->GetTargetMaterial();
   // Cryo target case
-  WindowsThickness = m_DetectorManager->GetWindowsThickness(); 
-  string WindowsMaterial = m_DetectorManager->GetWindowsMaterial();
+  WindowsThickness = 0;//m_DetectorManager->GetWindowsThickness(); 
+  string WindowsMaterial = "";//m_DetectorManager->GetWindowsMaterial();
 
-  // energy losses
+ // energy losses
   string light=NPL::ChangeNameToG4Standard(myReaction.GetNucleus3().GetName());
   string beam=NPL::ChangeNameToG4Standard(myReaction.GetNucleus1().GetName());
-
   LightTarget = NPL::EnergyLoss(light+"_"+TargetMaterial+".G4table","G4Table",100 );
   LightAl = NPL::EnergyLoss(light+"_Al.G4table","G4Table",100);
   LightSi = NPL::EnergyLoss(light+"_Si.G4table","G4Table",100);
diff --git a/Projects/MUGAST/ShowResults.C b/Projects/MUGAST/ShowResults.C
index 41ca7ece3..15bbb392d 100644
--- a/Projects/MUGAST/ShowResults.C
+++ b/Projects/MUGAST/ShowResults.C
@@ -52,8 +52,7 @@ using namespace std;
 #include "NPReaction.h"
 using namespace NPL;
 
-void ShowResults()
-{
+void ShowResults(){
    // get tree   
    TFile *f = new TFile("../../Outputs/Analysis/PhysicsTree.root");
    TTree *t = (TTree*) f->Get("PhysicsTree");
@@ -63,27 +62,47 @@ void ShowResults()
    TCanvas *c1 = new TCanvas("c1", "kinematic information", 600, 600);
    c1->Draw();
    // kinematic line
-   TH2F *hk = new TH2F("hk", "hk", 90, 90, 180, 200, 0, 12);
+   TH2F *hk = new TH2F("hk", "hk", 180, 0, 180, 200, 0, 60);
    hk->GetXaxis()->SetTitle("#Theta_{lab} (deg)");
    hk->GetYaxis()->SetTitle("E_{p} (MeV)");
-   t->Draw("ELab:ThetaLab>>hk");
-   // inset
-   TPad *pad = new TPad("pad1", "excitation energy", 0.45, 0.45, 0.87, 0.87);
-   pad->Draw();
-   pad->cd();
-   // excitation energy
-   TH1F *hx = new TH1F("hx", "hx", 150, 5, 8);
-   hx->SetXTitle("E_{X} (MeV)");
-   hx->SetYTitle("counts / (20 keV)");
-   t->Draw("Ex>>hx");
+   cout << "counts " << t->Draw("ELab:ThetaLab>>hk","Ex>0&&Ex<6","colz") << endl;
+   NPL::Reaction* reaction = new NPL::Reaction();
+   reaction->ReadConfigurationFile("28Mg.reaction");
+   reaction->GetKinematicLine3()->Draw("c");
+
+  new TCanvas();
+  TH1F *hCM = new TH1F("hCM", "hCM", 36, 0, 180); 
+  t->Draw("ThetaCM>>hCM","Ex>0&&Ex<6","",2900);
+  for(int i = 0 ; i < hCM->GetNbinsX();i++){
+    if(hCM->GetBinCenter(i)==37.5 || hCM->GetBinCenter(i)==97.5|| hCM->GetBinCenter(i)==167.5|| hCM->GetBinCenter(i)==42.5){
+      hCM->SetBinContent(i,0);
+      
+      }
+    
+    }
+ gPad->SetLogy();
+
+  TFile* file= new TFile("effMUGAST.root");
+  TH1* hOmega = (TH1*)file->FindObjectAny("hDetecThetaCM");
+  hOmega->Rebin(5);
+  hCM->Sumw2();
+  hCM->Divide(hOmega);
+  TGraph* g= new TGraph("22.jjj");
+  g->Draw("c");
+   hCM->Scale(g->Eval(32.5)/hCM->GetBinContent(hCM->FindBin(32.5)));
+  TGraph* g2= new TGraph("22.l2");
+  g2->Draw("c");
+  TGraph* g3= new TGraph("22.l3");
+  g3->Draw("c");
+
+
 }
 
 
-void CountingRates(Double_t ibeam = 1e5, Double_t ubt = 30)
-{
+void CountingRates(Double_t ibeam = 1e5, Double_t ubt = 30){
    // load event generator file
    NPL::Reaction* reaction = new NPL::Reaction();
-   reaction->ReadConfigurationFile("30Pdp.reaction");
+   reaction->ReadConfigurationFile("28Mg.reaction");
 //   reaction->ReadConfigurationFile("11Be_d3He.reaction");
    // get angular distribution
    TH1F *dsig = reaction->GetCrossSectionHist();
-- 
GitLab