23 std::vector<OnlMonClient*> OnlMonClient::m_list_us;
24 bool OnlMonClient::m_bl_clear_us =
true;
28 , m_title(
"Client Title")
34 , m_make_sp_hist(true)
36 memset(m_list_can, 0,
sizeof(m_list_can));
37 m_list_us.push_back(
this);
42 if (! m_hm)
delete m_hm;
44 ClearHistList(m_list_h1);
46 m_list_us.erase( find(m_list_us.begin(), m_list_us.end(),
this) );
51 cerr <<
"!!ERROR!! OnlMonClient::Clone(): virtual function called. Abort." << endl;
67 gStyle->SetLabelSize (0.05,
"X");
68 gStyle->SetTitleSize (0.05,
"X");
69 gStyle->SetLabelSize (0.06,
"YZ");
70 gStyle->SetTitleSize (0.06,
"YZ");
71 gStyle->SetTitleOffset(0.90,
"XY");
72 gStyle->SetTitleSize (0.10,
"");
76 m_h1_basic_id =
new TH1D(
"h1_basic_id" ,
"", 10, 0.5, 10.5);
77 m_h1_basic_cnt =
new TH1D(
"h1_basic_cnt",
"", 10, 0.5, 10.5);
81 SQRun* run_header = findNode::getClass<SQRun>(topNode,
"SQRun");
83 m_h1_basic_id->SetBinContent(BIN_RUN, run_header->
get_run_id());
90 SQEvent*
event = findNode::getClass<SQEvent>(topNode,
"SQEvent");
94 int ret_mutex = pthread_mutex_lock(mutex);
95 if (ret_mutex != 0) cout <<
"WARNING: mutex_lock returned " << ret_mutex <<
"." << endl;
97 int sp_id =
event->get_spill_id();
98 if (sp_id != m_spill_id_pre) {
100 if (m_spill_id_pre >= 0 && m_make_sp_hist) {
102 MakeSpillHist(m_spill_id_pre, sp_id);
104 cout <<
Name() <<
": Disable spill-by-spill hists." << endl;
105 MakeSpillHist(m_spill_id_pre);
109 m_spill_id_pre = sp_id;
111 m_h1_basic_cnt->AddBinContent(BIN_N_SP, 1);
112 m_h1_basic_id ->SetBinContent(BIN_RUN , event->get_run_id());
113 m_h1_basic_id ->SetBinContent(BIN_SPILL, sp_id);
114 m_h1_basic_id ->SetBinContent(BIN_EVENT, event->get_event_id());
115 int sp_curr = m_h1_basic_id->GetBinContent(BIN_SPILL_MIN);
116 if (sp_curr <= 0 || sp_curr > sp_id) m_h1_basic_id->SetBinContent(BIN_SPILL_MIN, sp_id);
117 sp_curr = m_h1_basic_id->GetBinContent(BIN_SPILL_MAX);
118 if (sp_curr < sp_id) m_h1_basic_id->SetBinContent(BIN_SPILL_MAX, sp_id);
120 m_h1_basic_cnt->AddBinContent(BIN_N_EVT, 1);
123 ret_mutex = pthread_mutex_unlock(mutex);
124 if (ret_mutex != 0) cout <<
"WARNING: mutex_unlock returned " << ret_mutex <<
"." << endl;
132 if (m_spill_id_pre >= 0 && m_make_sp_hist) {
133 MakeSpillHist(m_spill_id_pre);
136 ClearHistList(m_list_h1);
137 for (
unsigned int ih = 0; ih < m_hm->
nHistos(); ih++) {
138 TH1* h1_org = (TH1*)m_hm->
getHisto(ih);
139 TH1* h1 = (TH1*)h1_org->Clone(h1_org->GetName());
140 m_list_h1.push_back(h1);
144 int ret = DrawCanvas(
true);
146 cerr <<
"WARNING: OnlMonClient::End(): ret = " << ret << endl;
154 gSystem->mkdir(oss.str().c_str(),
true);
155 gSystem->Chmod(oss.str().c_str(), 0775);
156 oss <<
"/" <<
Name() <<
".root";
158 gSystem->Chmod(oss.str().c_str(), 0664);
165 if (run_id ) *run_id = (int)m_h1_basic_id->GetBinContent(BIN_RUN);
166 if (spill_id ) *spill_id = (int)m_h1_basic_id->GetBinContent(BIN_SPILL);
167 if (event_id ) *event_id = (int)m_h1_basic_id->GetBinContent(BIN_EVENT);
168 if (spill_id_min) *spill_id_min = (int)m_h1_basic_id->GetBinContent(BIN_SPILL_MIN);
169 if (spill_id_max) *spill_id_max = (int)m_h1_basic_id->GetBinContent(BIN_SPILL_MAX);
174 if (n_evt) *n_evt = (int)m_h1_basic_cnt->GetBinContent(BIN_N_EVT);
175 if (n_sp ) *n_sp = (int)m_h1_basic_cnt->GetBinContent(BIN_N_SP );
180 for (HistList_t::iterator it = m_list_h1.begin(); it != m_list_h1.end(); it++) {
181 if (name == (*it)->GetName())
return *it;
184 cerr <<
"!!ERROR!! OnlMonClient::FindMonHist() cannot find '" << name <<
"'. Abort." << endl;
192 cerr <<
"!!ERROR!! OnlMonClient::InitOnlMon(): virtual function called. Abort." << endl;
198 cerr <<
"!!ERROR!! OnlMonClient::InitRunOnlMon(): virtual function called. Abort." << endl;
204 cerr <<
"!!ERROR!! OnlMonClient::ProcessEventOnlMon(): virtual function called. Abort." << endl;
210 cerr <<
"!!ERROR!! OnlMonClient::EndOnlMon(): virtual function called. Abort." << endl;
216 cerr <<
"!!ERROR!! OnlMonClient::FindAllMonHist(): virtual function called. Abort." << endl;
222 cerr <<
"!!ERROR!! OnlMonClient::DrawMonitor(): virtual function called. Abort." << endl;
229 for (SelfList_t::iterator it = m_list_us.begin(); it != m_list_us.end(); it++) {
230 (*it)->ClearCanvasList();
235 int ret = ReceiveHist();
237 cout <<
"WARNING: The OnlMon server is NOT running." << endl;
239 }
else if (ret == 2) {
240 cout <<
"WARNING: The OnlMon server is NOT ready." << endl;
242 }
else if (ret != 0) {
243 cout <<
"WARNING: Unknown error in connecting to the OnlMon server." << endl;
254 m_hist_mode[h1->GetName()] = mode;
256 cerr <<
"WARNING: OnlMonClient::RegisterHist(): Cannot register hist (" << h1->GetName()
257 <<
"). You must call this function in InitRunOnlMon(), not InitOnlMon(). Do nothing." << endl;
265 sock->Send(
"NotReady");
270 int ret_mutex = pthread_mutex_lock(mutex);
271 if (ret_mutex != 0) cout <<
"WARNING: mutex_lock returned " << ret_mutex <<
"." << endl;
274 if (m_make_sp_hist) {
275 if (sp_min > 0 && sp_max == 0) {
279 sp_min = max0 - sp_min + 1;
281 MakeMergedHist(list_h1, sp_min, sp_max);
283 for (
unsigned int ih = 0; ih < m_hm->
nHistos(); ih++) {
284 TH1* h1_org = (TH1*)m_hm->
getHisto(ih);
285 TH1* h1 = (TH1*)h1_org->Clone(h1_org->GetName());
286 list_h1.push_back(h1);
290 ret_mutex = pthread_mutex_unlock(mutex);
291 if (ret_mutex != 0) cout <<
"WARNING: mutex_unlock returned " << ret_mutex <<
"." << endl;
293 TMessage outgoing(kMESS_OBJECT);
295 for (HistList_t::iterator it = list_h1.begin(); it != list_h1.end(); it++) {
298 outgoing.WriteObject(h1);
299 sock->Send(outgoing);
305 ClearHistList(list_h1);
307 sock->Send(
"Finished");
311 void OnlMonClient::ClearSpillHist()
313 for (Name2SpillHistMap_t::iterator it1 = m_map_hist_sp.begin(); it1 != m_map_hist_sp.end(); it1++) {
314 SpillHistMap_t* map_hist = &it1->second;
315 for (SpillHistMap_t::iterator it2 = map_hist->begin(); it2 != map_hist->end(); it2++) {
319 m_map_hist_sp.clear();
328 void OnlMonClient::MakeSpillHist(
const int spill_id,
const int spill_id_new)
330 bool add_dir = TH1::AddDirectoryStatus();
331 TH1::AddDirectory(
false);
332 for (
unsigned int ih = 0; ih < m_hm->
nHistos(); ih++) {
334 string name = h1->GetName();
336 oss << name <<
"_sp" << spill_id;
337 m_map_hist_sp[name][spill_id] = (TH1*)h1->Clone(oss.str().c_str());
339 if (spill_id_new > 0) m_map_hist_sp[name][spill_id_new] = h1;
341 TH1::AddDirectory(add_dir);
348 void OnlMonClient::DisableSpillHist()
350 if (! m_make_sp_hist)
return;
351 if (m_map_hist_sp.size() > 0) {
353 MakeMergedHist(list_h1);
354 for (
unsigned int ih = 0; ih < m_hm->
nHistos(); ih++) {
357 h1->Add(list_h1.at(ih));
359 ClearHistList(list_h1);
362 m_make_sp_hist =
false;
366 void OnlMonClient::MakeMergedHist(HistList_t& list_h1,
const int sp_min,
const int sp_max)
368 bool add_dir = TH1::AddDirectoryStatus();
369 TH1::AddDirectory(
false);
370 ClearHistList(list_h1);
371 for (
unsigned int ih = 0; ih < m_hm->
nHistos(); ih++) {
372 TH1* h1_org = (TH1*)m_hm->
getHisto(ih);
373 string name = h1_org->GetName();
374 TH1* h1 = (TH1*)h1_org->Clone(name.c_str());
376 SpillHistMap_t* map_hist = &m_map_hist_sp[name];
377 for (SpillHistMap_t::iterator it = map_hist->begin(); it != map_hist->end(); it++) {
378 int sp_id = it->first;
379 if (sp_min > 0 && sp_id < sp_min)
continue;
380 if (sp_max > 0 && sp_id > sp_max)
continue;
381 if (name ==
"h1_basic_id") {
382 h1->SetBinContent(BIN_RUN , TMath::Max(h1->GetBinContent(BIN_RUN ), it->second->GetBinContent(BIN_RUN )));
383 h1->SetBinContent(BIN_SPILL, TMath::Max(h1->GetBinContent(BIN_SPILL), it->second->GetBinContent(BIN_SPILL)));
384 h1->SetBinContent(BIN_EVENT, TMath::Max(h1->GetBinContent(BIN_EVENT), it->second->GetBinContent(BIN_EVENT)));
385 int sp_curr = h1->GetBinContent(BIN_SPILL_MIN);
386 if (sp_curr <= 0 || sp_curr > sp_id) h1->SetBinContent(BIN_SPILL_MIN, sp_id);
387 sp_curr = h1->GetBinContent(BIN_SPILL_MAX);
388 if (sp_curr < sp_id) h1->SetBinContent(BIN_SPILL_MAX, sp_id);
390 if (it == --map_hist->end()) h1->Add(it->second);
395 list_h1.push_back(h1);
397 TH1::AddDirectory(add_dir);
400 int OnlMonClient::ReceiveHist()
403 if (! sock)
return 1;
420 oss <<
"SUBSYS:" <<
Name() <<
" " << sp_lo <<
" " << sp_hi;
421 sock->Send(oss.str().c_str());
423 ClearHistList(m_list_h1);
426 TMessage *mess =
NULL;
431 }
else if (mess->What() == kMESS_STRING) {
433 mess->ReadString(str, 200);
436 if (strcmp(str,
"Finished") == 0)
break;
437 else if (strcmp(str,
"NotReady") == 0) {
441 }
else if (mess->What() == kMESS_OBJECT) {
442 TClass* cla = mess->GetClass();
443 TObject* obj = mess->ReadObject(cla);
444 cout <<
" Receive: " << cla->GetName() <<
" " << obj->GetName() << endl;
445 m_list_h1.push_back( (TH1*)obj->Clone() );
456 void OnlMonClient::ClearHistList(HistList_t& list_h1)
458 for (HistList_t::iterator it = list_h1.begin(); it != list_h1.end(); it++) {
466 if (num >= m_n_can) {
467 cerr <<
"ERROR OnlMonClient::GetCanvas(): Num out of range (" << num <<
" >= " << m_n_can <<
"). Abort.";
470 return m_list_can[num];
473 void OnlMonClient::ClearCanvasList()
475 for (
int ii = 0; ii < m_n_can; ii++) {
476 if (m_list_can[ii]) {
477 delete m_list_can[ii];
483 int OnlMonClient::DrawCanvas(
const bool at_end)
488 if (m_h1_basic_id == 0 || m_h1_basic_cnt == 0 || ret != 0) {
489 cout <<
"WARNING: Cannot find OnlMon histogram(s)." << endl;
493 int run_id, spill_id, event_id, spill_id_min, spill_id_max;
494 GetBasicID(&run_id, &spill_id, &event_id, &spill_id_min, &spill_id_max);
497 for (
int ii = 0; ii < m_n_can; ii++) {
499 m_list_can[ii]->
SetBasicID(run_id, spill_id, event_id, spill_id_min, spill_id_max);
501 m_list_can[ii]->
PreDraw(at_end);
506 for (
int ii = 0; ii < m_n_can; ii++) {
virtual int EndOnlMon(PHCompositeNode *topNode)
void SetBasicCount(const int n_evt=0, const int n_sp=0)
OnlMonCanvas * GetCanvas(const int num=0)
void SetBasicID(const int run_id, const int spill_id=0, const int event_id=0, const int spill_id_min=0, const int spill_id_max=0)
int SendHist(TSocket *sock, int sp_min, int sp_max)
void GetBasicCount(int *n_evt=0, int *n_sp=0)
unsigned int nHistos() const
int process_event(PHCompositeNode *topNode)
pthread_mutex_t * GetMutex()
virtual int InitOnlMon(PHCompositeNode *topNode)
int registerHistoManager(Fun4AllHistoManager *manager)
void AddSpill(const int id)
void GetSpillRange(int &sp_lo, int &sp_hi)
void PreDraw(const bool at_end=false)
TSocket * ConnectServer()
void RegisterHist(TH1 *h1, const HistMode_t mode=MODE_ADD)
void SetSpillSelectability(const bool val)
static std::string GetOnlMonDir()
virtual const std::string Name() const
Returns the name of this module.
virtual int FindAllMonHist()
TNamed * getHisto(const std::string &hname) const
int InitRun(PHCompositeNode *topNode)
int dumpHistos(const std::string &filename="", const std::string &openmode="RECREATE")
virtual int ProcessEventOnlMon(PHCompositeNode *topNode)
void PostDraw(const bool at_end=false)
void GetBasicID(int *run_id=0, int *spill_id=0, int *event_id=0, int *spill_id_min=0, int *spill_id_max=0)
An SQ interface class to hold one event header.
void FindFullSpillRange(int &id_min, int &id_max)
virtual int get_run_id() const
Return the run ID.
bool registerHisto(const std::string &hname, TNamed *h1d, const int replace=0)
static OnlMonComm * instance()
virtual OnlMonClient * Clone()
int End(PHCompositeNode *topNode)
Called at the end of all processing.
int Init(PHCompositeNode *topNode)
An SQ interface class to hold the run-level info.
static bool GetClearUsFlag()
static OnlMonServer * instance()
TH1 * FindMonHist(const std::string name, const bool non_null=true)
virtual int DrawMonitor()
virtual int InitRunOnlMon(PHCompositeNode *topNode)
Base class for the OnlMon subsystem module.