Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
io_online.hh
Go to the documentation of this file.
1 #ifndef IOONLINEINCLUDED
2 #define IOONLINEINCLUDED
3 
8 
13 
14 #include "TStreamerInfo.h"
15 #include "TFile.h"
16 #include "TTree.h"
18 
19 #include <map>
20 #include <vector>
21 
22 
23 using namespace KM3NETDAQ;
24 
25 /**
26  * Read a hit from a DAQ hit.
27  *
28  * \param hit hit
29  * \param daqhit DAQ hit
30  */
31 inline void read(Hit& hit, const JDAQHit& daqhit )
32 {
33  hit.channel_id = daqhit.getPMT();
34  hit.tot = daqhit.getToT();
35  hit.tdc = daqhit.getT(); // GetT() just return the bare TDC
36 }
37 
38 /**
39  * Read a hit from a DAQ key hit.
40  *
41  * \param hit hit
42  * \param daqhit DAQ key hit
43  */
44 inline void read(Hit& hit, const JDAQKeyHit& daqhit )
45 {
46  hit.id = hit.pmt_id = 0;
47 
48  hit.dom_id = daqhit.getModuleID();
49  hit.channel_id = daqhit.getPMT();
50  hit.tot = daqhit.getToT();
51  hit.tdc = daqhit.getT(); // GetT() just return the bare TDC
52 }
53 
54 /**
55  * Read an event from a DAQ event.
56  *
57  * \param evt evt
58  * \param de DAQ event
59  */
60 inline void read(Evt& evt, const JDAQEvent& de)
61 {
62  evt.run_id = de.getRunNumber();
63  evt.det_id = de.getDetectorID();
64  evt.frame_index = de.getFrameIndex();
65  evt.trigger_counter = de.getCounter();
66  evt.overlays = de.getOverlays();
67  evt.trigger_mask = de.getTriggerMask();
68  evt.t.SetSec( de.getTimesliceStart().getUTCseconds() );
69  evt.t.SetNanoSec( de.getTimesliceStart().getUTC16nanosecondcycles() * 16 );
70 
71  // The only way to know the hits that are also in the triggeredhits collection
72  // is by dom and channel id and time.
73 
74  Hit h;
76 
77  const std::vector<JDAQSnapshotHit>& snapshotHits = de.getHits<JDAQSnapshotHit>();
78  const std::vector<JDAQTriggeredHit>& triggeredHits = de.getHits<JDAQTriggeredHit>();
79 
80  // http://stackoverflow.com/questions/10735135/reallocation-in-stdvector-after-stdvector-reserve
81  evt.hits.clear();
82  evt.hits.reserve(snapshotHits.size());
83 
84  for (auto& daqhit : snapshotHits ) // JDAQSnapshotHit
85  {
86  read( h, daqhit );
87  h.trig = 0;
88  evt.hits.push_back( h );
89  M[ h.dom_id ][ h.channel_id ][ h.tdc ] = &(*evt.hits.rbegin());
90  }
91 
92  for (auto& daqtrighit : triggeredHits)
93  {
94  Hit* g = M[daqtrighit.getModuleID()][daqtrighit.getPMT()][daqtrighit.getT()];
95 
96  if (g)
97  g->trig = daqtrighit.getTriggerMask ();
98  else
99  THROW(Exception, "Failed to flag snaphot hit " << (int) daqtrighit.getModuleID() << "." << (int) daqtrighit.getPMT() << " " << daqtrighit.getT());
100  }
101 }
102 
103 /**
104  * Read an event from a DAQ time slice.
105  *
106  * \param evt evt
107  * \param ts DAQ time slice
108  */
109 
110 inline void read( Evt& evt, const JDAQTimeslice& ts )
111 {
112  evt.run_id = ts.getRunNumber();
113  evt.det_id = ts.getDetectorID();
114  evt.frame_index = ts.getFrameIndex();
115  evt.t.SetSec( ts.getTimesliceStart().getUTCseconds() );
116  evt.t.SetNanoSec( ts.getTimesliceStart().getUTC16nanosecondcycles() * 16 );
117  evt.hits.clear();
118 
119  // a timeslice is a vector of JDAQSuperFrame's, which is a JDAQFrame, which
120  // is an stl::vector-like object (supporting stl-like iteration.)
121 
122  Hit h; h.id = 0; h.pmt_id = 0;
123 
124  for (auto& sf : ts )
125  {
126  for (auto& daqhit : sf )
127  {
128  read( h, daqhit);
129  h.dom_id = sf.getModuleID();
130  evt.hits.push_back(h);
131  }
132  }
133 }
134 
135 
136 /**
137  * Get summary slice from given file with given frame index. This function will
138  * (re)build an index each time it encounters a new TFile as input.
139  *
140  * \param f pointer to ROOT file
141  * \param frame_index frame index
142  * \return pointer to summary slice
143  */
144 
145 inline JDAQSummaryslice* get_summary_slice( TFile* f , int frame_index )
146 {
147  static TFile* _f = 0;
148  static TTree* S = 0;
149  static TBranch* BS = 0;
150  JDAQSummaryslice *r = 0 ;
151 
152  if (!f) THROW(Exception, "get_summary_slice called with TFile pointer that is null");
153 
154 
155  // in principle, event if the pointer-value is the same, we could have been given a new file
156 
157  if ( !_f || _f->GetUUID().Compare( f->GetUUID() ) != 0 ) // setup for tree reading and build tree index
158  {
159  _f = f;
160 
161  // first we have to deal with the following....
162  // The streamer of JDAQSummaryFrame needs to know what to do since
163  // this information is not written to root file.
164 
165  const char* name = JDAQSummaryslice::Class()->GetName();
166 
167  JDAQSummaryFrame::ROOT_IO_VERSION = ((TStreamerInfo*)_f -> GetStreamerInfoList()->FindObject(name))->GetClassVersion();
168 
169  S = (TTree*) _f->Get( TTREE_ONLINE_SUMMARYSLICE );
170 
171  if (!S)
172  {
173  THROW(Exception, "Failed to get summary slice TTree : " << TTREE_ONLINE_SUMMARYSLICE );
174  }
175 
176  BS = S->GetBranch( TBRANCH_ONLINE_SUMMARYSLICE );
177 
178  if (!BS)
179  {
180  THROW(Exception, "Failed to get brach :" << TBRANCH_ONLINE_SUMMARYSLICE );
181  }
182 
183  std::cout << "building index to lookup summary slices..." << std::endl;
184  int n = S->BuildIndex("frame_index");
185  (void) n;
186  BS->SetAddress( &r );
187  }
188 
189  int nbytes = S->GetEntryWithIndex( frame_index ); // returns -1 if not found
190  if ( nbytes <= 0 )
191  {
192  THROW(Exception, "Failed to find summary slice entry with frame_index " << frame_index);
193  }
194  return r;
195 }
196 
197 
198 #endif
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
General exception.
Definition: Exception.hh:13
int getDetectorID() const
Get detector identifier.
int getRunNumber() const
Get run number.
JDAQUTCExtended getTimesliceStart() const
Get start of timeslice.
int getFrameIndex() const
Get frame index.
unsigned int getOverlays() const
Get number of overlays.
static JTriggerMask_t getTriggerMask(const JDAQTriggeredHit &hit)
Get trigger mask of given hit.
Definition: JDAQEvent.hh:226
const std::vector< T > & getHits() const
Get container with hits.
Hit data structure.
Definition: JDAQHit.hh:35
JPMT_t getPMT() const
Get PMT.
Definition: JDAQHit.hh:75
JTDC_t getT() const
Get time.
Definition: JDAQHit.hh:86
JTOT_t getToT() const
Get time-over-threshold.
Definition: JDAQHit.hh:97
int getModuleID() const
Get module identifier.
static int ROOT_IO_VERSION
Streamer version of JDAQSummaryslice as obtained from ROOT file.
JTriggerCounter_t getCounter() const
Get trigger counter.
uint32_t getUTC16nanosecondcycles() const
Get minor time.
uint32_t getUTCseconds() const
Get major time.
JDAQSummaryslice * get_summary_slice(TFile *f, int frame_index)
Get summary slice from given file with given frame index.
Definition: io_online.hh:145
void read(Hit &hit, const JDAQHit &daqhit)
Read a hit from a DAQ hit.
Definition: io_online.hh:31
const int n
Definition: JPolint.hh:786
data_type r[M+1]
Definition: JPolint.hh:868
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
static const char *const TTREE_ONLINE_SUMMARYSLICE
ROOT TTree name.
Definition: root.hh:17
static const char *const TBRANCH_ONLINE_SUMMARYSLICE
ROOT TBranch name.
Definition: root.hh:25
The Evt class respresent a Monte Carlo (MC) event as well as an offline event.
Definition: Evt.hh:21
int frame_index
from the raw data
Definition: Evt.hh:29
int run_id
DAQ run identifier.
Definition: Evt.hh:26
ULong64_t trigger_mask
trigger mask from raw data (i.e. the trigger bits)
Definition: Evt.hh:30
std::vector< Hit > hits
list of hits
Definition: Evt.hh:38
int det_id
detector identifier from DAQ
Definition: Evt.hh:23
ULong64_t trigger_counter
trigger counter
Definition: Evt.hh:31
TTimeStamp t
UTC time of the timeslice, or the event_time for MC. (default: 01 Jan 1970 00:00:00)
Definition: Evt.hh:33
unsigned int overlays
number of overlaying triggered events
Definition: Evt.hh:32
Definition: Hit.hh:10
int pmt_id
global PMT identifier as found in evt files
Definition: Hit.hh:20
int dom_id
module identifier from the data (unique in the detector).
Definition: Hit.hh:14
int id
Definition: Hit.hh:11
ULong64_t trig
non-zero if the hit is a trigger hit.
Definition: Hit.hh:18
unsigned int tdc
hit tdc (=time in ns)
Definition: Hit.hh:16
unsigned int channel_id
PMT channel id {0,1, .., 30} local to moduke.
Definition: Hit.hh:15
unsigned int tot
tot value as stored in raw data (int for pyroot)
Definition: Hit.hh:17