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