From 4bfa1cd58c0185635302416fa3d21c55bae0de89 Mon Sep 17 00:00:00 2001
From: adrien-matta <a.matta@surrey.ac.uk>
Date: Thu, 18 Jun 2015 10:37:08 +0100
Subject: [PATCH] * nanalys now working in continuous mode when online flag is
 used * npanalys keep updating the input file until the neumber of entry *
 change allowing online usage with simulation or DAQ

---
 NPLib/Core/RootInput.h           |  3 +-
 NPLib/Core/RootOutput.cxx        | 17 ++++--
 NPLib/Sharc/TSharcPhysics.cxx    | 10 +++-
 NPLib/Utility/npanalysis.cxx     | 94 +++++++++++++++++++++++++-------
 NPSimulation/Core/EventAction.cc |  8 ++-
 NPSimulation/Sharc/Sharc.cc      |  2 +-
 NPSimulation/Sharc/Sharc.hh      | 13 ++---
 7 files changed, 107 insertions(+), 40 deletions(-)

diff --git a/NPLib/Core/RootInput.h b/NPLib/Core/RootInput.h
index f08fd789a..687290657 100644
--- a/NPLib/Core/RootInput.h
+++ b/NPLib/Core/RootInput.h
@@ -70,7 +70,8 @@ public:
    // Return the private chain and file
    TChain*  GetChain()  {return pRootChain;}
    TFile*   GetFile()   {return pRootFile;}
-   
+   void     SetChain(TChain* c)  {pRootChain = c;} 
+
    // Add a Friend chain to the input chain
    void     AddFriendChain(string RunToAdd);
 
diff --git a/NPLib/Core/RootOutput.cxx b/NPLib/Core/RootOutput.cxx
index eb75feaea..29d55ea63 100644
--- a/NPLib/Core/RootOutput.cxx
+++ b/NPLib/Core/RootOutput.cxx
@@ -124,10 +124,11 @@ RootOutput::RootOutput(TString fileNameBase, TString treeNameBase){
   // Init TAsciiFile objects
   InitAsciiFiles();
   gDirectory->cd(currentPath->GetPath()); 
-  long int inter= -3000000000; 
-  pRootTree->SetAutoFlush(inter);
-  pRootTree->SetAutoSave(-inter);
-
+//  long int inter= -3000000000; 
+//  pRootTree->SetAutoFlush(inter);
+//  pRootTree->SetAutoSave(-inter);
+  pRootTree->SetAutoFlush(0);
+  pRootTree->SetAutoSave(0);
 }
 
 
@@ -142,13 +143,15 @@ void RootOutput::InitAsciiFiles(){
   pEventGenerator = new TAsciiFile();
   pEventGenerator->SetNameTitle("EventGenerator", fileNameEG.Data());
   pEventGenerator->Append(fileNameEG.Data());
-
+  pEventGenerator->Write();
+  
   // Detector configuration 
   // Get file name from NPOptionManager
   TString fileNameDC = OptionManager->GetDetectorFile();
   pDetectorConfiguration = new TAsciiFile();
   pDetectorConfiguration->SetNameTitle("DetectorConfiguration", fileNameDC.Data());
   pDetectorConfiguration->Append(fileNameDC.Data());
+  pDetectorConfiguration->Write();
 
   // Run to treat file
   // Get file name from NPOptionManager
@@ -157,6 +160,7 @@ void RootOutput::InitAsciiFiles(){
     TString fileNameRT = OptionManager->GetRunToReadFile();
     pRunToTreatFile->SetNameTitle("RunToTreat", fileNameRT.Data());
     pRunToTreatFile->Append(fileNameRT.Data());
+    pRunToTreatFile->Write();
   }
 
   // Calibration files
@@ -164,14 +168,17 @@ void RootOutput::InitAsciiFiles(){
   if (!OptionManager->IsDefault("Calibration")) {
     TString fileNameCal = OptionManager->GetCalibrationFile();
     pCalibrationFile->SetNameTitle("Calibration", fileNameCal.Data());
+    pCalibrationFile->Write();
   }
 
   // Analysis configuration files
   pAnalysisConfigFile = new TAsciiFile();
   pAnalysisConfigFile->SetNameTitle("AnalysisConfig", "AnalysisConfig");
+  pAnalysisConfigFile->Write();
 }
 
 
+
 ////////////////////////////////////////////////////////////////////////////////
 RootOutput::~RootOutput(){ 
   // The data is written to the file and the tree is closed:
diff --git a/NPLib/Sharc/TSharcPhysics.cxx b/NPLib/Sharc/TSharcPhysics.cxx
index 331ea8328..e21fb7d32 100644
--- a/NPLib/Sharc/TSharcPhysics.cxx
+++ b/NPLib/Sharc/TSharcPhysics.cxx
@@ -680,9 +680,13 @@ void TSharcPhysics::AddParameterToCalibrationManager(){
 ///////////////////////////////////////////////////////////////////////////
 void TSharcPhysics::InitializeRootInputRaw(){
   TChain* inputChain = RootInput::getInstance()->GetChain()   ;
-  inputChain->SetBranchStatus( "Sharc" , true )               ;
-  inputChain->SetBranchStatus( "fSharc_*" , true )               ;
-  inputChain->SetBranchAddress( "Sharc" , &m_EventData )      ;
+  static UInt_t* found =  new UInt_t[100] ;
+  inputChain->SetBranchStatus( "Sharc" , true );
+  // The following line is necessary only for system were the tree is splitted
+  // (older root version). The found argument silenced the Branches not found
+  // error for non splitted tree.
+  inputChain->SetBranchStatus( "fSharc_*",true,found);
+  inputChain->SetBranchAddress( "Sharc" , &m_EventData );
 
 }
 
diff --git a/NPLib/Utility/npanalysis.cxx b/NPLib/Utility/npanalysis.cxx
index 5bcba969f..2aa202ac6 100644
--- a/NPLib/Utility/npanalysis.cxx
+++ b/NPLib/Utility/npanalysis.cxx
@@ -11,6 +11,10 @@
 #include<stdio.h>
 #include<dlfcn.h>
 #include<stdlib.h>
+#include<unistd.h>
+
+// Root
+#include"TKey.h"
 
 #ifndef SHARED_LIB_EXTENSION_GLOB
 #define SHARED_LIB_EXTENSION_GLOB
@@ -64,7 +68,7 @@ int main(int argc , char** argv){
     if(myOptionManager->IsDefault("OutputFileName"))
       OutputfileName="PhysicsTree";
   }
-  
+
   // Case of Result tree produced
   else{
     TreeName="ResultTree";
@@ -109,8 +113,10 @@ int main(int argc , char** argv){
   myOptionManager->GetNumberOfEntryToAnalyse();
   int nentries = Chain->GetEntries();
   if(nentries> myOptionManager->GetNumberOfEntryToAnalyse() && myOptionManager->GetNumberOfEntryToAnalyse()>0)
-    nentries = myOptionManager->GetNumberOfEntryToAnalyse() ;  
-  std::cout << " Number of Event to be treated : " << nentries << std::endl;
+    nentries = myOptionManager->GetNumberOfEntryToAnalyse() ; 
+
+  TString ChainName = Chain->GetName();
+  std::cout << " Number of Event to be treated : " << nentries << " on chain " << ChainName << std::endl;
 
   unsigned int inter = 0;
   unsigned int treated = 0;
@@ -118,7 +124,7 @@ int main(int argc , char** argv){
   int displayed=0;
   clock_t end;
   clock_t begin = clock();
-
+  int new_nentries = 0 ;
   bool IsPhysics = myOptionManager->GetInputPhysicalTreeOption();
 
   if(UserAnalysis==NULL){ 
@@ -130,17 +136,33 @@ int main(int argc , char** argv){
         myDetector->BuildPhysicalEvent();
         // Fill the tree
         tree->Fill();
+
         ProgressDisplay(begin,end,treated,inter,nentries,mean_rate,displayed);
-        
-        if(myOptionManager->GetOnline() && i%5000==0){
-          myDetector->CheckSpectraServer(); 
+        if(myOptionManager->GetOnline() && i%10000==0){
+          bool first = true;
+          while(!Chain || first){
+            first = false;
+            myDetector->CheckSpectraServer();
+            RootInput::getInstance()->GetFile()->ReadKeys(kTRUE);
+
+            Chain = (TChain*)  RootInput::getInstance()->GetFile()->FindKeyAny(ChainName)->ReadObj();    
+            new_nentries = Chain->GetEntries();
+            if(new_nentries!=nentries){
+              RootInput::getInstance()->SetChain(Chain);
+              myDetector->InitializeRootInput();
+              nentries = Chain->GetEntries();
+            }
+            else{
+              first = true;
+            }
+          }
         }
       }
     }
-    
+
     else{
-     cout << "\033[1;31m ERROR: You are requesting to rebuild a Physics Tree without any User Analysis, nothing to be done\033[0m" <<endl;
-     exit(1);
+      cout << "\033[1;31m ERROR: You are requesting to rebuild a Physics Tree without any User Analysis, nothing to be done\033[0m" <<endl;
+      exit(1);
     }
   }
 
@@ -156,9 +178,25 @@ int main(int argc , char** argv){
         // Fill the tree      
         tree->Fill();
         ProgressDisplay(begin,end,treated,inter,nentries,mean_rate,displayed);
-        if(myOptionManager->GetOnline() && i%5000==0){
-          myDetector->CheckSpectraServer(); 
-        }
+        if(myOptionManager->GetOnline() && i%10000==0){
+          bool first = true;
+          while(!Chain || first){
+            first = false;
+            myDetector->CheckSpectraServer();
+            RootInput::getInstance()->GetFile()->ReadKeys(kTRUE);
+
+            Chain = (TChain*)  RootInput::getInstance()->GetFile()->FindKeyAny(ChainName)->ReadObj();    
+            new_nentries = Chain->GetEntries();
+            if(new_nentries!=nentries){
+              RootInput::getInstance()->SetChain(Chain);
+              myDetector->InitializeRootInput();
+              nentries = Chain->GetEntries();
+            }
+            else{
+              first = true;
+            }
+          }
+        }      
       }
     }
 
@@ -171,9 +209,25 @@ int main(int argc , char** argv){
         // Fill the tree      
         tree->Fill();
         ProgressDisplay(begin,end,treated,inter,nentries,mean_rate,displayed);
-        if(myOptionManager->GetOnline() && i%5000==0){
-          myDetector->CheckSpectraServer(); 
-        }
+        if(myOptionManager->GetOnline() && i%10000==0){
+          bool first = true;
+          while(!Chain || first){
+            first = false;
+            myDetector->CheckSpectraServer();
+            RootInput::getInstance()->GetFile()->ReadKeys(kTRUE);
+
+            Chain = (TChain*)  RootInput::getInstance()->GetFile()->FindKeyAny(ChainName)->ReadObj();    
+            new_nentries = Chain->GetEntries();
+            if(new_nentries!=nentries){
+              RootInput::getInstance()->SetChain(Chain);
+              myDetector->InitializeRootInput();
+              nentries = Chain->GetEntries();
+            }
+            else{
+              first = true;
+            }
+          }
+        }      
       }
     }
     UserAnalysis->End();
@@ -183,18 +237,18 @@ int main(int argc , char** argv){
   myDetector->StopThread();
 #endif
   ProgressDisplay(begin,end,treated,inter,nentries,mean_rate,displayed);
- 
+
   if(myOptionManager->GetOnline()){
     myDetector->CheckSpectraServer(); 
   }
 
-  
+
   if(myOptionManager->GetGenerateHistoOption())
     myDetector->WriteSpectra();
-  
+
   RootOutput::getInstance()->Destroy();
   RootInput::getInstance()->Destroy();
-  
+
   return 0;
 }
 
diff --git a/NPSimulation/Core/EventAction.cc b/NPSimulation/Core/EventAction.cc
index 78195ee83..6feb8ea6b 100644
--- a/NPSimulation/Core/EventAction.cc
+++ b/NPSimulation/Core/EventAction.cc
@@ -60,8 +60,12 @@ void EventAction::BeginOfEventAction(const G4Event* event){
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 void EventAction::EndOfEventAction(const G4Event* event){
   m_detector->ReadAllSensitive(event) ;
-  RootOutput *pAnalysis = RootOutput::getInstance();
-  pAnalysis->GetTree()->Fill();
+  static TTree* tree =  RootOutput::getInstance()->GetTree();
+  tree->Fill();
+  if(treated%10000==0){
+    tree->AutoSave();
+     RootOutput::getInstance()->GetFile()->SaveSelf(kTRUE);
+  }
 }
 
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
diff --git a/NPSimulation/Sharc/Sharc.cc b/NPSimulation/Sharc/Sharc.cc
index 62659186f..18fcbe31d 100644
--- a/NPSimulation/Sharc/Sharc.cc
+++ b/NPSimulation/Sharc/Sharc.cc
@@ -617,7 +617,6 @@ void Sharc::ConstructQQQDetector(G4LogicalVolume* world){
                       logicQQQDetector,"QQQ",world,false,DetNbr);
   }
 }
-
 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 // Add Detector branch to the EventTree.
 // Called After DetecorConstruction::AddDetector Method
@@ -731,6 +730,7 @@ void Sharc::ReadSensitive(const G4Event* event){
       
       m_Event->SetFront_DetectorNbr(DetNbr);
       m_Event->SetFront_StripNbr(QQQ_Wafer_NumberOf_AnnularStrip-StripFront+1); // Order is reverse (1 is outtermost strip)
+
       m_Event->SetFront_Energy(RandGauss::shoot(Energy, ResoEnergy));
       m_Event->SetFront_TimeCFD(RandGauss::shoot(Time, ResoTime));
       m_Event->SetFront_TimeLED(RandGauss::shoot(Time, ResoTime));
diff --git a/NPSimulation/Sharc/Sharc.hh b/NPSimulation/Sharc/Sharc.hh
index a0f2d9c12..29cb27d24 100644
--- a/NPSimulation/Sharc/Sharc.hh
+++ b/NPSimulation/Sharc/Sharc.hh
@@ -49,10 +49,7 @@ namespace SHARC{
   //const G4double ResoEnergy  = 0.035*MeV ;// = zzkeV of Resolution   //   Unit is MeV/2.35
   const G4double ResoEnergy  = 0.042*MeV ;// = zzkeV of Resolution   //   Unit is MeV/2.35
 
-  //const G4double EnergyThreshold = 0.4*MeV;
-  
-  // Change for TRex simulations
-  const G4double EnergyThreshold = 0.5*MeV;
+  const G4double EnergyThreshold = 0.1*MeV;
   // Geometry
   
   // BOX //
@@ -97,10 +94,10 @@ namespace SHARC{
   const G4double PAD_PCB_Border_ShortSide = 2.5*mm;
   
   // PAD Wafer
-//  const G4double PAD_Wafer_Width  = 51.00*mm;
-//  const G4double PAD_Wafer_Length = 77.00*mm;
-    const G4double PAD_Wafer_Width  = 42.00*mm;
-    const G4double PAD_Wafer_Length = 72.00*mm;
+  const G4double PAD_Wafer_Width  = 51.00*mm;
+  const G4double PAD_Wafer_Length = 77.00*mm;
+//    const G4double PAD_Wafer_Width  = 42.00*mm;
+//    const G4double PAD_Wafer_Length = 72.00*mm;
   
   const G4double PAD_Wafer_DeadLayer_Thickness = 0.1*um;
   
-- 
GitLab