From eb001ff6b6f9b434fae2a7cf89199cd2af456e26 Mon Sep 17 00:00:00 2001
From: Hugo Jacob <hugojacob57@gmail.com>
Date: Wed, 13 Dec 2023 18:25:53 +0100
Subject: [PATCH] Adding simple Physics classes and physics readers for ZDD,
 EXOGAM and TAC

---
 NPLib/Detectors/Exogam/CMakeLists.txt         |  7 ++-
 NPLib/Detectors/Exogam/TExogamData.cxx        |  1 -
 NPLib/Detectors/Exogam/TExogamData.h          | 32 +++++-----
 NPLib/Detectors/Exogam/TExogamPhysics.cxx     | 25 ++++----
 NPLib/Detectors/Exogam/TExogamPhysics.h       | 23 ++++---
 .../Detectors/Exogam/TExogamPhysicsReader.cxx | 33 ++++++++++
 NPLib/Detectors/Exogam/TExogamPhysicsReader.h | 60 ++++++++++++++++++
 NPLib/Detectors/TAC/CMakeLists.txt            |  7 ++-
 NPLib/Detectors/TAC/TTACData.h                |  3 +-
 NPLib/Detectors/TAC/TTACPhysics.cxx           | 62 ++++++++++++++++++-
 NPLib/Detectors/TAC/TTACPhysics.h             | 20 ++++--
 NPLib/Detectors/TAC/TTACPhysicsReader.cxx     | 33 ++++++++++
 NPLib/Detectors/TAC/TTACPhysicsReader.h       | 60 ++++++++++++++++++
 NPLib/Detectors/TAC/TTACSpectra.cxx           | 10 +--
 NPLib/Detectors/ZDD/TZDDPhysics.cxx           | 34 +++++-----
 NPLib/Detectors/ZDD/TZDDPhysics.h             | 11 ++--
 NPLib/Detectors/ZDD/TZDDSpectra.cxx           |  4 +-
 NPLib/Utility/npreader.cxx                    |  1 -
 Projects/E805/Analysis.cxx                    |  4 +-
 19 files changed, 346 insertions(+), 84 deletions(-)
 create mode 100644 NPLib/Detectors/Exogam/TExogamPhysicsReader.cxx
 create mode 100644 NPLib/Detectors/Exogam/TExogamPhysicsReader.h
 create mode 100644 NPLib/Detectors/TAC/TTACPhysicsReader.cxx
 create mode 100644 NPLib/Detectors/TAC/TTACPhysicsReader.h

diff --git a/NPLib/Detectors/Exogam/CMakeLists.txt b/NPLib/Detectors/Exogam/CMakeLists.txt
index 66c3fcef7..bd0a1f4b3 100644
--- a/NPLib/Detectors/Exogam/CMakeLists.txt
+++ b/NPLib/Detectors/Exogam/CMakeLists.txt
@@ -1,6 +1,7 @@
 add_custom_command(OUTPUT TExogamPhysicsDict.cxx COMMAND ${CMAKE_BINARY_DIR}/scripts/build_dict.sh TExogamPhysics.h TExogamPhysicsDict.cxx TExogamPhysics.rootmap libNPExogam.dylib DEPENDS TExogamPhysics.h)
 add_custom_command(OUTPUT TExogamDataDict.cxx COMMAND ${CMAKE_BINARY_DIR}/scripts/build_dict.sh TExogamData.h TExogamDataDict.cxx TExogamData.rootmap libNPExogam.dylib DEPENDS TExogamData.h)
-add_library(NPExogam SHARED TExogamData.cxx TExogamPhysics.cxx TExogamDataDict.cxx TExogamPhysicsDict.cxx TExogamSpectra.cxx)
-target_link_libraries(NPExogam ${ROOT_LIBRARIES} NPCore) 
-install(FILES TExogamData.h TExogamPhysics.h TExogamSpectra.h DESTINATION ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
+add_custom_command(OUTPUT TExogamPhysicsReaderDict.cxx COMMAND ${CMAKE_BINARY_DIR}/scripts/build_dict.sh TExogamPhysicsReader.h TExogamPhysicsReaderDict.cxx TExogamPhysicsReader.rootmap libNPExogam.dylib DEPENDS TExogamPhysicsReader.h)
+add_library(NPExogam SHARED TExogamData.cxx TExogamPhysics.cxx TExogamPhysicsReader.cxx TExogamDataDict.cxx TExogamPhysicsDict.cxx TExogamSpectra.cxx TExogamPhysicsReaderDict.cxx)
+target_link_libraries(NPExogam ${ROOT_LIBRARIES} -lTreePlayer -lSpectrum NPCore NPPhysics) 
+install(FILES TExogamData.h TExogamPhysics.h TExogamSpectra.h TExogamPhysicsReader.h DESTINATION ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
 
diff --git a/NPLib/Detectors/Exogam/TExogamData.cxx b/NPLib/Detectors/Exogam/TExogamData.cxx
index 53c426fd9..1cd5a0bc0 100644
--- a/NPLib/Detectors/Exogam/TExogamData.cxx
+++ b/NPLib/Detectors/Exogam/TExogamData.cxx
@@ -34,7 +34,6 @@ ClassImp(TExogamData)
 TExogamData::~TExogamData() {}
 
 void TExogamData::Clear() {
-  fExo_Flange.clear();
   fExo_Crystal.clear();
   fExo_E.clear();
   fExo_E_HG.clear(); // High gain x20
diff --git a/NPLib/Detectors/Exogam/TExogamData.h b/NPLib/Detectors/Exogam/TExogamData.h
index 1887cdbce..c5d377ba5 100644
--- a/NPLib/Detectors/Exogam/TExogamData.h
+++ b/NPLib/Detectors/Exogam/TExogamData.h
@@ -38,7 +38,6 @@ class TExogamData : public TObject {
   void Dump() const;
 
  public:
-  std::vector<unsigned int> fExo_Flange;
   std::vector<unsigned int> fExo_Crystal;
   std::vector<unsigned int> fExo_E;
   std::vector<unsigned int> fExo_E_HG; // High gain x20
@@ -53,21 +52,24 @@ class TExogamData : public TObject {
 
 
   /////////////////////           SETTERS           ////////////////////////
-  inline void SetExoFlange(unsigned int& Flange) { fExo_Flange.push_back(Flange); }
-  inline void SetExoCrystal(unsigned int& Crystal) { fExo_Crystal.push_back(Crystal); }
-  inline void SetExoE(unsigned int& Energy) { fExo_E.push_back(Energy); }
-  inline void SetExoEHG(unsigned int& Energy) { fExo_E_HG.push_back(Energy); }
-  inline void SetExoTS(unsigned long long& TS) { fExo_TS.push_back(TS); }
-  inline void SetExoTDC(unsigned int& TDC) { fExo_TDC.push_back(TDC); }
-  inline void SetExoBGO(unsigned int& BGO) { fExo_BGO.push_back(BGO); }
-  inline void SetExoCsI(unsigned int& CsI) { fExo_CsI.push_back(CsI); }
-  inline void SetExoOuter1(unsigned int& Outer1) { fExo_Outer1.push_back(Outer1); }
-  inline void SetExoOuter2(unsigned int& Outer2) { fExo_Outer2.push_back(Outer2); }
-  inline void SetExoOuter3(unsigned int& Outer3) { fExo_Outer3.push_back(Outer3); }
-  inline void SetExoOuter4(unsigned int& Outer4) { fExo_Outer4.push_back(Outer4); }
-
+  inline void SetExo(unsigned int& Crystal, unsigned int& Energy,
+  unsigned int& Energy_HG, unsigned long long& TS, unsigned int& TDC,
+  unsigned int& BGO, unsigned int& CsI, unsigned int& Outer1,
+  unsigned int& Outer2, unsigned int& Outer3,unsigned int& Outer4) { 
+  fExo_Crystal.push_back(Crystal);
+  fExo_E.push_back(Energy);
+  fExo_E_HG.push_back(Energy_HG);
+  fExo_TS.push_back(TS);
+  fExo_TDC.push_back(TDC);
+  fExo_BGO.push_back(BGO);
+  fExo_CsI.push_back(CsI);
+  fExo_Outer1.push_back(Outer1);
+  fExo_Outer2.push_back(Outer2);
+  fExo_Outer3.push_back(Outer3);
+  fExo_Outer4.push_back(Outer4);
+  }
   /////////////////////           GETTERS           ////////////////////////
-  inline unsigned int GetExoFlange(unsigned int& i) { return fExo_Flange[i]; }
+  inline unsigned int GetExoMult(unsigned int& i) { return fExo_Crystal[i]; }
   inline unsigned int GetExoCrystal(unsigned int& i) { return fExo_Crystal[i]; }
   inline unsigned int GetExoE(unsigned int& i) { return fExo_E[i]; }
   inline unsigned int GetExoEHG(unsigned int& i) { return fExo_E_HG[i]; }
diff --git a/NPLib/Detectors/Exogam/TExogamPhysics.cxx b/NPLib/Detectors/Exogam/TExogamPhysics.cxx
index 68b02ad7c..7575a007d 100644
--- a/NPLib/Detectors/Exogam/TExogamPhysics.cxx
+++ b/NPLib/Detectors/Exogam/TExogamPhysics.cxx
@@ -48,9 +48,9 @@ ClassImp(TExogamPhysics)
   m_Spectra = NULL;
   NumberOfClover = 0;
 
-  PreTreatedData = new TExogamData;
-  EventData = new TExogamData;
-  EventPhysics = this;
+  m_PreTreatedData = new TExogamData;
+  m_EventData = new TExogamData;
+  m_EventPhysics = this;
   NumberOfClover = 0;
   CloverMult = 0;
 }
@@ -161,8 +161,11 @@ void TExogamPhysics::PreTreat() {
 ///////////////////////////////////////////////////////////////////////////
 
 void TExogamPhysics::BuildPhysicalEvent() {
-  /*PreTreat();
-
+  if (NPOptionManager::getInstance()->IsReader() == true) {
+    m_EventData = &(**r_ReaderEventData);
+  }
+  PreTreat();
+/*
   if(PreTreatedData -> GetECCEMult() != PreTreatedData -> GetECCTMult()) cout << PreTreatedData -> GetECCEMult() << " "
   <<  PreTreatedData -> GetECCTMult() << endl;
 
@@ -479,9 +482,9 @@ void TExogamPhysics::InitSpectra() { m_Spectra = new TExogamSpectra(NumberOfClov
 
 ///////////////////////////////////////////////////////////////////////////
 void TExogamPhysics::FillSpectra() {
-  m_Spectra->FillRawSpectra(EventData);
-  m_Spectra->FillPreTreatedSpectra(PreTreatedData);
-  m_Spectra->FillPhysicsSpectra(EventPhysics);
+  m_Spectra->FillRawSpectra(m_EventData);
+  m_Spectra->FillPreTreatedSpectra(m_PreTreatedData);
+  m_Spectra->FillPhysicsSpectra(m_EventPhysics);
 }
 ///////////////////////////////////////////////////////////////////////////
 void TExogamPhysics::CheckSpectra() { m_Spectra->CheckSpectra(); }
@@ -587,7 +590,7 @@ void TExogamPhysics::InitializeRootInputRaw() {
   TChain* inputChain = RootInput::getInstance()->GetChain();
   inputChain->SetBranchStatus("EXOGAM", true);
   inputChain->SetBranchStatus("fEXO_*", true);
-  inputChain->SetBranchAddress("EXOGAM", &EventData);
+  inputChain->SetBranchAddress("EXOGAM", &m_EventData);
 
   /*
   TList* outputList = RootOutput::getInstance()->GetList();
@@ -623,7 +626,7 @@ void TExogamPhysics::InitializeRootInputPhysics() {
   inputChain->SetBranchStatus("DopplerCorrectedEnergy", true);
   inputChain->SetBranchStatus("Position", true);
   inputChain->SetBranchStatus("Theta", true);
-  inputChain->SetBranchAddress("EXOGAM", &EventPhysics);
+  inputChain->SetBranchAddress("EXOGAM", &m_EventPhysics);
 }
 
 /////////////////////////////////////////////////////////////////////
@@ -631,7 +634,7 @@ void TExogamPhysics::InitializeRootInputPhysics() {
 //	Create associated branches and associated private member DetectorPhysics address
 void TExogamPhysics::InitializeRootOutput() {
   TTree* outputTree = RootOutput::getInstance()->GetTree();
-  outputTree->Branch("EXOGAM", "TExogamPhysics", &EventPhysics);
+  outputTree->Branch("EXOGAM", "TExogamPhysics", &m_EventPhysics);
 
   // control histograms if needed
   /*
diff --git a/NPLib/Detectors/Exogam/TExogamPhysics.h b/NPLib/Detectors/Exogam/TExogamPhysics.h
index 2f528b5fd..ced6f1e29 100644
--- a/NPLib/Detectors/Exogam/TExogamPhysics.h
+++ b/NPLib/Detectors/Exogam/TExogamPhysics.h
@@ -31,6 +31,7 @@
 #include "NPVDetector.h"
 #include "TExogamData.h"
 #include "TExogamSpectra.h"
+#include "TExogamPhysicsReader.h"
 #include "NPInputParser.h"
 // ROOT 
 #include "TVector2.h" 
@@ -42,7 +43,7 @@ using namespace std ;
 // Forward Declaration
 class TExogamSpectra;
 
-class TExogamPhysics : public TObject, public NPL::VDetector{
+class TExogamPhysics : public TObject, public NPL::VDetector, public TExogamPhysicsReader{
  public:
   TExogamPhysics()	;
   ~TExogamPhysics() {};
@@ -131,8 +132,8 @@ class TExogamPhysics : public TObject, public NPL::VDetector{
 
   //	Those two method all to clear the Event Physics or Data
   void ClearEventPhysics()		{Clear();}		
-  void ClearEventData()			{EventData->Clear();}	
-  void ClearPreTreatedData()	        {PreTreatedData->Clear();}
+  void ClearEventData()			{m_EventData->Clear();}	
+  void ClearPreTreatedData()	        {m_PreTreatedData->Clear();}
 
     // Method related to the TSpectra classes, aimed at providing a framework for online applications
     // Instantiate the Spectra class and the histogramm throught it
@@ -147,9 +148,9 @@ class TExogamPhysics : public TObject, public NPL::VDetector{
  private:	//	Root Input and Output tree classes
 
  				
-  TExogamData* 	          EventData		;//!
-  TExogamData* 	          PreTreatedData	;//!
-  TExogamPhysics* 	  EventPhysics		;//!
+  TExogamData*         m_EventData;        //!
+  TExogamData*         m_PreTreatedData;   //!
+  TExogamPhysics*      m_EventPhysics;     //!
   
 
  public:		//	Specific to EXOGAM Array
@@ -165,10 +166,14 @@ class TExogamPhysics : public TObject, public NPL::VDetector{
   Double_t GetSegmentAngleTheta(int Clover, int Cristal, int Segment)  {return(Clover_Angles_Theta_Phi[Clover][Cristal][Segment][0]);};
  
   // Give and external TMustData object to TExogamPhysics. Needed for online analysis for example.
-  void SetRawDataPointer(TExogamData* rawDataPointer) {EventData = rawDataPointer;}
+  void SetRawDataPointer(TExogamData* rawDataPointer) {m_EventData = rawDataPointer;}
   // Retrieve raw and pre-treated data
-  TExogamData* GetRawData()        const {return EventData;}
-  TExogamData* GetPreTreatedData() const {return PreTreatedData;}
+  TExogamData* GetRawData()        const {return m_EventData;}
+  TExogamData* GetPreTreatedData() const {return m_PreTreatedData;}
+
+  private: // Variables for analysis
+
+  unsigned int m_EXO_Mult;
 
  
   private: // Spectra Class   
diff --git a/NPLib/Detectors/Exogam/TExogamPhysicsReader.cxx b/NPLib/Detectors/Exogam/TExogamPhysicsReader.cxx
new file mode 100644
index 000000000..480a52923
--- /dev/null
+++ b/NPLib/Detectors/Exogam/TExogamPhysicsReader.cxx
@@ -0,0 +1,33 @@
+/*****************************************************************************
+ * Copyright (C) 2009-2016   this file is part of the NPTool Project         *
+ *                                                                           *
+ * For the licensing terms see $NPTOOL/Licence/NPTool_Licence                *
+ * For the list of contributors see $NPTOOL/Licence/Contributors             *
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Original Author: Adrien MATTA  contact address: a.matta@surrey.ac.uk      *
+ *                                                                           *
+ * Creation Date  : febuary 2009                                             *
+ * Last update    : July 2021
+ *---------------------------------------------------------------------------*
+ * Decription:                                                               *
+ *  This class hold must2 TreeReader                                        *
+ *                                                                           *
+ *---------------------------------------------------------------------------*
+ * Comment:                                                                  *
+ *                                                                           *
+ *****************************************************************************/
+#include "TExogamPhysicsReader.h"
+#include "NPDetectorFactory.h"
+///////////////////////////////////////////////////////////////////////////
+
+ClassImp(TExogamPhysicsReader)
+
+TExogamPhysicsReader::TExogamPhysicsReader()
+{
+};
+
+void TExogamPhysicsReader::r_SetTreeReader(TTreeReader* TreeReader){
+r_ReaderEventData = new TTreeReaderValue<TExogamData>(*TreeReader,"Exogam");
+}; 
\ No newline at end of file
diff --git a/NPLib/Detectors/Exogam/TExogamPhysicsReader.h b/NPLib/Detectors/Exogam/TExogamPhysicsReader.h
new file mode 100644
index 000000000..89bb63331
--- /dev/null
+++ b/NPLib/Detectors/Exogam/TExogamPhysicsReader.h
@@ -0,0 +1,60 @@
+#ifndef TEXOGAMPHYSICSREADER_H
+#define TEXOGAMPHYSICSREADER_H
+/*****************************************************************************
+ * Copyright (C) 2009-2016    this file is part of the NPTool Project        *
+ *                                                                           *
+ * For the licensing terms see $NPTOOL/Licence/NPTool_Licence                *
+ * For the list of contributors see $NPTOOL/Licence/Contributors             *
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Original Author: Hugo Jacob  contact address: hjacob@ijclab.in2p3.fr      *
+ *                                                                           *
+ * Creation Date  : 2023                                                     *
+ * Last update    : 2023
+ *---------------------------------------------------------------------------*
+ * Decription:                                                               *
+ *  TTreader class for Exogam Physics                                           *
+ *                                                                           *
+ *---------------------------------------------------------------------------*
+ * Comment:                                                                  *
+ *                                                                           *
+ *                                                                           *
+ *                                                                           *
+ *****************************************************************************/
+// STL
+#include <map>
+#include <vector>
+// NPL
+#include "TExogamData.h"
+#include "TTreeReader.h"
+#include "TTreeReaderValue.h"
+#include "TTreeReaderArray.h"
+#include "RootInput.h"
+#include "RootOutput.h"
+#include "TTree.h"
+#include "NPVTreeReader.h"
+
+
+using namespace std;
+
+
+class TExogamPhysicsReader : public NPL::VTreeReader {
+public:
+  TExogamPhysicsReader();
+  ~TExogamPhysicsReader(){};
+  
+public:
+  void r_SetTreeReader(TTreeReader* TreeReader);
+private:
+  TTreeReader *dummy = new TTreeReader();
+public:
+  TTreeReaderValue<TExogamData>* r_ReaderEventData = new TTreeReaderValue<TExogamData>(*dummy,"");
+
+public:
+  ClassDef(TExogamPhysicsReader,0);
+
+};
+
+
+#endif
diff --git a/NPLib/Detectors/TAC/CMakeLists.txt b/NPLib/Detectors/TAC/CMakeLists.txt
index c52326331..52630eff9 100644
--- a/NPLib/Detectors/TAC/CMakeLists.txt
+++ b/NPLib/Detectors/TAC/CMakeLists.txt
@@ -1,6 +1,7 @@
 add_custom_command(OUTPUT TTACPhysicsDict.cxx COMMAND ../../scripts/build_dict.sh TTACPhysics.h TTACPhysicsDict.cxx TTACPhysics.rootmap libNPTAC.dylib DEPENDS TTACPhysics.h)
 add_custom_command(OUTPUT TTACDataDict.cxx COMMAND ../../scripts/build_dict.sh TTACData.h TTACDataDict.cxx TTACData.rootmap libNPTAC.dylib DEPENDS TTACData.h)
-add_library(NPTAC SHARED TTACSpectra.cxx TTACData.cxx TTACPhysics.cxx TTACDataDict.cxx TTACPhysicsDict.cxx )
-target_link_libraries(NPTAC ${ROOT_LIBRARIES} NPCore) 
-install(FILES TTACData.h TTACPhysics.h TTACSpectra.h DESTINATION ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
+add_custom_command(OUTPUT TTACPhysicsReaderDict.cxx COMMAND ${CMAKE_BINARY_DIR}/scripts/build_dict.sh TTACPhysicsReader.h TTACPhysicsReaderDict.cxx TTACPhysicsReader.rootmap libNPTAC.dylib DEPENDS TTACPhysicsReader.h)
+add_library(NPTAC SHARED TTACSpectra.cxx TTACData.cxx TTACPhysics.cxx TTACPhysicsReader.cxx TTACDataDict.cxx TTACPhysicsDict.cxx TTACPhysicsReaderDict.cxx)
+target_link_libraries(NPTAC ${ROOT_LIBRARIES} -lTreePlayer -lSpectrum NPCore NPPhysics) 
+install(FILES TTACData.h TTACPhysics.h TTACSpectra.h TTACPhysicsReader.h DESTINATION ${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
 
diff --git a/NPLib/Detectors/TAC/TTACData.h b/NPLib/Detectors/TAC/TTACData.h
index d06d335ac..9f620cb3c 100644
--- a/NPLib/Detectors/TAC/TTACData.h
+++ b/NPLib/Detectors/TAC/TTACData.h
@@ -72,8 +72,9 @@ class TTACData : public TObject {
   //////////////////////    GETTERS    ////////////////////////
   // Energy
   inline unsigned int GetTAC_Mult() const { return fTAC_Time.size(); }
-  inline unsigned int GetTAC_Channel(const unsigned int& i) const { return fTAC_Time[i]; } //!
+  inline unsigned int GetTAC_Time(const unsigned int& i) const { return fTAC_Time[i]; } //!
   inline unsigned int GetTAC_N(const unsigned int& i) const { return fTAC_N[i]; }        //!
+  inline std::string GetTAC_Name(const unsigned int& i) const { return fTAC_Name[i]; }        //!
   inline unsigned long long GetTAC_TS(const unsigned long long& i) const { return fTAC_TS[i]; }    //!
 
   //////////////////////////////////////////////////////////////
diff --git a/NPLib/Detectors/TAC/TTACPhysics.cxx b/NPLib/Detectors/TAC/TTACPhysics.cxx
index 3c4edafd5..130eeb8c4 100644
--- a/NPLib/Detectors/TAC/TTACPhysics.cxx
+++ b/NPLib/Detectors/TAC/TTACPhysics.cxx
@@ -28,6 +28,7 @@
 #include <cmath>
 #include <stdlib.h>
 #include <limits>
+#include <chrono>
 using namespace std;
 
 //   NPL
@@ -48,8 +49,7 @@ TTACPhysics::TTACPhysics()
      m_PreTreatedData(new TTACData),
      m_EventPhysics(this),
      m_Spectra(0),
-     m_E_RAW_Threshold(0), // adc channels
-     m_E_Threshold(0),     // MeV
+     m_TAC_Time_RAW_Threshold(0), // adc channels
      m_NumberOfDetectors(0) {
 }
 
@@ -72,17 +72,45 @@ void TTACPhysics::BuildSimplePhysicalEvent() {
 ///////////////////////////////////////////////////////////////////////////
 void TTACPhysics::BuildPhysicalEvent() {
   // apply thresholds and calibration
+  // auto start = std::chrono::high_resolution_clock::now();
+  if (NPOptionManager::getInstance()->IsReader() == true) {
+    m_EventData = &(**r_ReaderEventData);
+  }
+  // auto stop = std::chrono::high_resolution_clock::now();
+  // auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
+ 
+  // cout << "Time taken by function: "
+      //  << duration.count() << " microseconds" << endl;
+ 
+ 
+ 
   PreTreat();
-
+  Match_TAC();
 }
 
 ///////////////////////////////////////////////////////////////////////////
 void TTACPhysics::PreTreat() {
   ClearPreTreatedData();
+  m_TAC_Mult = m_EventData->GetTAC_Mult();
+  for (unsigned int i = 0; i < m_TAC_Mult; ++i) {
+    if (m_EventData->GetTAC_Time(i) > m_TAC_Time_RAW_Threshold) {
+        m_PreTreatedData->SetTAC(m_EventData->GetTAC_N(i), m_EventData->GetTAC_Time(i), m_EventData->GetTAC_TS(i), m_EventData->GetTAC_Name(i));
+    }
+  }
 
 }
 
 
+void TTACPhysics::Match_TAC(){
+  for(unsigned int i = 0; i < m_PreTreatedData->GetTAC_Mult(); i++){
+    SortTAC[m_PreTreatedData->GetTAC_Name(i)] = std::make_pair(m_PreTreatedData->GetTAC_Time(i),m_PreTreatedData->GetTAC_TS(i));
+  }
+  for(auto it = SortTAC.begin(); it != SortTAC.end(); ++it){
+  TAC_Name.push_back(it->first);
+  TAC_Time.push_back((it->second).first);
+  TAC_TS.push_back((it->second).second);
+  }
+}
 
 ///////////////////////////////////////////////////////////////////////////
 void TTACPhysics::ReadAnalysisConfig() {
@@ -92,6 +120,10 @@ void TTACPhysics::ReadAnalysisConfig() {
 
 ///////////////////////////////////////////////////////////////////////////
 void TTACPhysics::Clear() {
+  SortTAC.clear();
+  TAC_Name.clear();
+  TAC_Time.clear();
+  TAC_TS.clear();
 }
 
 
@@ -155,17 +187,35 @@ void TTACPhysics::AddParameterToCalibrationManager() {
 
 ///////////////////////////////////////////////////////////////////////////
 void TTACPhysics::InitializeRootInputRaw() {
+  TChain* inputChain = RootInput::getInstance()->GetChain();
+  // Option to use the nptreereader anaysis
+  if (NPOptionManager::getInstance()->IsReader() == true) {
+    TTreeReader* inputTreeReader = RootInput::getInstance()->GetTreeReader();
+    inputTreeReader->SetTree(inputChain);
+  }
+  // Option to use the standard npanalysis
+  else{
   TChain* inputChain = RootInput::getInstance()->GetChain();
   inputChain->SetBranchStatus("TAC",  true );
   inputChain->SetBranchAddress("TAC", &m_EventData );
+  }
 }
 
 
 
 ///////////////////////////////////////////////////////////////////////////
 void TTACPhysics::InitializeRootInputPhysics() {
+  TChain* inputChain = RootInput::getInstance()->GetChain();
+  // Option to use the nptreereader anaysis
+  if (NPOptionManager::getInstance()->IsReader() == true) {
+    TTreeReader* inputTreeReader = RootInput::getInstance()->GetTreeReader();
+    inputTreeReader->SetTree(inputChain);
+  }
+  // Option to use the standard npanalysis
+  else{
   TChain* inputChain = RootInput::getInstance()->GetChain();
   inputChain->SetBranchAddress("TAC", &m_EventPhysics);
+  }
 }
 
 
@@ -176,6 +226,9 @@ void TTACPhysics::InitializeRootOutput() {
   outputTree->Branch("TAC", "TTACPhysics", &m_EventPhysics);
 }
 
+void TTACPhysics::SetTreeReader(TTreeReader* TreeReader) {
+   TTACPhysicsReader::r_SetTreeReader(TreeReader);
+ }
 
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -185,6 +238,8 @@ NPL::VDetector* TTACPhysics::Construct() {
   return (NPL::VDetector*) new TTACPhysics();
 }
 
+NPL::VTreeReader* TTACPhysics::ConstructReader() { return (NPL::VTreeReader*)new TTACPhysicsReader(); }
+
 
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -196,6 +251,7 @@ class proxy_TAC{
     proxy_TAC(){
       NPL::DetectorFactory::getInstance()->AddToken("TAC","TAC");
       NPL::DetectorFactory::getInstance()->AddDetector("TAC",TTACPhysics::Construct);
+      NPL::DetectorFactory::getInstance()->AddDetectorReader("TAC", TTACPhysics::ConstructReader);
     }
 };
 
diff --git a/NPLib/Detectors/TAC/TTACPhysics.h b/NPLib/Detectors/TAC/TTACPhysics.h
index 64d231693..2d8974c7a 100644
--- a/NPLib/Detectors/TAC/TTACPhysics.h
+++ b/NPLib/Detectors/TAC/TTACPhysics.h
@@ -35,6 +35,7 @@ using namespace std;
 // NPTool headers
 #include "TTACData.h"
 #include "TTACSpectra.h"
+#include "TTACPhysicsReader.h"
 #include "NPCalibrationManager.h"
 #include "NPVDetector.h"
 #include "NPInputParser.h"
@@ -43,7 +44,7 @@ class TTACSpectra;
 
 
 
-class TTACPhysics : public TObject, public NPL::VDetector {
+class TTACPhysics : public TObject, public NPL::VDetector, public TTACPhysicsReader {
   //////////////////////////////////////////////////////////////
   // constructor and destructor
   public:
@@ -62,9 +63,10 @@ class TTACPhysics : public TObject, public NPL::VDetector {
   // data obtained after BuildPhysicalEvent() and stored in
   // output ROOT file
   public:
-    vector<int>      DetectorNumber;
-    vector<double>   Energy;
-    vector<double>   Time;
+    std::vector<unsigned int> TAC_Time;
+    std::vector<std::string> TAC_Name;
+    std::vector<unsigned long long> TAC_TS;
+
 
   /// A usefull method to bundle all operation to add a detector
   void AddDetector(TVector3 POS, string shape); 
@@ -124,6 +126,8 @@ class TTACPhysics : public TObject, public NPL::VDetector {
 
     // write spectra to ROOT output file
     void WriteSpectra();
+    
+    void SetTreeReader(TTreeReader* TreeReader);
 
 
   //////////////////////////////////////////////////////////////
@@ -132,6 +136,8 @@ class TTACPhysics : public TObject, public NPL::VDetector {
     // remove bad channels, calibrate the data and apply thresholds
     void PreTreat();
 
+    void Match_TAC();
+
     // clear the pre-treated object
     void ClearPreTreatedData()   {m_PreTreatedData->Clear();}
 
@@ -156,8 +162,9 @@ class TTACPhysics : public TObject, public NPL::VDetector {
   // parameters used in the analysis
   private:
     // thresholds
-    double m_E_RAW_Threshold; //!
-    double m_E_Threshold;     //!
+    double m_TAC_Time_RAW_Threshold = 0; //!
+    unsigned int m_TAC_Mult; //!
+    std::map<std::string,std::pair<unsigned int, unsigned long long>> SortTAC;//!
 
   // number of detectors
   private:
@@ -174,6 +181,7 @@ class TTACPhysics : public TObject, public NPL::VDetector {
   // Static constructor to be passed to the Detector Factory
   public:
     static NPL::VDetector* Construct();
+    static NPL::VTreeReader* ConstructReader();
 
     ClassDef(TTACPhysics,1)  // TACPhysics structure
 };
diff --git a/NPLib/Detectors/TAC/TTACPhysicsReader.cxx b/NPLib/Detectors/TAC/TTACPhysicsReader.cxx
new file mode 100644
index 000000000..8cf48f5b8
--- /dev/null
+++ b/NPLib/Detectors/TAC/TTACPhysicsReader.cxx
@@ -0,0 +1,33 @@
+/*****************************************************************************
+ * Copyright (C) 2009-2016   this file is part of the NPTool Project         *
+ *                                                                           *
+ * For the licensing terms see $NPTOOL/Licence/NPTool_Licence                *
+ * For the list of contributors see $NPTOOL/Licence/Contributors             *
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Original Author: Adrien MATTA  contact address: a.matta@surrey.ac.uk      *
+ *                                                                           *
+ * Creation Date  : febuary 2009                                             *
+ * Last update    : July 2021
+ *---------------------------------------------------------------------------*
+ * Decription:                                                               *
+ *  This class hold must2 TreeReader                                        *
+ *                                                                           *
+ *---------------------------------------------------------------------------*
+ * Comment:                                                                  *
+ *                                                                           *
+ *****************************************************************************/
+#include "TTACPhysicsReader.h"
+#include "NPDetectorFactory.h"
+///////////////////////////////////////////////////////////////////////////
+
+ClassImp(TTACPhysicsReader)
+
+TTACPhysicsReader::TTACPhysicsReader()
+{
+};
+
+void TTACPhysicsReader::r_SetTreeReader(TTreeReader* TreeReader){
+r_ReaderEventData = new TTreeReaderValue<TTACData>(*TreeReader,"TAC");
+}; 
\ No newline at end of file
diff --git a/NPLib/Detectors/TAC/TTACPhysicsReader.h b/NPLib/Detectors/TAC/TTACPhysicsReader.h
new file mode 100644
index 000000000..6e4b6e3ff
--- /dev/null
+++ b/NPLib/Detectors/TAC/TTACPhysicsReader.h
@@ -0,0 +1,60 @@
+#ifndef TTACPHYSICSREADER_H
+#define TTACPHYSICSREADER_H
+/*****************************************************************************
+ * Copyright (C) 2009-2016    this file is part of the NPTool Project        *
+ *                                                                           *
+ * For the licensing terms see $NPTOOL/Licence/NPTool_Licence                *
+ * For the list of contributors see $NPTOOL/Licence/Contributors             *
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Original Author: Hugo Jacob  contact address: hjacob@ijclab.in2p3.fr      *
+ *                                                                           *
+ * Creation Date  : 2023                                                     *
+ * Last update    : 2023
+ *---------------------------------------------------------------------------*
+ * Decription:                                                               *
+ *  TTreader class for TAC Physics                                           *
+ *                                                                           *
+ *---------------------------------------------------------------------------*
+ * Comment:                                                                  *
+ *                                                                           *
+ *                                                                           *
+ *                                                                           *
+ *****************************************************************************/
+// STL
+#include <map>
+#include <vector>
+// NPL
+#include "TTACData.h"
+#include "TTreeReader.h"
+#include "TTreeReaderValue.h"
+#include "TTreeReaderArray.h"
+#include "RootInput.h"
+#include "RootOutput.h"
+#include "TTree.h"
+#include "NPVTreeReader.h"
+
+
+using namespace std;
+
+
+class TTACPhysicsReader : public NPL::VTreeReader {
+public:
+  TTACPhysicsReader();
+  ~TTACPhysicsReader(){};
+  
+public:
+  void r_SetTreeReader(TTreeReader* TreeReader);
+private:
+  TTreeReader *dummy = new TTreeReader();
+public:
+  TTreeReaderValue<TTACData>* r_ReaderEventData = new TTreeReaderValue<TTACData>(*dummy,"");
+
+public:
+  ClassDef(TTACPhysicsReader,0);
+
+};
+
+
+#endif
diff --git a/NPLib/Detectors/TAC/TTACSpectra.cxx b/NPLib/Detectors/TAC/TTACSpectra.cxx
index be58ca908..c8b8cda4e 100644
--- a/NPLib/Detectors/TAC/TTACSpectra.cxx
+++ b/NPLib/Detectors/TAC/TTACSpectra.cxx
@@ -165,10 +165,10 @@ void TTACSpectra::FillPhysicsSpectra(TTACPhysics* Physics) {
   family= "TAC/PHY";
 
   // Energy vs time
-  unsigned int sizeE = Physics->Energy.size();
-  for(unsigned int i = 0 ; i < sizeE ; i++){
-    name = "TAC_ENERGY_TIME";
-    FillSpectra(family,name,Physics->Energy[i],Physics->Time[i]);
-  }
+  //unsigned int sizeE = Physics->.size();
+  //for(unsigned int i = 0 ; i < sizeE ; i++){
+  //  name = "TAC_ENERGY_TIME";
+  //  FillSpectra(family,name,Physics->Energy[i],Physics->Time[i]);
+  //}
 }
 
diff --git a/NPLib/Detectors/ZDD/TZDDPhysics.cxx b/NPLib/Detectors/ZDD/TZDDPhysics.cxx
index da445a541..9574a1b09 100644
--- a/NPLib/Detectors/ZDD/TZDDPhysics.cxx
+++ b/NPLib/Detectors/ZDD/TZDDPhysics.cxx
@@ -80,7 +80,8 @@ void TZDDPhysics::BuildPhysicalEvent() {
 
   // match energy and time together
   Match_IC();
-  // Match_E_T("Plastic");
+  if(IC_Nbr.size() > 0)
+    Match_PL();
   // Treat_DC();
 }
 
@@ -115,6 +116,7 @@ void TZDDPhysics::Match_IC(){
     }
   }
 }
+
 void TZDDPhysics::Match_IC1(){
 //////////////////////////////// Currently Simply matching if mult = 5, could be improved to treat mult > 5
     // CHecking that each IC is only encountered once and then sorting them in the right order with the map
@@ -132,20 +134,17 @@ void TZDDPhysics::Match_IC1(){
     IC_TS.push_back((it->second).second);
     }
 }
-//// FIXME
-//bool TZDDPhysics::fSortIC(int i, int j){
-//  //bool res = i < j;
-//  //if(res){
-//  //  double tempE = IC_E[i];
-//  //  double tempTS = IC_TS[i];
-//  //  IC_E[i] = IC_E[j];
-//  //  IC_TS[i] = IC_TS[j];
-//  //  IC_E[j] = tempE;
-//  //  IC_TS[j] = tempTS;
-//  //}
-//  return i < j;
-//}
 
+void TZDDPhysics::Match_PL(){
+  for(unsigned int i = 0; i < m_PreTreatedData->GetZDD_PLMult(); i++){
+    SortPL[m_PreTreatedData->GetZDD_PLN(i)] = std::make_pair(m_PreTreatedData->GetZDD_PLE(i),m_PreTreatedData->GetZDD_PLTS(i));
+  }
+  for(auto it = SortPL.begin(); it != SortPL.end(); ++it){
+  PL_Nbr.push_back(it->first);
+  PL_E.push_back((it->second).first);
+  PL_TS.push_back((it->second).second);
+  }
+}
 ///////////////////////////////////////////////////////////////////////////
 void TZDDPhysics::PreTreat() {
   // This method typically applies thresholds and calibrations
@@ -308,12 +307,13 @@ void TZDDPhysics::ReadAnalysisConfig() {
 void TZDDPhysics::Clear() {
   ICSum = 0;
   SortIC.clear();
+  SortPL.clear();
   IC_Nbr.clear();
   IC_E.clear();
   IC_TS.clear();
-  Plastic_DetectorNumber.clear();
-  Plastic_Energy.clear();
-  Plastic_Time.clear();
+  PL_Nbr.clear();
+  PL_E.clear();
+  PL_TS.clear();
   DC_DetectorNumber.clear();
   DC_DriftTime.clear();
 }
diff --git a/NPLib/Detectors/ZDD/TZDDPhysics.h b/NPLib/Detectors/ZDD/TZDDPhysics.h
index be278af2e..2847c289c 100644
--- a/NPLib/Detectors/ZDD/TZDDPhysics.h
+++ b/NPLib/Detectors/ZDD/TZDDPhysics.h
@@ -66,12 +66,12 @@ class TZDDPhysics : public TObject, public NPL::VDetector, public TZDDPhysicsRea
   public:
     vector<int>      IC_Nbr;
     vector<double>   IC_E;
-    vector<double>   IC_TS;
+    vector<unsigned long long>   IC_TS;
     double ICSum;
 
-    vector<int>      Plastic_DetectorNumber;
-    vector<double>   Plastic_Energy;
-    vector<double>   Plastic_Time;
+    vector<int>      PL_Nbr;
+    vector<double>   PL_E;
+    vector<unsigned long long>   PL_TS;
 
     vector<int>      DC_DetectorNumber;
     vector<double>   DC_DriftTime;
@@ -148,6 +148,8 @@ class TZDDPhysics : public TObject, public NPL::VDetector, public TZDDPhysicsRea
     // Matching IC
     void Match_IC();
     void Match_IC1();
+    
+    void Match_PL();
 
     // PreTreating Energy for IC and Plastic 
     void PreTreatEnergy(std::string Detector, CalibrationManager* Cal);
@@ -199,6 +201,7 @@ class TZDDPhysics : public TObject, public NPL::VDetector, public TZDDPhysicsRea
     unsigned int m_EXO_Mult; //!
     std::map<unsigned int, unsigned int> Map_IC;//!
     std::map<unsigned int,std::pair<unsigned int, unsigned long long>> SortIC;//!
+    std::map<unsigned int,std::pair<unsigned int, unsigned long long>> SortPL;//!
 
   private:
     int m_NumberOfDetectors;  //!
diff --git a/NPLib/Detectors/ZDD/TZDDSpectra.cxx b/NPLib/Detectors/ZDD/TZDDSpectra.cxx
index f81ca2796..4b78715eb 100644
--- a/NPLib/Detectors/ZDD/TZDDSpectra.cxx
+++ b/NPLib/Detectors/ZDD/TZDDSpectra.cxx
@@ -168,10 +168,10 @@ void TZDDSpectra::FillPhysicsSpectra(TZDDPhysics* Physics) {
   family= "ZDD/PHY";
 
   // Energy vs time
-  unsigned int sizeE = Physics->Plastic_Energy.size();
+  unsigned int sizeE = Physics->PL_E.size();
   for(unsigned int i = 0 ; i < sizeE ; i++){
     name = "ZDD_ENERGY_TIME";
-    FillSpectra(family,name,Physics->Plastic_Energy[i],Physics->Plastic_Time[i]);
+    FillSpectra(family,name,Physics->PL_E[i],Physics->PL_TS[i]);
   }
 }
 
diff --git a/NPLib/Utility/npreader.cxx b/NPLib/Utility/npreader.cxx
index 4803acfc1..ed1402777 100644
--- a/NPLib/Utility/npreader.cxx
+++ b/NPLib/Utility/npreader.cxx
@@ -187,7 +187,6 @@ int main(int argc , char** argv){
       while(inputTreeReader->Next()){
         
         // Build the current event
-          // std::cout << "test" << std::endl;
         if(UserAnalysis->UnallocateBeforeBuild()){
           myDetector->BuildPhysicalEvent();
           // User Analysis;
diff --git a/Projects/E805/Analysis.cxx b/Projects/E805/Analysis.cxx
index 63a1228cb..22906dc70 100755
--- a/Projects/E805/Analysis.cxx
+++ b/Projects/E805/Analysis.cxx
@@ -35,7 +35,7 @@ Analysis::~Analysis(){
 void Analysis::Init(){
   InitInputBranch();
   InitOutputBranch();
-  CATS = (TCATSPhysics*)  m_DetectorManager -> GetDetector("CATSDetector");
+  // CATS = (TCATSPhysics*)  m_DetectorManager -> GetDetector("CATSDetector");
   M2 = (TMust2Physics*)  m_DetectorManager -> GetDetector("M2Telescope");
   // 
   //reaction->ReadConfigurationFile(NPOptionManager::getInstance()->GetReactionFile());
@@ -76,8 +76,6 @@ void Analysis::Init(){
 bool Analysis::UnallocateBeforeBuild(){
   // return true;
   GATCONFMASTER = **GATCONFMASTER_;
-  if(GATCONFMASTER.size() > 1)
-  std::cout << GATCONFMASTER.size() << std::endl;
   return (GATCONFMASTER.size() == 1 && GATCONFMASTER[0] > 0); 
   //return true;
 }
-- 
GitLab