diff --git a/NPLib/Core/CMakeLists.txt b/NPLib/Core/CMakeLists.txt index ceb2b3c6e5cda54aa013a48713700f6a701fb23c..0e743e19c5a30e89e296f0ac652243333fbfcb1f 100644 --- a/NPLib/Core/CMakeLists.txt +++ b/NPLib/Core/CMakeLists.txt @@ -1,6 +1,7 @@ add_custom_command(OUTPUT TAsciiFileDict.cxx TAsciiFileDict_rdict.pcm TAsciiFile.rootmap COMMAND ../scripts/build_dict.sh TAsciiFile.h TAsciiFileDict.cxx TAsciiFile.rootmap libNPCore.so) add_custom_command(OUTPUT NPVDetectorDict.cxx NPVDetectorDict_rdict.pcm NPVDetector.rootmap COMMAND ../scripts/build_dict.sh NPVDetector.h NPVDetectorDict.cxx NPVDetector.rootmap libNPCore.so NPCoreLinkdef.h) add_custom_command(OUTPUT NPOnlineDict.cxx NPOnlineDict_rdict.pcm NPOnline.rootmap COMMAND ../scripts/build_dict.sh NPOnline.h NPOnlineDict.cxx NPOnline.rootmap libNPOnline.dylib NPOnlineLinkDef.h DEPENDS NPOnline.h) -add_library(NPCore SHARED NPVAnalysis.cxx NPAnalysisFactory.cxx NPCalibrationManager.cxx NPOptionManager.cxx RootOutput.cxx RootInput.cxx TAsciiFile.cxx TAsciiFileDict.cxx NPDetectorManager.cxx NPVDetector.cxx NPVDetectorDict.cxx NPVSpectra.cxx NPDetectorFactory.cxx NPOnline.cxx NPOnlineDict.cxx NPSpectraServer) +add_custom_command(OUTPUT NPSpectraServerDict.cxx NPSpectraServerDict_rdict.pcm NPSpectraServer.rootmap COMMAND ../scripts/build_dict.sh NPSpectraServer.h NPSpectraServerDict.cxx NPSpectraServer.rootmap libNPSpectraServer.dylib NPSpectraServerLinkDef.h DEPENDS NPSpectraServer.h) +add_library(NPCore SHARED NPVAnalysis.cxx NPAnalysisFactory.cxx NPCalibrationManager.cxx NPOptionManager.cxx RootOutput.cxx RootInput.cxx TAsciiFile.cxx TAsciiFileDict.cxx NPDetectorManager.cxx NPVDetector.cxx NPVDetectorDict.cxx NPVSpectra.cxx NPDetectorFactory.cxx NPOnline.cxx NPOnlineDict.cxx NPSpectraServer.cxx NPSpectraServerDict.cxx) target_link_libraries(NPCore ${ROOT_LIBRARIES}) install(FILES NPVAnalysis.h NPAnalysisFactory.h NPCalibrationManager.h NPOptionManager.h RootInput.h RootOutput.h TAsciiFile.h NPDetectorManager.h NPVDetector.h NPGlobalSystemOfUnits.h NPPhysicalConstants.h NPSystemOfUnits.h NPVSpectra.h NPDetectorFactory.h NPOnline.h NPSpectraServer.h DESTINATION ${CMAKE_INCLUDE_OUTPUT_DIRECTORY}) diff --git a/NPLib/Core/NPDetectorManager.cxx b/NPLib/Core/NPDetectorManager.cxx index 8d2a2d5f9d75e7f2321a0229d41dae396ce7fd3e..38dc007ea1010e9fd82569cf825375e09c52ca08 100644 --- a/NPLib/Core/NPDetectorManager.cxx +++ b/NPLib/Core/NPDetectorManager.cxx @@ -33,10 +33,9 @@ #include "RootInput.h" #include "NPOptionManager.h" #include "NPCalibrationManager.h" -#include "NPSpectraServer.h" + //Root #include"TCanvas.h" - ///////////////////////////////////////////////////////////////////////////////////////////////// // Default Constructor NPL::DetectorManager::DetectorManager(){ @@ -45,7 +44,7 @@ NPL::DetectorManager::DetectorManager(){ m_ClearEventDataPtr = &NPL::VDetector::ClearEventData ; m_FillSpectra = NULL; m_CheckSpectra = NULL; - + m_SpectraServer = NULL; if(NPOptionManager::getInstance()->GetGenerateHistoOption()){ m_FillSpectra = &NPL::VDetector::FillSpectra ; if(NPOptionManager::getInstance()->GetCheckHistoOption()) @@ -60,7 +59,8 @@ NPL::DetectorManager::~DetectorManager(){ #if __cplusplus > 199711L StopThread(); #endif - + if(m_SpectraServer) + m_SpectraServer->Destroy(); } ///////////////////////////////////////////////////////////////////////////////////////////////// @@ -429,23 +429,29 @@ bool NPL::DetectorManager::IsDone(){ } return true; } +#endif //////////////////////////////////////////////////////////////////////////////// void NPL::DetectorManager::SetSpectraServer(){ - NPL::SpectraServer* s = NPL::SpectraServer::getInstance(); + m_SpectraServer = NPL::SpectraServer::getInstance(); map<string,VDetector*>::iterator it; for (it = m_Detector.begin(); it != m_Detector.end(); ++it){ vector<TCanvas*> canvas = it->second->GetCanvas(); size_t mysize = canvas.size(); for (size_t i = 0 ; i < mysize ; i++) - s->AddCanvas(canvas[i]); + m_SpectraServer->AddCanvas(canvas[i]); } - - thread t( &NPL::SpectraServer::Start,s); - t.detach(); - system("nponline localhost 9090 &"); + system("nponline localhost 9090 & "); + m_SpectraServer->CheckRequest(); +} +//////////////////////////////////////////////////////////////////////////////// +void NPL::DetectorManager::StopSpectraServer(){ + m_SpectraServer->Destroy(); +} +//////////////////////////////////////////////////////////////////////////////// +void NPL::DetectorManager::CheckSpectraServer(){ + m_SpectraServer->CheckRequest(); } -#endif diff --git a/NPLib/Core/NPDetectorManager.h b/NPLib/Core/NPDetectorManager.h index db45a600f71d30457468b54132a6222caa165594..a5d56f9b49a4ca3a02a478a5aa701611d2a755de 100644 --- a/NPLib/Core/NPDetectorManager.h +++ b/NPLib/Core/NPDetectorManager.h @@ -22,7 +22,7 @@ *****************************************************************************/ // NPL #include "NPVDetector.h" - +#include "NPSpectraServer.h" // ROOT #include "TH1.h" @@ -60,6 +60,13 @@ namespace NPL{ vector< map< vector<string>, TH1* > > GetSpectra(); vector<string> GetDetectorList(); + public: // for online spectra server + void SetSpectraServer(); + void StopSpectraServer(); + void CheckSpectraServer(); + private : + NPL::SpectraServer* m_SpectraServer; + private: // The map containning all detectors // Using a Map one can access to any detector using its name @@ -71,7 +78,7 @@ namespace NPL{ VDetector_FuncPtr m_ClearEventDataPtr; VDetector_FuncPtr m_FillSpectra; VDetector_FuncPtr m_CheckSpectra; - + #if __cplusplus > 199711L private: // Thread Pool defined if C++11 is available vector<thread> m_ThreadPool; @@ -84,9 +91,7 @@ namespace NPL{ void InitThreadPool(); bool IsDone(); - public: // for online spectra server - void SetSpectraServer(); - #endif + #endif private: double m_TargetThickness; diff --git a/NPLib/Core/NPOnline.cxx b/NPLib/Core/NPOnline.cxx index 54dc15d8bb4f3c0b68bda31969dfe9dd1213e642..f5f47c243cf79822579b431674cbe320dab9a5dc 100644 --- a/NPLib/Core/NPOnline.cxx +++ b/NPLib/Core/NPOnline.cxx @@ -6,7 +6,7 @@ #include <dirent.h> // Root -#include "TRoot.h" +#include "TROOT.h" #include "TColor.h" #include "TSystem.h" #include "TString.h" @@ -64,7 +64,6 @@ void NPL::NPOnline::MakeGui(string address,int port){ m_Main->SetName("nponline"); m_Main->SetBackgroundColor(m_BgColor); m_Main->SetForegroundColor(m_FgColor); - // m_Main->SetLayoutBroken(kTRUE); // Button bar to hold the button m_ButtonBar= new TGVerticalFrame(m_Main,10000,42,kFixedSize); @@ -218,9 +217,8 @@ void NPL::NPOnline::MakeGui(string address,int port){ m_Main->Resize(m_Main->GetDefaultSize()); m_Main->MapWindow(); - m_Main->MoveResize(0,0,2000,1000); + m_Main->MoveResize(0,0,1000,500); - m_Main->SetLayoutBroken(kFALSE); } //////////////////////////////////////////////////////////////////////////////// @@ -240,6 +238,12 @@ NPL::NPOnline::~NPOnline(){ //////////////////////////////////////////////////////////////////////////////// void NPL::NPOnline::Connect(){ // Connect to SpectraServer + if(m_Sock){ + m_Sock->Close("force"); + delete m_Sock; + m_Sock = NULL; + } + m_Sock = new TSocket(m_Address->GetDisplayText(),(Int_t) m_Port->GetNumber()); if(m_Sock->IsValid()){ m_Connect->SetState(kButtonDisabled); @@ -253,7 +257,11 @@ void NPL::NPOnline::Connect(){ //////////////////////////////////////////////////////////////////////////////// void NPL::NPOnline::Update(){ if(!m_Sock || !(m_Sock->IsValid())){ - cout << "Spectra server not connected" << endl; + if(m_Sock){ + m_Sock->Close("force"); + delete m_Sock; + m_Sock = NULL; + } m_Connect->SetState(kButtonUp); return; } @@ -262,10 +270,15 @@ void NPL::NPOnline::Update(){ m_Sock->Send("RequestSpectra"); if(m_Sock->Recv(message)<=0){ - cout << "Spectra request failed " << endl; + if(m_Sock){ + m_Sock->Close("force"); + delete m_Sock; + m_Sock = NULL; + } + m_Connect->SetState(kButtonUp); return; } - + m_CanvasListTree->Clear(); m_CanvasList = (TList*) message->ReadObject(message->GetClass()); @@ -277,13 +290,13 @@ void NPL::NPOnline::Update(){ TGCompositeFrame* tab = m_Tab->GetTabContainer(c->GetName()); if(tab){ tab->RemoveAll(); - TRootEmbeddedCanvas* canvas = new TRootEmbeddedCanvas("Canvas",tab,700,490,!kSunkenFrame); + TRootEmbeddedCanvas* canvas = new TRootEmbeddedCanvas(c->GetName(),tab,100,100,!kSunkenFrame); c->UseCurrentStyle(); c->SetMargin(0,0,0,0); canvas->AdoptCanvas(c); tab->AddFrame(canvas,new TGLayoutHints(kLHintsLeft | kLHintsBottom | kLHintsExpandX | kLHintsExpandY)); - tab->SetLayoutManager(new TGVerticalLayout(tab)); + tab->SetLayoutManager(new TGHorizontalLayout(tab)); ExecuteMacro(c->GetName()); } } diff --git a/NPLib/Core/NPSpectraServer.cxx b/NPLib/Core/NPSpectraServer.cxx index bcfd35da596f43c5889ffb47f5c75cbce6584246..907233aa34d96864cf868d844069d2ad3b503522 100644 --- a/NPLib/Core/NPSpectraServer.cxx +++ b/NPLib/Core/NPSpectraServer.cxx @@ -1,5 +1,6 @@ #include "NPSpectraServer.h" -#include <unistd.h> +#include<cstdlib> +#include<unistd.h> #include<iostream> NPL::SpectraServer* NPL::SpectraServer::instance = 0 ; //////////////////////////////////////////////////////////////////////////////// @@ -9,15 +10,18 @@ NPL::SpectraServer* NPL::SpectraServer::getInstance(){ return instance; } - +//////////////////////////////////////////////////////////////////////////////// +void NPL::SpectraServer::Destroy(){ + delete instance ; + instance = 0 ; +} //////////////////////////////////////////////////////////////////////////////// NPL::SpectraServer::SpectraServer(){ m_Server= new TServerSocket(9090,true); if(!m_Server->IsValid()) exit(1); - - - m_stop = false; + + m_Server->SetCompressionSettings(1); // Add server socket to monitor so we are notified when a client needs to be // accepted m_Monitor = new TMonitor; @@ -35,39 +39,21 @@ void NPL::SpectraServer::AddCanvas(TCanvas* c){ } //////////////////////////////////////////////////////////////////////////////// -void NPL::SpectraServer::Start(){ +void NPL::SpectraServer::CheckRequest(){ if(m_Server){ - while(!m_stop){ - if(!m_Server) - return; - TSocket* s ; - if((s=m_Monitor->Select(20))!=(TSocket*)-1) + if((s=m_Monitor->Select(1))!=(TSocket*)-1) HandleSocket(s); - } } } //////////////////////////////////////////////////////////////////////////////// NPL::SpectraServer::~SpectraServer(){ - std::cout << 1 << std::endl; // tell the socket to stop - m_stop = true; - // wait for it to be stopped - sleep(100); - std::cout << 2 << std::endl; - m_Server->Close("force"); - - std::cout << 3 << std::endl; - delete m_Server; - std::cout << 4 << std::endl; - m_Server=0; delete m_Monitor; - std::cout << 5 << std::endl; - m_Monitor = 0; instance = 0 ; } diff --git a/NPLib/Core/NPSpectraServer.h b/NPLib/Core/NPSpectraServer.h index bc175fd8ce5ed96003a1460a703b5eb76211b598..c79d0ae7bc2740f3eb12020cc869b391e5a7fbb1 100644 --- a/NPLib/Core/NPSpectraServer.h +++ b/NPLib/Core/NPSpectraServer.h @@ -11,7 +11,7 @@ namespace NPL{ class SpectraServer{ public: static SpectraServer* getInstance(); - + void Destroy(); private: SpectraServer(); ~SpectraServer(); @@ -22,7 +22,7 @@ namespace NPL{ public: void HandleSocket(TSocket* s); void AddCanvas(TCanvas* c); - void Start(); + void CheckRequest(); private: bool m_stop; TServerSocket* m_Server; diff --git a/NPLib/Utility/npanalysis.cxx b/NPLib/Utility/npanalysis.cxx index afb4f98d20c3b46d77ed4984379a1678830091b7..5bcba969fd009ab9ad72243002abb0c283204dc4 100644 --- a/NPLib/Utility/npanalysis.cxx +++ b/NPLib/Utility/npanalysis.cxx @@ -102,7 +102,6 @@ int main(int argc , char** argv){ if(myOptionManager->GetOnline()){ // Request Detector manager to give the Spectra to the server myDetector->SetSpectraServer(); - } std::cout << std::endl << "///////// Starting Analysis ///////// "<< std::endl; @@ -132,6 +131,10 @@ 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(); + } } } @@ -153,6 +156,9 @@ 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(); + } } } @@ -165,6 +171,9 @@ 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(); + } } } UserAnalysis->End(); @@ -174,11 +183,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; }