diff --git a/Software/TbNtupleMaker/.svn/entries b/Software/TbNtupleMaker/.svn/entries new file mode 100644 index 0000000..60fd98e --- /dev/null +++ b/Software/TbNtupleMaker/.svn/entries @@ -0,0 +1,99 @@ +10 + +dir +60 +svn+ssh://svn.cern.ch/reps/uttestbeamsoft/TbNtupleMaker +svn+ssh://svn.cern.ch/reps/uttestbeamsoft + + + +2014-09-17T16:18:11.012127Z +53 +pgandini + + + + + + + + + + + + + + +4525493e-7705-40b1-a816-d608a930855b + +Alibava +dir + +Makefile +file + + + + +2014-08-01T15:29:11.000000Z +9bc47aa65fafa6f889df4c5a6a32b199 +2014-08-01T15:04:34.667335Z +3 +adavis +has-props + + + + + + + + + + + + + + + + + + + + +868 + +TbNtupleMaker.cpp +file + + + + +2014-10-07T12:28:16.000000Z +f5ed09324f7169fa889a4af97c3dacc6 +2014-09-17T16:18:11.012127Z +53 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +10735 + diff --git a/Software/TbNtupleMaker/.svn/prop-base/Makefile.svn-base b/Software/TbNtupleMaker/.svn/prop-base/Makefile.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/.svn/prop-base/Makefile.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/.svn/prop-base/TbNtupleMaker.cpp.svn-base b/Software/TbNtupleMaker/.svn/prop-base/TbNtupleMaker.cpp.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/.svn/prop-base/TbNtupleMaker.cpp.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/.svn/text-base/Makefile.svn-base b/Software/TbNtupleMaker/.svn/text-base/Makefile.svn-base new file mode 100644 index 0000000..b175775 --- /dev/null +++ b/Software/TbNtupleMaker/.svn/text-base/Makefile.svn-base @@ -0,0 +1,38 @@ +# +CC=$(CXX) +glib_cflags=$(shell pkg-config --cflags glib-2.0 gio-2.0) +glib_libs=$(shell pkg-config --libs glib-2.0 gio-2.0) + +ROOTC=$(shell root-config --cflags) +ROOTL=$(shell root-config --libs) +OPT=-g -fno-inline #-std=c++11 +CppFLAGS=$(OPT) -I. $(glib_cflags) +CXXFLAGS=-fPIC $(CppFLAGS) + +LDLIBS=$(alibava_libs) + + +extra_alibava_libs = Alibava/AsciiRoot.cc Alibava/ChanList.cc Alibava/Hit.cc Alibava/Tracer.cc Alibava/utils.cc + +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Linux) + SO=so + SO_FLAGS=-shared + CXXFLAGS += -D LINUX +endif +ifeq ($(UNAME_S),Darwin) + SO=dylib + SO_FLAGS=-dynamiclib -undefined dynamic_lookup -install_name @rpath/$@ + CXXFLAGS += -D OSX +endif + +all: TbNtupleMaker + +TbNtupleMaker: TbNtupleMaker.cpp + c++ -I$(OPT) $(CXXFLAGS) $(ROOTC) $(extra_alibava_libs) -o $@ $^ $(LDLIBS) $(ROOTL) $(gliblibs) + + + +clean: + rm -f *.o TbNtupleMaker + rm -rf *.dSYM diff --git a/Software/TbNtupleMaker/.svn/text-base/TbNtupleMaker.cpp.svn-base b/Software/TbNtupleMaker/.svn/text-base/TbNtupleMaker.cpp.svn-base new file mode 100644 index 0000000..fb2e3b7 --- /dev/null +++ b/Software/TbNtupleMaker/.svn/text-base/TbNtupleMaker.cpp.svn-base @@ -0,0 +1,312 @@ +//************************************************ +// Author: Federica Lionetto, Adam Davis, Biplab Dey, Paolo Gandini +// Created on: 07/30/2014 +//************************************************ + +/* +TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information: +- int NEvents, the number of events read from the header of the Alibava file; +- int RunType, the run type; +- std::vector PedByGain, the pedestals calculated by the Alibava; +- std::vector NoiseByGain, the noise calculated by the Alibava; +- std::vector Gain, the gain calculated by the Alibava; +- TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N-1); +- TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N-1); +- TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N-1); +- string Date = a->date(), the date read from the header of the Alibava file; +- std::vector ADC, the raw ADC counts, one per event; +- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event; +- double Temp, the temperature, one per event; +- int n, a counter, one per event (starting from 1). + +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. + +Compile with: + +make + +Run with: + +./TbNtupleMaker [Alibava file] [external path] + +where: +- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process; +- [external path] is the optional external path where the RootFiles folder will be created. + +For example: + +./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali + +or + +./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /... +*/ + +//************************************************ +// Types: +// 1 = calibration run +// 2 = laser synchronization run +// 3 = laser run +// 4 = radioactive source run +// 5 = pedestal run +//************************************************ + +#include +#include + +#include + +#include + +#include "TFile.h" +#include "TROOT.h" +#include "TSystem.h" +#include "TTree.h" + +#include "Alibava/AsciiRoot.h" + +#ifdef __MAKECINT__ +#pragma link C++ class vector+; +#endif + +using namespace std; + +// Number of Beetle channels. +const int N = 256; + +void TbNtupleMaker(char *filename, char *externalPath = 0); + +int main(int argc, char *argv[]) +{ + if(argc < 2) + { + cout << "**************************************************" << endl; + + cout << "Error! Input file missing..." << endl; + cout << "Please use the following format:" << endl; + cout << "./TbNtupleMaker [1] [2]" << endl; + cout << "with:" << endl; + cout << "[1] = Alibava file, complete path;" << endl; + cout << "[2] = External path, optional." << endl; + cout << "Type ./TbNtupleMaker --info for more information." << endl; + + cout << "**************************************************" << endl; + + return 0; + } + else if (string(argv[1]) == "--info") + { + cout << "**************************************************" << endl; + + cout << "TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information:" << endl; + cout << "- int NEvents, the number of events read from the header of the Alibava file;" << endl; + cout << "- int RunType, the run type;" << endl; + cout << "- std::vector PedByGain, the pedestals calculated by the Alibava;" << endl; + cout << "- std::vector NoiseByGain, the noise calculated by the Alibava;" << endl; + cout << "- std::vector Gain, the gain calculated by the Alibava;" << endl; + cout << "- TH1F histo_PedByGain;" << endl; + cout << "- TH1F histo_NoiseByGain;" << endl; + cout << "- TH1F histo_Gain;" << endl; + cout << "- string Date = a->date(), the date read from the header of the Alibava file;" << endl; + cout << "- std::vector ADC, the raw ADC counts, one per event;" << endl; + cout << "- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event;" << endl; + cout << "- double Temp, the temperature, one per event;" << endl; + cout << "- int n, a counter, one per event (starting from 1)." << endl; + + cout << "**************************************************" << endl; + + 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; + + cout << "**************************************************" << endl; + + cout << "Compile with:" << endl; + cout << "make" << endl; + cout << "Run with:" << endl; + cout << "./TbNtupleMaker [Alibava file] [external path]" << endl; + cout << "where:" << endl; + cout << "- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process;" << endl; + cout << "- [external path] is the optional external path where the RootFiles folder will be created." << endl; + cout << "For example:" << endl; + cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali" << endl; + cout << "or" << endl; + cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /..." << endl; + + cout << "**************************************************" << endl; + + return 0; + } + else + { + cout << "File to process: " << argv[1] << endl; + if (argc == 2) + TbNtupleMaker(argv[1]); + else if (argc == 3) + TbNtupleMaker(argv[1],argv[2]); + else + { + cout << "Error! Too many arguments given..." << endl; + + return 0; + } + + return 0; + } +} + + + +void TbNtupleMaker(char *filename, char *externalPath){ + cout << "**************************************************" << endl; + cout << "Decoding raw data..." << endl; + cout << "**************************************************" << endl; + + // Do not comment this line. + gROOT->ProcessLine("#include "); + + // Do some fanciness to get the directory right. + string filename_as_string = string(filename); + string prefix = filename_as_string.substr(0,filename_as_string.find_last_of('/')); + if (externalPath!=0) + prefix = string(externalPath); + string ali_filename = filename_as_string.substr(filename_as_string.find_last_of('/')+1); + TString outfilename = ((TString)prefix)+"/RootFiles/"+((TString)ali_filename); + outfilename.ReplaceAll(".ali",".root"); + + // Create a folder named RootFiles. Do not worry, nothing bad is going to happen if the folder already exists. + cout << "Create a folder named RootFiles" << endl; + + char *path_to_make = (char*)("mkdir "+prefix+"/RootFiles" ).c_str(); + cout << "Setting output to: " << path_to_make << endl; + + system(path_to_make); + + // Open ROOT file. + TFile *output = TFile::Open(outfilename,"RECREATE"); + + // Open Alibava file. + AsciiRoot *a = new AsciiRoot(filename); + + // Create tree structure in ROOT file, to be filled once per file. + int NEvents; + int RunType; + std::vector PedByGain; + std::vector NoiseByGain; + std::vector Gain; + TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N); + TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N); + TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N); + TH1F histo_Signal20("histo_Signal20","Signal calculated by Alibava > 20, distribution over the strips",N,0,N); + TH1F histo_Signal40("histo_Signal40","Signal calculated by Alibava > 40, distribution over the strips",N,0,N); + TH1F histo_Signal60("histo_Signal60","Signal calculated by Alibava > 60, distribution over the strips",N,0,N); + TH1F histo_TDC("histo_TDC","TDC distribution, ADC > 40",50,0,50); + string Date = a->date(); + + /////// vito + TH1D** ADC_ch = new TH1D*[256]; + TH2D* ADCvsEv = new TH2D("ADCvsEv_%d", + "ADCvsEv_%d", + 1e5, -0.5, 99999.5, + 200, 399.5, 600.5 ); + + for(int i=0; i<256; i++){ + + ADC_ch[i] = new TH1D(Form("ADC_ch_%d", i), + Form("ADC_ch_%d", i), + 1024, -0.5, 1023.5 ); + + } + + + // Allocate only necessary space. + PedByGain.reserve(N); + NoiseByGain.reserve(N); + Gain.reserve(N); + + // Create tree structure in ROOT file, to be filled once per event. + std::vector ADC; + double TDCTime; + double Temp; + int n; + + // Fill the header, once per file. + TTree *Header = new TTree("Header","Header"); + + Header->Branch("NEvents",&NEvents); + Header->Branch("RunType",&RunType); + Header->Branch("PedByGain",&PedByGain); + Header->Branch("NoiseByGain",&NoiseByGain); + Header->Branch("Gain",&Gain); + Header->Branch("Date",&Date); + + NEvents = a->get_nevents(); + cout << "Number of events in the Alibava file: " << NEvents << endl; + RunType = a->type(); + for(int iChannel=0; iChannelget_gain(iChannel); + PedByGain[iChannel]=a->ped(iChannel); + NoiseByGain[iChannel] = a->noise(iChannel); + histo_Gain.SetBinContent(iChannel+1,a->get_gain(iChannel)); + histo_PedByGain.SetBinContent(iChannel+1,a->ped(iChannel)); + histo_NoiseByGain.SetBinContent(iChannel+1,a->noise(iChannel)); + } + + Header->Fill(); + cout << "Information in the header:" << endl; + Header->Print(); + cout << "**************************************************" << endl; + cout << "Date from Alibava file: " << Date <Branch("n",&n); + EventInfo->Branch("ADC",&ADC); + EventInfo->Branch("TDCTime",&TDCTime); + EventInfo->Branch("Temp",&Temp); + + // Loop over events. + for (int iEvent=0; iEventread_event(); + + // Loop over Beetle channels. + for (int jChannel=0; jChanneldata(jChannel)-PedByGain[jChannel]>20) histo_Signal20.Fill(jChannel); + if(a->data(jChannel)-PedByGain[jChannel]>40) histo_Signal40.Fill(jChannel); + if(a->data(jChannel)-PedByGain[jChannel]>60) histo_Signal60.Fill(jChannel); + ADC.push_back(a->data(jChannel)); + ADC_ch[jChannel]->Fill( a->data(jChannel) ); + if(jChannel==200) ADCvsEv->Fill( iEvent, a->data(jChannel) ); + } + + + + n = iEvent+1; + TDCTime =a->time(); + for(int t=0; t40) {histo_TDC.Fill(TDCTime,1); break;} + Temp = a->temp(); + EventInfo->Fill(); + } + + + + + cout << "Information in the data: " << endl; + EventInfo->Print(); + + // Close Alibava file. + a->close(); + + // Write and close ROOT file. + for(int i=0; i<256; i++) + ADC_ch[i]->Write(); + ADCvsEv->Write(); + + output->Write(); + output->Close(); + + return; +} diff --git a/Software/TbNtupleMaker/Alibava/.svn/entries b/Software/TbNtupleMaker/Alibava/.svn/entries new file mode 100644 index 0000000..06d09fc --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/entries @@ -0,0 +1,402 @@ +10 + +dir +60 +svn+ssh://svn.cern.ch/reps/uttestbeamsoft/TbNtupleMaker/Alibava +svn+ssh://svn.cern.ch/reps/uttestbeamsoft + + + +2014-08-01T15:04:34.667335Z +3 +adavis + + + + + + + + + + + + + + +4525493e-7705-40b1-a816-d608a930855b + +Data.h +file + + + + +2014-08-01T15:29:10.000000Z +3414fc45868ba3fc614af9f9656c5eae +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +534 + +AsciiRoot.cc +file + + + + +2014-08-01T15:29:10.000000Z +9128c1fee0be552821280c4256e987fc +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +27793 + +AsciiRoot.h +file + + + + +2014-08-01T15:29:10.000000Z +bd86e71e91b2522c9ab0cd88d79010ac +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +6434 + +Tracer.cc +file + + + + +2014-08-01T15:29:10.000000Z +1e94d579f29cffebbd1712c7fbe3aaaf +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +821 + +utils.cc +file + + + + +2014-08-01T15:29:10.000000Z +195b1b4c0a26c44b2e16c5683fcc6ad0 +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +1431 + +Tracer.h +file + + + + +2014-08-01T15:29:10.000000Z +5e5a7dbc61e4ac62f62a3504e0dd76fb +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +851 + +utils.h +file + + + + +2014-08-01T15:29:10.000000Z +cc1dc60f40a33a19055f2351c7b78ad3 +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +1204 + +Hit.cc +file + + + + +2014-08-01T15:29:10.000000Z +312055db8abdb6034291a1dc2969e73a +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +377 + +ChanList.cc +file + + + + +2014-08-01T15:29:10.000000Z +2d53c320c22923a479eeae101f691a44 +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +3459 + +Hit.h +file + + + + +2014-08-01T15:29:10.000000Z +df70623e70b7368ca29caac39abd48d5 +2014-08-01T13:24:08.054812Z +1 +pgandini +has-props + + + + + + + + + + + + + + + + + + + + +705 + +ChanList.h +file + + + + +2014-08-01T15:29:10.000000Z +ca27e831a2ff3bdc615250abbd59ee35 +2014-08-01T15:04:34.667335Z +3 +adavis +has-props + + + + + + + + + + + + + + + + + + + + +2002 + diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.cc.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.cc.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.h.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.h.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.cc.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.cc.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.h.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.h.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/Data.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Data.h.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Data.h.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/Hit.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Hit.cc.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Hit.cc.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/Hit.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Hit.h.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Hit.h.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.cc.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.cc.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.h.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.h.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/utils.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/utils.cc.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/utils.cc.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/prop-base/utils.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/prop-base/utils.h.svn-base new file mode 100644 index 0000000..869ac71 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/prop-base/utils.h.svn-base @@ -0,0 +1,5 @@ +K 14 +svn:executable +V 1 +* +END diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.cc.svn-base new file mode 100644 index 0000000..a2da1e0 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.cc.svn-base @@ -0,0 +1,1062 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "AsciiRoot.h" +#include "utils.h" +#include "Tracer.h" +#include "Hit.h" +#include "ChanList.h" + + +#ifdef __APPLE__ +#define sighandler_t sig_t +#endif + +bool _A_do_run = true; +void _A_got_intr(int) +{ + _A_do_run = false; +} + +// decodes the header and returns a vector with the integers found +std::vector decode_header(const std::string &h, AsciiRoot::XtraValues &xtra) +{ + std::vector vout; + std::istringstream istr(h); + char *endptr, *str; + char buf[256]; + long val; + + xtra.clear(); + + while (istr) + { + istr.getline(buf, sizeof(buf), ';'); + if (!istr) + break; + + errno = 0; + val = strtol(buf, &endptr, 0); + + if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) + { + std::string sval(buf), sout; + sout = trim_str(sval); + if (!sout.empty()) + xtra.push_back( sout ); + } + else if ( endptr == buf || *endptr != '\0' ) + { + std::string sval(buf), sout; + sout = trim_str(sval); + if (!sout.empty()) + xtra.push_back( sout ); + } + else + { + vout.push_back(atoi(buf) ); + } + } + return vout; +} + +AsciiRoot::AsciiRoot(const char *nam, const char *pedfile, const char *gainfile) : + _nchan(max_nchan),_seedcut(10.), _neighcut(5.), _average_gain(1.), _version(2), _polarity(1) +{ + int i; + for (i=0;iclose(); + } +} + +void AsciiRoot::open(const char *name) +{ + ifile = new std::ifstream(name); + if (!(*ifile)) + { + std::cout << "Could not open data file: " << name << std::endl; + delete ifile; + ifile = 0; + return; + } + std::string header; + unsigned int ic, lheader; + char c; + ifile->read((char *)&_t0, sizeof(time_t)); + //std::cout << "64b: " << ctime(&_t0) << std::endl; + ifile->read((char *)&_type, sizeof(int)); + //std::cout << "type_ " << _type << std::endl; + if ( _type > 15 ) + { + ifile->seekg(0, std::ios::beg); + ifile->read((char *)&_t0, sizeof(int)); + ifile->read((char *)&_type, sizeof(int)); + //std::cout << "32b: " << ctime(&_t0) << std::endl; + } + + + ifile->read((char *)&lheader, sizeof(unsigned int)); + for (ic=0; ic<80; ic++) + { + ifile->read(&c, sizeof(char)); + //std::cout << "biplab header ch1 " << c << std::endl; + header.append(1, c); + } + header = trim_str(header); + //std::cout << "biplab header ch2 " << header << std::endl; + + if (header[0]!='V' && header[0]!='v') + { + _version = 0; + } + else + { + _version = int(header[1]-'0'); + header = header.substr(5); + } + + std::cout << "type: " << _type << " header: " << header << std::endl; + std::vector param = decode_header(header, _xtra); + ifile->read((char *)_ped, max_nchan*sizeof(double)); + ifile->read((char *)_noise, max_nchan*sizeof(double)); + switch (_type) + { + case 1: // calibration + case 2: // laser sync + _npoints = param[0]; + _from = param[1]; + _to = param[2]; + _step = param[3]; + break; + case 3: // laser run + case 4: // source run + case 5: // pedestal run + if (param.empty()) + _nevts = 100000; + else + _nevts = param[0]; + _npoints = _from = _to = _step = 0; + break; + } + data_start = ifile->tellg(); +} + +void AsciiRoot::rewind() +{ + if (ifile) + { + ifile->clear(); + ifile->seekg(data_start, std::ios::beg); + } +} + +void AsciiRoot::close() +{ + if (ifile) + { + ifile->close(); + delete ifile; + ifile = 0; + } +} + +void AsciiRoot::reset_data() +{ + memset(&_data, 0, sizeof(_data)); +} + +int AsciiRoot::read_event() +{ + if (ifile) + { + unsigned int header, size, user=0, code=0; + char *block_data=0; + if (_version) + { + do + { + do + { + ifile->read((char *)&header, sizeof(unsigned int)); + if (ifile->bad() || ifile->eof()) + return -1; + + code = (header>>16) & 0xFFFF; + } while ( code != 0xcafe ); + + code = header & 0x0fff; + user = header & 0x1000; + switch (code) + { + case NewFile: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + new_file(size, block_data); + break; + case StartOfRun: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + start_of_run(size, block_data); + break; + case DataBlock: + ifile->read((char *)&size, sizeof(unsigned int)); + if (user) + { + reset_data(); + block_data = new char[size]; + ifile->read(block_data, size); + new_data_block(size, block_data); + } + else + { + if ( _version == 1 ) + { + ifile->read((char *)&_data, sizeof(EventData)); + for (int ii=0; ii<2; ii++) + memset(_header[ii], 0, 16*sizeof(unsigned short)); + + } + else + { + ifile->read((char *)&_data.value, sizeof(double)); + ifile->read((char *)&_data.time, sizeof(unsigned int)); + ifile->read((char *)&_data.temp, sizeof(unsigned short)); + for (int ii=0; ii<2; ii++) + { + ifile->read((char *)_header[ii], 16*sizeof(unsigned short)); + ifile->read((char *)&_data.data[ii*128], 128*sizeof(unsigned short)); + } + } + } + + break; + case CheckPoint: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + check_point(size, block_data); + break; + case EndOfRun: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + end_of_run(size, block_data); + break; + default: + std::cout << "Unknown block data type: " << std::hex << header << " - " << code << std::dec << std::endl; + } + if (block_data) + { + delete [] block_data; + block_data = 0; + } + + } while ( code != DataBlock && !(ifile->bad() || ifile->eof()) ); + } + else + { + ifile->read((char *)&_data, sizeof(EventData)); + for (int ii=0; ii<2; ii++) + memset(_header[ii], 0, 16*sizeof(unsigned short)); + } + + if (ifile->eof()) + { + std::cout << "End of file" << std::endl; + return -1; + } + else if (ifile->bad()) + { + std::cout << "Problems with data file" << std::endl; + return -1; + } + else + { + //process_event(); + return 0; + } + } + else + return -1; +} + +void AsciiRoot::set_data(int nchan, const unsigned short int *data) +{ + int i; + _nchan = nchan; + for (i=0;i<_nchan;i++) + _data.data[i] = data[i]; +} + + +double AsciiRoot::time() const +{ + unsigned short fpart = _data.time & 0xffff; + short ipart = (_data.time & 0xffff0000)>>16; + if (ipart<0) + fpart *= -1; +// double tt = 100.*(1. -(ipart + (fpart/65535.))); + double tt = 100.0*(ipart + (fpart/65535.)); + return tt; +} + +double AsciiRoot::temp() const +{ + return 0.12*_data.temp - 39.8; +} + + +TH1 *AsciiRoot::show_pedestals() +{ + int ic; + TH1 *hst = create_h1("hPed","Pedestals",nchan(),-0.5, nchan()-0.5); + hst->SetYTitle("ADCs"); + hst->SetXTitle("Channel no."); + for (ic=0; icSetBinContent(ic+1, _ped[ic]); + + return hst; +} + +TH1 *AsciiRoot::show_noise() +{ + int ic; + TH1 *hst = create_h1("hNoise","Noise",nchan(),-0.5, nchan()-0.5); + if (gain()==1) + { + hst->SetYTitle("ADCs"); + } + else + { + hst->SetYTitle("e^{-} ENC"); + } + hst->SetXTitle("Channel no."); + for (ic=0; icSetBinContent(ic+1, noise(ic)); + + return hst; +} + + + +// Save ADC values in "ADCs" 2D-array of type unsigned short and size "evts x N". +//void AsciiRoot::get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts) +void AsciiRoot::get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts) +{ + int i = -1; + int ievt = -1; + + if (!ifile) + return; + + std::ifstream::pos_type here = ifile->tellg(); + std::cout << "==================================================" << std::endl; + std::cout << "Saving ADC values..." << std::endl; + std::cout << "==================================================" << std::endl; + // int evt_count(0); + // for (ievt=0; read_event()==0 ; ievt++){ evt_count++; } + // std::cout << "biplabd count events: " << evt_count << std::endl; + // rewind(); + + for (ievt=0; read_event()==0 && ievttellg(); + std::cout << "Computing fast pedestas..." << std::endl; + for (ievt=0; read_event()==0 && ievt 1.) + corr = 1.; + + if (corr < -1) + corr = -1.; + + _ped[i] += corr; + + if (fabs(xs) < 3.) + { + _noise[i] = _noise[i]*(1.0-wnoise) + xs*xs*wnoise; + } + } + } + } + std::cout << "\nDone" << std::endl; + rewind(); +} + + +TH2 *AsciiRoot::compute_pedestals(int mxevts, bool do_cmmd) +{ + if (!ifile) + return 0; + + if (mxevts<0) + mxevts = 100000000; + + int ievt, ichan; + TH2 *hst = create_h2("hRaw","Raw data",nchan(), -0.5,nchan()-0.5, 256, -0.5,1023.5); + TH2 *hsts = create_h2("hSig","Signal",nchan(), -0.5,nchan()-0.5,256, -127.5,127.5); + + + std::ifstream::pos_type here = ifile->tellg(); + std::cout << "Computing pedestas..." << std::endl; + for (ievt=0; read_event()==0 && ievtFill(ichan, data(ichan)-get_cmmd(ichan/128)); + + if (!(ievt%100)) + { + std::cout << "\revent " << std::setw(10) << ievt << std::flush; + } + } + std::cout << "\nDone" << std::endl; + rewind(); + + // TODO: _nchan can be updated in an event by event basis + // while here we are assuming that it is the same + // for all the events + for (ichan=0; ichanProjectionY("__hx__", ichan+1, ichan+1); + g->SetParameters(h1->GetSumOfWeights(), h1->GetMean(), h1->GetRMS()); + g->SetRange(h1->GetMean()-2.5*h1->GetRMS(), h1->GetMean()+2.5*h1->GetRMS()); + h1->Fit("g1", "q0wr"); + _ped[ichan] = h1->GetFunction("g1")->GetParameter(1); + _noise[ichan] = h1->GetFunction("g1")->GetParameter(2); + delete h1; + delete g; + } + + rewind(); + for (ievt=0; read_event()==0 && ievtFill(ichan, signal(ichan)); + + if (!(ievt%100)) + { + std::cout << "\revent " << std::setw(10) << ievt << std::flush; + } + } + std::cout << "\nDone" << std::endl; + rewind(); + + return hst; +} + + + + +void AsciiRoot::find_clusters(int ichip) +{ + int chan0=0; + int chan1=255; + if (ichip>=0 && ichip<2) + { + chan0 = ichip*128; + chan1 = (ichip+1)*128 -1; + } + + std::ostringstream ostr; + ostr << chan0 << '-' << chan1; + ChanList C(ostr.str().c_str()); + + clear(); + find_clusters(C); + _hits = C.hit_list(); +} + +void AsciiRoot::find_clusters(ChanList &C) +{ + // TODO: figure out how to determine the chip number in + // all the situations + int i, j, imax=-1, left, right; + double mxsig=-1.e20, sg, val; + bool used[C.Nch()]; + + for (i=0;i _seedcut) + { + val = fabs(signal(i)); + if (mxsig= C.Nch() ) + break; + + sg = signal(C[imax]); + used[imax]=true; + // Now look at neighbors + // first to the left + left = imax; + for (j=imax-1;j>=0;j--) + { + i = C[j]; + if ( used[j] || _signal[i]*polarity()<0.) + break; + + if ( fabs(sn(i)) > _neighcut ) + { + used[j] = true; + sg += signal(i); + left = j; + } + else + // TODO: this needs to be removed + // The idea is to merge to clusters that have only one strip in between + // In the laser runs this is a consequences of reflections... + { + int jx = j-1; + if (jx>=0 ) + { + if ( fabs(sn(C[jx])) > _neighcut ) + continue; + } + break; + } + } + + // now to the right + right = imax; + for (j=imax+1;j_neighcut ) + { + used[j] = true; + sg += signal(i); + right = j; + } + else + // TODO: this needs to be removed + // The idea is to merge to clusters that hanve only one strip in between + // In the laser runs this is a consequences of reflections... + { + int jx = i+1; + if (jx _neighcut ) + continue; + } + break; + } + } + C.add_hit(Hit(imax, left, right, sg)); + } +} + +void AsciiRoot::save_pedestals(const char *fnam) +{ + std::ofstream ofile(fnam); + if (!ofile) + { + std::cout << "Could not open " << fnam << " to save pedestals." << std::endl; + return; + } + + // TODO: _nchan can be updated in an event by event basis + // while here we are assuming that it is the same + // for all the events + int i; + for (i=0; i> _ped[i] >> std::ws >> _noise[i] >> std::ws; + _mask[i] = (_noise[i]>20. || _noise[i]<=0.); + } + ifile.close(); + TCanvas *pedcanvas = create_canvas("Pedcanvas", "Pedestal Values", 600, 400); + TH1 *pedestalhisto = create_h1("pedestalhisto", "Pedestal Values", 256, -0.5, 255.5); + for (i=0; i<256; i++) + { + pedestalhisto->Fill(i, _ped[i]); + } + pedcanvas->cd(1); + pedestalhisto->Draw(); +} + +void AsciiRoot::load_masking(const char *fnam) +{ + std::ifstream ifile(fnam); + if (!ifile) + { + std::cout << "Could not open masked.txt. " << std::endl; + return; + } + int val; + for (int i=0; i<500; i++) + { + ifile >> val >> std::ws; + if (ifile.eof()) + break; + if (val>255) + { + std::cout << "A value is greater than 255, causing an overflow crash. Please check the text file again. It has been set to 1 for continuation purposes. " << std::endl; + val = 1; + } + _mask[val] = true; + } +} + +void AsciiRoot::load_gain(const char *fnam) +{ + std::ifstream ifile(fnam); + if (!ifile) + { + std::cout << "Could not open " << fnam << " to load the gain." << std::endl; + return; + } + int i; + int ichan; + double val, xn, xm; + xn=xm=0.; + for (i=0; i> ichan >> std::ws; + if (ifile.eof()) + break; + ifile >> val; + if (ifile.eof()) + break; + + xn++; + + xm += val; + _gain[ichan] = val; + + ifile >> std::ws; + if (ifile.eof()) + break; + } + if (xn>0) + { + _average_gain = xm/xn; + } + ifile.close(); +} + +void AsciiRoot::process_event(bool do_cmmd) +{ + int i; + for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.; + } + if (do_cmmd) + { + int ichip=-1; + common_mode(); + + for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); + } + } +} + +void AsciiRoot::add_channel_list(const ChanList &C) +{ + chan_list.push_back(C); +} + + +void AsciiRoot::common_mode() +{ + ChanList C("0-127"); + common_mode(C); + + _cmmd[0] = C.CommonMode(); + _cnoise[0] = C.Noise(); + + + C.Set("128-255"); + common_mode(C); + + _cmmd[1] = C.CommonMode(); + _cnoise[1] = C.Noise(); +} + +void AsciiRoot::common_mode(ChanList &C, bool correct) +{ + int ip, i, j; + + double mean, sm, xn, xx, xs, xm, tmp; + bool use_it; + mean = sm = 0.; + for (ip=0;ip<3;ip++) + { + xn = xs = xm = 0.; + for (j=0; j0.) + { + mean = xm / xn; + sm = sqrt( xs/xn - mean*mean); + } + // std::cout << "...iter " << ip << ": xm " << mean << " xs: " << sm << std::endl; + } + C.CommonMode(mean); + C.Noise(sm); + + if (correct) + { + for ( j=0; j1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); + } + } +} + + + + +void AsciiRoot::spy_data(bool with_signal, int nevt) +{ + TVirtualPad *pad; + if (!ifile) + return; + + sighandler_t old_handler = ::signal(SIGINT, _A_got_intr); + _A_do_run = true; + + TCanvas *cnvs = (TCanvas *)gROOT->FindObject("cnvs"); + if (cnvs) + { + cnvs->Clear(); + } + else + cnvs = new TCanvas("cnvs","cnvs", 700, 800); + + cnvs->Divide(2,3); + + + TH1 *hsignal = create_h1("hsignal","signal (ADC)",256, -0.5, 255.0); + hsignal->SetXTitle("Channel"); + hsignal->SetYTitle("ADC"); + hsignal->SetMinimum(-300); + hsignal->SetMaximum(300); + + TH1 *helec = create_h1("helec","signal (elec)", 256, -0.5, 255.5); + helec->SetXTitle("Channel"); + helec->SetYTitle("electrons"); + helec->SetMinimum(-300/gain()); + helec->SetMaximum(300/gain()); + + TH1 *hraw = create_h1("hraw","Raw Data (around 512.)",256, 0., 256.); + hraw->SetXTitle("Channel"); + hraw->SetYTitle("ADC"); + hraw->SetMinimum(-300); + hraw->SetMaximum(+300); + + TH1 *hrawc = create_h1("hrawc","Raw Data (no commd)",256, 0., 256.); + hrawc->SetXTitle("Channel"); + hrawc->SetYTitle("ADC"); + hrawc->SetMinimum(-300); + hrawc->SetMaximum(+300); + + + TH1 *hcmmd[2]; + hcmmd[0] = create_h1("hcmmd0","Common mode (Chip 0)",50,-100.,100.); + hcmmd[0]->SetXTitle("Common mode"); + hcmmd[1] = create_h1("hcmmd1","Common mode (Chip 1)",50,-100.,100.); + hcmmd[1]->SetXTitle("Common mode"); + + int ievt,jevt; + for (ievt=jevt=0; read_event()==0 && _A_do_run && ievtSetBinContent(i+1, _signal[i]); + helec->SetBinContent(i+1, signal(i)); + hraw->SetBinContent(i+1,data(i)-512.); + hrawc->SetBinContent(i+1, data(i)-_ped[i]); + // TODO: why we draw the signal + common mode ? + // May be cause signal should be ~0... + hcmmd[ichip]->Fill(_signal[i]+get_cmmd(ichip)); + } + pad = cnvs->cd(1); + pad->SetGrid(1,1); + hsignal->Draw(); + pad = cnvs->cd(2); + pad->SetGrid(1,1); + helec->Draw(); + + pad = cnvs->cd(3); + pad->SetGrid(1,1); + hraw->Draw(); + + pad = cnvs->cd(4); + pad->SetGrid(1,1); + hrawc->Draw(); + + pad = cnvs->cd(5); + pad->SetGrid(1,1); + hcmmd[0]->Draw(); + + pad = cnvs->cd(6); + pad->SetGrid(1,1); + hcmmd[1]->Draw(); + + std::cout << std::setiosflags(std::ios::fixed); + std::cout << "*** Event " << jevt << " *****" << std::endl; + std::cout << "Common Mode:" << std::endl + << " Chip 0 " << std::setw(6) << std::setprecision(1) << get_cmmd(0) << " noise: " << get_cnoise(0) + << std::endl + << " Chip 1 " << std::setw(6) << std::setprecision(1) << get_cmmd(1) << " noise: " << get_cnoise(1) + << std::endl; + + std::cout << "Time: " << time() << " ns" << std::endl; + std::cout << "Signal chan(0) << " << signal(0) << " chan(1) " << signal(1) << std::endl; + std::cout << "Clusters: " << std::endl; + + HitList::iterator ip; + for (ip=begin(); ip!=end(); ++ip) + { + std::cout << " chan: " << ip->center() + << " sig: " + << std::setw(6) << std::setprecision(1) << ip->signal() + << " left: " << ip->left() << " right: " << ip->right() + << std::endl; + std::cout << '\t' << "channels: " << std::endl; + int j; + for (j=ip->left();j<=ip->right();j++) + std::cout << "\t " << j << " sn: " << _sn[j] << " signal: " << _signal[j] << " noise: " << _noise[j] << '\n'; + std::cout << std::endl; + } + + cnvs->Update(); + ievt++; + } + std::cout << std::endl; + _A_do_run= true; + ::signal(SIGINT, old_handler); +} + +bool is_text(const char *fnam) +{ + int nc; + char buffer[1024]; + std::ifstream ifile(fnam); + if (!fnam) + return false; + + ifile.read(buffer, sizeof(buffer)); + nc = ifile.gcount(); + ifile.close(); + if (!nc) // empty files are text + { + return true; + } + + std::string ss(buffer, nc); + ifile.close(); + + if ( ss.find('\0') != ss.npos ) + return false; + + double nontext = 0.; + double ntotal = 0.; + std::string::iterator ip; + for (ip=ss.begin(); ip!=ss.end(); ++ip) + { + ntotal++; + char c = *ip; + if ( (c<' ' || c >'~') && !strchr("\n\t\r\b", c) ) + nontext++; + } + if ( nontext/ntotal > 0.3 ) + return false; + + return true; +} + diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.h.svn-base new file mode 100644 index 0000000..e3950b5 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.h.svn-base @@ -0,0 +1,246 @@ +#ifndef __Alibava_AsciiRoot_h__ +#define __Alibava_AsciiRoot_h__ + +#include +#include "Data.h" +#include "Hit.h" +#include "ChanList.h" +#include +#include +#include + + +/** + * This is the class that reads the data files + */ + +class AsciiRoot +{ + public: + typedef std::vector XtraValues; + enum BlockType { NewFile=0, StartOfRun, DataBlock, CheckPoint, EndOfRun }; + private: + static const int max_nchan=256; + std::ifstream *ifile; + unsigned int data_start; + int _type; + time_t _t0; + int _npoints; + int _from; + int _to; + int _step; + int _nevts; + int _nchan; // current number of channels + XtraValues _xtra; // extra values from header + double _seedcut; + double _neighcut; + unsigned short _header[2][16]; + double _ped[max_nchan]; + double _noise[max_nchan]; + double _signal[max_nchan]; + double _sn[max_nchan]; + double _cmmd[2]; + double _cnoise[2]; + double _gain[max_nchan]; + double _average_gain; + bool _mask[max_nchan]; + int _version; + int _polarity; + HitList _hits; + + std::vector chan_list; + EventDataBlock _data; + + protected: + void reset_data(); + + public: + void set_data(int i, unsigned short x) { _data.data[i] = x; } + + public: + AsciiRoot(const char *nam=0, const char *pedfile=0, const char *gainfile=0); + virtual ~AsciiRoot(); + + bool valid() const + { + return (ifile!=0); + } + + void open(const char *name); + void close(); + void rewind(); + int read_event(); + virtual void check_point(int, const char *) {}; + virtual void new_file(int, const char *) {} + virtual void start_of_run(int, const char *) {} + virtual void end_of_run(int, const char *) {} + virtual void new_data_block(int, const char *) {}; + + // The data format version + int version() const { return _version; } + + + int polarity() const { return _polarity; } + void polarity(int x) { _polarity = ( x<0 ? -1 : 1); } + /* + * Sets the number of channels and the data in the case + * of non "standard" values. If data==0, then only the number + * of channels is changed + */ + void set_data(int nchan, const unsigned short *data=0); + int nchan() const { return _nchan; } + int type() const + { + return _type; + } + char *date() const + { + return ctime(&_t0); + } + double ped(int i) const + { + return _ped[i]/_gain[i]; + } + double noise(int i) const + { + return _noise[i]/_gain[i]; + } + double signal(int i) const + { + return _signal[i]/_gain[i]; + } + + double sn(int i) const + { + return _sn[i]; + } + + double get_cmmd(int i) const + { + return _cmmd[i]; + } + + double get_cnoise(int i) const + { + return _cnoise[i]; + } + + unsigned short data(int i) const + { + return _data.data[i]; + } + double value() const + { + return _data.value; + } + double time() const; + double temp() const; + int npts() const + { + return _npoints; + } + int from() const + { + return _from; + } + int to() const + { + return _to; + } + int step() const + { + return _step; + } + int nevts() const + { + return _step; + } + + void add_hit(const Hit &h) + { + _hits.push_back(h); + } + HitList::iterator begin() + { + return _hits.begin(); + } + HitList::iterator end() + { + return _hits.end(); + } + int nhits() const + { + return _hits.size(); + } + bool empty() const + { + return _hits.empty(); + } + const Hit &hit(int i) const + { + return _hits[i]; + } + void set_hit_list(const HitList &L) { _hits = L; } + void clear() + { + _hits.clear(); + } + + double get_gain(int i) const + { + return _gain[i]; + } + double gain() const + { + return _average_gain; + } + + double seed_cut() const + { + return _seedcut; + } + double neigh_cut() const + { + return _neighcut; + } + void set_cuts(double s, double n) + { + _seedcut = s; + _neighcut = n; + } + unsigned short get_header(int ichip, int ibit) { return _header[ichip][ibit]; } + + TH1 *show_pedestals(); + TH1 *show_noise(); + TH2 *compute_pedestals(int mxevts=-1, bool do_cmmd=true); + //void get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts); + void get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts); + int get_nevents(); + void compute_pedestals_fast(int mxevts = -1, double ped_weight=0.01, double noise_weight=0.001); + + + void process_event(bool do_cmmd=true); + void find_clusters(int ichip=-1); + void find_clusters(ChanList &C); + void save_pedestals(const char *fnam); + void load_pedestals(const char *fnam); + void load_gain(const char *fnam); + void load_masking(const char *fnam); + void spy_data(bool with_signal=false, int nevt=1); + void common_mode(); + void common_mode(ChanList &C, bool correct=false); + + int n_channel_list() const { return chan_list.size(); } + void add_channel_list(const ChanList &C); + void clear_channel_lists() { chan_list.clear(); } + ChanList get_channel_list(int i) const { return chan_list[i]; } + + int nxtra() const { return _xtra.size(); } + const std::string xtra(int i) const { return _xtra[i]; } + void add_xtra(const std::string &x) { _xtra.push_back(x); } + void add_xtra(const char *x) { _xtra.push_back(x); } +}; +// Return true if file is an ASCII text file +bool is_text(const char *); + +#endif diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/ChanList.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/ChanList.cc.svn-base new file mode 100644 index 0000000..4ccb3c9 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/ChanList.cc.svn-base @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +#include +#include "ChanList.h" + +static char svStr[8192]; + +////////////////////////////////////////////////////////////////////////// +// ChanList class +// +// Created: Mon Sep 7 18:50:38 1998 Author: Carlos Lacasta +// Purpose: +// +////////////////////////////////////////////////////////////////////////// +int cmp(const void *x1, const void *x2) +{ + double d1 = *(double *) x1 - *(double *) x2; + return (d1 == 0. ? 0 : (d1 < 0. ? -1 : 1)); +} +int icmp(const void *x1, const void *x2) +{ + int d1 = *(int *) x1 - *(int *) x2; + return (d1 == 0 ? 0 : (d1 < 0 ? -1 : 1)); +} + +ChanList::ChanList(int i1, int i2) : + nch(0), cm(0.), noise(0.) +{ + int i; + for (i = 0; i < nch; i++) + ch.push_back(i1 + i); + + nch = ch.size(); +} +ChanList::ChanList(const char *str) : + cm(0.), noise(0.) +{ + if (str == 0) + return; + Set(str); +} + +ChanList::ChanList(const ChanList &cl) +{ + copy(cl); +} + +ChanList &ChanList::operator=(const ChanList &cl) +{ + if (&cl == this) + return *this; + copy(cl); + return *this; +} + +void ChanList::copy(const ChanList &cl) +{ + if ( &cl != this ) + { + hits = cl.hits; + ch = cl.ch; + nch = ch.size(); + cm = cl.cm; + noise = cl.noise; + } +} +int ChanList::Set(const char *str) +{ + char *p, *q; + int i, is; + int ival1, ival2, wRange; + + ch.clear(); + strcpy(svStr, str); + p = q = svStr; + wRange = 0; + while (*p) + { + switch (*p) + { + case ',': + *p = 0; + if (wRange) + { + is = sscanf(q, "%d", &ival2); + for (i = ival1 + 1; i <= ival2; i++) + ch.push_back(i); + } + else + { + is = sscanf(q, "%d", &ival1); + ch.push_back(ival1); + } + wRange = 0; + q = p + 1; + p += is; + break; + case '-': + *p = 0; + wRange = 1; + is = sscanf(q, "%d", &ival1); + ch.push_back(ival1); + q = p + 1; + p += is; + break; + } + p++; + } + if (wRange) + { + is = sscanf(q, "%d", &ival2); + for (i = ival1 + 1; i <= ival2; i++) + ch.push_back(i); + } + else + { + is = sscanf(q, "%d", &ival2); + ch.push_back(ival2); + } + // sort + std::sort(ch.begin(), ch.end()); + nch = ch.size(); + return nch; +} + +std::ostream &operator<<(std::ostream &os, ChanList &cl) +{ + for (int i = 0; i < cl.Nch(); i++) + { + if (i && !(i % 15)) + os << std::endl; + os << std::setw(5) << cl[i]; + } + return os; +} + +int ChanList::ParseChanList(const char *str, ChanList **cl) +{ + int ireg, nreg = 0; + std::vector clst; + char *q, *p, *ptr; + + if (str == 0) + return 0; + if (cl == 0) + return 0; + p = ptr = strdup(str); + do + { + clst.push_back(p); + q = strchr(p, ';'); + if (q == 0) + break; + *q++ = 0; + p = q; + } while (*p); + + nreg = clst.size(); + *cl = new ChanList[nreg]; + for (ireg = 0; ireg < nreg; ireg++) + (*cl)[ireg].Set(clst[ireg]); + + free(ptr); + return nreg; +} diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/ChanList.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/ChanList.h.svn-base new file mode 100644 index 0000000..920763d --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/ChanList.h.svn-base @@ -0,0 +1,75 @@ +/* -*- mode: c++ -*- */ +#ifndef __ChanList_h__ +#define __ChanList_h__ + +#include +#include +#include "Hit.h" +////////////////////////////////////////////////////////////////////////// +// ChanList +// +// Created: Mon Sep 7 18:46:56 1998 Author: Carlos Lacasta +// Purpose: Class to handle channel lists +// +////////////////////////////////////////////////////////////////////////// + +class ChanList +{ + protected: + int nch; // number of channels in the list + std::vector ch; // list of channels + HitList hits; + double cm; // Common mode + double noise; // noise + void copy(const ChanList &); + public: + ChanList(const char *ch = 0); + ChanList(int, int); + ChanList(const ChanList &); + ChanList &operator=(const ChanList &); + virtual ~ChanList() + { + hits.clear(); + ch.clear(); + } + int Set(const char *); + int Nch() const + { + return nch; + } + int Chan(int x) const + { + return ch[x]; + } + + int operator[](int x) const { return ch[x]; } + void add_hit(const Hit &h) { hits.push_back(h); } + bool empty() const { return hits.empty(); } + int nhits() const { return hits.size(); } + void clear_hits() { hits.clear(); } + const Hit &get_hit(int i) const { return hits[i]; } + const HitList hit_list() const { return hits; } + + double CommonMode() const + { + return cm; + } + double Noise() const + { + return noise; + } + ChanList *CommonMode(double x) + { + cm = x; + return this; + } + ChanList *Noise(double x) + { + noise = x; + return this; + } + static int ParseChanList(const char *, ChanList **); +}; +std::ostream &operator<<(std::ostream &, ChanList&); +#endif + diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/Data.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/Data.h.svn-base new file mode 100644 index 0000000..25f27b2 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/Data.h.svn-base @@ -0,0 +1,32 @@ +#ifndef DATA_H_ +#define DATA_H_ + +/** + * Data is received from the USB port with this format + * + */ +struct EventBlock +{ + unsigned int time; + unsigned short temp; + unsigned short data[256]; +}; + + + +/** + * We add value as an estra parameter when moving the + * data within the application. It tells the value of + * the variables which are scanned + */ +struct EventData : public EventBlock +{ + double value; +}; + +struct EventDataBlock : public EventData +{ + unsigned short header[32]; +}; + +#endif /*DATA_H_*/ diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/Hit.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/Hit.cc.svn-base new file mode 100644 index 0000000..6d8962b --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/Hit.cc.svn-base @@ -0,0 +1,29 @@ +#include "Hit.h" + +Hit::Hit(int c, int l, int r, double s) : + _center(c), _left(l), _right(r), _sig(s) +{ +} + +Hit::Hit(const Hit &h) +{ + cpy(h); +} +Hit::~Hit() +{ +} + +void Hit::cpy(const Hit &h) +{ + _center = h._center; + _left = h._left; + _right = h._right; + _sig = h._sig; +} +Hit &Hit::operator=(const Hit &h) +{ + if (&h!=this) + cpy(h); + + return *this; +} diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/Hit.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/Hit.h.svn-base new file mode 100644 index 0000000..dca5006 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/Hit.h.svn-base @@ -0,0 +1,35 @@ +#ifndef __HIT_H__ +#define __HIT_H__ + +/** + * A class representing a hit + */ + +#include + +class Hit +{ + private: + int _center; + int _left; + int _right; + double _sig; + + void cpy(const Hit &h); + public: + Hit(int c=0, int l=0, int r=0, double s=0); + Hit(const Hit &h); + ~Hit(); + + Hit &operator=(const Hit &h); + + int center() const { return _center; } + int left() const { return _left; } + int right() const { return _right; } + double signal() const { return _sig; } + int width() const { return _right - _left + 1; } +}; +typedef std::vector< Hit> HitList; + + +#endif /*__HIT_H__*/ diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/Tracer.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/Tracer.cc.svn-base new file mode 100644 index 0000000..dc981c7 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/Tracer.cc.svn-base @@ -0,0 +1,48 @@ +#include + +#include "Tracer.h" + +Tracer::Tracer(const char *name, const char *title, int npts, int avrg) + : size(npts), average(avrg), cntr(0.), val(0.) +{ + hst = new TH1D(name, title, size, 0, size); + +} + +Tracer::~Tracer() +{ + delete hst; +} + +void Tracer::Draw(const char *opt) +{ + hst->Draw(opt); +} + +void Tracer::fill(double x) +{ + if (average>1.) + { + val = (x + cntr*val)/(++cntr); + if ( (int(cntr)%average)==0 ) + { + add_point(val); + } + } + else + add_point(x); +} + +void Tracer::add_point(double x) +{ + queue.push_back(x); + if (queue.size()>size) + queue.pop_front(); + + std::deque::iterator ip; + int ibin = 1; + for (ip=queue.begin();ip!=queue.end();++ip, ibin++) + { + hst->SetBinContent(ibin, *ip); + } +} diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/Tracer.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/Tracer.h.svn-base new file mode 100644 index 0000000..f7826a7 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/Tracer.h.svn-base @@ -0,0 +1,39 @@ +#ifndef TRACER_H_ +#define TRACER_H_ +#include + +class TH1; + +class Tracer +{ + private: + unsigned int size; + int average; + double cntr; + double val; + std::deque queue; + TH1 *hst; + + public: + /** + * Defines a tracer. At input npts defines the size of + * the buffer (a FIFO actually) with the values that the + * Tracer will remember. Also, if average is given, the + * Tracer will add as new points the average over 'average' + * inputs + */ + Tracer(const char*nam, const char *tit, int npts, int average=0); + virtual ~Tracer(); + + void Draw(const char *opt=""); + + TH1 *get_hst() + { + return hst; + } + + void fill(double val); + void add_point(double x); +}; + +#endif /*TRACER_H_*/ diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/utils.cc.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/utils.cc.svn-base new file mode 100644 index 0000000..398432c --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/utils.cc.svn-base @@ -0,0 +1,56 @@ +#include +#include "utils.h" + +TCanvas *create_canvas(const char *name, const char *title, int wx, int wy) +{ + TCanvas *cnvs = (TCanvas *)gROOT->FindObject("name"); + if (cnvs) + delete cnvs; + + if (wx<0 ||wy<0) + cnvs = new TCanvas(name, title); + else + cnvs = new TCanvas(name, title, wx, wy); + + return cnvs; +} + +TH1 *create_h1(const char *name, const char *tit, int n, double x1, double x2) +{ + TH1 *hst = (TH1 *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TH1D(name, tit, n, x1, x2); + return hst; +} + +TH2 *create_h2(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) +{ + TH2 *hst = (TH2 *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TH2D(name, tit, nx, x1, x2, ny, y1, y2); + return hst; +} + +TProfile *create_profile(const char *name, const char *tit, int n, double x1, double x2, double y1, double y2) +{ + TProfile *hst = (TProfile *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TProfile(name, tit, n, x1, x2, y1, y2); + return hst; +} + +TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) +{ + TProfile2D *hst = (TProfile2D *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TProfile2D(name, tit, nx, x1, x2, ny, y1, y2); + return hst; +} diff --git a/Software/TbNtupleMaker/Alibava/.svn/text-base/utils.h.svn-base b/Software/TbNtupleMaker/Alibava/.svn/text-base/utils.h.svn-base new file mode 100644 index 0000000..b82693c --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/.svn/text-base/utils.h.svn-base @@ -0,0 +1,48 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#include +#include +#include +#include +#include +#include +#include + +/** + * Utils + */ +inline bool isws(char c, char const * const wstr=" \t\n") +{ + return (strchr(wstr, c) != NULL); +} + +inline std::string trim_right(const std::string &s) +{ + std::string b=" \t\n"; + std::string str = s; + return str.erase(str.find_last_not_of(b) +1); +} + +inline std::string trim_left(const std::string &s) +{ + std::string b=" \t\n"; + std::string str = s; + return str.erase( 0, str.find_first_not_of(b) ); +} + +inline std::string trim_str(const std::string &s) +{ + std::string str = s; + return trim_left(trim_right(str) ); +} + +TCanvas *create_canvas(const char *name, const char *title, int wx=-1, int wy=-1); +TH1 *create_h1(const char *, const char *, int, double, double); +TH2 *create_h2(const char *, const char *, int, double, double, int, double, double); +TProfile *create_profile(const char *, const char *, int, double, double, double, double); +TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2); + + + +#endif /*UTILS_H_*/ diff --git a/Software/TbNtupleMaker/Alibava/AsciiRoot.cc b/Software/TbNtupleMaker/Alibava/AsciiRoot.cc new file mode 100755 index 0000000..a2da1e0 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/AsciiRoot.cc @@ -0,0 +1,1062 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "AsciiRoot.h" +#include "utils.h" +#include "Tracer.h" +#include "Hit.h" +#include "ChanList.h" + + +#ifdef __APPLE__ +#define sighandler_t sig_t +#endif + +bool _A_do_run = true; +void _A_got_intr(int) +{ + _A_do_run = false; +} + +// decodes the header and returns a vector with the integers found +std::vector decode_header(const std::string &h, AsciiRoot::XtraValues &xtra) +{ + std::vector vout; + std::istringstream istr(h); + char *endptr, *str; + char buf[256]; + long val; + + xtra.clear(); + + while (istr) + { + istr.getline(buf, sizeof(buf), ';'); + if (!istr) + break; + + errno = 0; + val = strtol(buf, &endptr, 0); + + if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) + { + std::string sval(buf), sout; + sout = trim_str(sval); + if (!sout.empty()) + xtra.push_back( sout ); + } + else if ( endptr == buf || *endptr != '\0' ) + { + std::string sval(buf), sout; + sout = trim_str(sval); + if (!sout.empty()) + xtra.push_back( sout ); + } + else + { + vout.push_back(atoi(buf) ); + } + } + return vout; +} + +AsciiRoot::AsciiRoot(const char *nam, const char *pedfile, const char *gainfile) : + _nchan(max_nchan),_seedcut(10.), _neighcut(5.), _average_gain(1.), _version(2), _polarity(1) +{ + int i; + for (i=0;iclose(); + } +} + +void AsciiRoot::open(const char *name) +{ + ifile = new std::ifstream(name); + if (!(*ifile)) + { + std::cout << "Could not open data file: " << name << std::endl; + delete ifile; + ifile = 0; + return; + } + std::string header; + unsigned int ic, lheader; + char c; + ifile->read((char *)&_t0, sizeof(time_t)); + //std::cout << "64b: " << ctime(&_t0) << std::endl; + ifile->read((char *)&_type, sizeof(int)); + //std::cout << "type_ " << _type << std::endl; + if ( _type > 15 ) + { + ifile->seekg(0, std::ios::beg); + ifile->read((char *)&_t0, sizeof(int)); + ifile->read((char *)&_type, sizeof(int)); + //std::cout << "32b: " << ctime(&_t0) << std::endl; + } + + + ifile->read((char *)&lheader, sizeof(unsigned int)); + for (ic=0; ic<80; ic++) + { + ifile->read(&c, sizeof(char)); + //std::cout << "biplab header ch1 " << c << std::endl; + header.append(1, c); + } + header = trim_str(header); + //std::cout << "biplab header ch2 " << header << std::endl; + + if (header[0]!='V' && header[0]!='v') + { + _version = 0; + } + else + { + _version = int(header[1]-'0'); + header = header.substr(5); + } + + std::cout << "type: " << _type << " header: " << header << std::endl; + std::vector param = decode_header(header, _xtra); + ifile->read((char *)_ped, max_nchan*sizeof(double)); + ifile->read((char *)_noise, max_nchan*sizeof(double)); + switch (_type) + { + case 1: // calibration + case 2: // laser sync + _npoints = param[0]; + _from = param[1]; + _to = param[2]; + _step = param[3]; + break; + case 3: // laser run + case 4: // source run + case 5: // pedestal run + if (param.empty()) + _nevts = 100000; + else + _nevts = param[0]; + _npoints = _from = _to = _step = 0; + break; + } + data_start = ifile->tellg(); +} + +void AsciiRoot::rewind() +{ + if (ifile) + { + ifile->clear(); + ifile->seekg(data_start, std::ios::beg); + } +} + +void AsciiRoot::close() +{ + if (ifile) + { + ifile->close(); + delete ifile; + ifile = 0; + } +} + +void AsciiRoot::reset_data() +{ + memset(&_data, 0, sizeof(_data)); +} + +int AsciiRoot::read_event() +{ + if (ifile) + { + unsigned int header, size, user=0, code=0; + char *block_data=0; + if (_version) + { + do + { + do + { + ifile->read((char *)&header, sizeof(unsigned int)); + if (ifile->bad() || ifile->eof()) + return -1; + + code = (header>>16) & 0xFFFF; + } while ( code != 0xcafe ); + + code = header & 0x0fff; + user = header & 0x1000; + switch (code) + { + case NewFile: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + new_file(size, block_data); + break; + case StartOfRun: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + start_of_run(size, block_data); + break; + case DataBlock: + ifile->read((char *)&size, sizeof(unsigned int)); + if (user) + { + reset_data(); + block_data = new char[size]; + ifile->read(block_data, size); + new_data_block(size, block_data); + } + else + { + if ( _version == 1 ) + { + ifile->read((char *)&_data, sizeof(EventData)); + for (int ii=0; ii<2; ii++) + memset(_header[ii], 0, 16*sizeof(unsigned short)); + + } + else + { + ifile->read((char *)&_data.value, sizeof(double)); + ifile->read((char *)&_data.time, sizeof(unsigned int)); + ifile->read((char *)&_data.temp, sizeof(unsigned short)); + for (int ii=0; ii<2; ii++) + { + ifile->read((char *)_header[ii], 16*sizeof(unsigned short)); + ifile->read((char *)&_data.data[ii*128], 128*sizeof(unsigned short)); + } + } + } + + break; + case CheckPoint: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + check_point(size, block_data); + break; + case EndOfRun: + ifile->read((char *)&size, sizeof(unsigned int)); + block_data = new char[size]; + ifile->read(block_data, size); + end_of_run(size, block_data); + break; + default: + std::cout << "Unknown block data type: " << std::hex << header << " - " << code << std::dec << std::endl; + } + if (block_data) + { + delete [] block_data; + block_data = 0; + } + + } while ( code != DataBlock && !(ifile->bad() || ifile->eof()) ); + } + else + { + ifile->read((char *)&_data, sizeof(EventData)); + for (int ii=0; ii<2; ii++) + memset(_header[ii], 0, 16*sizeof(unsigned short)); + } + + if (ifile->eof()) + { + std::cout << "End of file" << std::endl; + return -1; + } + else if (ifile->bad()) + { + std::cout << "Problems with data file" << std::endl; + return -1; + } + else + { + //process_event(); + return 0; + } + } + else + return -1; +} + +void AsciiRoot::set_data(int nchan, const unsigned short int *data) +{ + int i; + _nchan = nchan; + for (i=0;i<_nchan;i++) + _data.data[i] = data[i]; +} + + +double AsciiRoot::time() const +{ + unsigned short fpart = _data.time & 0xffff; + short ipart = (_data.time & 0xffff0000)>>16; + if (ipart<0) + fpart *= -1; +// double tt = 100.*(1. -(ipart + (fpart/65535.))); + double tt = 100.0*(ipart + (fpart/65535.)); + return tt; +} + +double AsciiRoot::temp() const +{ + return 0.12*_data.temp - 39.8; +} + + +TH1 *AsciiRoot::show_pedestals() +{ + int ic; + TH1 *hst = create_h1("hPed","Pedestals",nchan(),-0.5, nchan()-0.5); + hst->SetYTitle("ADCs"); + hst->SetXTitle("Channel no."); + for (ic=0; icSetBinContent(ic+1, _ped[ic]); + + return hst; +} + +TH1 *AsciiRoot::show_noise() +{ + int ic; + TH1 *hst = create_h1("hNoise","Noise",nchan(),-0.5, nchan()-0.5); + if (gain()==1) + { + hst->SetYTitle("ADCs"); + } + else + { + hst->SetYTitle("e^{-} ENC"); + } + hst->SetXTitle("Channel no."); + for (ic=0; icSetBinContent(ic+1, noise(ic)); + + return hst; +} + + + +// Save ADC values in "ADCs" 2D-array of type unsigned short and size "evts x N". +//void AsciiRoot::get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts) +void AsciiRoot::get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts) +{ + int i = -1; + int ievt = -1; + + if (!ifile) + return; + + std::ifstream::pos_type here = ifile->tellg(); + std::cout << "==================================================" << std::endl; + std::cout << "Saving ADC values..." << std::endl; + std::cout << "==================================================" << std::endl; + // int evt_count(0); + // for (ievt=0; read_event()==0 ; ievt++){ evt_count++; } + // std::cout << "biplabd count events: " << evt_count << std::endl; + // rewind(); + + for (ievt=0; read_event()==0 && ievttellg(); + std::cout << "Computing fast pedestas..." << std::endl; + for (ievt=0; read_event()==0 && ievt 1.) + corr = 1.; + + if (corr < -1) + corr = -1.; + + _ped[i] += corr; + + if (fabs(xs) < 3.) + { + _noise[i] = _noise[i]*(1.0-wnoise) + xs*xs*wnoise; + } + } + } + } + std::cout << "\nDone" << std::endl; + rewind(); +} + + +TH2 *AsciiRoot::compute_pedestals(int mxevts, bool do_cmmd) +{ + if (!ifile) + return 0; + + if (mxevts<0) + mxevts = 100000000; + + int ievt, ichan; + TH2 *hst = create_h2("hRaw","Raw data",nchan(), -0.5,nchan()-0.5, 256, -0.5,1023.5); + TH2 *hsts = create_h2("hSig","Signal",nchan(), -0.5,nchan()-0.5,256, -127.5,127.5); + + + std::ifstream::pos_type here = ifile->tellg(); + std::cout << "Computing pedestas..." << std::endl; + for (ievt=0; read_event()==0 && ievtFill(ichan, data(ichan)-get_cmmd(ichan/128)); + + if (!(ievt%100)) + { + std::cout << "\revent " << std::setw(10) << ievt << std::flush; + } + } + std::cout << "\nDone" << std::endl; + rewind(); + + // TODO: _nchan can be updated in an event by event basis + // while here we are assuming that it is the same + // for all the events + for (ichan=0; ichanProjectionY("__hx__", ichan+1, ichan+1); + g->SetParameters(h1->GetSumOfWeights(), h1->GetMean(), h1->GetRMS()); + g->SetRange(h1->GetMean()-2.5*h1->GetRMS(), h1->GetMean()+2.5*h1->GetRMS()); + h1->Fit("g1", "q0wr"); + _ped[ichan] = h1->GetFunction("g1")->GetParameter(1); + _noise[ichan] = h1->GetFunction("g1")->GetParameter(2); + delete h1; + delete g; + } + + rewind(); + for (ievt=0; read_event()==0 && ievtFill(ichan, signal(ichan)); + + if (!(ievt%100)) + { + std::cout << "\revent " << std::setw(10) << ievt << std::flush; + } + } + std::cout << "\nDone" << std::endl; + rewind(); + + return hst; +} + + + + +void AsciiRoot::find_clusters(int ichip) +{ + int chan0=0; + int chan1=255; + if (ichip>=0 && ichip<2) + { + chan0 = ichip*128; + chan1 = (ichip+1)*128 -1; + } + + std::ostringstream ostr; + ostr << chan0 << '-' << chan1; + ChanList C(ostr.str().c_str()); + + clear(); + find_clusters(C); + _hits = C.hit_list(); +} + +void AsciiRoot::find_clusters(ChanList &C) +{ + // TODO: figure out how to determine the chip number in + // all the situations + int i, j, imax=-1, left, right; + double mxsig=-1.e20, sg, val; + bool used[C.Nch()]; + + for (i=0;i _seedcut) + { + val = fabs(signal(i)); + if (mxsig= C.Nch() ) + break; + + sg = signal(C[imax]); + used[imax]=true; + // Now look at neighbors + // first to the left + left = imax; + for (j=imax-1;j>=0;j--) + { + i = C[j]; + if ( used[j] || _signal[i]*polarity()<0.) + break; + + if ( fabs(sn(i)) > _neighcut ) + { + used[j] = true; + sg += signal(i); + left = j; + } + else + // TODO: this needs to be removed + // The idea is to merge to clusters that have only one strip in between + // In the laser runs this is a consequences of reflections... + { + int jx = j-1; + if (jx>=0 ) + { + if ( fabs(sn(C[jx])) > _neighcut ) + continue; + } + break; + } + } + + // now to the right + right = imax; + for (j=imax+1;j_neighcut ) + { + used[j] = true; + sg += signal(i); + right = j; + } + else + // TODO: this needs to be removed + // The idea is to merge to clusters that hanve only one strip in between + // In the laser runs this is a consequences of reflections... + { + int jx = i+1; + if (jx _neighcut ) + continue; + } + break; + } + } + C.add_hit(Hit(imax, left, right, sg)); + } +} + +void AsciiRoot::save_pedestals(const char *fnam) +{ + std::ofstream ofile(fnam); + if (!ofile) + { + std::cout << "Could not open " << fnam << " to save pedestals." << std::endl; + return; + } + + // TODO: _nchan can be updated in an event by event basis + // while here we are assuming that it is the same + // for all the events + int i; + for (i=0; i> _ped[i] >> std::ws >> _noise[i] >> std::ws; + _mask[i] = (_noise[i]>20. || _noise[i]<=0.); + } + ifile.close(); + TCanvas *pedcanvas = create_canvas("Pedcanvas", "Pedestal Values", 600, 400); + TH1 *pedestalhisto = create_h1("pedestalhisto", "Pedestal Values", 256, -0.5, 255.5); + for (i=0; i<256; i++) + { + pedestalhisto->Fill(i, _ped[i]); + } + pedcanvas->cd(1); + pedestalhisto->Draw(); +} + +void AsciiRoot::load_masking(const char *fnam) +{ + std::ifstream ifile(fnam); + if (!ifile) + { + std::cout << "Could not open masked.txt. " << std::endl; + return; + } + int val; + for (int i=0; i<500; i++) + { + ifile >> val >> std::ws; + if (ifile.eof()) + break; + if (val>255) + { + std::cout << "A value is greater than 255, causing an overflow crash. Please check the text file again. It has been set to 1 for continuation purposes. " << std::endl; + val = 1; + } + _mask[val] = true; + } +} + +void AsciiRoot::load_gain(const char *fnam) +{ + std::ifstream ifile(fnam); + if (!ifile) + { + std::cout << "Could not open " << fnam << " to load the gain." << std::endl; + return; + } + int i; + int ichan; + double val, xn, xm; + xn=xm=0.; + for (i=0; i> ichan >> std::ws; + if (ifile.eof()) + break; + ifile >> val; + if (ifile.eof()) + break; + + xn++; + + xm += val; + _gain[ichan] = val; + + ifile >> std::ws; + if (ifile.eof()) + break; + } + if (xn>0) + { + _average_gain = xm/xn; + } + ifile.close(); +} + +void AsciiRoot::process_event(bool do_cmmd) +{ + int i; + for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.; + } + if (do_cmmd) + { + int ichip=-1; + common_mode(); + + for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); + } + } +} + +void AsciiRoot::add_channel_list(const ChanList &C) +{ + chan_list.push_back(C); +} + + +void AsciiRoot::common_mode() +{ + ChanList C("0-127"); + common_mode(C); + + _cmmd[0] = C.CommonMode(); + _cnoise[0] = C.Noise(); + + + C.Set("128-255"); + common_mode(C); + + _cmmd[1] = C.CommonMode(); + _cnoise[1] = C.Noise(); +} + +void AsciiRoot::common_mode(ChanList &C, bool correct) +{ + int ip, i, j; + + double mean, sm, xn, xx, xs, xm, tmp; + bool use_it; + mean = sm = 0.; + for (ip=0;ip<3;ip++) + { + xn = xs = xm = 0.; + for (j=0; j0.) + { + mean = xm / xn; + sm = sqrt( xs/xn - mean*mean); + } + // std::cout << "...iter " << ip << ": xm " << mean << " xs: " << sm << std::endl; + } + C.CommonMode(mean); + C.Noise(sm); + + if (correct) + { + for ( j=0; j1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); + } + } +} + + + + +void AsciiRoot::spy_data(bool with_signal, int nevt) +{ + TVirtualPad *pad; + if (!ifile) + return; + + sighandler_t old_handler = ::signal(SIGINT, _A_got_intr); + _A_do_run = true; + + TCanvas *cnvs = (TCanvas *)gROOT->FindObject("cnvs"); + if (cnvs) + { + cnvs->Clear(); + } + else + cnvs = new TCanvas("cnvs","cnvs", 700, 800); + + cnvs->Divide(2,3); + + + TH1 *hsignal = create_h1("hsignal","signal (ADC)",256, -0.5, 255.0); + hsignal->SetXTitle("Channel"); + hsignal->SetYTitle("ADC"); + hsignal->SetMinimum(-300); + hsignal->SetMaximum(300); + + TH1 *helec = create_h1("helec","signal (elec)", 256, -0.5, 255.5); + helec->SetXTitle("Channel"); + helec->SetYTitle("electrons"); + helec->SetMinimum(-300/gain()); + helec->SetMaximum(300/gain()); + + TH1 *hraw = create_h1("hraw","Raw Data (around 512.)",256, 0., 256.); + hraw->SetXTitle("Channel"); + hraw->SetYTitle("ADC"); + hraw->SetMinimum(-300); + hraw->SetMaximum(+300); + + TH1 *hrawc = create_h1("hrawc","Raw Data (no commd)",256, 0., 256.); + hrawc->SetXTitle("Channel"); + hrawc->SetYTitle("ADC"); + hrawc->SetMinimum(-300); + hrawc->SetMaximum(+300); + + + TH1 *hcmmd[2]; + hcmmd[0] = create_h1("hcmmd0","Common mode (Chip 0)",50,-100.,100.); + hcmmd[0]->SetXTitle("Common mode"); + hcmmd[1] = create_h1("hcmmd1","Common mode (Chip 1)",50,-100.,100.); + hcmmd[1]->SetXTitle("Common mode"); + + int ievt,jevt; + for (ievt=jevt=0; read_event()==0 && _A_do_run && ievtSetBinContent(i+1, _signal[i]); + helec->SetBinContent(i+1, signal(i)); + hraw->SetBinContent(i+1,data(i)-512.); + hrawc->SetBinContent(i+1, data(i)-_ped[i]); + // TODO: why we draw the signal + common mode ? + // May be cause signal should be ~0... + hcmmd[ichip]->Fill(_signal[i]+get_cmmd(ichip)); + } + pad = cnvs->cd(1); + pad->SetGrid(1,1); + hsignal->Draw(); + pad = cnvs->cd(2); + pad->SetGrid(1,1); + helec->Draw(); + + pad = cnvs->cd(3); + pad->SetGrid(1,1); + hraw->Draw(); + + pad = cnvs->cd(4); + pad->SetGrid(1,1); + hrawc->Draw(); + + pad = cnvs->cd(5); + pad->SetGrid(1,1); + hcmmd[0]->Draw(); + + pad = cnvs->cd(6); + pad->SetGrid(1,1); + hcmmd[1]->Draw(); + + std::cout << std::setiosflags(std::ios::fixed); + std::cout << "*** Event " << jevt << " *****" << std::endl; + std::cout << "Common Mode:" << std::endl + << " Chip 0 " << std::setw(6) << std::setprecision(1) << get_cmmd(0) << " noise: " << get_cnoise(0) + << std::endl + << " Chip 1 " << std::setw(6) << std::setprecision(1) << get_cmmd(1) << " noise: " << get_cnoise(1) + << std::endl; + + std::cout << "Time: " << time() << " ns" << std::endl; + std::cout << "Signal chan(0) << " << signal(0) << " chan(1) " << signal(1) << std::endl; + std::cout << "Clusters: " << std::endl; + + HitList::iterator ip; + for (ip=begin(); ip!=end(); ++ip) + { + std::cout << " chan: " << ip->center() + << " sig: " + << std::setw(6) << std::setprecision(1) << ip->signal() + << " left: " << ip->left() << " right: " << ip->right() + << std::endl; + std::cout << '\t' << "channels: " << std::endl; + int j; + for (j=ip->left();j<=ip->right();j++) + std::cout << "\t " << j << " sn: " << _sn[j] << " signal: " << _signal[j] << " noise: " << _noise[j] << '\n'; + std::cout << std::endl; + } + + cnvs->Update(); + ievt++; + } + std::cout << std::endl; + _A_do_run= true; + ::signal(SIGINT, old_handler); +} + +bool is_text(const char *fnam) +{ + int nc; + char buffer[1024]; + std::ifstream ifile(fnam); + if (!fnam) + return false; + + ifile.read(buffer, sizeof(buffer)); + nc = ifile.gcount(); + ifile.close(); + if (!nc) // empty files are text + { + return true; + } + + std::string ss(buffer, nc); + ifile.close(); + + if ( ss.find('\0') != ss.npos ) + return false; + + double nontext = 0.; + double ntotal = 0.; + std::string::iterator ip; + for (ip=ss.begin(); ip!=ss.end(); ++ip) + { + ntotal++; + char c = *ip; + if ( (c<' ' || c >'~') && !strchr("\n\t\r\b", c) ) + nontext++; + } + if ( nontext/ntotal > 0.3 ) + return false; + + return true; +} + diff --git a/Software/TbNtupleMaker/Alibava/AsciiRoot.h b/Software/TbNtupleMaker/Alibava/AsciiRoot.h new file mode 100755 index 0000000..e3950b5 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/AsciiRoot.h @@ -0,0 +1,246 @@ +#ifndef __Alibava_AsciiRoot_h__ +#define __Alibava_AsciiRoot_h__ + +#include +#include "Data.h" +#include "Hit.h" +#include "ChanList.h" +#include +#include +#include + + +/** + * This is the class that reads the data files + */ + +class AsciiRoot +{ + public: + typedef std::vector XtraValues; + enum BlockType { NewFile=0, StartOfRun, DataBlock, CheckPoint, EndOfRun }; + private: + static const int max_nchan=256; + std::ifstream *ifile; + unsigned int data_start; + int _type; + time_t _t0; + int _npoints; + int _from; + int _to; + int _step; + int _nevts; + int _nchan; // current number of channels + XtraValues _xtra; // extra values from header + double _seedcut; + double _neighcut; + unsigned short _header[2][16]; + double _ped[max_nchan]; + double _noise[max_nchan]; + double _signal[max_nchan]; + double _sn[max_nchan]; + double _cmmd[2]; + double _cnoise[2]; + double _gain[max_nchan]; + double _average_gain; + bool _mask[max_nchan]; + int _version; + int _polarity; + HitList _hits; + + std::vector chan_list; + EventDataBlock _data; + + protected: + void reset_data(); + + public: + void set_data(int i, unsigned short x) { _data.data[i] = x; } + + public: + AsciiRoot(const char *nam=0, const char *pedfile=0, const char *gainfile=0); + virtual ~AsciiRoot(); + + bool valid() const + { + return (ifile!=0); + } + + void open(const char *name); + void close(); + void rewind(); + int read_event(); + virtual void check_point(int, const char *) {}; + virtual void new_file(int, const char *) {} + virtual void start_of_run(int, const char *) {} + virtual void end_of_run(int, const char *) {} + virtual void new_data_block(int, const char *) {}; + + // The data format version + int version() const { return _version; } + + + int polarity() const { return _polarity; } + void polarity(int x) { _polarity = ( x<0 ? -1 : 1); } + /* + * Sets the number of channels and the data in the case + * of non "standard" values. If data==0, then only the number + * of channels is changed + */ + void set_data(int nchan, const unsigned short *data=0); + int nchan() const { return _nchan; } + int type() const + { + return _type; + } + char *date() const + { + return ctime(&_t0); + } + double ped(int i) const + { + return _ped[i]/_gain[i]; + } + double noise(int i) const + { + return _noise[i]/_gain[i]; + } + double signal(int i) const + { + return _signal[i]/_gain[i]; + } + + double sn(int i) const + { + return _sn[i]; + } + + double get_cmmd(int i) const + { + return _cmmd[i]; + } + + double get_cnoise(int i) const + { + return _cnoise[i]; + } + + unsigned short data(int i) const + { + return _data.data[i]; + } + double value() const + { + return _data.value; + } + double time() const; + double temp() const; + int npts() const + { + return _npoints; + } + int from() const + { + return _from; + } + int to() const + { + return _to; + } + int step() const + { + return _step; + } + int nevts() const + { + return _step; + } + + void add_hit(const Hit &h) + { + _hits.push_back(h); + } + HitList::iterator begin() + { + return _hits.begin(); + } + HitList::iterator end() + { + return _hits.end(); + } + int nhits() const + { + return _hits.size(); + } + bool empty() const + { + return _hits.empty(); + } + const Hit &hit(int i) const + { + return _hits[i]; + } + void set_hit_list(const HitList &L) { _hits = L; } + void clear() + { + _hits.clear(); + } + + double get_gain(int i) const + { + return _gain[i]; + } + double gain() const + { + return _average_gain; + } + + double seed_cut() const + { + return _seedcut; + } + double neigh_cut() const + { + return _neighcut; + } + void set_cuts(double s, double n) + { + _seedcut = s; + _neighcut = n; + } + unsigned short get_header(int ichip, int ibit) { return _header[ichip][ibit]; } + + TH1 *show_pedestals(); + TH1 *show_noise(); + TH2 *compute_pedestals(int mxevts=-1, bool do_cmmd=true); + //void get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts); + void get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts); + int get_nevents(); + void compute_pedestals_fast(int mxevts = -1, double ped_weight=0.01, double noise_weight=0.001); + + + void process_event(bool do_cmmd=true); + void find_clusters(int ichip=-1); + void find_clusters(ChanList &C); + void save_pedestals(const char *fnam); + void load_pedestals(const char *fnam); + void load_gain(const char *fnam); + void load_masking(const char *fnam); + void spy_data(bool with_signal=false, int nevt=1); + void common_mode(); + void common_mode(ChanList &C, bool correct=false); + + int n_channel_list() const { return chan_list.size(); } + void add_channel_list(const ChanList &C); + void clear_channel_lists() { chan_list.clear(); } + ChanList get_channel_list(int i) const { return chan_list[i]; } + + int nxtra() const { return _xtra.size(); } + const std::string xtra(int i) const { return _xtra[i]; } + void add_xtra(const std::string &x) { _xtra.push_back(x); } + void add_xtra(const char *x) { _xtra.push_back(x); } +}; +// Return true if file is an ASCII text file +bool is_text(const char *); + +#endif diff --git a/Software/TbNtupleMaker/Alibava/ChanList.cc b/Software/TbNtupleMaker/Alibava/ChanList.cc new file mode 100755 index 0000000..4ccb3c9 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/ChanList.cc @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +#include +#include "ChanList.h" + +static char svStr[8192]; + +////////////////////////////////////////////////////////////////////////// +// ChanList class +// +// Created: Mon Sep 7 18:50:38 1998 Author: Carlos Lacasta +// Purpose: +// +////////////////////////////////////////////////////////////////////////// +int cmp(const void *x1, const void *x2) +{ + double d1 = *(double *) x1 - *(double *) x2; + return (d1 == 0. ? 0 : (d1 < 0. ? -1 : 1)); +} +int icmp(const void *x1, const void *x2) +{ + int d1 = *(int *) x1 - *(int *) x2; + return (d1 == 0 ? 0 : (d1 < 0 ? -1 : 1)); +} + +ChanList::ChanList(int i1, int i2) : + nch(0), cm(0.), noise(0.) +{ + int i; + for (i = 0; i < nch; i++) + ch.push_back(i1 + i); + + nch = ch.size(); +} +ChanList::ChanList(const char *str) : + cm(0.), noise(0.) +{ + if (str == 0) + return; + Set(str); +} + +ChanList::ChanList(const ChanList &cl) +{ + copy(cl); +} + +ChanList &ChanList::operator=(const ChanList &cl) +{ + if (&cl == this) + return *this; + copy(cl); + return *this; +} + +void ChanList::copy(const ChanList &cl) +{ + if ( &cl != this ) + { + hits = cl.hits; + ch = cl.ch; + nch = ch.size(); + cm = cl.cm; + noise = cl.noise; + } +} +int ChanList::Set(const char *str) +{ + char *p, *q; + int i, is; + int ival1, ival2, wRange; + + ch.clear(); + strcpy(svStr, str); + p = q = svStr; + wRange = 0; + while (*p) + { + switch (*p) + { + case ',': + *p = 0; + if (wRange) + { + is = sscanf(q, "%d", &ival2); + for (i = ival1 + 1; i <= ival2; i++) + ch.push_back(i); + } + else + { + is = sscanf(q, "%d", &ival1); + ch.push_back(ival1); + } + wRange = 0; + q = p + 1; + p += is; + break; + case '-': + *p = 0; + wRange = 1; + is = sscanf(q, "%d", &ival1); + ch.push_back(ival1); + q = p + 1; + p += is; + break; + } + p++; + } + if (wRange) + { + is = sscanf(q, "%d", &ival2); + for (i = ival1 + 1; i <= ival2; i++) + ch.push_back(i); + } + else + { + is = sscanf(q, "%d", &ival2); + ch.push_back(ival2); + } + // sort + std::sort(ch.begin(), ch.end()); + nch = ch.size(); + return nch; +} + +std::ostream &operator<<(std::ostream &os, ChanList &cl) +{ + for (int i = 0; i < cl.Nch(); i++) + { + if (i && !(i % 15)) + os << std::endl; + os << std::setw(5) << cl[i]; + } + return os; +} + +int ChanList::ParseChanList(const char *str, ChanList **cl) +{ + int ireg, nreg = 0; + std::vector clst; + char *q, *p, *ptr; + + if (str == 0) + return 0; + if (cl == 0) + return 0; + p = ptr = strdup(str); + do + { + clst.push_back(p); + q = strchr(p, ';'); + if (q == 0) + break; + *q++ = 0; + p = q; + } while (*p); + + nreg = clst.size(); + *cl = new ChanList[nreg]; + for (ireg = 0; ireg < nreg; ireg++) + (*cl)[ireg].Set(clst[ireg]); + + free(ptr); + return nreg; +} diff --git a/Software/TbNtupleMaker/Alibava/ChanList.h b/Software/TbNtupleMaker/Alibava/ChanList.h new file mode 100755 index 0000000..920763d --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/ChanList.h @@ -0,0 +1,75 @@ +/* -*- mode: c++ -*- */ +#ifndef __ChanList_h__ +#define __ChanList_h__ + +#include +#include +#include "Hit.h" +////////////////////////////////////////////////////////////////////////// +// ChanList +// +// Created: Mon Sep 7 18:46:56 1998 Author: Carlos Lacasta +// Purpose: Class to handle channel lists +// +////////////////////////////////////////////////////////////////////////// + +class ChanList +{ + protected: + int nch; // number of channels in the list + std::vector ch; // list of channels + HitList hits; + double cm; // Common mode + double noise; // noise + void copy(const ChanList &); + public: + ChanList(const char *ch = 0); + ChanList(int, int); + ChanList(const ChanList &); + ChanList &operator=(const ChanList &); + virtual ~ChanList() + { + hits.clear(); + ch.clear(); + } + int Set(const char *); + int Nch() const + { + return nch; + } + int Chan(int x) const + { + return ch[x]; + } + + int operator[](int x) const { return ch[x]; } + void add_hit(const Hit &h) { hits.push_back(h); } + bool empty() const { return hits.empty(); } + int nhits() const { return hits.size(); } + void clear_hits() { hits.clear(); } + const Hit &get_hit(int i) const { return hits[i]; } + const HitList hit_list() const { return hits; } + + double CommonMode() const + { + return cm; + } + double Noise() const + { + return noise; + } + ChanList *CommonMode(double x) + { + cm = x; + return this; + } + ChanList *Noise(double x) + { + noise = x; + return this; + } + static int ParseChanList(const char *, ChanList **); +}; +std::ostream &operator<<(std::ostream &, ChanList&); +#endif + diff --git a/Software/TbNtupleMaker/Alibava/Data.h b/Software/TbNtupleMaker/Alibava/Data.h new file mode 100755 index 0000000..25f27b2 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/Data.h @@ -0,0 +1,32 @@ +#ifndef DATA_H_ +#define DATA_H_ + +/** + * Data is received from the USB port with this format + * + */ +struct EventBlock +{ + unsigned int time; + unsigned short temp; + unsigned short data[256]; +}; + + + +/** + * We add value as an estra parameter when moving the + * data within the application. It tells the value of + * the variables which are scanned + */ +struct EventData : public EventBlock +{ + double value; +}; + +struct EventDataBlock : public EventData +{ + unsigned short header[32]; +}; + +#endif /*DATA_H_*/ diff --git a/Software/TbNtupleMaker/Alibava/Hit.cc b/Software/TbNtupleMaker/Alibava/Hit.cc new file mode 100755 index 0000000..6d8962b --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/Hit.cc @@ -0,0 +1,29 @@ +#include "Hit.h" + +Hit::Hit(int c, int l, int r, double s) : + _center(c), _left(l), _right(r), _sig(s) +{ +} + +Hit::Hit(const Hit &h) +{ + cpy(h); +} +Hit::~Hit() +{ +} + +void Hit::cpy(const Hit &h) +{ + _center = h._center; + _left = h._left; + _right = h._right; + _sig = h._sig; +} +Hit &Hit::operator=(const Hit &h) +{ + if (&h!=this) + cpy(h); + + return *this; +} diff --git a/Software/TbNtupleMaker/Alibava/Hit.h b/Software/TbNtupleMaker/Alibava/Hit.h new file mode 100755 index 0000000..dca5006 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/Hit.h @@ -0,0 +1,35 @@ +#ifndef __HIT_H__ +#define __HIT_H__ + +/** + * A class representing a hit + */ + +#include + +class Hit +{ + private: + int _center; + int _left; + int _right; + double _sig; + + void cpy(const Hit &h); + public: + Hit(int c=0, int l=0, int r=0, double s=0); + Hit(const Hit &h); + ~Hit(); + + Hit &operator=(const Hit &h); + + int center() const { return _center; } + int left() const { return _left; } + int right() const { return _right; } + double signal() const { return _sig; } + int width() const { return _right - _left + 1; } +}; +typedef std::vector< Hit> HitList; + + +#endif /*__HIT_H__*/ diff --git a/Software/TbNtupleMaker/Alibava/Tracer.cc b/Software/TbNtupleMaker/Alibava/Tracer.cc new file mode 100755 index 0000000..dc981c7 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/Tracer.cc @@ -0,0 +1,48 @@ +#include + +#include "Tracer.h" + +Tracer::Tracer(const char *name, const char *title, int npts, int avrg) + : size(npts), average(avrg), cntr(0.), val(0.) +{ + hst = new TH1D(name, title, size, 0, size); + +} + +Tracer::~Tracer() +{ + delete hst; +} + +void Tracer::Draw(const char *opt) +{ + hst->Draw(opt); +} + +void Tracer::fill(double x) +{ + if (average>1.) + { + val = (x + cntr*val)/(++cntr); + if ( (int(cntr)%average)==0 ) + { + add_point(val); + } + } + else + add_point(x); +} + +void Tracer::add_point(double x) +{ + queue.push_back(x); + if (queue.size()>size) + queue.pop_front(); + + std::deque::iterator ip; + int ibin = 1; + for (ip=queue.begin();ip!=queue.end();++ip, ibin++) + { + hst->SetBinContent(ibin, *ip); + } +} diff --git a/Software/TbNtupleMaker/Alibava/Tracer.h b/Software/TbNtupleMaker/Alibava/Tracer.h new file mode 100755 index 0000000..f7826a7 --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/Tracer.h @@ -0,0 +1,39 @@ +#ifndef TRACER_H_ +#define TRACER_H_ +#include + +class TH1; + +class Tracer +{ + private: + unsigned int size; + int average; + double cntr; + double val; + std::deque queue; + TH1 *hst; + + public: + /** + * Defines a tracer. At input npts defines the size of + * the buffer (a FIFO actually) with the values that the + * Tracer will remember. Also, if average is given, the + * Tracer will add as new points the average over 'average' + * inputs + */ + Tracer(const char*nam, const char *tit, int npts, int average=0); + virtual ~Tracer(); + + void Draw(const char *opt=""); + + TH1 *get_hst() + { + return hst; + } + + void fill(double val); + void add_point(double x); +}; + +#endif /*TRACER_H_*/ diff --git a/Software/TbNtupleMaker/Alibava/utils.cc b/Software/TbNtupleMaker/Alibava/utils.cc new file mode 100755 index 0000000..398432c --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/utils.cc @@ -0,0 +1,56 @@ +#include +#include "utils.h" + +TCanvas *create_canvas(const char *name, const char *title, int wx, int wy) +{ + TCanvas *cnvs = (TCanvas *)gROOT->FindObject("name"); + if (cnvs) + delete cnvs; + + if (wx<0 ||wy<0) + cnvs = new TCanvas(name, title); + else + cnvs = new TCanvas(name, title, wx, wy); + + return cnvs; +} + +TH1 *create_h1(const char *name, const char *tit, int n, double x1, double x2) +{ + TH1 *hst = (TH1 *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TH1D(name, tit, n, x1, x2); + return hst; +} + +TH2 *create_h2(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) +{ + TH2 *hst = (TH2 *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TH2D(name, tit, nx, x1, x2, ny, y1, y2); + return hst; +} + +TProfile *create_profile(const char *name, const char *tit, int n, double x1, double x2, double y1, double y2) +{ + TProfile *hst = (TProfile *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TProfile(name, tit, n, x1, x2, y1, y2); + return hst; +} + +TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) +{ + TProfile2D *hst = (TProfile2D *)gROOT->FindObject(name); + if (hst) + delete hst; + + hst = new TProfile2D(name, tit, nx, x1, x2, ny, y1, y2); + return hst; +} diff --git a/Software/TbNtupleMaker/Alibava/utils.h b/Software/TbNtupleMaker/Alibava/utils.h new file mode 100755 index 0000000..b82693c --- /dev/null +++ b/Software/TbNtupleMaker/Alibava/utils.h @@ -0,0 +1,48 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#include +#include +#include +#include +#include +#include +#include + +/** + * Utils + */ +inline bool isws(char c, char const * const wstr=" \t\n") +{ + return (strchr(wstr, c) != NULL); +} + +inline std::string trim_right(const std::string &s) +{ + std::string b=" \t\n"; + std::string str = s; + return str.erase(str.find_last_not_of(b) +1); +} + +inline std::string trim_left(const std::string &s) +{ + std::string b=" \t\n"; + std::string str = s; + return str.erase( 0, str.find_first_not_of(b) ); +} + +inline std::string trim_str(const std::string &s) +{ + std::string str = s; + return trim_left(trim_right(str) ); +} + +TCanvas *create_canvas(const char *name, const char *title, int wx=-1, int wy=-1); +TH1 *create_h1(const char *, const char *, int, double, double); +TH2 *create_h2(const char *, const char *, int, double, double, int, double, double); +TProfile *create_profile(const char *, const char *, int, double, double, double, double); +TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2); + + + +#endif /*UTILS_H_*/ diff --git a/Software/TbNtupleMaker/Makefile b/Software/TbNtupleMaker/Makefile new file mode 100755 index 0000000..3232cf8 --- /dev/null +++ b/Software/TbNtupleMaker/Makefile @@ -0,0 +1,38 @@ +# +CC=$(CXX) +glib_cflags=$(shell pkg-config --cflags glib-2.0 gio-2.0) +glib_libs=$(shell pkg-config --libs glib-2.0 gio-2.0) + +ROOTC=$(shell root-config --cflags) +ROOTL=$(shell root-config --libs) +OPT=-g -fno-inline #-std=c++11 +CppFLAGS=$(OPT) -I. $(glib_cflags) +CXXFLAGS=-fPIC $(CppFLAGS) + +LDLIBS=$(alibava_libs) + + +extra_alibava_libs = Alibava/AsciiRoot.cc Alibava/ChanList.cc Alibava/Hit.cc Alibava/Tracer.cc Alibava/utils.cc + +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Linux) + SO=so + SO_FLAGS=-shared + CXXFLAGS += -D LINUX +endif +ifeq ($(UNAME_S),Darwin) + SO=dylib + SO_FLAGS=-dynamiclib -undefined dynamic_lookup -install_name @rpath/$@ + CXXFLAGS += -D OSX +endif + +all: TbNtupleMaker + +TbNtupleMaker: TbNtupleMaker.C + c++ -I$(OPT) $(CXXFLAGS) $(ROOTC) $(extra_alibava_libs) -o $@ $^ $(LDLIBS) $(ROOTL) $(gliblibs) + + + +clean: + rm -f *.o TbNtupleMaker + rm -rf *.dSYM diff --git a/Software/TbNtupleMaker/TbNtupleMaker b/Software/TbNtupleMaker/TbNtupleMaker new file mode 100755 index 0000000..bf99686 --- /dev/null +++ b/Software/TbNtupleMaker/TbNtupleMaker Binary files differ diff --git a/Software/TbNtupleMaker/TbNtupleMaker.C b/Software/TbNtupleMaker/TbNtupleMaker.C new file mode 100755 index 0000000..fb2e3b7 --- /dev/null +++ b/Software/TbNtupleMaker/TbNtupleMaker.C @@ -0,0 +1,312 @@ +//************************************************ +// Author: Federica Lionetto, Adam Davis, Biplab Dey, Paolo Gandini +// Created on: 07/30/2014 +//************************************************ + +/* +TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information: +- int NEvents, the number of events read from the header of the Alibava file; +- int RunType, the run type; +- std::vector PedByGain, the pedestals calculated by the Alibava; +- std::vector NoiseByGain, the noise calculated by the Alibava; +- std::vector Gain, the gain calculated by the Alibava; +- TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N-1); +- TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N-1); +- TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N-1); +- string Date = a->date(), the date read from the header of the Alibava file; +- std::vector ADC, the raw ADC counts, one per event; +- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event; +- double Temp, the temperature, one per event; +- int n, a counter, one per event (starting from 1). + +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. + +Compile with: + +make + +Run with: + +./TbNtupleMaker [Alibava file] [external path] + +where: +- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process; +- [external path] is the optional external path where the RootFiles folder will be created. + +For example: + +./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali + +or + +./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /... +*/ + +//************************************************ +// Types: +// 1 = calibration run +// 2 = laser synchronization run +// 3 = laser run +// 4 = radioactive source run +// 5 = pedestal run +//************************************************ + +#include +#include + +#include + +#include + +#include "TFile.h" +#include "TROOT.h" +#include "TSystem.h" +#include "TTree.h" + +#include "Alibava/AsciiRoot.h" + +#ifdef __MAKECINT__ +#pragma link C++ class vector+; +#endif + +using namespace std; + +// Number of Beetle channels. +const int N = 256; + +void TbNtupleMaker(char *filename, char *externalPath = 0); + +int main(int argc, char *argv[]) +{ + if(argc < 2) + { + cout << "**************************************************" << endl; + + cout << "Error! Input file missing..." << endl; + cout << "Please use the following format:" << endl; + cout << "./TbNtupleMaker [1] [2]" << endl; + cout << "with:" << endl; + cout << "[1] = Alibava file, complete path;" << endl; + cout << "[2] = External path, optional." << endl; + cout << "Type ./TbNtupleMaker --info for more information." << endl; + + cout << "**************************************************" << endl; + + return 0; + } + else if (string(argv[1]) == "--info") + { + cout << "**************************************************" << endl; + + cout << "TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information:" << endl; + cout << "- int NEvents, the number of events read from the header of the Alibava file;" << endl; + cout << "- int RunType, the run type;" << endl; + cout << "- std::vector PedByGain, the pedestals calculated by the Alibava;" << endl; + cout << "- std::vector NoiseByGain, the noise calculated by the Alibava;" << endl; + cout << "- std::vector Gain, the gain calculated by the Alibava;" << endl; + cout << "- TH1F histo_PedByGain;" << endl; + cout << "- TH1F histo_NoiseByGain;" << endl; + cout << "- TH1F histo_Gain;" << endl; + cout << "- string Date = a->date(), the date read from the header of the Alibava file;" << endl; + cout << "- std::vector ADC, the raw ADC counts, one per event;" << endl; + cout << "- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event;" << endl; + cout << "- double Temp, the temperature, one per event;" << endl; + cout << "- int n, a counter, one per event (starting from 1)." << endl; + + cout << "**************************************************" << endl; + + 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; + + cout << "**************************************************" << endl; + + cout << "Compile with:" << endl; + cout << "make" << endl; + cout << "Run with:" << endl; + cout << "./TbNtupleMaker [Alibava file] [external path]" << endl; + cout << "where:" << endl; + cout << "- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process;" << endl; + cout << "- [external path] is the optional external path where the RootFiles folder will be created." << endl; + cout << "For example:" << endl; + cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali" << endl; + cout << "or" << endl; + cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /..." << endl; + + cout << "**************************************************" << endl; + + return 0; + } + else + { + cout << "File to process: " << argv[1] << endl; + if (argc == 2) + TbNtupleMaker(argv[1]); + else if (argc == 3) + TbNtupleMaker(argv[1],argv[2]); + else + { + cout << "Error! Too many arguments given..." << endl; + + return 0; + } + + return 0; + } +} + + + +void TbNtupleMaker(char *filename, char *externalPath){ + cout << "**************************************************" << endl; + cout << "Decoding raw data..." << endl; + cout << "**************************************************" << endl; + + // Do not comment this line. + gROOT->ProcessLine("#include "); + + // Do some fanciness to get the directory right. + string filename_as_string = string(filename); + string prefix = filename_as_string.substr(0,filename_as_string.find_last_of('/')); + if (externalPath!=0) + prefix = string(externalPath); + string ali_filename = filename_as_string.substr(filename_as_string.find_last_of('/')+1); + TString outfilename = ((TString)prefix)+"/RootFiles/"+((TString)ali_filename); + outfilename.ReplaceAll(".ali",".root"); + + // Create a folder named RootFiles. Do not worry, nothing bad is going to happen if the folder already exists. + cout << "Create a folder named RootFiles" << endl; + + char *path_to_make = (char*)("mkdir "+prefix+"/RootFiles" ).c_str(); + cout << "Setting output to: " << path_to_make << endl; + + system(path_to_make); + + // Open ROOT file. + TFile *output = TFile::Open(outfilename,"RECREATE"); + + // Open Alibava file. + AsciiRoot *a = new AsciiRoot(filename); + + // Create tree structure in ROOT file, to be filled once per file. + int NEvents; + int RunType; + std::vector PedByGain; + std::vector NoiseByGain; + std::vector Gain; + TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N); + TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N); + TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N); + TH1F histo_Signal20("histo_Signal20","Signal calculated by Alibava > 20, distribution over the strips",N,0,N); + TH1F histo_Signal40("histo_Signal40","Signal calculated by Alibava > 40, distribution over the strips",N,0,N); + TH1F histo_Signal60("histo_Signal60","Signal calculated by Alibava > 60, distribution over the strips",N,0,N); + TH1F histo_TDC("histo_TDC","TDC distribution, ADC > 40",50,0,50); + string Date = a->date(); + + /////// vito + TH1D** ADC_ch = new TH1D*[256]; + TH2D* ADCvsEv = new TH2D("ADCvsEv_%d", + "ADCvsEv_%d", + 1e5, -0.5, 99999.5, + 200, 399.5, 600.5 ); + + for(int i=0; i<256; i++){ + + ADC_ch[i] = new TH1D(Form("ADC_ch_%d", i), + Form("ADC_ch_%d", i), + 1024, -0.5, 1023.5 ); + + } + + + // Allocate only necessary space. + PedByGain.reserve(N); + NoiseByGain.reserve(N); + Gain.reserve(N); + + // Create tree structure in ROOT file, to be filled once per event. + std::vector ADC; + double TDCTime; + double Temp; + int n; + + // Fill the header, once per file. + TTree *Header = new TTree("Header","Header"); + + Header->Branch("NEvents",&NEvents); + Header->Branch("RunType",&RunType); + Header->Branch("PedByGain",&PedByGain); + Header->Branch("NoiseByGain",&NoiseByGain); + Header->Branch("Gain",&Gain); + Header->Branch("Date",&Date); + + NEvents = a->get_nevents(); + cout << "Number of events in the Alibava file: " << NEvents << endl; + RunType = a->type(); + for(int iChannel=0; iChannelget_gain(iChannel); + PedByGain[iChannel]=a->ped(iChannel); + NoiseByGain[iChannel] = a->noise(iChannel); + histo_Gain.SetBinContent(iChannel+1,a->get_gain(iChannel)); + histo_PedByGain.SetBinContent(iChannel+1,a->ped(iChannel)); + histo_NoiseByGain.SetBinContent(iChannel+1,a->noise(iChannel)); + } + + Header->Fill(); + cout << "Information in the header:" << endl; + Header->Print(); + cout << "**************************************************" << endl; + cout << "Date from Alibava file: " << Date <Branch("n",&n); + EventInfo->Branch("ADC",&ADC); + EventInfo->Branch("TDCTime",&TDCTime); + EventInfo->Branch("Temp",&Temp); + + // Loop over events. + for (int iEvent=0; iEventread_event(); + + // Loop over Beetle channels. + for (int jChannel=0; jChanneldata(jChannel)-PedByGain[jChannel]>20) histo_Signal20.Fill(jChannel); + if(a->data(jChannel)-PedByGain[jChannel]>40) histo_Signal40.Fill(jChannel); + if(a->data(jChannel)-PedByGain[jChannel]>60) histo_Signal60.Fill(jChannel); + ADC.push_back(a->data(jChannel)); + ADC_ch[jChannel]->Fill( a->data(jChannel) ); + if(jChannel==200) ADCvsEv->Fill( iEvent, a->data(jChannel) ); + } + + + + n = iEvent+1; + TDCTime =a->time(); + for(int t=0; t40) {histo_TDC.Fill(TDCTime,1); break;} + Temp = a->temp(); + EventInfo->Fill(); + } + + + + + cout << "Information in the data: " << endl; + EventInfo->Print(); + + // Close Alibava file. + a->close(); + + // Write and close ROOT file. + for(int i=0; i<256; i++) + ADC_ch[i]->Write(); + ADCvsEv->Write(); + + output->Write(); + output->Close(); + + return; +} diff --git a/Software/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Info.plist b/Software/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Info.plist new file mode 100644 index 0000000..e1c4d22 --- /dev/null +++ b/Software/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.TbNtupleMaker + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Software/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Resources/DWARF/TbNtupleMaker b/Software/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Resources/DWARF/TbNtupleMaker new file mode 100644 index 0000000..9a371dc --- /dev/null +++ b/Software/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Resources/DWARF/TbNtupleMaker Binary files differ diff --git a/TbNtupleMaker/.svn/entries b/TbNtupleMaker/.svn/entries deleted file mode 100644 index 60fd98e..0000000 --- a/TbNtupleMaker/.svn/entries +++ /dev/null @@ -1,99 +0,0 @@ -10 - -dir -60 -svn+ssh://svn.cern.ch/reps/uttestbeamsoft/TbNtupleMaker -svn+ssh://svn.cern.ch/reps/uttestbeamsoft - - - -2014-09-17T16:18:11.012127Z -53 -pgandini - - - - - - - - - - - - - - -4525493e-7705-40b1-a816-d608a930855b - -Alibava -dir - -Makefile -file - - - - -2014-08-01T15:29:11.000000Z -9bc47aa65fafa6f889df4c5a6a32b199 -2014-08-01T15:04:34.667335Z -3 -adavis -has-props - - - - - - - - - - - - - - - - - - - - -868 - -TbNtupleMaker.cpp -file - - - - -2014-10-07T12:28:16.000000Z -f5ed09324f7169fa889a4af97c3dacc6 -2014-09-17T16:18:11.012127Z -53 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -10735 - diff --git a/TbNtupleMaker/.svn/prop-base/Makefile.svn-base b/TbNtupleMaker/.svn/prop-base/Makefile.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/.svn/prop-base/Makefile.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/.svn/prop-base/TbNtupleMaker.cpp.svn-base b/TbNtupleMaker/.svn/prop-base/TbNtupleMaker.cpp.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/.svn/prop-base/TbNtupleMaker.cpp.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/.svn/text-base/Makefile.svn-base b/TbNtupleMaker/.svn/text-base/Makefile.svn-base deleted file mode 100644 index b175775..0000000 --- a/TbNtupleMaker/.svn/text-base/Makefile.svn-base +++ /dev/null @@ -1,38 +0,0 @@ -# -CC=$(CXX) -glib_cflags=$(shell pkg-config --cflags glib-2.0 gio-2.0) -glib_libs=$(shell pkg-config --libs glib-2.0 gio-2.0) - -ROOTC=$(shell root-config --cflags) -ROOTL=$(shell root-config --libs) -OPT=-g -fno-inline #-std=c++11 -CppFLAGS=$(OPT) -I. $(glib_cflags) -CXXFLAGS=-fPIC $(CppFLAGS) - -LDLIBS=$(alibava_libs) - - -extra_alibava_libs = Alibava/AsciiRoot.cc Alibava/ChanList.cc Alibava/Hit.cc Alibava/Tracer.cc Alibava/utils.cc - -UNAME_S := $(shell uname -s) -ifeq ($(UNAME_S),Linux) - SO=so - SO_FLAGS=-shared - CXXFLAGS += -D LINUX -endif -ifeq ($(UNAME_S),Darwin) - SO=dylib - SO_FLAGS=-dynamiclib -undefined dynamic_lookup -install_name @rpath/$@ - CXXFLAGS += -D OSX -endif - -all: TbNtupleMaker - -TbNtupleMaker: TbNtupleMaker.cpp - c++ -I$(OPT) $(CXXFLAGS) $(ROOTC) $(extra_alibava_libs) -o $@ $^ $(LDLIBS) $(ROOTL) $(gliblibs) - - - -clean: - rm -f *.o TbNtupleMaker - rm -rf *.dSYM diff --git a/TbNtupleMaker/.svn/text-base/TbNtupleMaker.cpp.svn-base b/TbNtupleMaker/.svn/text-base/TbNtupleMaker.cpp.svn-base deleted file mode 100644 index fb2e3b7..0000000 --- a/TbNtupleMaker/.svn/text-base/TbNtupleMaker.cpp.svn-base +++ /dev/null @@ -1,312 +0,0 @@ -//************************************************ -// Author: Federica Lionetto, Adam Davis, Biplab Dey, Paolo Gandini -// Created on: 07/30/2014 -//************************************************ - -/* -TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information: -- int NEvents, the number of events read from the header of the Alibava file; -- int RunType, the run type; -- std::vector PedByGain, the pedestals calculated by the Alibava; -- std::vector NoiseByGain, the noise calculated by the Alibava; -- std::vector Gain, the gain calculated by the Alibava; -- TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N-1); -- TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N-1); -- TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N-1); -- string Date = a->date(), the date read from the header of the Alibava file; -- std::vector ADC, the raw ADC counts, one per event; -- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event; -- double Temp, the temperature, one per event; -- int n, a counter, one per event (starting from 1). - -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. - -Compile with: - -make - -Run with: - -./TbNtupleMaker [Alibava file] [external path] - -where: -- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process; -- [external path] is the optional external path where the RootFiles folder will be created. - -For example: - -./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali - -or - -./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /... -*/ - -//************************************************ -// Types: -// 1 = calibration run -// 2 = laser synchronization run -// 3 = laser run -// 4 = radioactive source run -// 5 = pedestal run -//************************************************ - -#include -#include - -#include - -#include - -#include "TFile.h" -#include "TROOT.h" -#include "TSystem.h" -#include "TTree.h" - -#include "Alibava/AsciiRoot.h" - -#ifdef __MAKECINT__ -#pragma link C++ class vector+; -#endif - -using namespace std; - -// Number of Beetle channels. -const int N = 256; - -void TbNtupleMaker(char *filename, char *externalPath = 0); - -int main(int argc, char *argv[]) -{ - if(argc < 2) - { - cout << "**************************************************" << endl; - - cout << "Error! Input file missing..." << endl; - cout << "Please use the following format:" << endl; - cout << "./TbNtupleMaker [1] [2]" << endl; - cout << "with:" << endl; - cout << "[1] = Alibava file, complete path;" << endl; - cout << "[2] = External path, optional." << endl; - cout << "Type ./TbNtupleMaker --info for more information." << endl; - - cout << "**************************************************" << endl; - - return 0; - } - else if (string(argv[1]) == "--info") - { - cout << "**************************************************" << endl; - - cout << "TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information:" << endl; - cout << "- int NEvents, the number of events read from the header of the Alibava file;" << endl; - cout << "- int RunType, the run type;" << endl; - cout << "- std::vector PedByGain, the pedestals calculated by the Alibava;" << endl; - cout << "- std::vector NoiseByGain, the noise calculated by the Alibava;" << endl; - cout << "- std::vector Gain, the gain calculated by the Alibava;" << endl; - cout << "- TH1F histo_PedByGain;" << endl; - cout << "- TH1F histo_NoiseByGain;" << endl; - cout << "- TH1F histo_Gain;" << endl; - cout << "- string Date = a->date(), the date read from the header of the Alibava file;" << endl; - cout << "- std::vector ADC, the raw ADC counts, one per event;" << endl; - cout << "- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event;" << endl; - cout << "- double Temp, the temperature, one per event;" << endl; - cout << "- int n, a counter, one per event (starting from 1)." << endl; - - cout << "**************************************************" << endl; - - 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; - - cout << "**************************************************" << endl; - - cout << "Compile with:" << endl; - cout << "make" << endl; - cout << "Run with:" << endl; - cout << "./TbNtupleMaker [Alibava file] [external path]" << endl; - cout << "where:" << endl; - cout << "- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process;" << endl; - cout << "- [external path] is the optional external path where the RootFiles folder will be created." << endl; - cout << "For example:" << endl; - cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali" << endl; - cout << "or" << endl; - cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /..." << endl; - - cout << "**************************************************" << endl; - - return 0; - } - else - { - cout << "File to process: " << argv[1] << endl; - if (argc == 2) - TbNtupleMaker(argv[1]); - else if (argc == 3) - TbNtupleMaker(argv[1],argv[2]); - else - { - cout << "Error! Too many arguments given..." << endl; - - return 0; - } - - return 0; - } -} - - - -void TbNtupleMaker(char *filename, char *externalPath){ - cout << "**************************************************" << endl; - cout << "Decoding raw data..." << endl; - cout << "**************************************************" << endl; - - // Do not comment this line. - gROOT->ProcessLine("#include "); - - // Do some fanciness to get the directory right. - string filename_as_string = string(filename); - string prefix = filename_as_string.substr(0,filename_as_string.find_last_of('/')); - if (externalPath!=0) - prefix = string(externalPath); - string ali_filename = filename_as_string.substr(filename_as_string.find_last_of('/')+1); - TString outfilename = ((TString)prefix)+"/RootFiles/"+((TString)ali_filename); - outfilename.ReplaceAll(".ali",".root"); - - // Create a folder named RootFiles. Do not worry, nothing bad is going to happen if the folder already exists. - cout << "Create a folder named RootFiles" << endl; - - char *path_to_make = (char*)("mkdir "+prefix+"/RootFiles" ).c_str(); - cout << "Setting output to: " << path_to_make << endl; - - system(path_to_make); - - // Open ROOT file. - TFile *output = TFile::Open(outfilename,"RECREATE"); - - // Open Alibava file. - AsciiRoot *a = new AsciiRoot(filename); - - // Create tree structure in ROOT file, to be filled once per file. - int NEvents; - int RunType; - std::vector PedByGain; - std::vector NoiseByGain; - std::vector Gain; - TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N); - TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N); - TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N); - TH1F histo_Signal20("histo_Signal20","Signal calculated by Alibava > 20, distribution over the strips",N,0,N); - TH1F histo_Signal40("histo_Signal40","Signal calculated by Alibava > 40, distribution over the strips",N,0,N); - TH1F histo_Signal60("histo_Signal60","Signal calculated by Alibava > 60, distribution over the strips",N,0,N); - TH1F histo_TDC("histo_TDC","TDC distribution, ADC > 40",50,0,50); - string Date = a->date(); - - /////// vito - TH1D** ADC_ch = new TH1D*[256]; - TH2D* ADCvsEv = new TH2D("ADCvsEv_%d", - "ADCvsEv_%d", - 1e5, -0.5, 99999.5, - 200, 399.5, 600.5 ); - - for(int i=0; i<256; i++){ - - ADC_ch[i] = new TH1D(Form("ADC_ch_%d", i), - Form("ADC_ch_%d", i), - 1024, -0.5, 1023.5 ); - - } - - - // Allocate only necessary space. - PedByGain.reserve(N); - NoiseByGain.reserve(N); - Gain.reserve(N); - - // Create tree structure in ROOT file, to be filled once per event. - std::vector ADC; - double TDCTime; - double Temp; - int n; - - // Fill the header, once per file. - TTree *Header = new TTree("Header","Header"); - - Header->Branch("NEvents",&NEvents); - Header->Branch("RunType",&RunType); - Header->Branch("PedByGain",&PedByGain); - Header->Branch("NoiseByGain",&NoiseByGain); - Header->Branch("Gain",&Gain); - Header->Branch("Date",&Date); - - NEvents = a->get_nevents(); - cout << "Number of events in the Alibava file: " << NEvents << endl; - RunType = a->type(); - for(int iChannel=0; iChannelget_gain(iChannel); - PedByGain[iChannel]=a->ped(iChannel); - NoiseByGain[iChannel] = a->noise(iChannel); - histo_Gain.SetBinContent(iChannel+1,a->get_gain(iChannel)); - histo_PedByGain.SetBinContent(iChannel+1,a->ped(iChannel)); - histo_NoiseByGain.SetBinContent(iChannel+1,a->noise(iChannel)); - } - - Header->Fill(); - cout << "Information in the header:" << endl; - Header->Print(); - cout << "**************************************************" << endl; - cout << "Date from Alibava file: " << Date <Branch("n",&n); - EventInfo->Branch("ADC",&ADC); - EventInfo->Branch("TDCTime",&TDCTime); - EventInfo->Branch("Temp",&Temp); - - // Loop over events. - for (int iEvent=0; iEventread_event(); - - // Loop over Beetle channels. - for (int jChannel=0; jChanneldata(jChannel)-PedByGain[jChannel]>20) histo_Signal20.Fill(jChannel); - if(a->data(jChannel)-PedByGain[jChannel]>40) histo_Signal40.Fill(jChannel); - if(a->data(jChannel)-PedByGain[jChannel]>60) histo_Signal60.Fill(jChannel); - ADC.push_back(a->data(jChannel)); - ADC_ch[jChannel]->Fill( a->data(jChannel) ); - if(jChannel==200) ADCvsEv->Fill( iEvent, a->data(jChannel) ); - } - - - - n = iEvent+1; - TDCTime =a->time(); - for(int t=0; t40) {histo_TDC.Fill(TDCTime,1); break;} - Temp = a->temp(); - EventInfo->Fill(); - } - - - - - cout << "Information in the data: " << endl; - EventInfo->Print(); - - // Close Alibava file. - a->close(); - - // Write and close ROOT file. - for(int i=0; i<256; i++) - ADC_ch[i]->Write(); - ADCvsEv->Write(); - - output->Write(); - output->Close(); - - return; -} diff --git a/TbNtupleMaker/Alibava/.svn/entries b/TbNtupleMaker/Alibava/.svn/entries deleted file mode 100644 index 06d09fc..0000000 --- a/TbNtupleMaker/Alibava/.svn/entries +++ /dev/null @@ -1,402 +0,0 @@ -10 - -dir -60 -svn+ssh://svn.cern.ch/reps/uttestbeamsoft/TbNtupleMaker/Alibava -svn+ssh://svn.cern.ch/reps/uttestbeamsoft - - - -2014-08-01T15:04:34.667335Z -3 -adavis - - - - - - - - - - - - - - -4525493e-7705-40b1-a816-d608a930855b - -Data.h -file - - - - -2014-08-01T15:29:10.000000Z -3414fc45868ba3fc614af9f9656c5eae -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -534 - -AsciiRoot.cc -file - - - - -2014-08-01T15:29:10.000000Z -9128c1fee0be552821280c4256e987fc -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -27793 - -AsciiRoot.h -file - - - - -2014-08-01T15:29:10.000000Z -bd86e71e91b2522c9ab0cd88d79010ac -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -6434 - -Tracer.cc -file - - - - -2014-08-01T15:29:10.000000Z -1e94d579f29cffebbd1712c7fbe3aaaf -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -821 - -utils.cc -file - - - - -2014-08-01T15:29:10.000000Z -195b1b4c0a26c44b2e16c5683fcc6ad0 -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -1431 - -Tracer.h -file - - - - -2014-08-01T15:29:10.000000Z -5e5a7dbc61e4ac62f62a3504e0dd76fb -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -851 - -utils.h -file - - - - -2014-08-01T15:29:10.000000Z -cc1dc60f40a33a19055f2351c7b78ad3 -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -1204 - -Hit.cc -file - - - - -2014-08-01T15:29:10.000000Z -312055db8abdb6034291a1dc2969e73a -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -377 - -ChanList.cc -file - - - - -2014-08-01T15:29:10.000000Z -2d53c320c22923a479eeae101f691a44 -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -3459 - -Hit.h -file - - - - -2014-08-01T15:29:10.000000Z -df70623e70b7368ca29caac39abd48d5 -2014-08-01T13:24:08.054812Z -1 -pgandini -has-props - - - - - - - - - - - - - - - - - - - - -705 - -ChanList.h -file - - - - -2014-08-01T15:29:10.000000Z -ca27e831a2ff3bdc615250abbd59ee35 -2014-08-01T15:04:34.667335Z -3 -adavis -has-props - - - - - - - - - - - - - - - - - - - - -2002 - diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.cc.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.cc.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.cc.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.h.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.h.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/AsciiRoot.h.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.cc.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.cc.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.cc.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.h.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.h.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/ChanList.h.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/Data.h.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/Data.h.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/Data.h.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/Hit.cc.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/Hit.cc.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/Hit.cc.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/Hit.h.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/Hit.h.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/Hit.h.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.cc.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.cc.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.cc.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.h.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.h.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/Tracer.h.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/utils.cc.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/utils.cc.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/utils.cc.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/prop-base/utils.h.svn-base b/TbNtupleMaker/Alibava/.svn/prop-base/utils.h.svn-base deleted file mode 100644 index 869ac71..0000000 --- a/TbNtupleMaker/Alibava/.svn/prop-base/utils.h.svn-base +++ /dev/null @@ -1,5 +0,0 @@ -K 14 -svn:executable -V 1 -* -END diff --git a/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.cc.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.cc.svn-base deleted file mode 100644 index a2da1e0..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.cc.svn-base +++ /dev/null @@ -1,1062 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "AsciiRoot.h" -#include "utils.h" -#include "Tracer.h" -#include "Hit.h" -#include "ChanList.h" - - -#ifdef __APPLE__ -#define sighandler_t sig_t -#endif - -bool _A_do_run = true; -void _A_got_intr(int) -{ - _A_do_run = false; -} - -// decodes the header and returns a vector with the integers found -std::vector decode_header(const std::string &h, AsciiRoot::XtraValues &xtra) -{ - std::vector vout; - std::istringstream istr(h); - char *endptr, *str; - char buf[256]; - long val; - - xtra.clear(); - - while (istr) - { - istr.getline(buf, sizeof(buf), ';'); - if (!istr) - break; - - errno = 0; - val = strtol(buf, &endptr, 0); - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) - { - std::string sval(buf), sout; - sout = trim_str(sval); - if (!sout.empty()) - xtra.push_back( sout ); - } - else if ( endptr == buf || *endptr != '\0' ) - { - std::string sval(buf), sout; - sout = trim_str(sval); - if (!sout.empty()) - xtra.push_back( sout ); - } - else - { - vout.push_back(atoi(buf) ); - } - } - return vout; -} - -AsciiRoot::AsciiRoot(const char *nam, const char *pedfile, const char *gainfile) : - _nchan(max_nchan),_seedcut(10.), _neighcut(5.), _average_gain(1.), _version(2), _polarity(1) -{ - int i; - for (i=0;iclose(); - } -} - -void AsciiRoot::open(const char *name) -{ - ifile = new std::ifstream(name); - if (!(*ifile)) - { - std::cout << "Could not open data file: " << name << std::endl; - delete ifile; - ifile = 0; - return; - } - std::string header; - unsigned int ic, lheader; - char c; - ifile->read((char *)&_t0, sizeof(time_t)); - //std::cout << "64b: " << ctime(&_t0) << std::endl; - ifile->read((char *)&_type, sizeof(int)); - //std::cout << "type_ " << _type << std::endl; - if ( _type > 15 ) - { - ifile->seekg(0, std::ios::beg); - ifile->read((char *)&_t0, sizeof(int)); - ifile->read((char *)&_type, sizeof(int)); - //std::cout << "32b: " << ctime(&_t0) << std::endl; - } - - - ifile->read((char *)&lheader, sizeof(unsigned int)); - for (ic=0; ic<80; ic++) - { - ifile->read(&c, sizeof(char)); - //std::cout << "biplab header ch1 " << c << std::endl; - header.append(1, c); - } - header = trim_str(header); - //std::cout << "biplab header ch2 " << header << std::endl; - - if (header[0]!='V' && header[0]!='v') - { - _version = 0; - } - else - { - _version = int(header[1]-'0'); - header = header.substr(5); - } - - std::cout << "type: " << _type << " header: " << header << std::endl; - std::vector param = decode_header(header, _xtra); - ifile->read((char *)_ped, max_nchan*sizeof(double)); - ifile->read((char *)_noise, max_nchan*sizeof(double)); - switch (_type) - { - case 1: // calibration - case 2: // laser sync - _npoints = param[0]; - _from = param[1]; - _to = param[2]; - _step = param[3]; - break; - case 3: // laser run - case 4: // source run - case 5: // pedestal run - if (param.empty()) - _nevts = 100000; - else - _nevts = param[0]; - _npoints = _from = _to = _step = 0; - break; - } - data_start = ifile->tellg(); -} - -void AsciiRoot::rewind() -{ - if (ifile) - { - ifile->clear(); - ifile->seekg(data_start, std::ios::beg); - } -} - -void AsciiRoot::close() -{ - if (ifile) - { - ifile->close(); - delete ifile; - ifile = 0; - } -} - -void AsciiRoot::reset_data() -{ - memset(&_data, 0, sizeof(_data)); -} - -int AsciiRoot::read_event() -{ - if (ifile) - { - unsigned int header, size, user=0, code=0; - char *block_data=0; - if (_version) - { - do - { - do - { - ifile->read((char *)&header, sizeof(unsigned int)); - if (ifile->bad() || ifile->eof()) - return -1; - - code = (header>>16) & 0xFFFF; - } while ( code != 0xcafe ); - - code = header & 0x0fff; - user = header & 0x1000; - switch (code) - { - case NewFile: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - new_file(size, block_data); - break; - case StartOfRun: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - start_of_run(size, block_data); - break; - case DataBlock: - ifile->read((char *)&size, sizeof(unsigned int)); - if (user) - { - reset_data(); - block_data = new char[size]; - ifile->read(block_data, size); - new_data_block(size, block_data); - } - else - { - if ( _version == 1 ) - { - ifile->read((char *)&_data, sizeof(EventData)); - for (int ii=0; ii<2; ii++) - memset(_header[ii], 0, 16*sizeof(unsigned short)); - - } - else - { - ifile->read((char *)&_data.value, sizeof(double)); - ifile->read((char *)&_data.time, sizeof(unsigned int)); - ifile->read((char *)&_data.temp, sizeof(unsigned short)); - for (int ii=0; ii<2; ii++) - { - ifile->read((char *)_header[ii], 16*sizeof(unsigned short)); - ifile->read((char *)&_data.data[ii*128], 128*sizeof(unsigned short)); - } - } - } - - break; - case CheckPoint: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - check_point(size, block_data); - break; - case EndOfRun: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - end_of_run(size, block_data); - break; - default: - std::cout << "Unknown block data type: " << std::hex << header << " - " << code << std::dec << std::endl; - } - if (block_data) - { - delete [] block_data; - block_data = 0; - } - - } while ( code != DataBlock && !(ifile->bad() || ifile->eof()) ); - } - else - { - ifile->read((char *)&_data, sizeof(EventData)); - for (int ii=0; ii<2; ii++) - memset(_header[ii], 0, 16*sizeof(unsigned short)); - } - - if (ifile->eof()) - { - std::cout << "End of file" << std::endl; - return -1; - } - else if (ifile->bad()) - { - std::cout << "Problems with data file" << std::endl; - return -1; - } - else - { - //process_event(); - return 0; - } - } - else - return -1; -} - -void AsciiRoot::set_data(int nchan, const unsigned short int *data) -{ - int i; - _nchan = nchan; - for (i=0;i<_nchan;i++) - _data.data[i] = data[i]; -} - - -double AsciiRoot::time() const -{ - unsigned short fpart = _data.time & 0xffff; - short ipart = (_data.time & 0xffff0000)>>16; - if (ipart<0) - fpart *= -1; -// double tt = 100.*(1. -(ipart + (fpart/65535.))); - double tt = 100.0*(ipart + (fpart/65535.)); - return tt; -} - -double AsciiRoot::temp() const -{ - return 0.12*_data.temp - 39.8; -} - - -TH1 *AsciiRoot::show_pedestals() -{ - int ic; - TH1 *hst = create_h1("hPed","Pedestals",nchan(),-0.5, nchan()-0.5); - hst->SetYTitle("ADCs"); - hst->SetXTitle("Channel no."); - for (ic=0; icSetBinContent(ic+1, _ped[ic]); - - return hst; -} - -TH1 *AsciiRoot::show_noise() -{ - int ic; - TH1 *hst = create_h1("hNoise","Noise",nchan(),-0.5, nchan()-0.5); - if (gain()==1) - { - hst->SetYTitle("ADCs"); - } - else - { - hst->SetYTitle("e^{-} ENC"); - } - hst->SetXTitle("Channel no."); - for (ic=0; icSetBinContent(ic+1, noise(ic)); - - return hst; -} - - - -// Save ADC values in "ADCs" 2D-array of type unsigned short and size "evts x N". -//void AsciiRoot::get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts) -void AsciiRoot::get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts) -{ - int i = -1; - int ievt = -1; - - if (!ifile) - return; - - std::ifstream::pos_type here = ifile->tellg(); - std::cout << "==================================================" << std::endl; - std::cout << "Saving ADC values..." << std::endl; - std::cout << "==================================================" << std::endl; - // int evt_count(0); - // for (ievt=0; read_event()==0 ; ievt++){ evt_count++; } - // std::cout << "biplabd count events: " << evt_count << std::endl; - // rewind(); - - for (ievt=0; read_event()==0 && ievttellg(); - std::cout << "Computing fast pedestas..." << std::endl; - for (ievt=0; read_event()==0 && ievt 1.) - corr = 1.; - - if (corr < -1) - corr = -1.; - - _ped[i] += corr; - - if (fabs(xs) < 3.) - { - _noise[i] = _noise[i]*(1.0-wnoise) + xs*xs*wnoise; - } - } - } - } - std::cout << "\nDone" << std::endl; - rewind(); -} - - -TH2 *AsciiRoot::compute_pedestals(int mxevts, bool do_cmmd) -{ - if (!ifile) - return 0; - - if (mxevts<0) - mxevts = 100000000; - - int ievt, ichan; - TH2 *hst = create_h2("hRaw","Raw data",nchan(), -0.5,nchan()-0.5, 256, -0.5,1023.5); - TH2 *hsts = create_h2("hSig","Signal",nchan(), -0.5,nchan()-0.5,256, -127.5,127.5); - - - std::ifstream::pos_type here = ifile->tellg(); - std::cout << "Computing pedestas..." << std::endl; - for (ievt=0; read_event()==0 && ievtFill(ichan, data(ichan)-get_cmmd(ichan/128)); - - if (!(ievt%100)) - { - std::cout << "\revent " << std::setw(10) << ievt << std::flush; - } - } - std::cout << "\nDone" << std::endl; - rewind(); - - // TODO: _nchan can be updated in an event by event basis - // while here we are assuming that it is the same - // for all the events - for (ichan=0; ichanProjectionY("__hx__", ichan+1, ichan+1); - g->SetParameters(h1->GetSumOfWeights(), h1->GetMean(), h1->GetRMS()); - g->SetRange(h1->GetMean()-2.5*h1->GetRMS(), h1->GetMean()+2.5*h1->GetRMS()); - h1->Fit("g1", "q0wr"); - _ped[ichan] = h1->GetFunction("g1")->GetParameter(1); - _noise[ichan] = h1->GetFunction("g1")->GetParameter(2); - delete h1; - delete g; - } - - rewind(); - for (ievt=0; read_event()==0 && ievtFill(ichan, signal(ichan)); - - if (!(ievt%100)) - { - std::cout << "\revent " << std::setw(10) << ievt << std::flush; - } - } - std::cout << "\nDone" << std::endl; - rewind(); - - return hst; -} - - - - -void AsciiRoot::find_clusters(int ichip) -{ - int chan0=0; - int chan1=255; - if (ichip>=0 && ichip<2) - { - chan0 = ichip*128; - chan1 = (ichip+1)*128 -1; - } - - std::ostringstream ostr; - ostr << chan0 << '-' << chan1; - ChanList C(ostr.str().c_str()); - - clear(); - find_clusters(C); - _hits = C.hit_list(); -} - -void AsciiRoot::find_clusters(ChanList &C) -{ - // TODO: figure out how to determine the chip number in - // all the situations - int i, j, imax=-1, left, right; - double mxsig=-1.e20, sg, val; - bool used[C.Nch()]; - - for (i=0;i _seedcut) - { - val = fabs(signal(i)); - if (mxsig= C.Nch() ) - break; - - sg = signal(C[imax]); - used[imax]=true; - // Now look at neighbors - // first to the left - left = imax; - for (j=imax-1;j>=0;j--) - { - i = C[j]; - if ( used[j] || _signal[i]*polarity()<0.) - break; - - if ( fabs(sn(i)) > _neighcut ) - { - used[j] = true; - sg += signal(i); - left = j; - } - else - // TODO: this needs to be removed - // The idea is to merge to clusters that have only one strip in between - // In the laser runs this is a consequences of reflections... - { - int jx = j-1; - if (jx>=0 ) - { - if ( fabs(sn(C[jx])) > _neighcut ) - continue; - } - break; - } - } - - // now to the right - right = imax; - for (j=imax+1;j_neighcut ) - { - used[j] = true; - sg += signal(i); - right = j; - } - else - // TODO: this needs to be removed - // The idea is to merge to clusters that hanve only one strip in between - // In the laser runs this is a consequences of reflections... - { - int jx = i+1; - if (jx _neighcut ) - continue; - } - break; - } - } - C.add_hit(Hit(imax, left, right, sg)); - } -} - -void AsciiRoot::save_pedestals(const char *fnam) -{ - std::ofstream ofile(fnam); - if (!ofile) - { - std::cout << "Could not open " << fnam << " to save pedestals." << std::endl; - return; - } - - // TODO: _nchan can be updated in an event by event basis - // while here we are assuming that it is the same - // for all the events - int i; - for (i=0; i> _ped[i] >> std::ws >> _noise[i] >> std::ws; - _mask[i] = (_noise[i]>20. || _noise[i]<=0.); - } - ifile.close(); - TCanvas *pedcanvas = create_canvas("Pedcanvas", "Pedestal Values", 600, 400); - TH1 *pedestalhisto = create_h1("pedestalhisto", "Pedestal Values", 256, -0.5, 255.5); - for (i=0; i<256; i++) - { - pedestalhisto->Fill(i, _ped[i]); - } - pedcanvas->cd(1); - pedestalhisto->Draw(); -} - -void AsciiRoot::load_masking(const char *fnam) -{ - std::ifstream ifile(fnam); - if (!ifile) - { - std::cout << "Could not open masked.txt. " << std::endl; - return; - } - int val; - for (int i=0; i<500; i++) - { - ifile >> val >> std::ws; - if (ifile.eof()) - break; - if (val>255) - { - std::cout << "A value is greater than 255, causing an overflow crash. Please check the text file again. It has been set to 1 for continuation purposes. " << std::endl; - val = 1; - } - _mask[val] = true; - } -} - -void AsciiRoot::load_gain(const char *fnam) -{ - std::ifstream ifile(fnam); - if (!ifile) - { - std::cout << "Could not open " << fnam << " to load the gain." << std::endl; - return; - } - int i; - int ichan; - double val, xn, xm; - xn=xm=0.; - for (i=0; i> ichan >> std::ws; - if (ifile.eof()) - break; - ifile >> val; - if (ifile.eof()) - break; - - xn++; - - xm += val; - _gain[ichan] = val; - - ifile >> std::ws; - if (ifile.eof()) - break; - } - if (xn>0) - { - _average_gain = xm/xn; - } - ifile.close(); -} - -void AsciiRoot::process_event(bool do_cmmd) -{ - int i; - for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.; - } - if (do_cmmd) - { - int ichip=-1; - common_mode(); - - for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); - } - } -} - -void AsciiRoot::add_channel_list(const ChanList &C) -{ - chan_list.push_back(C); -} - - -void AsciiRoot::common_mode() -{ - ChanList C("0-127"); - common_mode(C); - - _cmmd[0] = C.CommonMode(); - _cnoise[0] = C.Noise(); - - - C.Set("128-255"); - common_mode(C); - - _cmmd[1] = C.CommonMode(); - _cnoise[1] = C.Noise(); -} - -void AsciiRoot::common_mode(ChanList &C, bool correct) -{ - int ip, i, j; - - double mean, sm, xn, xx, xs, xm, tmp; - bool use_it; - mean = sm = 0.; - for (ip=0;ip<3;ip++) - { - xn = xs = xm = 0.; - for (j=0; j0.) - { - mean = xm / xn; - sm = sqrt( xs/xn - mean*mean); - } - // std::cout << "...iter " << ip << ": xm " << mean << " xs: " << sm << std::endl; - } - C.CommonMode(mean); - C.Noise(sm); - - if (correct) - { - for ( j=0; j1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); - } - } -} - - - - -void AsciiRoot::spy_data(bool with_signal, int nevt) -{ - TVirtualPad *pad; - if (!ifile) - return; - - sighandler_t old_handler = ::signal(SIGINT, _A_got_intr); - _A_do_run = true; - - TCanvas *cnvs = (TCanvas *)gROOT->FindObject("cnvs"); - if (cnvs) - { - cnvs->Clear(); - } - else - cnvs = new TCanvas("cnvs","cnvs", 700, 800); - - cnvs->Divide(2,3); - - - TH1 *hsignal = create_h1("hsignal","signal (ADC)",256, -0.5, 255.0); - hsignal->SetXTitle("Channel"); - hsignal->SetYTitle("ADC"); - hsignal->SetMinimum(-300); - hsignal->SetMaximum(300); - - TH1 *helec = create_h1("helec","signal (elec)", 256, -0.5, 255.5); - helec->SetXTitle("Channel"); - helec->SetYTitle("electrons"); - helec->SetMinimum(-300/gain()); - helec->SetMaximum(300/gain()); - - TH1 *hraw = create_h1("hraw","Raw Data (around 512.)",256, 0., 256.); - hraw->SetXTitle("Channel"); - hraw->SetYTitle("ADC"); - hraw->SetMinimum(-300); - hraw->SetMaximum(+300); - - TH1 *hrawc = create_h1("hrawc","Raw Data (no commd)",256, 0., 256.); - hrawc->SetXTitle("Channel"); - hrawc->SetYTitle("ADC"); - hrawc->SetMinimum(-300); - hrawc->SetMaximum(+300); - - - TH1 *hcmmd[2]; - hcmmd[0] = create_h1("hcmmd0","Common mode (Chip 0)",50,-100.,100.); - hcmmd[0]->SetXTitle("Common mode"); - hcmmd[1] = create_h1("hcmmd1","Common mode (Chip 1)",50,-100.,100.); - hcmmd[1]->SetXTitle("Common mode"); - - int ievt,jevt; - for (ievt=jevt=0; read_event()==0 && _A_do_run && ievtSetBinContent(i+1, _signal[i]); - helec->SetBinContent(i+1, signal(i)); - hraw->SetBinContent(i+1,data(i)-512.); - hrawc->SetBinContent(i+1, data(i)-_ped[i]); - // TODO: why we draw the signal + common mode ? - // May be cause signal should be ~0... - hcmmd[ichip]->Fill(_signal[i]+get_cmmd(ichip)); - } - pad = cnvs->cd(1); - pad->SetGrid(1,1); - hsignal->Draw(); - pad = cnvs->cd(2); - pad->SetGrid(1,1); - helec->Draw(); - - pad = cnvs->cd(3); - pad->SetGrid(1,1); - hraw->Draw(); - - pad = cnvs->cd(4); - pad->SetGrid(1,1); - hrawc->Draw(); - - pad = cnvs->cd(5); - pad->SetGrid(1,1); - hcmmd[0]->Draw(); - - pad = cnvs->cd(6); - pad->SetGrid(1,1); - hcmmd[1]->Draw(); - - std::cout << std::setiosflags(std::ios::fixed); - std::cout << "*** Event " << jevt << " *****" << std::endl; - std::cout << "Common Mode:" << std::endl - << " Chip 0 " << std::setw(6) << std::setprecision(1) << get_cmmd(0) << " noise: " << get_cnoise(0) - << std::endl - << " Chip 1 " << std::setw(6) << std::setprecision(1) << get_cmmd(1) << " noise: " << get_cnoise(1) - << std::endl; - - std::cout << "Time: " << time() << " ns" << std::endl; - std::cout << "Signal chan(0) << " << signal(0) << " chan(1) " << signal(1) << std::endl; - std::cout << "Clusters: " << std::endl; - - HitList::iterator ip; - for (ip=begin(); ip!=end(); ++ip) - { - std::cout << " chan: " << ip->center() - << " sig: " - << std::setw(6) << std::setprecision(1) << ip->signal() - << " left: " << ip->left() << " right: " << ip->right() - << std::endl; - std::cout << '\t' << "channels: " << std::endl; - int j; - for (j=ip->left();j<=ip->right();j++) - std::cout << "\t " << j << " sn: " << _sn[j] << " signal: " << _signal[j] << " noise: " << _noise[j] << '\n'; - std::cout << std::endl; - } - - cnvs->Update(); - ievt++; - } - std::cout << std::endl; - _A_do_run= true; - ::signal(SIGINT, old_handler); -} - -bool is_text(const char *fnam) -{ - int nc; - char buffer[1024]; - std::ifstream ifile(fnam); - if (!fnam) - return false; - - ifile.read(buffer, sizeof(buffer)); - nc = ifile.gcount(); - ifile.close(); - if (!nc) // empty files are text - { - return true; - } - - std::string ss(buffer, nc); - ifile.close(); - - if ( ss.find('\0') != ss.npos ) - return false; - - double nontext = 0.; - double ntotal = 0.; - std::string::iterator ip; - for (ip=ss.begin(); ip!=ss.end(); ++ip) - { - ntotal++; - char c = *ip; - if ( (c<' ' || c >'~') && !strchr("\n\t\r\b", c) ) - nontext++; - } - if ( nontext/ntotal > 0.3 ) - return false; - - return true; -} - diff --git a/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.h.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.h.svn-base deleted file mode 100644 index e3950b5..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/AsciiRoot.h.svn-base +++ /dev/null @@ -1,246 +0,0 @@ -#ifndef __Alibava_AsciiRoot_h__ -#define __Alibava_AsciiRoot_h__ - -#include -#include "Data.h" -#include "Hit.h" -#include "ChanList.h" -#include -#include -#include - - -/** - * This is the class that reads the data files - */ - -class AsciiRoot -{ - public: - typedef std::vector XtraValues; - enum BlockType { NewFile=0, StartOfRun, DataBlock, CheckPoint, EndOfRun }; - private: - static const int max_nchan=256; - std::ifstream *ifile; - unsigned int data_start; - int _type; - time_t _t0; - int _npoints; - int _from; - int _to; - int _step; - int _nevts; - int _nchan; // current number of channels - XtraValues _xtra; // extra values from header - double _seedcut; - double _neighcut; - unsigned short _header[2][16]; - double _ped[max_nchan]; - double _noise[max_nchan]; - double _signal[max_nchan]; - double _sn[max_nchan]; - double _cmmd[2]; - double _cnoise[2]; - double _gain[max_nchan]; - double _average_gain; - bool _mask[max_nchan]; - int _version; - int _polarity; - HitList _hits; - - std::vector chan_list; - EventDataBlock _data; - - protected: - void reset_data(); - - public: - void set_data(int i, unsigned short x) { _data.data[i] = x; } - - public: - AsciiRoot(const char *nam=0, const char *pedfile=0, const char *gainfile=0); - virtual ~AsciiRoot(); - - bool valid() const - { - return (ifile!=0); - } - - void open(const char *name); - void close(); - void rewind(); - int read_event(); - virtual void check_point(int, const char *) {}; - virtual void new_file(int, const char *) {} - virtual void start_of_run(int, const char *) {} - virtual void end_of_run(int, const char *) {} - virtual void new_data_block(int, const char *) {}; - - // The data format version - int version() const { return _version; } - - - int polarity() const { return _polarity; } - void polarity(int x) { _polarity = ( x<0 ? -1 : 1); } - /* - * Sets the number of channels and the data in the case - * of non "standard" values. If data==0, then only the number - * of channels is changed - */ - void set_data(int nchan, const unsigned short *data=0); - int nchan() const { return _nchan; } - int type() const - { - return _type; - } - char *date() const - { - return ctime(&_t0); - } - double ped(int i) const - { - return _ped[i]/_gain[i]; - } - double noise(int i) const - { - return _noise[i]/_gain[i]; - } - double signal(int i) const - { - return _signal[i]/_gain[i]; - } - - double sn(int i) const - { - return _sn[i]; - } - - double get_cmmd(int i) const - { - return _cmmd[i]; - } - - double get_cnoise(int i) const - { - return _cnoise[i]; - } - - unsigned short data(int i) const - { - return _data.data[i]; - } - double value() const - { - return _data.value; - } - double time() const; - double temp() const; - int npts() const - { - return _npoints; - } - int from() const - { - return _from; - } - int to() const - { - return _to; - } - int step() const - { - return _step; - } - int nevts() const - { - return _step; - } - - void add_hit(const Hit &h) - { - _hits.push_back(h); - } - HitList::iterator begin() - { - return _hits.begin(); - } - HitList::iterator end() - { - return _hits.end(); - } - int nhits() const - { - return _hits.size(); - } - bool empty() const - { - return _hits.empty(); - } - const Hit &hit(int i) const - { - return _hits[i]; - } - void set_hit_list(const HitList &L) { _hits = L; } - void clear() - { - _hits.clear(); - } - - double get_gain(int i) const - { - return _gain[i]; - } - double gain() const - { - return _average_gain; - } - - double seed_cut() const - { - return _seedcut; - } - double neigh_cut() const - { - return _neighcut; - } - void set_cuts(double s, double n) - { - _seedcut = s; - _neighcut = n; - } - unsigned short get_header(int ichip, int ibit) { return _header[ichip][ibit]; } - - TH1 *show_pedestals(); - TH1 *show_noise(); - TH2 *compute_pedestals(int mxevts=-1, bool do_cmmd=true); - //void get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts); - void get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts); - int get_nevents(); - void compute_pedestals_fast(int mxevts = -1, double ped_weight=0.01, double noise_weight=0.001); - - - void process_event(bool do_cmmd=true); - void find_clusters(int ichip=-1); - void find_clusters(ChanList &C); - void save_pedestals(const char *fnam); - void load_pedestals(const char *fnam); - void load_gain(const char *fnam); - void load_masking(const char *fnam); - void spy_data(bool with_signal=false, int nevt=1); - void common_mode(); - void common_mode(ChanList &C, bool correct=false); - - int n_channel_list() const { return chan_list.size(); } - void add_channel_list(const ChanList &C); - void clear_channel_lists() { chan_list.clear(); } - ChanList get_channel_list(int i) const { return chan_list[i]; } - - int nxtra() const { return _xtra.size(); } - const std::string xtra(int i) const { return _xtra[i]; } - void add_xtra(const std::string &x) { _xtra.push_back(x); } - void add_xtra(const char *x) { _xtra.push_back(x); } -}; -// Return true if file is an ASCII text file -bool is_text(const char *); - -#endif diff --git a/TbNtupleMaker/Alibava/.svn/text-base/ChanList.cc.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/ChanList.cc.svn-base deleted file mode 100644 index 4ccb3c9..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/ChanList.cc.svn-base +++ /dev/null @@ -1,168 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "ChanList.h" - -static char svStr[8192]; - -////////////////////////////////////////////////////////////////////////// -// ChanList class -// -// Created: Mon Sep 7 18:50:38 1998 Author: Carlos Lacasta -// Purpose: -// -////////////////////////////////////////////////////////////////////////// -int cmp(const void *x1, const void *x2) -{ - double d1 = *(double *) x1 - *(double *) x2; - return (d1 == 0. ? 0 : (d1 < 0. ? -1 : 1)); -} -int icmp(const void *x1, const void *x2) -{ - int d1 = *(int *) x1 - *(int *) x2; - return (d1 == 0 ? 0 : (d1 < 0 ? -1 : 1)); -} - -ChanList::ChanList(int i1, int i2) : - nch(0), cm(0.), noise(0.) -{ - int i; - for (i = 0; i < nch; i++) - ch.push_back(i1 + i); - - nch = ch.size(); -} -ChanList::ChanList(const char *str) : - cm(0.), noise(0.) -{ - if (str == 0) - return; - Set(str); -} - -ChanList::ChanList(const ChanList &cl) -{ - copy(cl); -} - -ChanList &ChanList::operator=(const ChanList &cl) -{ - if (&cl == this) - return *this; - copy(cl); - return *this; -} - -void ChanList::copy(const ChanList &cl) -{ - if ( &cl != this ) - { - hits = cl.hits; - ch = cl.ch; - nch = ch.size(); - cm = cl.cm; - noise = cl.noise; - } -} -int ChanList::Set(const char *str) -{ - char *p, *q; - int i, is; - int ival1, ival2, wRange; - - ch.clear(); - strcpy(svStr, str); - p = q = svStr; - wRange = 0; - while (*p) - { - switch (*p) - { - case ',': - *p = 0; - if (wRange) - { - is = sscanf(q, "%d", &ival2); - for (i = ival1 + 1; i <= ival2; i++) - ch.push_back(i); - } - else - { - is = sscanf(q, "%d", &ival1); - ch.push_back(ival1); - } - wRange = 0; - q = p + 1; - p += is; - break; - case '-': - *p = 0; - wRange = 1; - is = sscanf(q, "%d", &ival1); - ch.push_back(ival1); - q = p + 1; - p += is; - break; - } - p++; - } - if (wRange) - { - is = sscanf(q, "%d", &ival2); - for (i = ival1 + 1; i <= ival2; i++) - ch.push_back(i); - } - else - { - is = sscanf(q, "%d", &ival2); - ch.push_back(ival2); - } - // sort - std::sort(ch.begin(), ch.end()); - nch = ch.size(); - return nch; -} - -std::ostream &operator<<(std::ostream &os, ChanList &cl) -{ - for (int i = 0; i < cl.Nch(); i++) - { - if (i && !(i % 15)) - os << std::endl; - os << std::setw(5) << cl[i]; - } - return os; -} - -int ChanList::ParseChanList(const char *str, ChanList **cl) -{ - int ireg, nreg = 0; - std::vector clst; - char *q, *p, *ptr; - - if (str == 0) - return 0; - if (cl == 0) - return 0; - p = ptr = strdup(str); - do - { - clst.push_back(p); - q = strchr(p, ';'); - if (q == 0) - break; - *q++ = 0; - p = q; - } while (*p); - - nreg = clst.size(); - *cl = new ChanList[nreg]; - for (ireg = 0; ireg < nreg; ireg++) - (*cl)[ireg].Set(clst[ireg]); - - free(ptr); - return nreg; -} diff --git a/TbNtupleMaker/Alibava/.svn/text-base/ChanList.h.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/ChanList.h.svn-base deleted file mode 100644 index 920763d..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/ChanList.h.svn-base +++ /dev/null @@ -1,75 +0,0 @@ -/* -*- mode: c++ -*- */ -#ifndef __ChanList_h__ -#define __ChanList_h__ - -#include -#include -#include "Hit.h" -////////////////////////////////////////////////////////////////////////// -// ChanList -// -// Created: Mon Sep 7 18:46:56 1998 Author: Carlos Lacasta -// Purpose: Class to handle channel lists -// -////////////////////////////////////////////////////////////////////////// - -class ChanList -{ - protected: - int nch; // number of channels in the list - std::vector ch; // list of channels - HitList hits; - double cm; // Common mode - double noise; // noise - void copy(const ChanList &); - public: - ChanList(const char *ch = 0); - ChanList(int, int); - ChanList(const ChanList &); - ChanList &operator=(const ChanList &); - virtual ~ChanList() - { - hits.clear(); - ch.clear(); - } - int Set(const char *); - int Nch() const - { - return nch; - } - int Chan(int x) const - { - return ch[x]; - } - - int operator[](int x) const { return ch[x]; } - void add_hit(const Hit &h) { hits.push_back(h); } - bool empty() const { return hits.empty(); } - int nhits() const { return hits.size(); } - void clear_hits() { hits.clear(); } - const Hit &get_hit(int i) const { return hits[i]; } - const HitList hit_list() const { return hits; } - - double CommonMode() const - { - return cm; - } - double Noise() const - { - return noise; - } - ChanList *CommonMode(double x) - { - cm = x; - return this; - } - ChanList *Noise(double x) - { - noise = x; - return this; - } - static int ParseChanList(const char *, ChanList **); -}; -std::ostream &operator<<(std::ostream &, ChanList&); -#endif - diff --git a/TbNtupleMaker/Alibava/.svn/text-base/Data.h.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/Data.h.svn-base deleted file mode 100644 index 25f27b2..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/Data.h.svn-base +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef DATA_H_ -#define DATA_H_ - -/** - * Data is received from the USB port with this format - * - */ -struct EventBlock -{ - unsigned int time; - unsigned short temp; - unsigned short data[256]; -}; - - - -/** - * We add value as an estra parameter when moving the - * data within the application. It tells the value of - * the variables which are scanned - */ -struct EventData : public EventBlock -{ - double value; -}; - -struct EventDataBlock : public EventData -{ - unsigned short header[32]; -}; - -#endif /*DATA_H_*/ diff --git a/TbNtupleMaker/Alibava/.svn/text-base/Hit.cc.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/Hit.cc.svn-base deleted file mode 100644 index 6d8962b..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/Hit.cc.svn-base +++ /dev/null @@ -1,29 +0,0 @@ -#include "Hit.h" - -Hit::Hit(int c, int l, int r, double s) : - _center(c), _left(l), _right(r), _sig(s) -{ -} - -Hit::Hit(const Hit &h) -{ - cpy(h); -} -Hit::~Hit() -{ -} - -void Hit::cpy(const Hit &h) -{ - _center = h._center; - _left = h._left; - _right = h._right; - _sig = h._sig; -} -Hit &Hit::operator=(const Hit &h) -{ - if (&h!=this) - cpy(h); - - return *this; -} diff --git a/TbNtupleMaker/Alibava/.svn/text-base/Hit.h.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/Hit.h.svn-base deleted file mode 100644 index dca5006..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/Hit.h.svn-base +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef __HIT_H__ -#define __HIT_H__ - -/** - * A class representing a hit - */ - -#include - -class Hit -{ - private: - int _center; - int _left; - int _right; - double _sig; - - void cpy(const Hit &h); - public: - Hit(int c=0, int l=0, int r=0, double s=0); - Hit(const Hit &h); - ~Hit(); - - Hit &operator=(const Hit &h); - - int center() const { return _center; } - int left() const { return _left; } - int right() const { return _right; } - double signal() const { return _sig; } - int width() const { return _right - _left + 1; } -}; -typedef std::vector< Hit> HitList; - - -#endif /*__HIT_H__*/ diff --git a/TbNtupleMaker/Alibava/.svn/text-base/Tracer.cc.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/Tracer.cc.svn-base deleted file mode 100644 index dc981c7..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/Tracer.cc.svn-base +++ /dev/null @@ -1,48 +0,0 @@ -#include - -#include "Tracer.h" - -Tracer::Tracer(const char *name, const char *title, int npts, int avrg) - : size(npts), average(avrg), cntr(0.), val(0.) -{ - hst = new TH1D(name, title, size, 0, size); - -} - -Tracer::~Tracer() -{ - delete hst; -} - -void Tracer::Draw(const char *opt) -{ - hst->Draw(opt); -} - -void Tracer::fill(double x) -{ - if (average>1.) - { - val = (x + cntr*val)/(++cntr); - if ( (int(cntr)%average)==0 ) - { - add_point(val); - } - } - else - add_point(x); -} - -void Tracer::add_point(double x) -{ - queue.push_back(x); - if (queue.size()>size) - queue.pop_front(); - - std::deque::iterator ip; - int ibin = 1; - for (ip=queue.begin();ip!=queue.end();++ip, ibin++) - { - hst->SetBinContent(ibin, *ip); - } -} diff --git a/TbNtupleMaker/Alibava/.svn/text-base/Tracer.h.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/Tracer.h.svn-base deleted file mode 100644 index f7826a7..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/Tracer.h.svn-base +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef TRACER_H_ -#define TRACER_H_ -#include - -class TH1; - -class Tracer -{ - private: - unsigned int size; - int average; - double cntr; - double val; - std::deque queue; - TH1 *hst; - - public: - /** - * Defines a tracer. At input npts defines the size of - * the buffer (a FIFO actually) with the values that the - * Tracer will remember. Also, if average is given, the - * Tracer will add as new points the average over 'average' - * inputs - */ - Tracer(const char*nam, const char *tit, int npts, int average=0); - virtual ~Tracer(); - - void Draw(const char *opt=""); - - TH1 *get_hst() - { - return hst; - } - - void fill(double val); - void add_point(double x); -}; - -#endif /*TRACER_H_*/ diff --git a/TbNtupleMaker/Alibava/.svn/text-base/utils.cc.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/utils.cc.svn-base deleted file mode 100644 index 398432c..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/utils.cc.svn-base +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include "utils.h" - -TCanvas *create_canvas(const char *name, const char *title, int wx, int wy) -{ - TCanvas *cnvs = (TCanvas *)gROOT->FindObject("name"); - if (cnvs) - delete cnvs; - - if (wx<0 ||wy<0) - cnvs = new TCanvas(name, title); - else - cnvs = new TCanvas(name, title, wx, wy); - - return cnvs; -} - -TH1 *create_h1(const char *name, const char *tit, int n, double x1, double x2) -{ - TH1 *hst = (TH1 *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TH1D(name, tit, n, x1, x2); - return hst; -} - -TH2 *create_h2(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) -{ - TH2 *hst = (TH2 *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TH2D(name, tit, nx, x1, x2, ny, y1, y2); - return hst; -} - -TProfile *create_profile(const char *name, const char *tit, int n, double x1, double x2, double y1, double y2) -{ - TProfile *hst = (TProfile *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TProfile(name, tit, n, x1, x2, y1, y2); - return hst; -} - -TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) -{ - TProfile2D *hst = (TProfile2D *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TProfile2D(name, tit, nx, x1, x2, ny, y1, y2); - return hst; -} diff --git a/TbNtupleMaker/Alibava/.svn/text-base/utils.h.svn-base b/TbNtupleMaker/Alibava/.svn/text-base/utils.h.svn-base deleted file mode 100644 index b82693c..0000000 --- a/TbNtupleMaker/Alibava/.svn/text-base/utils.h.svn-base +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef UTILS_H_ -#define UTILS_H_ - -#include -#include -#include -#include -#include -#include -#include - -/** - * Utils - */ -inline bool isws(char c, char const * const wstr=" \t\n") -{ - return (strchr(wstr, c) != NULL); -} - -inline std::string trim_right(const std::string &s) -{ - std::string b=" \t\n"; - std::string str = s; - return str.erase(str.find_last_not_of(b) +1); -} - -inline std::string trim_left(const std::string &s) -{ - std::string b=" \t\n"; - std::string str = s; - return str.erase( 0, str.find_first_not_of(b) ); -} - -inline std::string trim_str(const std::string &s) -{ - std::string str = s; - return trim_left(trim_right(str) ); -} - -TCanvas *create_canvas(const char *name, const char *title, int wx=-1, int wy=-1); -TH1 *create_h1(const char *, const char *, int, double, double); -TH2 *create_h2(const char *, const char *, int, double, double, int, double, double); -TProfile *create_profile(const char *, const char *, int, double, double, double, double); -TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2); - - - -#endif /*UTILS_H_*/ diff --git a/TbNtupleMaker/Alibava/AsciiRoot.cc b/TbNtupleMaker/Alibava/AsciiRoot.cc deleted file mode 100755 index a2da1e0..0000000 --- a/TbNtupleMaker/Alibava/AsciiRoot.cc +++ /dev/null @@ -1,1062 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "AsciiRoot.h" -#include "utils.h" -#include "Tracer.h" -#include "Hit.h" -#include "ChanList.h" - - -#ifdef __APPLE__ -#define sighandler_t sig_t -#endif - -bool _A_do_run = true; -void _A_got_intr(int) -{ - _A_do_run = false; -} - -// decodes the header and returns a vector with the integers found -std::vector decode_header(const std::string &h, AsciiRoot::XtraValues &xtra) -{ - std::vector vout; - std::istringstream istr(h); - char *endptr, *str; - char buf[256]; - long val; - - xtra.clear(); - - while (istr) - { - istr.getline(buf, sizeof(buf), ';'); - if (!istr) - break; - - errno = 0; - val = strtol(buf, &endptr, 0); - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) - { - std::string sval(buf), sout; - sout = trim_str(sval); - if (!sout.empty()) - xtra.push_back( sout ); - } - else if ( endptr == buf || *endptr != '\0' ) - { - std::string sval(buf), sout; - sout = trim_str(sval); - if (!sout.empty()) - xtra.push_back( sout ); - } - else - { - vout.push_back(atoi(buf) ); - } - } - return vout; -} - -AsciiRoot::AsciiRoot(const char *nam, const char *pedfile, const char *gainfile) : - _nchan(max_nchan),_seedcut(10.), _neighcut(5.), _average_gain(1.), _version(2), _polarity(1) -{ - int i; - for (i=0;iclose(); - } -} - -void AsciiRoot::open(const char *name) -{ - ifile = new std::ifstream(name); - if (!(*ifile)) - { - std::cout << "Could not open data file: " << name << std::endl; - delete ifile; - ifile = 0; - return; - } - std::string header; - unsigned int ic, lheader; - char c; - ifile->read((char *)&_t0, sizeof(time_t)); - //std::cout << "64b: " << ctime(&_t0) << std::endl; - ifile->read((char *)&_type, sizeof(int)); - //std::cout << "type_ " << _type << std::endl; - if ( _type > 15 ) - { - ifile->seekg(0, std::ios::beg); - ifile->read((char *)&_t0, sizeof(int)); - ifile->read((char *)&_type, sizeof(int)); - //std::cout << "32b: " << ctime(&_t0) << std::endl; - } - - - ifile->read((char *)&lheader, sizeof(unsigned int)); - for (ic=0; ic<80; ic++) - { - ifile->read(&c, sizeof(char)); - //std::cout << "biplab header ch1 " << c << std::endl; - header.append(1, c); - } - header = trim_str(header); - //std::cout << "biplab header ch2 " << header << std::endl; - - if (header[0]!='V' && header[0]!='v') - { - _version = 0; - } - else - { - _version = int(header[1]-'0'); - header = header.substr(5); - } - - std::cout << "type: " << _type << " header: " << header << std::endl; - std::vector param = decode_header(header, _xtra); - ifile->read((char *)_ped, max_nchan*sizeof(double)); - ifile->read((char *)_noise, max_nchan*sizeof(double)); - switch (_type) - { - case 1: // calibration - case 2: // laser sync - _npoints = param[0]; - _from = param[1]; - _to = param[2]; - _step = param[3]; - break; - case 3: // laser run - case 4: // source run - case 5: // pedestal run - if (param.empty()) - _nevts = 100000; - else - _nevts = param[0]; - _npoints = _from = _to = _step = 0; - break; - } - data_start = ifile->tellg(); -} - -void AsciiRoot::rewind() -{ - if (ifile) - { - ifile->clear(); - ifile->seekg(data_start, std::ios::beg); - } -} - -void AsciiRoot::close() -{ - if (ifile) - { - ifile->close(); - delete ifile; - ifile = 0; - } -} - -void AsciiRoot::reset_data() -{ - memset(&_data, 0, sizeof(_data)); -} - -int AsciiRoot::read_event() -{ - if (ifile) - { - unsigned int header, size, user=0, code=0; - char *block_data=0; - if (_version) - { - do - { - do - { - ifile->read((char *)&header, sizeof(unsigned int)); - if (ifile->bad() || ifile->eof()) - return -1; - - code = (header>>16) & 0xFFFF; - } while ( code != 0xcafe ); - - code = header & 0x0fff; - user = header & 0x1000; - switch (code) - { - case NewFile: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - new_file(size, block_data); - break; - case StartOfRun: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - start_of_run(size, block_data); - break; - case DataBlock: - ifile->read((char *)&size, sizeof(unsigned int)); - if (user) - { - reset_data(); - block_data = new char[size]; - ifile->read(block_data, size); - new_data_block(size, block_data); - } - else - { - if ( _version == 1 ) - { - ifile->read((char *)&_data, sizeof(EventData)); - for (int ii=0; ii<2; ii++) - memset(_header[ii], 0, 16*sizeof(unsigned short)); - - } - else - { - ifile->read((char *)&_data.value, sizeof(double)); - ifile->read((char *)&_data.time, sizeof(unsigned int)); - ifile->read((char *)&_data.temp, sizeof(unsigned short)); - for (int ii=0; ii<2; ii++) - { - ifile->read((char *)_header[ii], 16*sizeof(unsigned short)); - ifile->read((char *)&_data.data[ii*128], 128*sizeof(unsigned short)); - } - } - } - - break; - case CheckPoint: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - check_point(size, block_data); - break; - case EndOfRun: - ifile->read((char *)&size, sizeof(unsigned int)); - block_data = new char[size]; - ifile->read(block_data, size); - end_of_run(size, block_data); - break; - default: - std::cout << "Unknown block data type: " << std::hex << header << " - " << code << std::dec << std::endl; - } - if (block_data) - { - delete [] block_data; - block_data = 0; - } - - } while ( code != DataBlock && !(ifile->bad() || ifile->eof()) ); - } - else - { - ifile->read((char *)&_data, sizeof(EventData)); - for (int ii=0; ii<2; ii++) - memset(_header[ii], 0, 16*sizeof(unsigned short)); - } - - if (ifile->eof()) - { - std::cout << "End of file" << std::endl; - return -1; - } - else if (ifile->bad()) - { - std::cout << "Problems with data file" << std::endl; - return -1; - } - else - { - //process_event(); - return 0; - } - } - else - return -1; -} - -void AsciiRoot::set_data(int nchan, const unsigned short int *data) -{ - int i; - _nchan = nchan; - for (i=0;i<_nchan;i++) - _data.data[i] = data[i]; -} - - -double AsciiRoot::time() const -{ - unsigned short fpart = _data.time & 0xffff; - short ipart = (_data.time & 0xffff0000)>>16; - if (ipart<0) - fpart *= -1; -// double tt = 100.*(1. -(ipart + (fpart/65535.))); - double tt = 100.0*(ipart + (fpart/65535.)); - return tt; -} - -double AsciiRoot::temp() const -{ - return 0.12*_data.temp - 39.8; -} - - -TH1 *AsciiRoot::show_pedestals() -{ - int ic; - TH1 *hst = create_h1("hPed","Pedestals",nchan(),-0.5, nchan()-0.5); - hst->SetYTitle("ADCs"); - hst->SetXTitle("Channel no."); - for (ic=0; icSetBinContent(ic+1, _ped[ic]); - - return hst; -} - -TH1 *AsciiRoot::show_noise() -{ - int ic; - TH1 *hst = create_h1("hNoise","Noise",nchan(),-0.5, nchan()-0.5); - if (gain()==1) - { - hst->SetYTitle("ADCs"); - } - else - { - hst->SetYTitle("e^{-} ENC"); - } - hst->SetXTitle("Channel no."); - for (ic=0; icSetBinContent(ic+1, noise(ic)); - - return hst; -} - - - -// Save ADC values in "ADCs" 2D-array of type unsigned short and size "evts x N". -//void AsciiRoot::get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts) -void AsciiRoot::get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts) -{ - int i = -1; - int ievt = -1; - - if (!ifile) - return; - - std::ifstream::pos_type here = ifile->tellg(); - std::cout << "==================================================" << std::endl; - std::cout << "Saving ADC values..." << std::endl; - std::cout << "==================================================" << std::endl; - // int evt_count(0); - // for (ievt=0; read_event()==0 ; ievt++){ evt_count++; } - // std::cout << "biplabd count events: " << evt_count << std::endl; - // rewind(); - - for (ievt=0; read_event()==0 && ievttellg(); - std::cout << "Computing fast pedestas..." << std::endl; - for (ievt=0; read_event()==0 && ievt 1.) - corr = 1.; - - if (corr < -1) - corr = -1.; - - _ped[i] += corr; - - if (fabs(xs) < 3.) - { - _noise[i] = _noise[i]*(1.0-wnoise) + xs*xs*wnoise; - } - } - } - } - std::cout << "\nDone" << std::endl; - rewind(); -} - - -TH2 *AsciiRoot::compute_pedestals(int mxevts, bool do_cmmd) -{ - if (!ifile) - return 0; - - if (mxevts<0) - mxevts = 100000000; - - int ievt, ichan; - TH2 *hst = create_h2("hRaw","Raw data",nchan(), -0.5,nchan()-0.5, 256, -0.5,1023.5); - TH2 *hsts = create_h2("hSig","Signal",nchan(), -0.5,nchan()-0.5,256, -127.5,127.5); - - - std::ifstream::pos_type here = ifile->tellg(); - std::cout << "Computing pedestas..." << std::endl; - for (ievt=0; read_event()==0 && ievtFill(ichan, data(ichan)-get_cmmd(ichan/128)); - - if (!(ievt%100)) - { - std::cout << "\revent " << std::setw(10) << ievt << std::flush; - } - } - std::cout << "\nDone" << std::endl; - rewind(); - - // TODO: _nchan can be updated in an event by event basis - // while here we are assuming that it is the same - // for all the events - for (ichan=0; ichanProjectionY("__hx__", ichan+1, ichan+1); - g->SetParameters(h1->GetSumOfWeights(), h1->GetMean(), h1->GetRMS()); - g->SetRange(h1->GetMean()-2.5*h1->GetRMS(), h1->GetMean()+2.5*h1->GetRMS()); - h1->Fit("g1", "q0wr"); - _ped[ichan] = h1->GetFunction("g1")->GetParameter(1); - _noise[ichan] = h1->GetFunction("g1")->GetParameter(2); - delete h1; - delete g; - } - - rewind(); - for (ievt=0; read_event()==0 && ievtFill(ichan, signal(ichan)); - - if (!(ievt%100)) - { - std::cout << "\revent " << std::setw(10) << ievt << std::flush; - } - } - std::cout << "\nDone" << std::endl; - rewind(); - - return hst; -} - - - - -void AsciiRoot::find_clusters(int ichip) -{ - int chan0=0; - int chan1=255; - if (ichip>=0 && ichip<2) - { - chan0 = ichip*128; - chan1 = (ichip+1)*128 -1; - } - - std::ostringstream ostr; - ostr << chan0 << '-' << chan1; - ChanList C(ostr.str().c_str()); - - clear(); - find_clusters(C); - _hits = C.hit_list(); -} - -void AsciiRoot::find_clusters(ChanList &C) -{ - // TODO: figure out how to determine the chip number in - // all the situations - int i, j, imax=-1, left, right; - double mxsig=-1.e20, sg, val; - bool used[C.Nch()]; - - for (i=0;i _seedcut) - { - val = fabs(signal(i)); - if (mxsig= C.Nch() ) - break; - - sg = signal(C[imax]); - used[imax]=true; - // Now look at neighbors - // first to the left - left = imax; - for (j=imax-1;j>=0;j--) - { - i = C[j]; - if ( used[j] || _signal[i]*polarity()<0.) - break; - - if ( fabs(sn(i)) > _neighcut ) - { - used[j] = true; - sg += signal(i); - left = j; - } - else - // TODO: this needs to be removed - // The idea is to merge to clusters that have only one strip in between - // In the laser runs this is a consequences of reflections... - { - int jx = j-1; - if (jx>=0 ) - { - if ( fabs(sn(C[jx])) > _neighcut ) - continue; - } - break; - } - } - - // now to the right - right = imax; - for (j=imax+1;j_neighcut ) - { - used[j] = true; - sg += signal(i); - right = j; - } - else - // TODO: this needs to be removed - // The idea is to merge to clusters that hanve only one strip in between - // In the laser runs this is a consequences of reflections... - { - int jx = i+1; - if (jx _neighcut ) - continue; - } - break; - } - } - C.add_hit(Hit(imax, left, right, sg)); - } -} - -void AsciiRoot::save_pedestals(const char *fnam) -{ - std::ofstream ofile(fnam); - if (!ofile) - { - std::cout << "Could not open " << fnam << " to save pedestals." << std::endl; - return; - } - - // TODO: _nchan can be updated in an event by event basis - // while here we are assuming that it is the same - // for all the events - int i; - for (i=0; i> _ped[i] >> std::ws >> _noise[i] >> std::ws; - _mask[i] = (_noise[i]>20. || _noise[i]<=0.); - } - ifile.close(); - TCanvas *pedcanvas = create_canvas("Pedcanvas", "Pedestal Values", 600, 400); - TH1 *pedestalhisto = create_h1("pedestalhisto", "Pedestal Values", 256, -0.5, 255.5); - for (i=0; i<256; i++) - { - pedestalhisto->Fill(i, _ped[i]); - } - pedcanvas->cd(1); - pedestalhisto->Draw(); -} - -void AsciiRoot::load_masking(const char *fnam) -{ - std::ifstream ifile(fnam); - if (!ifile) - { - std::cout << "Could not open masked.txt. " << std::endl; - return; - } - int val; - for (int i=0; i<500; i++) - { - ifile >> val >> std::ws; - if (ifile.eof()) - break; - if (val>255) - { - std::cout << "A value is greater than 255, causing an overflow crash. Please check the text file again. It has been set to 1 for continuation purposes. " << std::endl; - val = 1; - } - _mask[val] = true; - } -} - -void AsciiRoot::load_gain(const char *fnam) -{ - std::ifstream ifile(fnam); - if (!ifile) - { - std::cout << "Could not open " << fnam << " to load the gain." << std::endl; - return; - } - int i; - int ichan; - double val, xn, xm; - xn=xm=0.; - for (i=0; i> ichan >> std::ws; - if (ifile.eof()) - break; - ifile >> val; - if (ifile.eof()) - break; - - xn++; - - xm += val; - _gain[ichan] = val; - - ifile >> std::ws; - if (ifile.eof()) - break; - } - if (xn>0) - { - _average_gain = xm/xn; - } - ifile.close(); -} - -void AsciiRoot::process_event(bool do_cmmd) -{ - int i; - for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.; - } - if (do_cmmd) - { - int ichip=-1; - common_mode(); - - for (i=0; i1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); - } - } -} - -void AsciiRoot::add_channel_list(const ChanList &C) -{ - chan_list.push_back(C); -} - - -void AsciiRoot::common_mode() -{ - ChanList C("0-127"); - common_mode(C); - - _cmmd[0] = C.CommonMode(); - _cnoise[0] = C.Noise(); - - - C.Set("128-255"); - common_mode(C); - - _cmmd[1] = C.CommonMode(); - _cnoise[1] = C.Noise(); -} - -void AsciiRoot::common_mode(ChanList &C, bool correct) -{ - int ip, i, j; - - double mean, sm, xn, xx, xs, xm, tmp; - bool use_it; - mean = sm = 0.; - for (ip=0;ip<3;ip++) - { - xn = xs = xm = 0.; - for (j=0; j0.) - { - mean = xm / xn; - sm = sqrt( xs/xn - mean*mean); - } - // std::cout << "...iter " << ip << ": xm " << mean << " xs: " << sm << std::endl; - } - C.CommonMode(mean); - C.Noise(sm); - - if (correct) - { - for ( j=0; j1. && !_mask[i] ? _signal[i]/_noise[i] : 0.); - } - } -} - - - - -void AsciiRoot::spy_data(bool with_signal, int nevt) -{ - TVirtualPad *pad; - if (!ifile) - return; - - sighandler_t old_handler = ::signal(SIGINT, _A_got_intr); - _A_do_run = true; - - TCanvas *cnvs = (TCanvas *)gROOT->FindObject("cnvs"); - if (cnvs) - { - cnvs->Clear(); - } - else - cnvs = new TCanvas("cnvs","cnvs", 700, 800); - - cnvs->Divide(2,3); - - - TH1 *hsignal = create_h1("hsignal","signal (ADC)",256, -0.5, 255.0); - hsignal->SetXTitle("Channel"); - hsignal->SetYTitle("ADC"); - hsignal->SetMinimum(-300); - hsignal->SetMaximum(300); - - TH1 *helec = create_h1("helec","signal (elec)", 256, -0.5, 255.5); - helec->SetXTitle("Channel"); - helec->SetYTitle("electrons"); - helec->SetMinimum(-300/gain()); - helec->SetMaximum(300/gain()); - - TH1 *hraw = create_h1("hraw","Raw Data (around 512.)",256, 0., 256.); - hraw->SetXTitle("Channel"); - hraw->SetYTitle("ADC"); - hraw->SetMinimum(-300); - hraw->SetMaximum(+300); - - TH1 *hrawc = create_h1("hrawc","Raw Data (no commd)",256, 0., 256.); - hrawc->SetXTitle("Channel"); - hrawc->SetYTitle("ADC"); - hrawc->SetMinimum(-300); - hrawc->SetMaximum(+300); - - - TH1 *hcmmd[2]; - hcmmd[0] = create_h1("hcmmd0","Common mode (Chip 0)",50,-100.,100.); - hcmmd[0]->SetXTitle("Common mode"); - hcmmd[1] = create_h1("hcmmd1","Common mode (Chip 1)",50,-100.,100.); - hcmmd[1]->SetXTitle("Common mode"); - - int ievt,jevt; - for (ievt=jevt=0; read_event()==0 && _A_do_run && ievtSetBinContent(i+1, _signal[i]); - helec->SetBinContent(i+1, signal(i)); - hraw->SetBinContent(i+1,data(i)-512.); - hrawc->SetBinContent(i+1, data(i)-_ped[i]); - // TODO: why we draw the signal + common mode ? - // May be cause signal should be ~0... - hcmmd[ichip]->Fill(_signal[i]+get_cmmd(ichip)); - } - pad = cnvs->cd(1); - pad->SetGrid(1,1); - hsignal->Draw(); - pad = cnvs->cd(2); - pad->SetGrid(1,1); - helec->Draw(); - - pad = cnvs->cd(3); - pad->SetGrid(1,1); - hraw->Draw(); - - pad = cnvs->cd(4); - pad->SetGrid(1,1); - hrawc->Draw(); - - pad = cnvs->cd(5); - pad->SetGrid(1,1); - hcmmd[0]->Draw(); - - pad = cnvs->cd(6); - pad->SetGrid(1,1); - hcmmd[1]->Draw(); - - std::cout << std::setiosflags(std::ios::fixed); - std::cout << "*** Event " << jevt << " *****" << std::endl; - std::cout << "Common Mode:" << std::endl - << " Chip 0 " << std::setw(6) << std::setprecision(1) << get_cmmd(0) << " noise: " << get_cnoise(0) - << std::endl - << " Chip 1 " << std::setw(6) << std::setprecision(1) << get_cmmd(1) << " noise: " << get_cnoise(1) - << std::endl; - - std::cout << "Time: " << time() << " ns" << std::endl; - std::cout << "Signal chan(0) << " << signal(0) << " chan(1) " << signal(1) << std::endl; - std::cout << "Clusters: " << std::endl; - - HitList::iterator ip; - for (ip=begin(); ip!=end(); ++ip) - { - std::cout << " chan: " << ip->center() - << " sig: " - << std::setw(6) << std::setprecision(1) << ip->signal() - << " left: " << ip->left() << " right: " << ip->right() - << std::endl; - std::cout << '\t' << "channels: " << std::endl; - int j; - for (j=ip->left();j<=ip->right();j++) - std::cout << "\t " << j << " sn: " << _sn[j] << " signal: " << _signal[j] << " noise: " << _noise[j] << '\n'; - std::cout << std::endl; - } - - cnvs->Update(); - ievt++; - } - std::cout << std::endl; - _A_do_run= true; - ::signal(SIGINT, old_handler); -} - -bool is_text(const char *fnam) -{ - int nc; - char buffer[1024]; - std::ifstream ifile(fnam); - if (!fnam) - return false; - - ifile.read(buffer, sizeof(buffer)); - nc = ifile.gcount(); - ifile.close(); - if (!nc) // empty files are text - { - return true; - } - - std::string ss(buffer, nc); - ifile.close(); - - if ( ss.find('\0') != ss.npos ) - return false; - - double nontext = 0.; - double ntotal = 0.; - std::string::iterator ip; - for (ip=ss.begin(); ip!=ss.end(); ++ip) - { - ntotal++; - char c = *ip; - if ( (c<' ' || c >'~') && !strchr("\n\t\r\b", c) ) - nontext++; - } - if ( nontext/ntotal > 0.3 ) - return false; - - return true; -} - diff --git a/TbNtupleMaker/Alibava/AsciiRoot.h b/TbNtupleMaker/Alibava/AsciiRoot.h deleted file mode 100755 index e3950b5..0000000 --- a/TbNtupleMaker/Alibava/AsciiRoot.h +++ /dev/null @@ -1,246 +0,0 @@ -#ifndef __Alibava_AsciiRoot_h__ -#define __Alibava_AsciiRoot_h__ - -#include -#include "Data.h" -#include "Hit.h" -#include "ChanList.h" -#include -#include -#include - - -/** - * This is the class that reads the data files - */ - -class AsciiRoot -{ - public: - typedef std::vector XtraValues; - enum BlockType { NewFile=0, StartOfRun, DataBlock, CheckPoint, EndOfRun }; - private: - static const int max_nchan=256; - std::ifstream *ifile; - unsigned int data_start; - int _type; - time_t _t0; - int _npoints; - int _from; - int _to; - int _step; - int _nevts; - int _nchan; // current number of channels - XtraValues _xtra; // extra values from header - double _seedcut; - double _neighcut; - unsigned short _header[2][16]; - double _ped[max_nchan]; - double _noise[max_nchan]; - double _signal[max_nchan]; - double _sn[max_nchan]; - double _cmmd[2]; - double _cnoise[2]; - double _gain[max_nchan]; - double _average_gain; - bool _mask[max_nchan]; - int _version; - int _polarity; - HitList _hits; - - std::vector chan_list; - EventDataBlock _data; - - protected: - void reset_data(); - - public: - void set_data(int i, unsigned short x) { _data.data[i] = x; } - - public: - AsciiRoot(const char *nam=0, const char *pedfile=0, const char *gainfile=0); - virtual ~AsciiRoot(); - - bool valid() const - { - return (ifile!=0); - } - - void open(const char *name); - void close(); - void rewind(); - int read_event(); - virtual void check_point(int, const char *) {}; - virtual void new_file(int, const char *) {} - virtual void start_of_run(int, const char *) {} - virtual void end_of_run(int, const char *) {} - virtual void new_data_block(int, const char *) {}; - - // The data format version - int version() const { return _version; } - - - int polarity() const { return _polarity; } - void polarity(int x) { _polarity = ( x<0 ? -1 : 1); } - /* - * Sets the number of channels and the data in the case - * of non "standard" values. If data==0, then only the number - * of channels is changed - */ - void set_data(int nchan, const unsigned short *data=0); - int nchan() const { return _nchan; } - int type() const - { - return _type; - } - char *date() const - { - return ctime(&_t0); - } - double ped(int i) const - { - return _ped[i]/_gain[i]; - } - double noise(int i) const - { - return _noise[i]/_gain[i]; - } - double signal(int i) const - { - return _signal[i]/_gain[i]; - } - - double sn(int i) const - { - return _sn[i]; - } - - double get_cmmd(int i) const - { - return _cmmd[i]; - } - - double get_cnoise(int i) const - { - return _cnoise[i]; - } - - unsigned short data(int i) const - { - return _data.data[i]; - } - double value() const - { - return _data.value; - } - double time() const; - double temp() const; - int npts() const - { - return _npoints; - } - int from() const - { - return _from; - } - int to() const - { - return _to; - } - int step() const - { - return _step; - } - int nevts() const - { - return _step; - } - - void add_hit(const Hit &h) - { - _hits.push_back(h); - } - HitList::iterator begin() - { - return _hits.begin(); - } - HitList::iterator end() - { - return _hits.end(); - } - int nhits() const - { - return _hits.size(); - } - bool empty() const - { - return _hits.empty(); - } - const Hit &hit(int i) const - { - return _hits[i]; - } - void set_hit_list(const HitList &L) { _hits = L; } - void clear() - { - _hits.clear(); - } - - double get_gain(int i) const - { - return _gain[i]; - } - double gain() const - { - return _average_gain; - } - - double seed_cut() const - { - return _seedcut; - } - double neigh_cut() const - { - return _neighcut; - } - void set_cuts(double s, double n) - { - _seedcut = s; - _neighcut = n; - } - unsigned short get_header(int ichip, int ibit) { return _header[ichip][ibit]; } - - TH1 *show_pedestals(); - TH1 *show_noise(); - TH2 *compute_pedestals(int mxevts=-1, bool do_cmmd=true); - //void get_ADC_values(unsigned short *ADCs, const Int_t N, const Int_t evts); - void get_ADC_time_temp_values(unsigned short *ADCs, double *_time, double *_temp, const Int_t N, const Int_t evts); - int get_nevents(); - void compute_pedestals_fast(int mxevts = -1, double ped_weight=0.01, double noise_weight=0.001); - - - void process_event(bool do_cmmd=true); - void find_clusters(int ichip=-1); - void find_clusters(ChanList &C); - void save_pedestals(const char *fnam); - void load_pedestals(const char *fnam); - void load_gain(const char *fnam); - void load_masking(const char *fnam); - void spy_data(bool with_signal=false, int nevt=1); - void common_mode(); - void common_mode(ChanList &C, bool correct=false); - - int n_channel_list() const { return chan_list.size(); } - void add_channel_list(const ChanList &C); - void clear_channel_lists() { chan_list.clear(); } - ChanList get_channel_list(int i) const { return chan_list[i]; } - - int nxtra() const { return _xtra.size(); } - const std::string xtra(int i) const { return _xtra[i]; } - void add_xtra(const std::string &x) { _xtra.push_back(x); } - void add_xtra(const char *x) { _xtra.push_back(x); } -}; -// Return true if file is an ASCII text file -bool is_text(const char *); - -#endif diff --git a/TbNtupleMaker/Alibava/ChanList.cc b/TbNtupleMaker/Alibava/ChanList.cc deleted file mode 100755 index 4ccb3c9..0000000 --- a/TbNtupleMaker/Alibava/ChanList.cc +++ /dev/null @@ -1,168 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "ChanList.h" - -static char svStr[8192]; - -////////////////////////////////////////////////////////////////////////// -// ChanList class -// -// Created: Mon Sep 7 18:50:38 1998 Author: Carlos Lacasta -// Purpose: -// -////////////////////////////////////////////////////////////////////////// -int cmp(const void *x1, const void *x2) -{ - double d1 = *(double *) x1 - *(double *) x2; - return (d1 == 0. ? 0 : (d1 < 0. ? -1 : 1)); -} -int icmp(const void *x1, const void *x2) -{ - int d1 = *(int *) x1 - *(int *) x2; - return (d1 == 0 ? 0 : (d1 < 0 ? -1 : 1)); -} - -ChanList::ChanList(int i1, int i2) : - nch(0), cm(0.), noise(0.) -{ - int i; - for (i = 0; i < nch; i++) - ch.push_back(i1 + i); - - nch = ch.size(); -} -ChanList::ChanList(const char *str) : - cm(0.), noise(0.) -{ - if (str == 0) - return; - Set(str); -} - -ChanList::ChanList(const ChanList &cl) -{ - copy(cl); -} - -ChanList &ChanList::operator=(const ChanList &cl) -{ - if (&cl == this) - return *this; - copy(cl); - return *this; -} - -void ChanList::copy(const ChanList &cl) -{ - if ( &cl != this ) - { - hits = cl.hits; - ch = cl.ch; - nch = ch.size(); - cm = cl.cm; - noise = cl.noise; - } -} -int ChanList::Set(const char *str) -{ - char *p, *q; - int i, is; - int ival1, ival2, wRange; - - ch.clear(); - strcpy(svStr, str); - p = q = svStr; - wRange = 0; - while (*p) - { - switch (*p) - { - case ',': - *p = 0; - if (wRange) - { - is = sscanf(q, "%d", &ival2); - for (i = ival1 + 1; i <= ival2; i++) - ch.push_back(i); - } - else - { - is = sscanf(q, "%d", &ival1); - ch.push_back(ival1); - } - wRange = 0; - q = p + 1; - p += is; - break; - case '-': - *p = 0; - wRange = 1; - is = sscanf(q, "%d", &ival1); - ch.push_back(ival1); - q = p + 1; - p += is; - break; - } - p++; - } - if (wRange) - { - is = sscanf(q, "%d", &ival2); - for (i = ival1 + 1; i <= ival2; i++) - ch.push_back(i); - } - else - { - is = sscanf(q, "%d", &ival2); - ch.push_back(ival2); - } - // sort - std::sort(ch.begin(), ch.end()); - nch = ch.size(); - return nch; -} - -std::ostream &operator<<(std::ostream &os, ChanList &cl) -{ - for (int i = 0; i < cl.Nch(); i++) - { - if (i && !(i % 15)) - os << std::endl; - os << std::setw(5) << cl[i]; - } - return os; -} - -int ChanList::ParseChanList(const char *str, ChanList **cl) -{ - int ireg, nreg = 0; - std::vector clst; - char *q, *p, *ptr; - - if (str == 0) - return 0; - if (cl == 0) - return 0; - p = ptr = strdup(str); - do - { - clst.push_back(p); - q = strchr(p, ';'); - if (q == 0) - break; - *q++ = 0; - p = q; - } while (*p); - - nreg = clst.size(); - *cl = new ChanList[nreg]; - for (ireg = 0; ireg < nreg; ireg++) - (*cl)[ireg].Set(clst[ireg]); - - free(ptr); - return nreg; -} diff --git a/TbNtupleMaker/Alibava/ChanList.h b/TbNtupleMaker/Alibava/ChanList.h deleted file mode 100755 index 920763d..0000000 --- a/TbNtupleMaker/Alibava/ChanList.h +++ /dev/null @@ -1,75 +0,0 @@ -/* -*- mode: c++ -*- */ -#ifndef __ChanList_h__ -#define __ChanList_h__ - -#include -#include -#include "Hit.h" -////////////////////////////////////////////////////////////////////////// -// ChanList -// -// Created: Mon Sep 7 18:46:56 1998 Author: Carlos Lacasta -// Purpose: Class to handle channel lists -// -////////////////////////////////////////////////////////////////////////// - -class ChanList -{ - protected: - int nch; // number of channels in the list - std::vector ch; // list of channels - HitList hits; - double cm; // Common mode - double noise; // noise - void copy(const ChanList &); - public: - ChanList(const char *ch = 0); - ChanList(int, int); - ChanList(const ChanList &); - ChanList &operator=(const ChanList &); - virtual ~ChanList() - { - hits.clear(); - ch.clear(); - } - int Set(const char *); - int Nch() const - { - return nch; - } - int Chan(int x) const - { - return ch[x]; - } - - int operator[](int x) const { return ch[x]; } - void add_hit(const Hit &h) { hits.push_back(h); } - bool empty() const { return hits.empty(); } - int nhits() const { return hits.size(); } - void clear_hits() { hits.clear(); } - const Hit &get_hit(int i) const { return hits[i]; } - const HitList hit_list() const { return hits; } - - double CommonMode() const - { - return cm; - } - double Noise() const - { - return noise; - } - ChanList *CommonMode(double x) - { - cm = x; - return this; - } - ChanList *Noise(double x) - { - noise = x; - return this; - } - static int ParseChanList(const char *, ChanList **); -}; -std::ostream &operator<<(std::ostream &, ChanList&); -#endif - diff --git a/TbNtupleMaker/Alibava/Data.h b/TbNtupleMaker/Alibava/Data.h deleted file mode 100755 index 25f27b2..0000000 --- a/TbNtupleMaker/Alibava/Data.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef DATA_H_ -#define DATA_H_ - -/** - * Data is received from the USB port with this format - * - */ -struct EventBlock -{ - unsigned int time; - unsigned short temp; - unsigned short data[256]; -}; - - - -/** - * We add value as an estra parameter when moving the - * data within the application. It tells the value of - * the variables which are scanned - */ -struct EventData : public EventBlock -{ - double value; -}; - -struct EventDataBlock : public EventData -{ - unsigned short header[32]; -}; - -#endif /*DATA_H_*/ diff --git a/TbNtupleMaker/Alibava/Hit.cc b/TbNtupleMaker/Alibava/Hit.cc deleted file mode 100755 index 6d8962b..0000000 --- a/TbNtupleMaker/Alibava/Hit.cc +++ /dev/null @@ -1,29 +0,0 @@ -#include "Hit.h" - -Hit::Hit(int c, int l, int r, double s) : - _center(c), _left(l), _right(r), _sig(s) -{ -} - -Hit::Hit(const Hit &h) -{ - cpy(h); -} -Hit::~Hit() -{ -} - -void Hit::cpy(const Hit &h) -{ - _center = h._center; - _left = h._left; - _right = h._right; - _sig = h._sig; -} -Hit &Hit::operator=(const Hit &h) -{ - if (&h!=this) - cpy(h); - - return *this; -} diff --git a/TbNtupleMaker/Alibava/Hit.h b/TbNtupleMaker/Alibava/Hit.h deleted file mode 100755 index dca5006..0000000 --- a/TbNtupleMaker/Alibava/Hit.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef __HIT_H__ -#define __HIT_H__ - -/** - * A class representing a hit - */ - -#include - -class Hit -{ - private: - int _center; - int _left; - int _right; - double _sig; - - void cpy(const Hit &h); - public: - Hit(int c=0, int l=0, int r=0, double s=0); - Hit(const Hit &h); - ~Hit(); - - Hit &operator=(const Hit &h); - - int center() const { return _center; } - int left() const { return _left; } - int right() const { return _right; } - double signal() const { return _sig; } - int width() const { return _right - _left + 1; } -}; -typedef std::vector< Hit> HitList; - - -#endif /*__HIT_H__*/ diff --git a/TbNtupleMaker/Alibava/Tracer.cc b/TbNtupleMaker/Alibava/Tracer.cc deleted file mode 100755 index dc981c7..0000000 --- a/TbNtupleMaker/Alibava/Tracer.cc +++ /dev/null @@ -1,48 +0,0 @@ -#include - -#include "Tracer.h" - -Tracer::Tracer(const char *name, const char *title, int npts, int avrg) - : size(npts), average(avrg), cntr(0.), val(0.) -{ - hst = new TH1D(name, title, size, 0, size); - -} - -Tracer::~Tracer() -{ - delete hst; -} - -void Tracer::Draw(const char *opt) -{ - hst->Draw(opt); -} - -void Tracer::fill(double x) -{ - if (average>1.) - { - val = (x + cntr*val)/(++cntr); - if ( (int(cntr)%average)==0 ) - { - add_point(val); - } - } - else - add_point(x); -} - -void Tracer::add_point(double x) -{ - queue.push_back(x); - if (queue.size()>size) - queue.pop_front(); - - std::deque::iterator ip; - int ibin = 1; - for (ip=queue.begin();ip!=queue.end();++ip, ibin++) - { - hst->SetBinContent(ibin, *ip); - } -} diff --git a/TbNtupleMaker/Alibava/Tracer.h b/TbNtupleMaker/Alibava/Tracer.h deleted file mode 100755 index f7826a7..0000000 --- a/TbNtupleMaker/Alibava/Tracer.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef TRACER_H_ -#define TRACER_H_ -#include - -class TH1; - -class Tracer -{ - private: - unsigned int size; - int average; - double cntr; - double val; - std::deque queue; - TH1 *hst; - - public: - /** - * Defines a tracer. At input npts defines the size of - * the buffer (a FIFO actually) with the values that the - * Tracer will remember. Also, if average is given, the - * Tracer will add as new points the average over 'average' - * inputs - */ - Tracer(const char*nam, const char *tit, int npts, int average=0); - virtual ~Tracer(); - - void Draw(const char *opt=""); - - TH1 *get_hst() - { - return hst; - } - - void fill(double val); - void add_point(double x); -}; - -#endif /*TRACER_H_*/ diff --git a/TbNtupleMaker/Alibava/utils.cc b/TbNtupleMaker/Alibava/utils.cc deleted file mode 100755 index 398432c..0000000 --- a/TbNtupleMaker/Alibava/utils.cc +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include "utils.h" - -TCanvas *create_canvas(const char *name, const char *title, int wx, int wy) -{ - TCanvas *cnvs = (TCanvas *)gROOT->FindObject("name"); - if (cnvs) - delete cnvs; - - if (wx<0 ||wy<0) - cnvs = new TCanvas(name, title); - else - cnvs = new TCanvas(name, title, wx, wy); - - return cnvs; -} - -TH1 *create_h1(const char *name, const char *tit, int n, double x1, double x2) -{ - TH1 *hst = (TH1 *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TH1D(name, tit, n, x1, x2); - return hst; -} - -TH2 *create_h2(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) -{ - TH2 *hst = (TH2 *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TH2D(name, tit, nx, x1, x2, ny, y1, y2); - return hst; -} - -TProfile *create_profile(const char *name, const char *tit, int n, double x1, double x2, double y1, double y2) -{ - TProfile *hst = (TProfile *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TProfile(name, tit, n, x1, x2, y1, y2); - return hst; -} - -TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2) -{ - TProfile2D *hst = (TProfile2D *)gROOT->FindObject(name); - if (hst) - delete hst; - - hst = new TProfile2D(name, tit, nx, x1, x2, ny, y1, y2); - return hst; -} diff --git a/TbNtupleMaker/Alibava/utils.h b/TbNtupleMaker/Alibava/utils.h deleted file mode 100755 index b82693c..0000000 --- a/TbNtupleMaker/Alibava/utils.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef UTILS_H_ -#define UTILS_H_ - -#include -#include -#include -#include -#include -#include -#include - -/** - * Utils - */ -inline bool isws(char c, char const * const wstr=" \t\n") -{ - return (strchr(wstr, c) != NULL); -} - -inline std::string trim_right(const std::string &s) -{ - std::string b=" \t\n"; - std::string str = s; - return str.erase(str.find_last_not_of(b) +1); -} - -inline std::string trim_left(const std::string &s) -{ - std::string b=" \t\n"; - std::string str = s; - return str.erase( 0, str.find_first_not_of(b) ); -} - -inline std::string trim_str(const std::string &s) -{ - std::string str = s; - return trim_left(trim_right(str) ); -} - -TCanvas *create_canvas(const char *name, const char *title, int wx=-1, int wy=-1); -TH1 *create_h1(const char *, const char *, int, double, double); -TH2 *create_h2(const char *, const char *, int, double, double, int, double, double); -TProfile *create_profile(const char *, const char *, int, double, double, double, double); -TProfile2D *create_profile2d(const char *name, const char *tit, int nx, double x1, double x2, int ny, double y1, double y2); - - - -#endif /*UTILS_H_*/ diff --git a/TbNtupleMaker/Makefile b/TbNtupleMaker/Makefile deleted file mode 100755 index 3232cf8..0000000 --- a/TbNtupleMaker/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -# -CC=$(CXX) -glib_cflags=$(shell pkg-config --cflags glib-2.0 gio-2.0) -glib_libs=$(shell pkg-config --libs glib-2.0 gio-2.0) - -ROOTC=$(shell root-config --cflags) -ROOTL=$(shell root-config --libs) -OPT=-g -fno-inline #-std=c++11 -CppFLAGS=$(OPT) -I. $(glib_cflags) -CXXFLAGS=-fPIC $(CppFLAGS) - -LDLIBS=$(alibava_libs) - - -extra_alibava_libs = Alibava/AsciiRoot.cc Alibava/ChanList.cc Alibava/Hit.cc Alibava/Tracer.cc Alibava/utils.cc - -UNAME_S := $(shell uname -s) -ifeq ($(UNAME_S),Linux) - SO=so - SO_FLAGS=-shared - CXXFLAGS += -D LINUX -endif -ifeq ($(UNAME_S),Darwin) - SO=dylib - SO_FLAGS=-dynamiclib -undefined dynamic_lookup -install_name @rpath/$@ - CXXFLAGS += -D OSX -endif - -all: TbNtupleMaker - -TbNtupleMaker: TbNtupleMaker.C - c++ -I$(OPT) $(CXXFLAGS) $(ROOTC) $(extra_alibava_libs) -o $@ $^ $(LDLIBS) $(ROOTL) $(gliblibs) - - - -clean: - rm -f *.o TbNtupleMaker - rm -rf *.dSYM diff --git a/TbNtupleMaker/TbNtupleMaker b/TbNtupleMaker/TbNtupleMaker deleted file mode 100755 index bf99686..0000000 --- a/TbNtupleMaker/TbNtupleMaker +++ /dev/null Binary files differ diff --git a/TbNtupleMaker/TbNtupleMaker.C b/TbNtupleMaker/TbNtupleMaker.C deleted file mode 100755 index fb2e3b7..0000000 --- a/TbNtupleMaker/TbNtupleMaker.C +++ /dev/null @@ -1,312 +0,0 @@ -//************************************************ -// Author: Federica Lionetto, Adam Davis, Biplab Dey, Paolo Gandini -// Created on: 07/30/2014 -//************************************************ - -/* -TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information: -- int NEvents, the number of events read from the header of the Alibava file; -- int RunType, the run type; -- std::vector PedByGain, the pedestals calculated by the Alibava; -- std::vector NoiseByGain, the noise calculated by the Alibava; -- std::vector Gain, the gain calculated by the Alibava; -- TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N-1); -- TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N-1); -- TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N-1); -- string Date = a->date(), the date read from the header of the Alibava file; -- std::vector ADC, the raw ADC counts, one per event; -- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event; -- double Temp, the temperature, one per event; -- int n, a counter, one per event (starting from 1). - -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. - -Compile with: - -make - -Run with: - -./TbNtupleMaker [Alibava file] [external path] - -where: -- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process; -- [external path] is the optional external path where the RootFiles folder will be created. - -For example: - -./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali - -or - -./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /... -*/ - -//************************************************ -// Types: -// 1 = calibration run -// 2 = laser synchronization run -// 3 = laser run -// 4 = radioactive source run -// 5 = pedestal run -//************************************************ - -#include -#include - -#include - -#include - -#include "TFile.h" -#include "TROOT.h" -#include "TSystem.h" -#include "TTree.h" - -#include "Alibava/AsciiRoot.h" - -#ifdef __MAKECINT__ -#pragma link C++ class vector+; -#endif - -using namespace std; - -// Number of Beetle channels. -const int N = 256; - -void TbNtupleMaker(char *filename, char *externalPath = 0); - -int main(int argc, char *argv[]) -{ - if(argc < 2) - { - cout << "**************************************************" << endl; - - cout << "Error! Input file missing..." << endl; - cout << "Please use the following format:" << endl; - cout << "./TbNtupleMaker [1] [2]" << endl; - cout << "with:" << endl; - cout << "[1] = Alibava file, complete path;" << endl; - cout << "[2] = External path, optional." << endl; - cout << "Type ./TbNtupleMaker --info for more information." << endl; - - cout << "**************************************************" << endl; - - return 0; - } - else if (string(argv[1]) == "--info") - { - cout << "**************************************************" << endl; - - cout << "TbNtupleMaker reads an Alibava file and creates a ROOT file (same filename) with the following information:" << endl; - cout << "- int NEvents, the number of events read from the header of the Alibava file;" << endl; - cout << "- int RunType, the run type;" << endl; - cout << "- std::vector PedByGain, the pedestals calculated by the Alibava;" << endl; - cout << "- std::vector NoiseByGain, the noise calculated by the Alibava;" << endl; - cout << "- std::vector Gain, the gain calculated by the Alibava;" << endl; - cout << "- TH1F histo_PedByGain;" << endl; - cout << "- TH1F histo_NoiseByGain;" << endl; - cout << "- TH1F histo_Gain;" << endl; - cout << "- string Date = a->date(), the date read from the header of the Alibava file;" << endl; - cout << "- std::vector ADC, the raw ADC counts, one per event;" << endl; - cout << "- double TDCTime, the TDC time (meaningful only in radioactive source runs), one per event;" << endl; - cout << "- double Temp, the temperature, one per event;" << endl; - cout << "- int n, a counter, one per event (starting from 1)." << endl; - - cout << "**************************************************" << endl; - - 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; - - cout << "**************************************************" << endl; - - cout << "Compile with:" << endl; - cout << "make" << endl; - cout << "Run with:" << endl; - cout << "./TbNtupleMaker [Alibava file] [external path]" << endl; - cout << "where:" << endl; - cout << "- [Alibava file] is the complete path, including the folder and the filename, of the Alibava file one wants to process;" << endl; - cout << "- [external path] is the optional external path where the RootFiles folder will be created." << endl; - cout << "For example:" << endl; - cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali" << endl; - cout << "or" << endl; - cout << "./TbNtupleMaker /.../run_xxxxxx_xxxx_xxxx.ali /..." << endl; - - cout << "**************************************************" << endl; - - return 0; - } - else - { - cout << "File to process: " << argv[1] << endl; - if (argc == 2) - TbNtupleMaker(argv[1]); - else if (argc == 3) - TbNtupleMaker(argv[1],argv[2]); - else - { - cout << "Error! Too many arguments given..." << endl; - - return 0; - } - - return 0; - } -} - - - -void TbNtupleMaker(char *filename, char *externalPath){ - cout << "**************************************************" << endl; - cout << "Decoding raw data..." << endl; - cout << "**************************************************" << endl; - - // Do not comment this line. - gROOT->ProcessLine("#include "); - - // Do some fanciness to get the directory right. - string filename_as_string = string(filename); - string prefix = filename_as_string.substr(0,filename_as_string.find_last_of('/')); - if (externalPath!=0) - prefix = string(externalPath); - string ali_filename = filename_as_string.substr(filename_as_string.find_last_of('/')+1); - TString outfilename = ((TString)prefix)+"/RootFiles/"+((TString)ali_filename); - outfilename.ReplaceAll(".ali",".root"); - - // Create a folder named RootFiles. Do not worry, nothing bad is going to happen if the folder already exists. - cout << "Create a folder named RootFiles" << endl; - - char *path_to_make = (char*)("mkdir "+prefix+"/RootFiles" ).c_str(); - cout << "Setting output to: " << path_to_make << endl; - - system(path_to_make); - - // Open ROOT file. - TFile *output = TFile::Open(outfilename,"RECREATE"); - - // Open Alibava file. - AsciiRoot *a = new AsciiRoot(filename); - - // Create tree structure in ROOT file, to be filled once per file. - int NEvents; - int RunType; - std::vector PedByGain; - std::vector NoiseByGain; - std::vector Gain; - TH1F histo_PedByGain("histo_PedByGain","histo_PedByGain",N,0,N); - TH1F histo_NoiseByGain("histo_NoiseByGain","histo_NoiseByGain",N,0,N); - TH1F histo_Gain("histo_Gain","histo_Gain",N,0,N); - TH1F histo_Signal20("histo_Signal20","Signal calculated by Alibava > 20, distribution over the strips",N,0,N); - TH1F histo_Signal40("histo_Signal40","Signal calculated by Alibava > 40, distribution over the strips",N,0,N); - TH1F histo_Signal60("histo_Signal60","Signal calculated by Alibava > 60, distribution over the strips",N,0,N); - TH1F histo_TDC("histo_TDC","TDC distribution, ADC > 40",50,0,50); - string Date = a->date(); - - /////// vito - TH1D** ADC_ch = new TH1D*[256]; - TH2D* ADCvsEv = new TH2D("ADCvsEv_%d", - "ADCvsEv_%d", - 1e5, -0.5, 99999.5, - 200, 399.5, 600.5 ); - - for(int i=0; i<256; i++){ - - ADC_ch[i] = new TH1D(Form("ADC_ch_%d", i), - Form("ADC_ch_%d", i), - 1024, -0.5, 1023.5 ); - - } - - - // Allocate only necessary space. - PedByGain.reserve(N); - NoiseByGain.reserve(N); - Gain.reserve(N); - - // Create tree structure in ROOT file, to be filled once per event. - std::vector ADC; - double TDCTime; - double Temp; - int n; - - // Fill the header, once per file. - TTree *Header = new TTree("Header","Header"); - - Header->Branch("NEvents",&NEvents); - Header->Branch("RunType",&RunType); - Header->Branch("PedByGain",&PedByGain); - Header->Branch("NoiseByGain",&NoiseByGain); - Header->Branch("Gain",&Gain); - Header->Branch("Date",&Date); - - NEvents = a->get_nevents(); - cout << "Number of events in the Alibava file: " << NEvents << endl; - RunType = a->type(); - for(int iChannel=0; iChannelget_gain(iChannel); - PedByGain[iChannel]=a->ped(iChannel); - NoiseByGain[iChannel] = a->noise(iChannel); - histo_Gain.SetBinContent(iChannel+1,a->get_gain(iChannel)); - histo_PedByGain.SetBinContent(iChannel+1,a->ped(iChannel)); - histo_NoiseByGain.SetBinContent(iChannel+1,a->noise(iChannel)); - } - - Header->Fill(); - cout << "Information in the header:" << endl; - Header->Print(); - cout << "**************************************************" << endl; - cout << "Date from Alibava file: " << Date <Branch("n",&n); - EventInfo->Branch("ADC",&ADC); - EventInfo->Branch("TDCTime",&TDCTime); - EventInfo->Branch("Temp",&Temp); - - // Loop over events. - for (int iEvent=0; iEventread_event(); - - // Loop over Beetle channels. - for (int jChannel=0; jChanneldata(jChannel)-PedByGain[jChannel]>20) histo_Signal20.Fill(jChannel); - if(a->data(jChannel)-PedByGain[jChannel]>40) histo_Signal40.Fill(jChannel); - if(a->data(jChannel)-PedByGain[jChannel]>60) histo_Signal60.Fill(jChannel); - ADC.push_back(a->data(jChannel)); - ADC_ch[jChannel]->Fill( a->data(jChannel) ); - if(jChannel==200) ADCvsEv->Fill( iEvent, a->data(jChannel) ); - } - - - - n = iEvent+1; - TDCTime =a->time(); - for(int t=0; t40) {histo_TDC.Fill(TDCTime,1); break;} - Temp = a->temp(); - EventInfo->Fill(); - } - - - - - cout << "Information in the data: " << endl; - EventInfo->Print(); - - // Close Alibava file. - a->close(); - - // Write and close ROOT file. - for(int i=0; i<256; i++) - ADC_ch[i]->Write(); - ADCvsEv->Write(); - - output->Write(); - output->Close(); - - return; -} diff --git a/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Info.plist b/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Info.plist deleted file mode 100644 index e1c4d22..0000000 --- a/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Info.plist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - CFBundleDevelopmentRegion - English - CFBundleIdentifier - com.apple.xcode.dsym.TbNtupleMaker - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - dSYM - CFBundleSignature - ???? - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - - diff --git a/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Resources/DWARF/TbNtupleMaker b/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Resources/DWARF/TbNtupleMaker deleted file mode 100644 index 9a371dc..0000000 --- a/TbNtupleMaker/TbNtupleMaker.dSYM/Contents/Resources/DWARF/TbNtupleMaker +++ /dev/null Binary files differ