Newer
Older
TestStandRepository / Software / TbNtupleMaker / TbNtupleMaker.C
@Federica Lionetto Federica Lionetto on 5 Nov 2014 9 KB Modify ProcessRawData
  1. //************************************************
  2. // Author: Federica Lionetto, Adam Davis, Biplab Dey, Paolo Gandini
  3. // Created on: 30/07/2014
  4. //************************************************
  5.  
  6. /*
  7. IMPORTANT
  8.  
  9. I cannot display std::vector<double> PedByGain, std::vector<double> NoiseByGain, and std::vector<double> Gain in the ROOT file that is created by TbNtupleMaker.
  10. Further investigation is needed in order to understand if the format is fine or not.
  11. */
  12.  
  13.  
  14.  
  15. /*
  16. TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information:
  17. - int NEvents, the number of events read from the header of the Alibava file;
  18. - int RunType, the run type;
  19. - std::vector<double> PedByGain, the pedestals calculated by the Alibava;
  20. - std::vector<double> NoiseByGain, the noise calculated by the Alibava;
  21. - std::vector<double> Gain, the gain calculated by the Alibava;
  22. - TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N-1);
  23. - TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N-1);
  24. - TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N-1);
  25. - string Date, the date read from the header of the Alibava file;
  26. - std::vector<unsigned short> ADC, the raw ADC counts, one per event;
  27. - double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event;
  28. - double Temp, the temperature, one per event;
  29. - int n, a counter, one per event (starting from 1).
  30.  
  31. The ROOT file will be saved in a folder named RootFiles in the same folder where the Alibava file is (if no external path is provided) or in the folder specified by the external path.
  32.  
  33. Compile with:
  34.  
  35. make
  36.  
  37. Run with:
  38.  
  39. ./TbNtupleMaker [Alibava file] [external path]
  40.  
  41. where:
  42. - [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process;
  43. - [external path] is the optional external path where the RootFiles folder will be created.
  44.  
  45. For example:
  46.  
  47. ./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali
  48.  
  49. or
  50.  
  51. ./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /...
  52. */
  53.  
  54. //************************************************
  55. // Run types:
  56. // 1 = calibration run
  57. // 2 = laser synchronization run
  58. // 3 = laser run
  59. // 4 = radioactive source run
  60. // 5 = pedestal run
  61. //************************************************
  62.  
  63. #include <iostream>
  64. #include <string>
  65.  
  66. #include <vector>
  67.  
  68. #include <cstdlib>
  69.  
  70. #include "TFile.h"
  71. #include "TROOT.h"
  72. #include "TSystem.h"
  73. #include "TTree.h"
  74.  
  75. #include "Alibava/AsciiRoot.h"
  76.  
  77. #ifdef __MAKECINT__
  78. #pragma link C++ class vector<float>+;
  79. #endif
  80.  
  81. using namespace std;
  82.  
  83. // Number of Beetle channels.
  84. const int N = 256;
  85.  
  86. void TbNtupleMaker(char *filename, char *externalPath = 0);
  87.  
  88. int main(int argc, char *argv[])
  89. {
  90. if(argc < 2)
  91. {
  92. cout << "**************************************************" << endl;
  93.  
  94. cout << "Error! Input file missing..." << endl;
  95. cout << "Please use the following format:" << endl;
  96. cout << "./TbNtupleMaker [1] [2]" << endl;
  97. cout << "with:" << endl;
  98. cout << "[1] = Alibava file, complete path;" << endl;
  99. cout << "[2] = External path, optional." << endl;
  100. cout << "Type ./TbNtupleMaker --info for more information." << endl;
  101.  
  102. cout << "**************************************************" << endl;
  103. return 0;
  104. }
  105. else if (string(argv[1]) == "--info")
  106. {
  107. cout << "**************************************************" << endl;
  108.  
  109. cout << "TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information:" << endl;
  110. cout << "- int NEvents, the number of events read from the header of the Alibava file;" << endl;
  111. cout << "- int RunType, the run type;" << endl;
  112. cout << "- std::vector<double> PedByGain, the pedestals calculated by the Alibava;" << endl;
  113. cout << "- std::vector<double> NoiseByGain, the noise calculated by the Alibava;" << endl;
  114. cout << "- std::vector<double> Gain, the gain calculated by the Alibava;" << endl;
  115. cout << "- TH1F histo_PedByGain;" << endl;
  116. cout << "- TH1F histo_NoiseByGain;" << endl;
  117. cout << "- TH1F histo_Gain;" << endl;
  118. cout << "- string Date, the date read from the header of the Alibava file;" << endl;
  119. cout << "- std::vector<unsigned short> ADC, the raw ADC counts, one per event;" << endl;
  120. cout << "- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event;" << endl;
  121. cout << "- double Temp, the temperature, one per event;" << endl;
  122. cout << "- int n, a counter, one per event (starting from 1)." << endl;
  123.  
  124. cout << "**************************************************" << endl;
  125.  
  126. cout << "The ROOT file will be saved in a folder named RootFiles in the same folder where the Alibava file is (if no external path is provided) or in the folder specified by the external path." << endl;
  127. cout << "**************************************************" << endl;
  128.  
  129. cout << "Compile with:" << endl;
  130. cout << "make" << endl;
  131. cout << "Run with:" << endl;
  132. cout << "./TbNtupleMaker [Alibava file] [external path]" << endl;
  133. cout << "where:" << endl;
  134. cout << "- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process;" << endl;
  135. cout << "- [external path] is the optional external path where the RootFiles folder will be created." << endl;
  136. cout << "For example:" << endl;
  137. cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali" << endl;
  138. cout << "or" << endl;
  139. cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /..." << endl;
  140.  
  141. cout << "**************************************************" << endl;
  142. return 0;
  143. }
  144. else
  145. {
  146. cout << "File to process: " << argv[1] << endl;
  147. if (argc == 2)
  148. TbNtupleMaker(argv[1]);
  149. else if (argc == 3)
  150. TbNtupleMaker(argv[1],argv[2]);
  151. else
  152. {
  153. cout << "Error! Too many arguments given..." << endl;
  154.  
  155. return 0;
  156. }
  157. return 0;
  158. }
  159. }
  160.  
  161.  
  162.  
  163. void TbNtupleMaker(char *filename, char *externalPath){
  164. cout << "**************************************************" << endl;
  165. cout << "Decoding raw data..." << endl;
  166. cout << "**************************************************" << endl;
  167. // Do not comment this line.
  168. gROOT->ProcessLine("#include <vector>");
  169.  
  170. // Do some fanciness to get the directory right.
  171. string filename_as_string = string(filename);
  172. string prefix = filename_as_string.substr(0,filename_as_string.find_last_of('/'));
  173. if (externalPath!=0)
  174. prefix = string(externalPath);
  175. string ali_filename = filename_as_string.substr(filename_as_string.find_last_of('/')+1);
  176. TString outfilename = ((TString)prefix)+"/RootFiles/"+((TString)ali_filename);
  177. outfilename.ReplaceAll(".ali",".root");
  178. // Create a folder named RootFiles. Do not worry, nothing bad is going to happen if the folder already exists.
  179. cout << "Create a folder named RootFiles" << endl;
  180. cout << "Setting output to: " << prefix+"/RootFiles" << endl;
  181. string path_to_make = "mkdir "+prefix+"/RootFiles";
  182. system(path_to_make.c_str());
  183.  
  184. // Open ROOT file.
  185. TFile *output = TFile::Open(outfilename,"RECREATE");
  186. // Open Alibava file.
  187. AsciiRoot *a = new AsciiRoot(filename);
  188.  
  189. // Create tree structure in ROOT file, to be filled once per file.
  190. int NEvents;
  191. int RunType;
  192. std::vector<double> PedByGain;
  193. std::vector<double> NoiseByGain;
  194. std::vector<double> Gain;
  195. TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N);
  196. TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N);
  197. TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N);
  198. string Date;
  199.  
  200. // Allocate only necessary space.
  201. PedByGain.reserve(N);
  202. NoiseByGain.reserve(N);
  203. Gain.reserve(N);
  204.  
  205. // Create tree structure in ROOT file, to be filled once per event.
  206. std::vector<unsigned short> ADC;
  207. double TDCTime;
  208. double Temp;
  209. int n;
  210.  
  211. // Fill the header, once per file.
  212. TTree *Header = new TTree("Header","Header");
  213.  
  214. Header->Branch("NEvents",&NEvents);
  215. Header->Branch("RunType",&RunType);
  216. Header->Branch("PedByGain",&PedByGain);
  217. Header->Branch("NoiseByGain",&NoiseByGain);
  218. Header->Branch("Gain",&Gain);
  219. Header->Branch("Date",&Date);
  220.  
  221. NEvents = a->get_nevents();
  222. cout << "Number of events in the Alibava file: " << NEvents << endl;
  223.  
  224. RunType = a->type();
  225. cout << "Run type of the Alibava file: " << RunType << endl;
  226.  
  227. for(int iChannel=0; iChannel<N;++iChannel)
  228. {
  229. PedByGain[iChannel]=a->ped(iChannel);
  230. NoiseByGain[iChannel] = a->noise(iChannel);
  231. Gain[iChannel]= a->get_gain(iChannel);
  232. histo_PedByGain.SetBinContent(iChannel+1,a->ped(iChannel));
  233. histo_NoiseByGain.SetBinContent(iChannel+1,a->noise(iChannel));
  234. histo_Gain.SetBinContent(iChannel+1,a->get_gain(iChannel));
  235. }
  236.  
  237. Date = a->date();
  238. cout << "Date of the Alibava file: " << Date << endl;
  239.  
  240. Header->Fill();
  241.  
  242. cout << "Information in the header:" << endl;
  243. Header->Print();
  244. // Fill the data, once per event.
  245. TTree *EventInfo = new TTree("EventInfo","EventInfo");
  246. EventInfo->Branch("ADC",&ADC);
  247. EventInfo->Branch("TDCTime",&TDCTime);
  248. EventInfo->Branch("Temp",&Temp);
  249. EventInfo->Branch("n",&n);
  250. // Loop over events.
  251. for (int iEvent=0; iEvent<NEvents; iEvent++)
  252. {
  253. ADC.clear();
  254. a->read_event();
  255.  
  256. // Loop over Beetle channels.
  257. for (int jChannel=0; jChannel<N; jChannel++)
  258. ADC.push_back(a->data(jChannel));
  259.  
  260. TDCTime =a->time();
  261. Temp = a->temp();
  262. n = iEvent+1;
  263. EventInfo->Fill();
  264. }
  265.  
  266. cout << "Information in the data: " << endl;
  267. EventInfo->Print();
  268.  
  269. // Close Alibava file.
  270. a->close();
  271.  
  272. // Write ROOT file.
  273. output->Write();
  274.  
  275. // Close ROOT file.
  276. output->Close();
  277.  
  278. return;
  279. }