ROOTANA
midas2root.cxx
Go to the documentation of this file.
1 // Example Program for converting MIDAS format to ROOT format.
2 //
3 // T. Lindner (Jan 2016)
4 //
5 // Example is for the CAEN V792 ADC module
6 
7 #include <stdio.h>
8 #include <iostream>
9 #include <time.h>
10 #include <vector>
11 
12 #include "TRootanaEventLoop.hxx"
13 #include "TFile.h"
14 #include "TTree.h"
15 
16 #include "TAnaManager.hxx"
17 
18 #ifdef USE_V792
19 #include "TV792Data.hxx"
20 #endif
21 
22 class Analyzer: public TRootanaEventLoop {
23 
24 public:
25 
26  // An analysis manager. Define and fill histograms in
27  // analysis manager.
29 
30  // The tree to fill.
31  TTree *fTree;
32 
33  int timestamp;
35 #ifdef USE_V792
36  // CAEN V792 tree variables
37  int nchannels;
38  int adc_value[32];
39 #endif
40 
41 
43 
44  };
45 
46  virtual ~Analyzer() {};
47 
48  void Initialize(){
49 
50 
51  }
52 
53 
54  void BeginRun(int transition,int run,int time){
55 
56  // Create a TTree
57  fTree = new TTree("midas_data","MIDAS data");
58 
59  fTree->Branch("timestamp",&timestamp,"timestamp/I");
60  fTree->Branch("serialnumber",&serialnumber,"serialnumber/I");
61 
62 #ifdef USE_V792
63  fTree->Branch("nchannels",&nchannels,"nchannels/I");
64  fTree->Branch("adc_value",adc_value,"adc_value[nchannels]/I");
65 #endif
66 
67  }
68 
69 
70  void EndRun(int transition,int run,int time){
71  printf("\n");
72  }
73 
74 
75 
76  // Main work here; create ttree events for every sequenced event in
77  // Lecroy data packets.
78  bool ProcessMidasEvent(TDataContainer& dataContainer){
79 
80  serialnumber = dataContainer.GetMidasEvent().GetSerialNumber();
81  if(serialnumber%10 == 0) printf(".");
82  timestamp = dataContainer.GetMidasEvent().GetTimeStamp();
83 
84 #ifdef USE_V792
85  TV792Data *data = dataContainer.GetEventData<TV792Data>("ADC0");
86  if(data){
87  nchannels = 32;
88  for(int i = 0; i < nchannels;i++) adc_value[i] = 0;
89 
90  /// Get the Vector of ADC Measurements.
91  std::vector<VADCMeasurement> measurements = data->GetMeasurements();
92  for(unsigned int i = 0; i < measurements.size(); i++){ // loop over measurements
93 
94  int chan = measurements[i].GetChannel();
95  uint32_t adc = measurements[i].GetMeasurement();
96 
97  if(chan >= 0 && chan < nchannels)
98  adc_value[chan] = adc;
99  }
100  }
101 #endif
102 
103  fTree->Fill();
104 
105  return true;
106 
107  };
108 
109  // Complicated method to set correct filename when dealing with subruns.
110  std::string SetFullOutputFileName(int run, std::string midasFilename)
111  {
112  char buff[128];
113  Int_t in_num = 0, part = 0;
114  Int_t num[2] = { 0, 0 }; // run and subrun values
115  // get run/subrun numbers from file name
116  for (int i=0; ; ++i) {
117  char ch = midasFilename[i];
118  if (!ch) break;
119  if (ch == '/') {
120  // skip numbers in the directory name
121  num[0] = num[1] = in_num = part = 0;
122  } else if (ch >= '0' && ch <= '9' && part < 2) {
123  num[part] = num[part] * 10 + (ch - '0');
124  in_num = 1;
125  } else if (in_num) {
126  in_num = 0;
127  ++part;
128  }
129  }
130  if (part == 2) {
131  if (run != num[0]) {
132  std::cerr << "File name run number (" << num[0]
133  << ") disagrees with MIDAS run (" << run << ")" << std::endl;
134  exit(1);
135  }
136  sprintf(buff,"output_%.6d_%.4d.root", run, num[1]);
137  printf("Using filename %s\n",buff);
138  } else {
139  sprintf(buff,"output_%.6d.root", run);
140  }
141  return std::string(buff);
142  };
143 
144 
145 
146 
147 
148 };
149 
150 
151 int main(int argc, char *argv[])
152 {
153 
154  Analyzer::CreateSingleton<Analyzer>();
155  return Analyzer::Get().ExecuteLoop(argc, argv);
156 
157 }
158 
Definition: ana.cxx:14
std::string SetFullOutputFileName(int run, std::string midasFilename)
Definition: midas2root.cxx:110
void BeginRun(int transition, int run, int time)
Definition: midas2root.cxx:54
bool ProcessMidasEvent(TDataContainer &dataContainer)
Definition: midas2root.cxx:78
TAnaManager * anaManager
Definition: ana.cxx:23
int serialnumber
Definition: midas2root.cxx:34
TTree * fTree
Definition: midas2root.cxx:31
void Initialize()
Definition: midas2root.cxx:48
void EndRun(int transition, int run, int time)
Definition: midas2root.cxx:70
virtual ~Analyzer()
Definition: midas2root.cxx:46
int timestamp
Definition: midas2root.cxx:33
T * GetEventData(const char *name)
Add a templated function that returns event data in the format that we want.
TMidasEvent & GetMidasEvent() const
uint32_t GetTimeStamp() const
return the time stamp (unix time in seconds)
uint32_t GetSerialNumber() const
return the serial number
static TRootanaEventLoop & Get(void)
int ExecuteLoop(int argc, char *argv[])
Method to actually process the Midas information, either as file or online.
Class for storing data from CAEN V792 module.
Definition: TV792Data.hxx:53
std::vector< VADCMeasurement > & GetMeasurements()
Get the Vector of TDC Measurements.
Definition: TV792Data.hxx:77
int main(int argc, char *argv[])
Definition: midas2root.cxx:151