Belle II Software  release-05-02-19
EclDisplayModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Mikhail Remnev, Dmitry Matvienko *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 //This module
12 #include <ecl/modules/eclDisplay/EclDisplayModule.h>
13 
14 //Root
15 #include <TApplication.h>
16 #include <TSystem.h>
17 #include <TFile.h>
18 
19 //Framework
20 #include <framework/utilities/EnvironmentVariables.h>
21 
22 //ECL
23 #include <ecl/dataobjects/ECLCalDigit.h>
24 #include <ecl/modules/eclDisplay/EclFrame.h>
25 #include <ecl/modules/eclDisplay/EclData.h>
26 #include <ecl/modules/eclDisplay/geometry.h>
27 
28 using namespace Belle2;
29 using namespace ECLDisplayUtility;
30 
31 //-----------------------------------------------------------------
32 // Register the Module
33 //-----------------------------------------------------------------
34 REG_MODULE(EclDisplay)
35 
36 //-----------------------------------------------------------------
37 // Implementation
38 //-----------------------------------------------------------------
39 
40 EclDisplayModule::EclDisplayModule() : Module()
41 {
42  // Set module properties
43  setDescription("Event display module for ECL.");
44 
45  // Parameter definitions
46  addParam("showDisplay", m_showDisplay,
47  "Show GUI. Off by default because GUI crashes automatic tests.", false);
48  addParam("keepOpen", m_keepOpen,
49  "Keep window open after all events have been processed", false);
50  addParam("displayEnergy", m_displayEnergy,
51  "If true, energy distribution per channel (shaper, crate) is displayed. Otherwise, number of counts is displayed", false);
52  addParam("displayMode", m_displayMode,
53  "Default display mode. Can be later changed in GUI.", 9);
54  addParam("autoDisplay", m_autoDisplay,
55  "If true, events are displayed as soon as they are loaded.", true);
56  addParam("InitFileName", m_eclMapperInitFileName,
57  "Initialization file for eclMapper", std::string("ecl/data/ecl_channels_map.txt"));
58 
59  m_evtNum = 0;
60 }
61 
63 {
64 }
65 
67 {
68  m_eclarray.isRequired();
69 
70  if (!m_showDisplay) {
71  m_frame_closed = true;
72  } else {
73  // Check if X display is available.
74  std::string display = EnvironmentVariables::get("DISPLAY", "");
75  if (display == "") {
76  B2WARNING("Environment variable DISPLAY is not set, event display won't be opened");
77  m_frame_closed = true;
78  }
79  }
80 
81  if (!m_frame_closed)
82  initFrame();
83 }
84 
86 {
87  //== Init temporary file so TTree is not kept in memory.
88  m_tempname = "ecldisplay_tmp";
89 
90  if (gSystem->TempFileName(m_tempname) == 0) {
91  throw std::runtime_error("ECLDisplay: failed to create temp file.");
92  } else {
93  m_tempfile = new TFile(m_tempname, "recreate");
94  gSystem->Unlink(m_tempname);
95  }
96 
97  SetMode(m_displayEnergy);
98  m_app = new TApplication("ECLDisplay App", 0, 0);
99  m_data = new EclData();
101 
102  m_frame->Connect("CloseWindow()", "Belle2::EclDisplayModule", this, "handleClosedFrame()");
103 
104  B2DEBUG(100, "EclDisplayModule::create ECLFrame");
105 }
106 
108 {
109  m_frame_closed = true;
110 }
111 
113 {
114  // Initialize channel mapper at run start to account for possible
115  // changes in ECL mapping between runs.
116  if (!m_mapper.initFromDB()) {
117  B2FATAL("ECL Display:: Can't initialize eclChannelMapper");
118  }
119 }
120 
122 {
123  // EclFrame is closed, skipping data reading.
124  if (m_frame_closed) return;
125 
126  int added_entries = 0;
127 
128  for (int i = 0; i < m_eclarray.getEntries(); i++) {
129  ECLCalDigit* record = m_eclarray[i];
130  if (record->getEnergy() >= 1e-4) { //TODO: Move to constant ENERGY_THRESHOLD.
131  if (m_data->addEvent(record, m_evtNum) == 0) {
132  added_entries++;
133  }
134  }
135  }
136 
137  if (m_autoDisplay) {
138  m_data->update(true);
139  gSystem->ProcessEvents();
140  if (!m_frame_closed)
141  m_frame->loadNewData();
142  }
143  if (added_entries > 0)
144  m_evtNum++;
145 }
146 
148 {
149 }
150 
152 {
153  if (m_keepOpen) {
154  if (!m_frame_closed) {
155  m_data->update(false);
156  m_frame->loadNewData();
157  }
158 
159  while (!m_frame_closed) {
160  gSystem->ProcessEvents();
161  gSystem->Sleep(0);
162  }
163  }
164 
165  if (m_frame) delete m_frame;
166  if (m_data) delete m_data;
167 }
168 
Belle2::ECLCalDigit::getEnergy
double getEnergy() const
Get Calibrated Energy.
Definition: ECLCalDigit.h:134
Belle2::EclDisplayModule::m_tempfile
TFile * m_tempfile
Temporary file to store TTree.
Definition: EclDisplayModule.h:118
Belle2::EclDisplayModule::m_eclarray
StoreArray< ECLCalDigit > m_eclarray
Displayed ECL events.
Definition: EclDisplayModule.h:114
Belle2::EclDisplayModule::m_showDisplay
bool m_showDisplay
Show GUI.
Definition: EclDisplayModule.h:89
Belle2::ECLCalDigit
Class to store calibrated ECLDigits: ECLCalDigits.
Definition: ECLCalDigit.h:38
Belle2::EclDisplayModule::m_displayMode
int m_displayMode
Default display mode.
Definition: EclDisplayModule.h:96
Belle2::EclDisplayModule::m_frame_closed
bool m_frame_closed
Flag to check if EclFrame is closed;.
Definition: EclDisplayModule.h:103
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::EclDisplayModule::m_keepOpen
bool m_keepOpen
Keep window open after all events have been processed.
Definition: EclDisplayModule.h:91
Belle2::EclDisplayModule::m_tempname
TString m_tempname
Name of temporary file.
Definition: EclDisplayModule.h:120
Belle2::EclData::update
void update(bool reset_event_ranges=false)
Update time_min, time_max, event_counts and energy_sums.
Definition: EclData.cc:368
Belle2::EclDisplayModule::m_displayEnergy
bool m_displayEnergy
If true, energy distribution in ECL is displayed.
Definition: EclDisplayModule.h:93
Belle2::EclData::addEvent
int addEvent(ECLCalDigit *event, int evtn)
Add ECLDigit event to inner TTree (m_tree).
Definition: EclData.cc:428
Belle2::EclDisplayModule::handleClosedFrame
void handleClosedFrame()
This method is called when EclFrame is closed.
Definition: EclDisplayModule.cc:107
Belle2::EclDisplayModule::initialize
virtual void initialize() override
Initialize EclChannelMapper.
Definition: EclDisplayModule.cc:66
Belle2::EclDisplayModule::terminate
virtual void terminate() override
Wait till EclFrame is closed then free allocated resources.
Definition: EclDisplayModule.cc:151
Belle2::EnvironmentVariables::get
static std::string get(const std::string &name, const std::string &fallback="")
Get the value of an environment variable or the given fallback value if the variable is not set.
Definition: EnvironmentVariables.cc:35
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::EclDisplayModule::initFrame
void initFrame()
Initialize EclFrame.
Definition: EclDisplayModule.cc:85
Belle2::EclFrame
Root TGMainFrame that contains multiple widgets that display the ECLSimHit's w.r.t.
Definition: EclFrame.h:43
Belle2::EclDisplayModule::m_app
TApplication * m_app
Application to contain EclFrame.
Definition: EclDisplayModule.h:109
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::EclDisplayModule::m_frame
EclFrame * m_frame
Root GUI to display ECL data.
Definition: EclDisplayModule.h:107
Belle2::EclDisplayModule::m_data
EclData * m_data
Class that provides interface for quick and comprehensive analysis of large number of events.
Definition: EclDisplayModule.h:112
Belle2::EclFrame::loadNewData
void loadNewData()
Update view of the data.
Definition: EclFrame.cc:378
Belle2::EclDisplayModule::~EclDisplayModule
virtual ~EclDisplayModule()
Module destructor.
Definition: EclDisplayModule.cc:62
Belle2::EclData
This class contains data for ECLSimHit's and provides several relevant conversion functions for bette...
Definition: EclData.h:41
Belle2::ECL::ECLChannelMapper::initFromDB
bool initFromDB()
Initialize channel mapper from the conditions database.
Definition: ECLChannelMapper.cc:105
Belle2::EclDisplayModule::m_mapper
ECL::ECLChannelMapper m_mapper
Channel mapper to show channel <-> (crate, shaper) distributions.
Definition: EclDisplayModule.h:116
Belle2::EclDisplayModule::beginRun
virtual void beginRun() override
Empty method.
Definition: EclDisplayModule.cc:112
Belle2::EclDisplayModule::m_evtNum
int m_evtNum
Counter of added events.
Definition: EclDisplayModule.h:105
Belle2::EclDisplayModule::m_autoDisplay
bool m_autoDisplay
If true, events are displayed as soon as they are loaded.
Definition: EclDisplayModule.h:98
Belle2::EclDisplayModule::endRun
virtual void endRun() override
Empty method.
Definition: EclDisplayModule.cc:147
Belle2::EclDisplayModule::event
virtual void event() override
Handle event.
Definition: EclDisplayModule.cc:121