Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JClusterBuilder.cc
Go to the documentation of this file.
1 /**
2  * \file
3  Example program to demonstrate the usage of JClusterBuilder
4 
5  If the option -v ("verbose") is used, the number of coincidence clusters
6  is printed for each DOM for each time slice.
7 **/
8 
9 // c++ standard library
10 #include <string>
11 
12 // JPP
13 #include "Jeep/JParser.hh"
14 #include "JDAQ/JDAQ.hh"
15 #include "JDAQ/JDAQTimeslice.hh"
16 #include "JDAQ/JDAQSummaryslice.hh"
17 #include "JDAQ/JDAQSuperFrame.hh"
19 #include "JSupport/JSupport.hh"
20 #include "JDetector/JDetector.hh"
23 #include "JMonitor/JCluster.hh"
25 
26 // root
27 #include "TH2D.h"
28 
29 // namespaces
30 using namespace std ;
31 using namespace KM3NETDAQ ;
32 using namespace JPP;
33 using namespace JDETECTOR ;
34 using namespace JTRIGGER ;
35 using namespace JMONITOR ;
36 
37 int main(int argc, char **argv) {
38  const unsigned int max_multiplicity = 31 ;
39 
40  //------------------------------------------------------------
41  // READ COMMAND-LINE OPTIONS
42  //------------------------------------------------------------
44  string detectorFile ;
45  double window ;
46  int maxnslices ;
47  bool verbose ;
48  string ofname ;
49 
50  try {
51  JParser<> zap;
52 
53  zap['f'] = make_field(input) ;
54  zap['m'] = make_field(maxnslices) = 100 ;
55  zap['a'] = make_field(detectorFile) ;
56  zap['w'] = make_field(window) ;
57  zap['v'] = make_field(verbose) ;
58  zap['o'] = make_field(ofname) = "out.root" ;
59  zap(argc, argv);
60  }
61  catch(const exception &error) {
62  cerr << error.what() << endl ;
63  cout << "Use option -h! to display command line options." << endl ;
64  exit(1) ;
65  }
66 
67  //------------------------------------------------------------
68  // OPEN DETECTOR FILE
69  //------------------------------------------------------------
71  try {
72  load(detectorFile, detector);
73  }
74  catch(const JException& error) {
75  cerr << "FATAL ERROR. Could not open detector file '" << detectorFile << "'." << endl ;
76  exit(1) ;
77  }
78  JModuleRouter moduleRouter(detector) ;
79 
80  //------------------------------------------------------------
81  // ALLOCATE HISTOGRAMS
82  //------------------------------------------------------------
83  const int ncolors = 5 ;
84  const int nice_colors[ncolors] = { kRed, kBlue, kBlack, kViolet, kCyan } ;
85 
86  // demonstration plots: the ToT distribution for hits in clusters
87  // of different sizes
88  vector<TH1D*> hToT(max_multiplicity+1,NULL) ;
89  for( unsigned int m=0; m<max_multiplicity+1; ++m ) {
90  char hname[200] ;
91  sprintf( hname, "hToT_m%u", m ) ;
92  char htitle[300] ;
93  sprintf( htitle, "Exclusive %u-fold coincidence clusters;ToT [ns];a.u. (normalized)", m ) ;
94  hToT[m] = new TH1D( hname, htitle, 256, -0.5, 255.5 ) ;
95  hToT[m]->SetLineColor( nice_colors[m%ncolors] ) ;
96  hToT[m]->SetLineWidth(2) ;
97  }
98 
99  // demonstration plots: the time distribution of the hits in the clusters
100  vector<TH1D*> ht(max_multiplicity+1,NULL) ;
101  for( unsigned int m=0; m<max_multiplicity+1; ++m ) {
102  char hname[200] ;
103  sprintf( hname, "ht_m%u", m ) ;
104  char htitle[300] ;
105  sprintf( htitle, "Exclusive %u-fold coincidence clusters;Time after first hits [ns];a.u. (normalized)", m ) ;
106  const int margin = 5 ; // ns
107  double xmin = -margin ;
108  double xmax = ceil(window) + margin ;
109  int nbins = (int) round(xmax-xmin) ;
110  ht[m] = new TH1D( hname, htitle, nbins, xmin, xmax ) ;
111  ht[m]->SetLineColor( nice_colors[m%ncolors] ) ;
112  ht[m]->SetLineWidth(2) ;
113  }
114 
115 
116  // another demonstration plot showing the relation between cluster size
117  // and cluster multiplicity (they are practically always the same)
118  TH2D hSizeVsMultiplicity("hSizeVsMultiplicity",";cluster size;cluster multiplicity",
119  32,-0.5,31.5,
120  32,-0.5,31.5 ) ;
121  hSizeVsMultiplicity.SetOption("colz") ;
122 
123  //------------------------------------------------------------
124  // READ FILES
125  //------------------------------------------------------------
126  cout << endl ;
127  cout << "---------- Reading file(s) ----------" << endl ;
128 
130 
131  JMONITOR::JClusterBuilder cluster_builder(window,true) ;
132 
133  unsigned int nTS = 0 ; // time slice counter
134  while( scan.hasNext() ) {
135  JDAQTimeslice* ts = scan.next() ;
136  if( (int)nTS == maxnslices ) break ;
137  ++nTS ;
138 
139  if( verbose ) {
140  cout << "------ Frame index = " << ts->getFrameIndex() << endl ;
141  }
142 
143  // loop over the JDAQSuperFrames
144  for(JDAQTimeslice::const_iterator sf = ts->begin() ; sf!=ts->end() ; ++sf ) {
145  // ignore frames without hits
146  if( sf->size() == 0 ) continue ;
147 
148  int moduleID = sf->getModuleID() ;
149  int localID = moduleRouter.getAddress(moduleID).first ;
150  JModule module = detector[localID] ;
151 
152  // build clusters
153  cluster_builder.reset(*sf,module) ;
154 
155  // print number of clusters found in this frame
156  if( verbose ) {
157  cout << "--- " << "S" << module.getString() << "F" << module.getFloor() << endl ;
158  cout << setw(10) << "multiplicity"
159  << setw(20) << "excl. nclusters"
160  << setw(20) << "incl. nclusters"
161  << endl ;
162  for( unsigned int m=2; m<=max_multiplicity; ++m) {
163  if( cluster_builder.getNclusters(m) != 0 ) {
164  cout << setw(10) << m
165  << setw(20) << cluster_builder.getNclusters(m)
166  << setw(20) << cluster_builder.getInclusiveNclusters(m)
167  << endl ;
168  }
169  }
170  }
171 
172  /**
173  loop over all clusters
174  the begin/end_inclusive_m(M) methods return the begin and end iterator
175  for clusters of multiplicity M or higher
176  **/
177  for( vector<JCluster>::const_iterator it=cluster_builder.begin_inclusive_m(0); it!=cluster_builder.end_inclusive_m(0); ++it ) {
178  hSizeVsMultiplicity.Fill( it->size(), it->getMultiplicity() ) ;
179  }
180 
181  /**
182  loop over clusters of a given multiplicity
183  the begin/end_m(M) methods return the begin and end iterator for
184  clusters of multiplicity exactly M
185  **/
186  for( unsigned int m=0; m<=max_multiplicity; ++m) {
187  for( vector<JCluster>::const_iterator it=cluster_builder.begin_m(m); it!=cluster_builder.end_m(m); ++it ) {
188  // loop over the hits in the cluster to fill ToT histogram
189  for( JHitL1::const_iterator hit=it->begin(); hit!=it->end(); ++hit ) {
190  hToT[m]->Fill( hit->getToT() ) ;
191  }
192  // loop over the hits after the first hit to fill dt histogram
193  if( it->size() > 1 ) {
194  JHitL1::const_iterator first_hit = it->begin() ;
195  JHitL1::const_iterator hit(first_hit) ;
196  for( ++hit; hit!=it->end(); ++hit ) {
197  ht[m]->Fill( hit->getT() - first_hit->getT() ) ;
198  }
199  }
200  }
201  }
202 
203  } // end of loop over JDAQSuperFrames
204 
205  if( verbose ) cout << endl ;
206 
207  } // end of loop over JDAQTimeSlices
208 
209  cout << "Read " << nTS << " time slices." << endl ;
210 
211  TFile* f = new TFile( ofname.c_str(), "recreate" ) ;
212 
213  hSizeVsMultiplicity.Write() ;
214 
215  for( unsigned int m=0; m<=max_multiplicity; ++m) {
216  if( hToT[m]->GetEntries()>0 ) {
217  // normalize
218  hToT[m]->Scale( 1.0/hToT[m]->Integral() ) ;
219  // write
220  hToT[m]->Write() ;
221  }
222  if( ht[m]->GetEntries()>0 ) {
223  // normalize
224  ht[m]->Scale( 1.0/ht[m]->Integral() ) ;
225  // write
226  ht[m]->Write() ;
227  }
228  }
229 
230  f->Close() ;
231  delete f ;
232  cout << "Output in '" << ofname << "'." << endl ;
233 
234  cout << endl ;
235 }
Utility class to parse command line options.
Definition: JParser.hh:1410
General exception.
Definition: JException.hh:40
virtual const pointer_type & next()
Get next element.
Data structure for a composite optical module.
Definition: JModule.hh:47
unsigned int getInclusiveNclusters(const unsigned int multiplicity) const
return the number of clusters with at least the given multiplicity
Detector data structure.
Definition: JDetector.hh:77
Router for direct addressing of module data in detector data structure.
void reset(const JDAQSuperFrame &frame, const JModule &module)
This is a way to re-use the allocated memory.
Data structure for detector geometry and calibration.
vector< JCluster >::const_iterator end_m(unsigned int multiplicity) const
returns end iterator for clusters with exactly the given multiplicity
int getFrameIndex() const
Get frame index.
int first
index of module in detector data structure
int getFloor() const
Get floor number.
int getString() const
Get string number.
Detector file.
Definition: JHead.hh:126
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
Data time slice.
void load(const JString &file_name, JDetector &detector)
Load detector from input file.
vector< JCluster >::const_iterator end_inclusive_m(unsigned int multiplicity) const
returns end iterator for clusters with at least the given multiplicity
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Direct access to module in detector data structure.
virtual bool hasNext()
Check availability of next element.
Utility class to parse command line options.
unsigned int getNclusters(const unsigned int multiplicity) const
return the number of clusters with exactly the given multiplicity
const JModuleAddress & getAddress(const JObjectID &id) const
Get address of module.
ROOT TTree parameter settings.
vector< JCluster >::const_iterator begin_inclusive_m(unsigned int multiplicity) const
returns begin iterator for clusters with at least the given multiplicity
KM3NeT DAQ constants, bit handling, etc.
Local coincidence cluster builder.
vector< JCluster >::const_iterator begin_m(unsigned int multiplicity) const
returns begin iterator for clusters with exactly the given multiplicity
int main(int argc, char *argv[])
Definition: Main.cpp:15