T2StatFileManager.cc
Go to the documentation of this file.
1 
8 #include "T2StatFileManager.h"
9 
10 #include <utl/Reader.h>
11 #include <utl/ErrorLogger.h>
12 
13 #include <fwk/CentralConfig.h>
14 
15 #include <sstream>
16 
17 #include <TChain.h>
18 #include <TDirectory.h>
19 #include <TFile.h>
20 #include <TEventList.h>
21 
22 using namespace fwk;
23 using namespace det;
24 using namespace sdet;
25 using namespace utl;
26 using namespace std;
27 
28 
29 REGISTER_S_MANAGER("T2StatFileManager", T2StatFileManager);
30 
31 
32 T2StatFileManager::~T2StatFileManager()
33 {
34  delete fDataTree;
35  delete fDir;
36 }
37 
38 
39 void
40 T2StatFileManager::Init(const std::string& configLink)
41 {
42  VManager::Init(configLink);
43 
44  INFO_TERSE("Initializing T2StatFileManager ... ");
45 
46  fVerbosity = 0;
47  if (fBranch.GetChild("verbosity")) {
48  fBranch.GetChild("verbosity").GetData(fVerbosity);
49  cout << " T2StatFileManager verbosity level = " << fVerbosity << endl;
50  }
51 
52  fCommsCrisis = 0;
53  if (fBranch.GetChild("CommsCrisisTreat")) {
54  fBranch.GetChild("CommsCrisisTreat").GetData(fCommsCrisis);
55  }
56 
57  if (fVerbosity)
58  cout << " Comms-Crisis Treatment = " << fCommsCrisis << endl;
59 
60  fBranch.GetChild("files").GetData(fAllT2Files);
61 
62  if (fVerbosity) {
63  ostringstream msg;
64  msg << fAllT2Files.size() <<" T2Stat files to be processed.";
65  INFO(msg);
66  }
67 
68  //Add all files to Chain
69  fDataTree = new TChain("TreeT2Stat");
70  for (unsigned int i = 0; i < fAllT2Files.size(); ++i) {
71  TFile* const file = TFile::Open(fAllT2Files[i].c_str(), "READ");
72  if (!file || file->IsZombie()) {
73  ostringstream err;
74  err << " T2StatFileManager input file=" << fAllT2Files[i].c_str()
75  << " is corrupt! This file will be skipped!";
76  INFO_TERSE(err);
77  } else {
78  if (fVerbosity)
79  cout << " Adding T2Stat file: " << fAllT2Files[i].c_str() << " ..."<<endl;
80  fDataTree->Add(fAllT2Files[i].c_str());
81  }
82  }
83 
84  if (fVerbosity) {
85  ostringstream msg;
86  msg << " T2Stat files added.";
87  INFO(msg);
88  }
89 
90  if (fDataTree) {
91  // list of properties
92  fAvailableComponents.insert("station_in_acquisition");
93 
94  const unsigned int nentries = fDataTree->GetEntries();
95 
96  if (fVerbosity) {
97  ostringstream msg;
98  msg << nentries <<" entries to be processed... It can take a while..." << endl;
99  INFO(msg);
100  }
101 
102  unsigned int treeTimeIn;
103  unsigned int treeTimeOut;
104  unsigned int treeId;
105 
106  fDataTree->SetBranchAddress("Id", &treeId);
107  fDataTree->SetBranchAddress("tIn", &treeTimeIn);
108  fDataTree->SetBranchAddress("tOut", &treeTimeOut);
109 
110  //Create eventlist for each station, and compute startGPS and endGPS limits.
111  fDir = new TDirectory("dir", "dir", 0); // Directory to hold eventlist.
112  for (unsigned int i = 0; i < nentries; ++i) {
113  fDataTree->GetEntry(i);
114  if (!i || treeTimeIn < fstartGPS)
115  fstartGPS = treeTimeIn;
116  if (!i || treeTimeOut > fendGPS)
117  fendGPS = treeTimeOut;
118 
119  ostringstream listname;
120  listname << "Station_" << treeId;
121 
122  if (fDir->FindObject(listname.str().c_str())) { // Add to the list
123  TEventList* const list = (TEventList*)fDir->FindObject(listname.str().c_str());
124  if (fVerbosity > 4)
125  cout << " Adding to StationList: " << listname.str().c_str() << endl;
126  //list->Enter(fDataTree->GetChainEntryNumber(i));
127  list->Enter(fDataTree->GetEntryNumber(i));
128  } else { // Create new list for that station
129  TEventList* const list = new TEventList(listname.str().c_str(), listname.str().c_str(), 0, 0);
130  if (fVerbosity > 3)
131  cout << " Creating StationList: " << listname.str().c_str() << endl;
132  list->Enter(fDataTree->GetEntryNumber(i));
133  //list->Enter(fDataTree->GetChainEntryNumber(i));
134  list->DirectoryAutoAdd(fDir);
135  fStationList.push_back(treeId); // Save station id of stations
136  }
137  } // Loop over entries in chain
138  }
139 
140  INFO_TERSE("Initialization complete.");
141 }
142 
143 
145 T2StatFileManager::GetOkFlag(int& returnData,
146  const std::string& componentProperty,
147  const std::string& componentName,
148  const IndexMap& componentIndex)
149  const
150 {
151  if (!fDataTree ||
152  fAvailableComponents.find(componentProperty) == fAvailableComponents.end()) {
153  return VManager::eNotFound;
154  }
155 
156  if (!ReadData()) {
157  ostringstream warn;
158  warn << "*****************************************************\n"
159  " property=" << componentProperty << " component=" << componentName
160  << " index=(";
161  for (IndexMap::const_iterator imap = componentIndex.begin();
162  imap != componentIndex.end(); ++imap) {
163  warn << "\"" << imap->first << "\"=\"" << imap->second << "\" ";
164  }
165  warn << ") NOT found in data source for specified time!\n"
166  "Returning zero\n"
167  "*****************************************************";
168  WARNING(warn);
169  returnData = 0;
170  return VManager::eNotFound;
171  }
172 
173  if (componentProperty == "station_in_acquisition") {
174  if (componentName == "is_in_acquisition") {
175  returnData = GetStationStatus(componentIndex);
176  return VManager::eFound;
177  }
178  }
179 
180  return VManager::eNotFound;
181 }
182 
183 
184 bool
185 T2StatFileManager::ReadData()
186  const
187 {
188  if (fVerbosity > 2)
189  INFO( " enter ReadData ");
190 
191  const unsigned int now = det::Detector::GetInstance().GetTime().GetGPSSecond();
192  if (now < fstartGPS || now > fendGPS) {
193  ostringstream err;
194  err << "Detector Time not found! GPSTime=" << now << ", T2File limits: " << fstartGPS << " - "<< fendGPS;
195  ERROR(err);
196  return false;
197  }
198  return true;
199 }
200 
201 
202 int
203 T2StatFileManager::GetStationStatus(const IndexMap& componentIndex)
204  const
205 {
206  const IndexMap::const_iterator iStationId = componentIndex.find("stationId");
207  if (iStationId == componentIndex.end()) {
208  INFO_TERSE("Called T2StatFileManager with wrong number of arguments!");
209  return -1;
210  }
211 
212  const unsigned int stationId = boost::lexical_cast<unsigned int>(iStationId->second);
213 
214  int isActive = -1;
215  bool stationComm = false;
216 
217  for (unsigned int i = 0; i < fStationList.size(); ++i)
218  if (fStationList[i] == stationId)
219  stationComm = true;
220 
221  if (fVerbosity > 2)
222  cout << "Looking for station: " << stationId << endl;
223 
224  if (stationComm) {
225  isActive = 0;
226  const unsigned int now = det::Detector::GetInstance().GetTime().GetGPSSecond();
227 
228  unsigned int treeTimeIn;
229  unsigned int treeTimeOut;
230  unsigned int treeId;
231 
232  fDataTree->SetBranchAddress("Id", &treeId);
233  fDataTree->SetBranchAddress("tIn", &treeTimeIn);
234  fDataTree->SetBranchAddress("tOut", &treeTimeOut);
235 
236  //Get List of station id
237  ostringstream listname;
238  listname << "Station_" << stationId;
239  TEventList* const list = (TEventList*)fDir->FindObject(listname.str().c_str());
240  const int entries = list->GetN();
241  if (fVerbosity > 2)
242  cout << entries << " entries for station: " << stationId << endl;
243  for (int i = 0; i < entries; ++i) { // Iteration only over the selected station.
244  fDataTree->GetEntry(list->GetEntry(i));
245  if (treeId != stationId) {
246  ostringstream msg;
247  msg << "This should never happen!!! Found id " << treeId
248  << " in " << listname.str() << " list";
249  ERROR(msg);
250  }
251  if (now < treeTimeIn)
252  continue;
253  if (now > treeTimeOut)
254  continue;
255  if (fCommsCrisis && treeTimeOut-treeTimeIn < 120)
256  continue;
257  isActive = 1;
258  break;
259  }
260  } else {
261  if (fVerbosity > 1) {
262  ostringstream warn;
263  warn << "*****************************************************\n"
264  " Station: " << stationId << " not commisioned\n"
265  "*****************************************************";
266  WARNING(warn);
267  }
268  isActive = 0;
269  }
270  return isActive;
271 }
#define INFO_TERSE(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:175
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
void Init()
Initialise the registry.
#define REGISTER_S_MANAGER(_name_, _Type_)
#define WARNING(message)
Macro for logging warning messages.
Definition: ErrorLogger.h:163
const string file
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127

, generated on Tue Sep 26 2023.