From aadf14476fd7490b87702af8716f62e48d042fd0 Mon Sep 17 00:00:00 2001
From: moukaddam <mhd.moukaddam@gmail.com>
Date: Sun, 4 Dec 2016 17:46:34 +0000
Subject: [PATCH] Updating FPDTamu Physics class

---
 NPLib/Detectors/FPDTamu/TFPDTamuPhysics.cxx | 470 +++++++++++++++++---
 NPLib/Detectors/FPDTamu/TFPDTamuPhysics.h   |  23 +-
 2 files changed, 422 insertions(+), 71 deletions(-)

diff --git a/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.cxx b/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.cxx
index baa784868..3f8978064 100644
--- a/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.cxx
+++ b/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.cxx
@@ -35,6 +35,7 @@ using namespace std;
 #include "RootInput.h"
 #include "RootOutput.h"
 #include "NPDetectorFactory.h"
+#include "NPSystemOfUnits.h"
 
 //   ROOT
 #include "TChain.h"
@@ -82,18 +83,22 @@ void TFPDTamuPhysics::BuildPhysicalEvent() {
   //Micro
   // match the energy and time together (not implemented yet) and fill the vectors    
   mysizeE = m_PreTreatedData->Get_Micro_Energy_Mult();
+
   for (UShort_t e = 0; e < mysizeE ; e++) {
         MicroRowNumber.push_back(m_PreTreatedData->Get_Micro_E_RowNbr(e));
         MicroColNumber.push_back(m_PreTreatedData->Get_Micro_E_ColNbr(e));
         //Calculate position in X and Z for each of the pads
-        MicroPositionX.push_back((m_PreTreatedData->Get_Micro_E_ColNbr(e)-3)*0.20); // 0.20 is the Col pitch
-        MicroPositionZ.push_back((m_PreTreatedData->Get_Micro_E_RowNbr(e)-3)*0.50); // 0.20 is the Col pitch
+        int col = m_PreTreatedData->Get_Micro_E_ColNbr(e) ;
+        int row = m_PreTreatedData->Get_Micro_E_RowNbr(e) ; 
+        int det = row/4; // 4 rows in the detector
+        MicroPositionX.push_back(((col-3)*44.0)/NPUNITS::cm); // 0.20 is the Col pitch
+        MicroPositionZ.push_back((((row+0.5)*32.5)+MicroLeftPos[det].Z())/NPUNITS::cm);
         //Pass the corresponding Energy, Charge 
         MicroEnergy.push_back(m_PreTreatedData->Get_Micro_Energy(e)); //calibrated
         MicroCharge.push_back(m_EventData->Get_Micro_Energy(e)); //uncalibrated
       }    
 
-  //AWire
+   //AWire
   //separate Left and right detectors 
   vector<double> awireLeftDetNumber, awireRightDetNumber;
   vector<double> awireLeftCharge, awireRightCharge;
@@ -128,18 +133,18 @@ void TFPDTamuPhysics::BuildPhysicalEvent() {
           AWireLeftCharge.push_back(EnergyL);
           AWireRightCharge.push_back(EnergyR);
           // calculate position in X and Z
-          double wire_length = 1.0; //check me!
-          AWirePositionX.push_back(wire_length*(EnergyL-EnergyR)/(EnergyL+EnergyR)); //check me!
-          AWirePositionZ.push_back(det*10+0.5); //check me!, or directly from configuration
+          double wire_length = 2*fabs(AWireLeftPos[det].X())/NPUNITS::cm; 
+          AWirePositionX.push_back(wire_length*(EnergyL-EnergyR)/(EnergyL+EnergyR));
+          AWirePositionZ.push_back(AWireLeftPos[det].Z()/NPUNITS::cm);
         }
       }
     }
   // Calculate beam direction from X and Z position //check me!
-    double a = 1, b = 0, Zplast = 30; // X = aZ + b // check me!
-    BeamDirection.SetXYZ(1,0,a); //(1,a) is the direction vector in the plane X,Z. no information about Y 
-    BeamDirection.Unit();  
+    double a = 1, b = 0, Zplast = +547; // X = aZ + b // check me!
+    IonDirection.SetXYZ(1,0,a); //(1,a) is the direction vector in the plane X,Z. no information about Y 
+    IonDirection.Unit();  
   //Calculate position on Plastic from AWire data provided Z of the Plastic
-    PositionOnPlasticX = a*Zplast + b;
+    PlastPositionX_AW = a*Zplast + b;
     //cout << " AWire Left and Right sizes are in agreement,  L: " << mysizeL << "  R: "<< mysizeR<<endl;
     //m_PreTreatedData->Dump();
     //cin.get();
@@ -150,24 +155,29 @@ void TFPDTamuPhysics::BuildPhysicalEvent() {
     //cin.get();
   }
 
-  //Plast
+  //Plastic
   //separate Left and right detectors 
   vector<double> plastLeftCharge, plastRightCharge;
   mysizeE = m_PreTreatedData->Get_Plast_Energy_Mult();
   for (UShort_t e = 0; e < mysizeE ; e++) {
+    //collect info
     int side = m_PreTreatedData->Get_Plast_E_DetectorSide(e);
     double charge = m_PreTreatedData->Get_Plast_Energy(e);
-    if (side==1) { 
+    
+    // skip values lower than a certain threshold
+    if (charge<100) 
+      continue;  
+    
+    //redistribute
+    if (side==1)
       plastRightCharge.push_back(charge);
-    }
-    else {
+    else
       plastLeftCharge.push_back(charge);
-    }
-  }   
+  }  
+
   // match the left and right
   mysizeL = plastLeftCharge.size();
   mysizeR = plastRightCharge.size();
-  //cout << " " << mysizeL << " " << mysizeR << endl ;
   if (mysizeL==mysizeR){ 
     for (UShort_t l = 0; l < mysizeL ; l++) {
       for (UShort_t r = 0; r < mysizeR ; r++) {
@@ -177,9 +187,10 @@ void TFPDTamuPhysics::BuildPhysicalEvent() {
           PlastLeftCharge.push_back(EnergyL);
           PlastRightCharge.push_back(EnergyR);
           // calculate position in X, Z is known
-          double plast_length = 1.0; //check me!
+          double plast_length = 2*PlastLeftPos.X()/NPUNITS::cm; //check me!
+          PlastCharge.push_back(sqrt(EnergyL*EnergyR));
           PlastPositionX.push_back(plast_length*(EnergyL-EnergyR)/(EnergyL+EnergyR));
-          PlastPositionZ.push_back(99); //check me!, directly from configuration
+          PlastPositionZ.push_back(PlastLeftPos.Z()/NPUNITS::cm); //check me!, directly from configuration
       }
     }
   }
@@ -207,6 +218,64 @@ void TFPDTamuPhysics::BuildPhysicalEvent() {
 
 }
 
+double TFPDTamuPhysics::GetMicroGroupEnergy(int lrow, int hrow, int lcol, int hcol){
+
+  int dummy,row,col; 
+  double energy = 0;  
+  
+  //avoid zeros
+  if (lrow==0 || hrow==0 || lcol==0 || hcol)
+    cout << " \033[1;311mWARNING: '0' value detected, TFPDTamuPhysics::GetMicroGroupEnergy() uses values >=1 " << endl;
+  //check validity
+  if (lrow>hrow) { 
+    dummy = lrow;
+    lrow = hrow;
+    hrow = dummy;
+  }
+  if (lcol>hcol) { 
+    dummy = lcol;
+    lcol = hcol;
+    hcol = dummy;
+  }
+
+  // group energies
+  for (UShort_t e = 0; e < MicroRowNumber.size() ; e++) {
+    row = MicroRowNumber.at(e)+1;
+    col = MicroColNumber.at(e)+1;
+    if ( (row>=lrow && row<=hrow) && (col>=lcol && col<=hcol) )
+      energy += MicroEnergy.at(e);
+  } 
+
+  return energy ; 
+}
+
+double TFPDTamuPhysics::GetMicroRowGeomEnergy(int lrow, int hrow){
+
+  int dummy; 
+  double energy = 0;
+  int sample = 0;   
+  
+  //avoid zeros
+  if (lrow==0 || hrow==0 )
+    cout << " \033[1;311mWARNING: '0' value detected, TFPDTamuPhysics::GetMicroRowGeomEnergy() uses values >=1 " << endl;
+  //check validity
+  if (lrow>hrow) { 
+    dummy = lrow;
+    lrow = hrow;
+    hrow = dummy;
+  }
+  // group energies
+  for (int r = lrow; r < hrow ; r++) {
+      double esample = GetMicroGroupEnergy(r,r,1,7);
+      if( esample > 0 ){
+        sample++;  
+        energy *= esample ;
+        }
+  } 
+
+  return pow(energy,1./sample) ; 
+}
+
 ///////////////////////////////////////////////////////////////////////////
 void TFPDTamuPhysics::PreTreat() {
   // This method typically applies thresholds and calibrations
@@ -429,16 +498,110 @@ void TFPDTamuPhysics::Clear() {
   AWirePositionX.clear();
   AWirePositionZ.clear();
   //Plastic scintillator
-  PlastDetNumber.clear();
   PlastLeftCharge.clear();
   PlastRightCharge.clear();
+  PlastCharge.clear();
   PlastPositionX.clear();
   PlastPositionZ.clear();
   PlastTime.clear();
-  //Calculated AWire and Plastic
-  PositionOnPlasticX = -99 ; 
-  BeamDirection.SetXYZ(0,0,0);
+
+  //Calculated 
+  PlastPositionX_AW = -99 ; //from AWire and Plastic Z
+  IonDirection.SetXYZ(0,0,0); // from AWire
+  
+}
+
+//////////////////////////////////////////////////////////////////////
+void TFPDTamuPhysics::Dump() const {
+  // This method is very useful for debuging and worth the dev.
+  cout << "XXXXXXXXXXXXXXXXXXXXXXXX New Event [TFPDTamuPhysics::Dump()] XXXXXXXXXXXXXXXXX" << endl;
+
+  cout << "  ...oooOOOooo...   Delta  ...oooOOOooo...   " << endl;
+  // Energy
+  size_t mysize = DeltaEnergy.size();
+  cout << "E Mult: " << mysize << endl;
+  for (size_t i = 0 ; i < mysize ; i++){
+    cout << "DetNbr: " << DeltaDetNumber[i]
+         << " Charge: " << DeltaCharge[i]
+         << " Energy: " << DeltaEnergy[i]
+         <<endl;
+  }
+  // Time
+  mysize = DeltaTime.size();
+  cout << "T Mult: " << mysize << endl;
+  for (size_t i = 0 ; i < mysize ; i++){
+    cout << "DetNbr: " << DeltaDetNumber[i]
+         << " Time: " << DeltaTime[i]
+         <<endl;
+  }
+
+ cout << "  ...oooOOOooo...   Avalanche Wire  ...oooOOOooo...   " << endl;
+  // Energy
+  mysize = AWireLeftCharge.size();
+  cout << "Charge Mult: " << mysize << endl;
+  for (size_t i = 0 ; i < mysize ; i++){
+    cout << "DetNbr: " << AWireDetNumber[i]
+         << " Left: " << AWireLeftCharge[i]
+         << " Right: " << AWireRightCharge[i]
+         << " XPos: " << AWirePositionX[i]
+         << " ZPos: " << AWirePositionZ[i]
+         <<endl;
+  }
+
+ cout << "  ...oooOOOooo...   Micromega  ...oooOOOooo...   " << endl;
+  // Energy
+  mysize = MicroRowNumber.size();
+  cout << " Row Charge:"  
+  for (size_t i = 0 ; i < MicroRowNumber.size() ; i++)
+    cout << " " << MicroRowNumber[i];
+  cout<<endl;
+    cout << " Col Charge:"  
+  for (size_t i = 0 ; i < MicroColNumber.size() ; i++)
+    cout << " " << MicroRowNumber[i];
+  cout<<endl;
+      cout << " energy: "<<endl; 
+  for (size_t i = 0 ; i < MicroColNumber.size() ; i++)
+    cout << " " << MicroRowNumber[i];
+  cout<<endl;
+        cout << " energy: "<< energy << endl;
+  for (size_t i = 0 ; i < MicroColNumber.size() ; i++)
+    cout << " " << MicroRowNumber[i];
+  cout<<endl;                                                                                                                                                                                                                                                                                                                                                                             
+    cout << " Charge: " << AWireRightCharge[i]
+    cout << " Energy: " << AWirePositionX[i]
+    cout << " XPos: " << AWirePositionX[i]
+    cout << " ZPos: " << AWirePositionZ[i]
+         <<endl;
+  }
+
+ cout << "  ...oooOOOooo...   Plastic Scintillator  ...oooOOOooo...   " << endl;
+    // Energy
+  cout << " Left Charge:"  
+  for (size_t i = 0 ; i < PlastLeftCharge.size() ; i++)
+    cout << " " << PlastLeftCharge[i];
+  cout<<endl;
+  
+  cout << " Right Charge:"  
+  for (size_t i = 0 ; i < PlastRightCharge.size() ; i++)
+    cout << " " << PlastRightCharge[i];
+  cout<<endl;
   
+  cout << " total Charge:"  
+  for (size_t i = 0 ; i < PlastCharge.size() ; i++)
+    cout << " " << PlastCharge[i];
+  cout<<endl;
+
+  cout << " XPos:"  
+  for (size_t i = 0 ; i < PlastPositionX.size() ; i++)
+    cout << " " << PlastPositionX[i];
+  cout<<endl;
+
+  cout << " ZPos:"  
+  for (size_t i = 0 ; i < PlastPositionZ.size() ; i++)
+    cout << " " << PlastPositionZ[i];
+  cout<<endl;
+
+  }
 }
 
 
@@ -451,7 +614,7 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
   string DataBuffer             ;
   
   double X, Y, Z; 
-  TVector3 A , B , C , D;
+  TVector3 A(-99,-99,-99) , B(-99,-99,-99);
   
   //Check if geometrical position read
   bool check_Delta_USL = false      ;
@@ -460,8 +623,8 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
   bool check_Micro_USR = false      ;
   bool check_AWire_L = false        ;
   bool check_AWire_R = false        ;
-  bool check_Plast_L = false        ;
-  bool check_Plast_R = false        ;
+  bool check_Plast_USL = false        ;
+  bool check_Plast_USR = false        ;
 
   // Check if the sub-detectors are there
   bool check_Delta = false          ;
@@ -480,6 +643,9 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
   //enabling readng several detectors
   bool ReadingStatus = false        ;
   bool ReadingDelta = false         ;
+  bool ReadingMicro = false         ;
+  bool ReadingAWire = false         ;
+  bool ReadingPlast = false         ;
 
   while (!ConfigFile.eof()){
 
@@ -505,14 +671,14 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
       }
 
       //   Finding another telescope (safety), toggle out
-      else if (DataBuffer.compare(0, name.length(), name) == 0) {
+      else 
+        if (DataBuffer.compare(0, name.length(), name) == 0) {
         cout << "\033[1;311mWARNING: Another detector is found before standard sequence of Token, Error may occured in detector definition\033[0m" << endl ;
         ReadingStatus = false ;
       }
 
       //DeltaE, Ionisation chamber
       else if (DataBuffer=="DELTA") {
-
             ReadingDelta = true;
             cout << "____//////" << endl ;
             cout << "____FPD TAMU DeltaE found:  "<< endl;
@@ -540,7 +706,7 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
                   ConfigFile >> DataBuffer ;
                   Z = atof(DataBuffer.c_str()) ;
                   A = TVector3(X, Y, Z);
-                  cout << "____ ____ Upstream left corner position: ( " << A.X() << " ; " << A.Y() 
+                  cout << "          Upstream left corner position: ( " << A.X() << " ; " << A.Y() 
                   << " ; " << A.Z() << " )" << endl;
               }
               //Reading Upstream right point
@@ -553,7 +719,7 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
                   ConfigFile >> DataBuffer ;
                   Z = atof(DataBuffer.c_str()) ;
                   B = TVector3(X, Y, Z);
-                  cout << "____ ____ Upstream right corner position: ( " << B.X() << " ; " << B.Y() 
+                  cout << "          Upstream right corner position: ( " << B.X() << " ; " << B.Y() 
                   << " ; " << B.Z() << " )" << endl;
               }
               //  If All necessary information there, toggle out
@@ -565,34 +731,203 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
                 subname="";
                 check_Delta_USL = false;
                 check_Delta_USR = false;
+                DeltaLeftPos.push_back(A);
+                DeltaRightPos.push_back(B);
                 A.SetXYZ(-99,-99,-99);
                 B.SetXYZ(-99,-99,-99);
-                cout << "____FPD DeltaE configuration successful" << endl ;
+                cout << "          FPD DeltaE configuration successful" << endl ;
                 }
             }// end of while(ReadingDelta)
       }// end of if (DataBuffer=="DELTA")
 
-      else if (DataBuffer=="MICRO") { // Micromega
-        check_Micro = true;
-        //ConfigFile >> DataBuffer ;
-        cout << "____//////" << endl ;
-        cout << "____FPD TAMU Micromega found "<< endl;
-        cout << "____FPD Micromega configuration successful" << endl ;
-      }
-
-      else if (DataBuffer=="AWIRE") { // Avalanche wire
-        check_AWire = true;
-        cout << "____//////" << endl ;
-        cout << "____FPD TAMU Avalanche wire found "<< endl;
-        cout << "____FPD Avalanche wire configuration successful" << endl ;
-      }
+      else 
+        if (DataBuffer=="MICRO") { // Micromega
+
+          ReadingMicro = true;
+          cout << "____//////" << endl ;
+          cout << "____FPD TAMU Micromega found:  "<< endl; 
+          //   Reading Micro Block
+          while(ReadingMicro){
+            ConfigFile >> DataBuffer ;
+            subname="MICRO";
+            //   Comment Line
+            if (DataBuffer.compare(0, 1, "%") == 0) {   
+              ConfigFile.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
+            }
+            //   Finding another telescope (safety), toggle out
+            else if (DataBuffer.compare(0, subname.length(), subname) == 0) {
+              cout << "\033[1;311mWARNING: Another MICRO is found before standard sequence of Token, Error may occured in detector definition\033[0m" << endl ;
+              ReadingMicro = false ;
+            }
+            //Reading Upstream left point
+            else if (DataBuffer=="UPSTREAM-LEFT="){
+                check_Micro_USL = true;
+                ConfigFile >> DataBuffer ;
+                X = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Y = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Z = atof(DataBuffer.c_str()) ;
+                A = TVector3(X, Y, Z);
+                cout << "          Upstream left corner position: ( " << A.X() << " ; " << A.Y() 
+                << " ; " << A.Z() << " )" << endl;
+            }
+            //Reading Upstream right point
+            else if (DataBuffer=="UPSTREAM-RIGHT="){
+                check_Micro_USR = true;
+                ConfigFile >> DataBuffer ;
+                X = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Y = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Z = atof(DataBuffer.c_str()) ;
+                B = TVector3(X, Y, Z);
+                cout << "          Upstream right corner position: ( " << B.X() << " ; " << B.Y() 
+                << " ; " << B.Z() << " )" << endl;
+            }
+            //  If All necessary information there, toggle out
+            if (check_Micro_USL && check_Micro_USR ){ 
+              AddMicro( A, B );
+              check_Micro = true ;
+              //prepare for another Delta if necessary
+              ReadingMicro = false;
+              subname="";
+              check_Micro_USL = false;
+              check_Micro_USR = false;
+              MicroLeftPos.push_back(A);
+              MicroRightPos.push_back(B);
+              A.SetXYZ(-99,-99,-99);
+              B.SetXYZ(-99,-99,-99);
+              cout << "          FPD Micromega configuration successful" << endl ;
+              }
+            }// end of while(ReadingMicro)
+      } //end of if (DataBuffer=="MICRO")
+
+      else if (DataBuffer=="AWIRE") { // Micromega
+
+          ReadingAWire = true;
+          cout << "____//////" << endl ;
+          cout << "____FPD TAMU Avalanche Wire found:  "<< endl; 
+          //   Reading AWire Block
+          while(ReadingAWire){
+            ConfigFile >> DataBuffer ;
+            subname="AWIRE";
+            //   Comment Line
+            if (DataBuffer.compare(0, 1, "%") == 0) {   
+              ConfigFile.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
+            }
+            //   Finding another telescope (safety), toggle out
+            else if (DataBuffer.compare(0, subname.length(), subname) == 0) {
+              cout << "\033[1;311mWARNING: Another AWIRE is found before standard sequence of Token, Error may occured in detector definition\033[0m" << endl ;
+              ReadingAWire = false ;
+            }
+            //Reading Upstream left point
+            else if (DataBuffer=="LEFT="){
+                check_AWire_L = true;
+                ConfigFile >> DataBuffer ;
+                X = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Y = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Z = atof(DataBuffer.c_str()) ;
+                A = TVector3(X, Y, Z);
+                cout << "           left end position: ( " << A.X() << " ; " << A.Y() 
+                << " ; " << A.Z() << " )" << endl;
+            }
+            //Reading Upstream right point
+            else if (DataBuffer=="RIGHT="){
+                check_AWire_R = true;
+                ConfigFile >> DataBuffer ;
+                X = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Y = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Z = atof(DataBuffer.c_str()) ;
+                B = TVector3(X, Y, Z);
+                cout << "           right end position: ( " << B.X() << " ; " << B.Y() 
+                << " ; " << B.Z() << " )" << endl;
+            }
+            //  If All necessary information there, toggle out
+            if (check_AWire_L && check_AWire_R ){ 
+              AddAWire( A, B );
+              check_AWire = true ;
+              //prepare for another Delta if necessary
+              ReadingAWire = false;
+              subname="";
+              check_AWire_L = false;
+              check_AWire_R = false;
+              AWireLeftPos.push_back(A);
+              AWireRightPos.push_back(B);
+              A.SetXYZ(-99,-99,-99);
+              B.SetXYZ(-99,-99,-99);
+              cout << "          FPD Avalanche Wire configuration successful" << endl ;
+              }
+            }// end of while(ReadingAwire)
+      } //end of if (DataBuffer=="AWIRE")
+
+      else if (DataBuffer=="PLAST") { // Micromega
+
+          ReadingPlast = true;
+          cout << "____//////" << endl ;
+          cout << "____FPD TAMU Plastic found:  "<< endl; 
+          //   Reading Plast Block
+          while(ReadingPlast){
+            ConfigFile >> DataBuffer ;
+            subname="PLAST";
+            //   Comment Line
+            if (DataBuffer.compare(0, 1, "%") == 0) {   
+              ConfigFile.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
+            }
+            //   Finding another telescope (safety), toggle out
+            else if (DataBuffer.compare(0, subname.length(), subname) == 0) {
+              cout << "\033[1;311mWARNING: Another PLAST is found before standard sequence of Token, Only one plastic is allowed\033[0m" << endl ;
+              exit(-1);
+              ReadingPlast = false ;
+            }
+            //Reading Upstream left point
+            else if (DataBuffer=="UPSTREAM-LEFT="){
+                check_Plast_USL = true;
+                ConfigFile >> DataBuffer ;
+                X = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Y = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Z = atof(DataBuffer.c_str()) ;
+                A = TVector3(X, Y, Z);
+                cout << "           left end position: ( " << A.X() << " ; " << A.Y() 
+                << " ; " << A.Z() << " )" << endl;
+            }
+            //Reading Upstream right point
+            else if (DataBuffer=="UPSTREAM-RIGHT="){
+                check_Plast_USR = true;
+                ConfigFile >> DataBuffer ;
+                X = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Y = atof(DataBuffer.c_str()) ;
+                ConfigFile >> DataBuffer ;
+                Z = atof(DataBuffer.c_str()) ;
+                B = TVector3(X, Y, Z);
+                cout << "           right end position: ( " << B.X() << " ; " << B.Y() 
+                << " ; " << B.Z() << " )" << endl;
+            }
+            //  If All necessary information there, toggle out
+            if (check_Plast_USL && check_Plast_USR ){ 
+              AddPlast( A, B );
+              check_Plast = true ;
+              //prepare for another Delta if necessary
+              ReadingPlast = false;
+              subname="";
+              check_Plast_USL = false;
+              check_Plast_USR = false;
+              PlastLeftPos=A;
+              PlastRightPos=B;
+              A.SetXYZ(-99,-99,-99);
+              B.SetXYZ(-99,-99,-99);
+              cout << "          FPD Plastic configuration successful" << endl ;
+              }
+            }// end of while(ReadingPlast)
+      } //end of if (DataBuffer=="PLAST")
 
-      else if (DataBuffer=="PLAST") { // Plastique
-        check_Plast = true;
-        cout << "____//////" << endl ;
-        cout << "____FPD TAMU Plastic scintillator found "<< endl;
-        cout << "____FPD Plastic scintillator configuration successful" << endl ;
-      }
       /*
       //Angle method
       else if (DataBuffer=="THETA=") {
@@ -650,7 +985,7 @@ void TFPDTamuPhysics::ReadConfiguration(string Path) {
       //   If All necessary information there, toggle out
 
       if ( (check_Delta && check_Micro && check_AWire && check_Plast) ){
-        m_NumberOfDetectors++;
+        m_NumberOfDetectors++; // Number of FPD is always one!
         //   Reinitialisation of Check Boolean
         check_Theta = false          ;
         check_Phi  = false           ;
@@ -738,20 +1073,19 @@ void TFPDTamuPhysics::AddParameterToCalibrationManager() {
 
   CalibrationManager* Cal = CalibrationManager::getInstance();
   for (int i = 0; i < m_NumberOfDelta; ++i) {
-    cout << "Adding parameters " << m_NumberOfDelta << endl; 
     Cal->AddParameter("FPDTamu", "Delta_R"+ NPL::itoa(i+1)+"_C1_E",
                                  "Delta_R"+ NPL::itoa(i+1)+"_C1_E");
     Cal->AddParameter("FPDTamu", "Delta_R"+ NPL::itoa(i+1)+"_C1_T",
                                  "Delta_R"+ NPL::itoa(i+1)+"_C1_T");
   }
 
-  for (int i = 0; i < m_NumberOfMicro; ++i) {
+  for (int i = 0; i < m_NumberOfMicro; ++i) { // in case there's 2 micromega add up the rows
     for (int iRow = 0; iRow < 4; ++iRow) {
       for (int iCol = 0; iCol < 7; ++iCol) {
-      Cal->AddParameter("FPDTamu", "Micro"+NPL::itoa(i+1)+"_R"+ NPL::itoa(iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_E",
-                                   "Micro"+NPL::itoa(i+1)+"_R"+ NPL::itoa(iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_E");
-      Cal->AddParameter("FPDTamu", "Micro"+NPL::itoa(i+1)+"_R"+ NPL::itoa(iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_T",
-                                   "Micro"+NPL::itoa(i+1)+"_R"+ NPL::itoa(iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_T");      
+      Cal->AddParameter("FPDTamu", "Micro_R"+ NPL::itoa((4*i)+iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_E",
+                                   "Micro_R"+ NPL::itoa((4*i)+iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_E");
+      Cal->AddParameter("FPDTamu", "Micro_R"+ NPL::itoa((4*i)+iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_T",
+                                   "Micro_R"+ NPL::itoa((4*i)+iRow+1)+"_C"+ NPL::itoa(iCol+1)+"_T");      
       }
     }
   }
@@ -759,18 +1093,18 @@ void TFPDTamuPhysics::AddParameterToCalibrationManager() {
   for (int i = 0; i < m_NumberOfAWire; ++i) {
       for (int iCol = 0; iCol < 2; ++iCol) { // 2 cols for left and right
       Cal->AddParameter("FPDTamu", "AWire_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_E",
-                           "FPDTamu_AWire_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_E");
+                                   "AWire_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_E");
       Cal->AddParameter("FPDTamu", "AWire_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_T",
-                           "FPDTamu_AWire_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_T");      
+                                   "AWire_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_T");      
       }
   }
 
   for (int i = 0; i < m_NumberOfPlast; ++i) { // Always 1 
       for (int iCol = 0; iCol < 2; ++iCol) { // 2 cols for left and right
       Cal->AddParameter("FPDTamu", "Plast_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_E",
-                           "FPDTamu_Plast_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_E");
+                                   "Plast_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_E");
       Cal->AddParameter("FPDTamu", "Plast_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_T",
-                           "FPDTamu_Plast_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_T");      
+                                   "Plast_R"+ NPL::itoa(i+1)+"_C"+ NPL::itoa(iCol+1)+"_T");      
       }
   }
 
@@ -810,14 +1144,14 @@ void TFPDTamuPhysics::InitializeRootInputPhysics() {
   inputChain->SetBranchStatus( "AWirePositionX" , true );
   inputChain->SetBranchStatus( "AWirePositionZ" , true );
   //Plastic scintillator
-  inputChain->SetBranchStatus( "PlastDetNumber" , true );
   inputChain->SetBranchStatus( "PlastLeftCharge" , true );
   inputChain->SetBranchStatus( "PlastRightCharge" , true );
+  inputChain->SetBranchStatus( "PlastCharge" , true );
   inputChain->SetBranchStatus( "PlastPositionX" , true );
   inputChain->SetBranchStatus( "PlastPositionZ" , true );
   //Calculated AWire and Plastic
-  inputChain->SetBranchStatus( "PositionOnPlasticX" , true );
-  inputChain->SetBranchStatus( "BeamDirection" , true );
+  inputChain->SetBranchStatus( "PlastPositionX_AW" , true );
+  inputChain->SetBranchStatus( "IonDirection" , true );
 
   inputChain->SetBranchAddress("FPDTamu", &m_EventPhysics);
 }
@@ -833,21 +1167,23 @@ void TFPDTamuPhysics::InitializeRootOutput() {
 
 void TFPDTamuPhysics::AddDelta(TVector3 corner_UPL, TVector3 corner_UPR){
   m_NumberOfDelta++      ;
+  if (m_NumberOfDelta>2) cout << " \033[1;311mWARNING: FPDTamu can have up to 2 Delta-E ion chambers  " << endl;
 
 }
 
 void TFPDTamuPhysics::AddMicro(TVector3 corner_UPL, TVector3 corner_UPR){
   m_NumberOfMicro++      ;
-
+  if (m_NumberOfMicro>2) cout << " \033[1;311mWARNING: FPDTamu can have up to 2 Micormegas " << endl;
 }
 
 void TFPDTamuPhysics::AddAWire(TVector3 corner_UPL, TVector3 corner_UPR){
   m_NumberOfAWire++      ;
-
+  if (m_NumberOfAWire>4) cout << " \033[1;311mWARNING: FPDTamu has 4 Avalanche Wires scintillator " << endl;
 }
 
 void TFPDTamuPhysics::AddPlast(TVector3 corner_UPL, TVector3 corner_UPR){
   m_NumberOfPlast++      ;
+  if (m_NumberOfPlast>1) cout << " \033[1;311mWARNING: FPDTamu has one plastic scintillator " << endl;
 
 }
 
diff --git a/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.h b/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.h
index bbadde95c..d7e34f0b8 100644
--- a/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.h
+++ b/NPLib/Detectors/FPDTamu/TFPDTamuPhysics.h
@@ -85,16 +85,16 @@ class TFPDTamuPhysics : public TObject, public NPL::VDetector {
   vector<double> AWirePositionX;
   vector<double> AWirePositionZ;
   //Plastic scintillator
-  vector<int>    PlastDetNumber;
   vector<double> PlastLeftCharge;
   vector<double> PlastRightCharge;
+  vector<double> PlastCharge;
   vector<double> PlastPositionX;
   vector<double> PlastPositionZ;
   vector<double> PlastTime;
 
   //Calculated AWire and Plastic
-  double   PositionOnPlasticX;
-  TVector3 BeamDirection;//!
+  double   PlastPositionX_AW;
+  TVector3 IonDirection;//!
 
   //////////////////////////////////////////////////////////////
   // methods inherited from the VDetector ABC class
@@ -168,6 +168,8 @@ class TFPDTamuPhysics : public TObject, public NPL::VDetector {
     // needed for online analysis for example
     void SetRawDataPointer(TFPDTamuData* rawDataPointer) {m_EventData = rawDataPointer;}
     
+    void Dump() const;
+
   // objects are not written in the TTree
   private:
     TFPDTamuData*         m_EventData;        //!
@@ -178,7 +180,11 @@ class TFPDTamuPhysics : public TObject, public NPL::VDetector {
   public:
     TFPDTamuData* GetRawData()        const {return m_EventData;}
     TFPDTamuData* GetPreTreatedData() const {return m_PreTreatedData;}
-
+    
+    // Micromega specific used in analysis
+    double GetMicroGroupEnergy(int lrow, int hrow, int lcol, int hcol) ; 
+    double GetMicroRowGeomEnergy(int lrow, int hrow);
+  
   // parameters used in the analysis
   private:
     // thresholds
@@ -194,6 +200,15 @@ class TFPDTamuPhysics : public TObject, public NPL::VDetector {
     void AddAWire(TVector3 A, TVector3 B);  //!
     void AddPlast(TVector3 A, TVector3 B);  //!
 
+    vector<TVector3> DeltaLeftPos;  //!
+    vector<TVector3> DeltaRightPos; //!
+    vector<TVector3> MicroLeftPos;  //!
+    vector<TVector3> MicroRightPos; //!
+    vector<TVector3> AWireLeftPos;  //!
+    vector<TVector3> AWireRightPos; //!
+    TVector3 PlastLeftPos;  //!
+    TVector3 PlastRightPos; //!
+
   // number of detectors
   private:
     int m_NumberOfDetectors;  //!
-- 
GitLab