AdcRoiToTree_tool.cc
Go to the documentation of this file.
1 // AdcRoiToTree_tool.cc
2 
3 #include "AdcRoiToTree.h"
4 #include "TFile.h"
5 #include "TTree.h"
6 #include <iostream>
7 
8 using std::string;
9 using std::cout;
10 using std::endl;
12 
13 //**********************************************************************
14 // Class methods.
15 //**********************************************************************
16 
18 : m_LogLevel(ps.get<int>("LogLevel")),
19  m_OutFile(ps.get<Name>("OutFile")),
20  m_MetadataFields(ps.get<NameVector>("MetadataFields"))
21 {
22  const string myname = "AdcRoiToTree::ctor: ";
23  if ( m_LogLevel >=2 ) cout << myname << "Creating output file." << endl;
24  TFile* pfil = TFile::Open(m_OutFile.c_str(), "RECREATE");
25  if ( pfil == nullptr || ! pfil->IsOpen() ) {
26  cout << myname << "ERROR: Unable to create output file " << m_OutFile << endl;
27  } else {
28  TreeData tdat;
29  TTree* ptre = new TTree(treeName().c_str(), "ADC ROIs");
30  ptre->Branch("run", &tdat.run);
31  ptre->Branch("event", &tdat.event);
32  ptre->Branch("channel", &tdat.channel);
33  ptre->Branch("status", &tdat.status);
34  tdat.mdata.resize(m_MetadataFields.size());
35  float* pmd = &tdat.mdata[0];
36  for ( Name mnam : m_MetadataFields ) {
37  ptre->Branch(mnam.c_str(), pmd++);
38  }
39  ptre->Branch("nroi", &tdat.nroi);
40  ptre->Branch("nsam", &tdat.nsam[0], "nsam[nroi]/i");
41  ptre->Branch("isam", &tdat.isam[0], "isam[nroi]/i");
42  ptre->Branch("qroi", &tdat.qroi[0], "qroi[nroi]/F");
43  ptre->Branch("hmin", &tdat.hmin[0], "hmin[nroi]/F");
44  ptre->Branch("hmax", &tdat.hmax[0], "hmax[nroi]/F");
45  ptre->ResetBranchAddresses();
46  ptre->Write();
47  pfil->Close();
48  }
49  delete pfil;
50  // Display the configuration.
51  if ( m_LogLevel>= 1 ) {
52  cout << myname << " LogLevel: " << m_LogLevel << endl;
53  cout << myname << " OutFile: " << m_OutFile << endl;
54  cout << myname << " MetadataFields: [";
55  bool first = true;
56  for ( Name mnam : m_MetadataFields ) {
57  if ( first ) first = false;
58  else cout << ", ";
59  cout << mnam;
60  }
61  cout << "]" << endl;
62  }
63 }
64 
65 //**********************************************************************
66 
68  const string myname = "AdcRoiToTree::dtor: ";
69  if ( m_LogLevel >= 1 ) {
70  cout << myname << "Exiting." << endl;
71  TFile* pfil = TFile::Open(m_OutFile.c_str(), "READ");
72  if ( pfil == nullptr || ! pfil->IsOpen() ) {
73  cout << myname << "ERROR: Unable to open output file " << m_OutFile << endl;
74  } else {
75  TTree* ptre = dynamic_cast<TTree*>(pfil->Get(treeName().c_str()));
76  if ( ptre == nullptr ) {
77  cout << myname << "ERROR: Unable to open tree " << treeName() << endl;
78  } else {
79  cout << myname << "Tree entry count is " << ptre->GetEntries() << endl;
80  }
81  }
82  delete pfil;
83  }
84 }
85 
86 //**********************************************************************
87 
89  const string myname = "AdcRoiToTree::viewMap: ";
90  DataMap ret;
91  TFile* pfil = TFile::Open(m_OutFile.c_str(), "UPDATE");
92  if ( pfil == nullptr || ! pfil->IsOpen() ) {
93  cout << myname << "ERROR: Unable to open output file " << m_OutFile << endl;
94  return ret.setStatus(1);
95  }
96  TTree* ptre = dynamic_cast<TTree*>(pfil->Get(treeName().c_str()));
97  if ( ptre == nullptr ) {
98  cout << myname << "ERROR: Unable to open tree " << treeName() << endl;
99  return ret.setStatus(2);
100  }
101  Index nfill = 0;
102  Index maxroi = 1000;
103  TreeData tdat;
104  tdat.nsam.resize(maxroi);
105  tdat.isam.resize(maxroi);
106  tdat.qroi.resize(maxroi);
107  tdat.hmin.resize(maxroi);
108  tdat.hmax.resize(maxroi);
109  ptre->SetBranchAddress("run", &tdat.run);
110  ptre->SetBranchAddress("event", &tdat.event);
111  ptre->SetBranchAddress("channel", &tdat.channel);
112  ptre->SetBranchAddress("status", &tdat.status);
113  tdat.mdata.resize(m_MetadataFields.size());
114  float* pmd = &tdat.mdata[0];
115  for ( Name mnam : m_MetadataFields ) {
116  ptre->SetBranchAddress(mnam.c_str(), pmd++);
117  }
118  ptre->SetBranchAddress("nroi", &tdat.nroi);
119  ptre->SetBranchAddress("nsam", &tdat.nsam[0]);
120  ptre->SetBranchAddress("isam", &tdat.isam[0]);
121  ptre->SetBranchAddress("qroi", &tdat.qroi[0]);
122  ptre->SetBranchAddress("hmin", &tdat.hmin[0]);
123  ptre->SetBranchAddress("hmax", &tdat.hmax[0]);
124  //ptre->SetBranchAddress("nsample", pflt, "nsample[nroi]/F");
125  for ( const auto& iacd : acds ) {
126  const AdcChannelData& acd = iacd.second;
127  tdat.run = acd.run();
128  tdat.event = acd.event();
129  tdat.channel = acd.channel();
130  tdat.status = acd.channelStatus();
131  Index idat = 0;
132  for ( Name mnam : m_MetadataFields ) {
133  if ( ! acd.hasMetadata(mnam) ) {
134  cout << myname << "WARNING: Run/event/channel "
135  << acd.run() << "/" << acd.event() << "/" << acd.channel()
136  << " does not have metadata field " << mnam << endl;
137  }
138  tdat.mdata[idat++] = acd.getMetadata(mnam, 0.0);
139  }
140  tdat.nroi = acd.rois.size();
141  Index nroi = std::min(tdat.nroi, maxroi);
142  for ( Index iroi=0; iroi<nroi; ++iroi ) {
143 
144  Index isam1 = acd.rois[iroi].first;
145  Index isam2 = acd.rois[iroi].second;
146  float qroi = 0.0;
147  float hmin = 0.0;
148  float hmax = 0.0;
149  bool haveSamples = false;
150  if ( acd.samples.size() > isam2 ) {
151  for ( Index isam=isam1; isam<=isam2; ++isam ) {
152  float qsam = acd.samples[isam];
153  qroi += qsam;
154  if ( haveSamples ) {
155  if ( qsam < hmin ) hmin = qsam;
156  if ( qsam > hmax ) hmax = qsam;
157  } else {
158  hmin = qsam;
159  hmax = qsam;
160  haveSamples = true;
161  }
162  }
163  } else {
164  cout << myname << "WARNING: Ignoring missing samples for run " << acd.run()
165  << ", event " << acd.event() << ", channel " << acd.channel() << endl;
166  }
167  tdat.nsam[iroi] = 1 + isam2 - isam1;
168  tdat.isam[iroi] = isam1;
169  tdat.qroi[iroi] = qroi;
170  tdat.hmin[iroi] = hmin;
171  tdat.hmax[iroi] = hmax;
172  }
173  if ( m_LogLevel >= 3 ) {
174  cout << myname << "Filling run " << tdat.run << ", event " << tdat.event
175  << ", channel " << tdat.channel << ", nroi " << tdat.nroi << endl;
176  if ( m_LogLevel >= 4 ) {
177  string spre = myname + " nsam: [";
178  for ( Index iroi=0; iroi<nroi; ++iroi ) {
179  cout << spre;
180  cout << tdat.nsam[iroi];
181  spre = ", ";
182  }
183  cout << "]" << endl;
184  spre = myname + " isam: [";
185  for ( Index iroi=0; iroi<nroi; ++iroi ) {
186  cout << spre;
187  cout << tdat.isam[iroi];
188  spre = ", ";
189  }
190  cout << "]" << endl;
191  spre = myname + " qroi: [";
192  for ( Index iroi=0; iroi<nroi; ++iroi ) {
193  cout << spre;
194  cout << tdat.qroi[iroi];
195  spre = ", ";
196  }
197  cout << "]" << endl;
198  }
199  }
200  ptre->Fill();
201  }
202  ptre->ResetBranchAddresses();
203  ptre->Write();
204  gDirectory->Purge();
205  pfil->Close();
206  delete pfil;
207  ret.setInt("art_nfill", nfill);
208  return ret;
209 }
210 
211 //**********************************************************************
212 
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
Name treeName() const
Definition: AdcRoiToTree.h:58
NameVector m_MetadataFields
Definition: AdcRoiToTree.h:65
AdcRoiVector rois
bool hasMetadata(Name mname) const
AdcIndex run() const
float getMetadata(Name mname, float def=0.0) const
void setInt(Name name, int val)
Definition: DataMap.h:131
AdcIndex event() const
static constexpr double ps
Definition: Units.h:99
Index channelStatus() const
Channel channel() const
std::string Name
Definition: AdcRoiToTree.h:26
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
~AdcRoiToTree() override
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
DataMap viewMap(const AdcChannelDataMap &acds) const override
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
std::vector< Name > NameVector
Definition: AdcRoiToTree.h:27
AdcRoiToTree(fhicl::ParameterSet const &ps)