diff --git a/AculCalib/AculCalParsScint.cpp b/AculCalib/AculCalParsScint.cpp index c537b505f49f0c388f021bc78597a1fa6b5447f3..fada257520dfc69b8146db7ef467b8d41567e0e9 100644 --- a/AculCalib/AculCalParsScint.cpp +++ b/AculCalib/AculCalParsScint.cpp @@ -123,12 +123,12 @@ void AculCalParsScint::SetPars() { sscanf(line.Data(), "%*s %s %d", fname, &noCuts); fFilePars[fEnergyPoints].SetNoCuts(noCuts); fFilePars[fEnergyPoints].SetCutFileName(fname); - cout << line << endl; - cout << noCuts << endl; +// cout << line << endl; +// cout << noCuts << endl; // fCutsFileName = fname; for (Int_t j = 0; j < noCuts; j++) { line.ReadLine(infile); - cout << line << endl; +// cout << line << endl; if ( line.BeginsWith("#") || line.BeginsWith("//") ) { j--; continue; @@ -139,14 +139,14 @@ void AculCalParsScint::SetPars() { } fFilePars[fEnergyPoints].LoadCuts(); line.ReadLine(infile); - cout << line << endl; +// cout << line << endl; // continue; } // cout << fNoCrystals << endl; for (Int_t j = 0; j < fNoCrystals; j++) { // line.ReadLine(infile); - cout << j << endl; +// cout << j << endl; if ( line.BeginsWith("#") || line.BeginsWith("//") ) { j--; line.ReadLine(infile); diff --git a/Utilities/CalPars.cpp b/Utilities/CalPars.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d30da42fe891633593146cca367325e8882422c --- /dev/null +++ b/Utilities/CalPars.cpp @@ -0,0 +1,93 @@ +#include "CalPars.h" + +ClassImp(CalPars); + + +CalPars::CalPars(const char* clbfile) { + + ReadParFile(clbfile); + +} + +CalPars::~CalPars() { + + printf("CalPars::Destructor called.\n"); + +} + +Double_t CalPars::GetA(Int_t i) { + + if (i >= fA.GetSize()) { + return 0.; + } + return fA[i]; +} + +Double_t CalPars::GetB(Int_t i) { + + if (i >= fB.GetSize()) { + return 0.; + } + return fB[i]; +} + +Double_t CalPars::GetC(Int_t i) { + + if (i >= fC.GetSize()) { + return 0.; + } + return fC[i]; +} + +void CalPars::ReadParFile(const char* clbfile) { + + clbFile = clbfile; + + std::ifstream infile(clbfile); + if ( !infile.is_open() ) { + printf("CalPars::ReadParFile: File %s was not open.\n", clbfile); + printf("CalPars::ReadParFile: Calibration parameters are empty!!!\n"); + return; + } + + TString line; + Int_t nopars; + Double_t a, b; + Int_t c; //threshold + + printf("CalPars::ReadParFile: File %s was open.\n", clbfile); + + line.ReadLine(infile); + sscanf(line.Data(), "%*d %d", &nopars); +// Info("CalPars::ReadParFile", "%d calibration parameters will be loaded", nopars); + printf("CalPars::ReadParFile: %d calibration parameters will be loaded\n", nopars); + + fA.Set(nopars); + fB.Set(nopars); + fC.Set(nopars); + + for (Int_t i = 0; i +#include + +#include "TArrayD.h" +#include "TArrayI.h" +#include "TString.h" +#include "TObject.h" + +using std::cout; +using std::endl; + +class CalPars /*: TObject*/ { + +private: + TArrayD fA; + TArrayD fB; + TArrayI fC; + + TString clbFile; + +public: + + CalPars() : fA(0), fB(0), fC(0) {}; + CalPars(const char* clbfile); + virtual ~CalPars(); + + Double_t GetA(Int_t i); + Double_t GetB(Int_t i); + Double_t GetC(Int_t i); + + void ReadParFile(const char* clbfile); + void PrintClbPars(); + + ClassDef(CalPars, 1); +}; diff --git a/Utilities/ConfigDictionary.cpp b/Utilities/ConfigDictionary.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e3790be97eed63747cb27fd329887a292fd37fa --- /dev/null +++ b/Utilities/ConfigDictionary.cpp @@ -0,0 +1,229 @@ +#include "ConfigDictionary.h" + +ClassImp(ConfigDictionary); + +////////////////////////////////////////////////////////////////////////////// +// BEGIN_HTML +//

Config(uration)Dictionary class

+//
+// Author: Bartlomiej Hnatio 2012-08-06 +//

+// This is very useful class to convert strings containing pairs "key"="value" +// into fast dictionaries. This strings are often read from external files. +// From dictionary created in such way one can easily +// extract values in few supported formats (integer, double, boolean, string) +// It can also be used in opposite direction: when dictionary is created with +// given values and keys, one can create config string, which is most +// convenient to write in external files. +//


+// Most simple example of usage: +//

Suppose you have two variables fD and fI: +//
-------------------
+//    Double_t fD;
+//    Int_t fI;
+//
+//fD = 3.14;
+//fI = 2012;
+//-----------------------
+// To save its parameters into file you can create ConfigDictionary class +// instance and use SetDouble and SetInt functions to +// insert parameters values with arbitrarly defined keys (let them be +// really long and descriptive in this example): +//
---------------------
+//ConfigDictionary CD;
+//CD.SetDouble("Most important variable D",fD);
+//CD.SetInt("Equally important integer I",fI);
+//---------------------
+// Now configuration string saved in ConfigDictionary CD +// can be obtained with ToString method and should look like: +//
+//"Most important variable D"="3.14" "Equally important integer I"="2012"
+//
+//
It can be easily saved to a file using simplest fstream +// methods. And the advantage is that as a key one can use any string, +// so configuration file created in such way is very self-explanatory. +//
+//
+// Now lets suppose the opposite action - loading config from file. +// Imagine, that you have 1000 objects of A class, which config was saved +// to file - one object per line: +//
-----------------
+//"Most important variable D"="3.14" "Equally important integer I"="2012"
+//"Equally important integer I"="1011" "Most important variable D"="8.15"
+//"Most important variable D"="13.16" "Equally important integer I"="10"
+//(...)
+//----------------
+// Please notice that order in which pairs of keys and values are placed +// in file doesn't make any difference to the ConfigDictionary class. +// To recreate objects, you just read each line containing single +// config string and then create with it ConfigDictionary object +// using special constructor with string as argument, +// or using FromString method. After this, you can +// extract parameters (using GetDouble and GetInt methods) +// with using same keys as were used for saving, and ConfigDictionary +// will return their values: +//
----------------------
+//string line;//This line you read from file
+//ConfigDictionary CD(line);
+//fD = CD.GetDouble("Most important variable D");
+//fI = CD.GetInt("Equally important integer I");
+//--------------------
+//
+// And last but not least: what happens, if key requested by user doesn't +// exist in dictionary? (This can be caused by many reasons, mostly errors on +// user side, but not always). +// When you try to extract non-existent key using Get functions, +// an exception is risen. In normal program it should end execution and print +// some information about where program stopped working. +// But lets say that you don't want that, i.e. program may use default +// configs instead of those from files, or not all keys were that important. +// You can surround all uses of Get methods with try/catch clause. +// So when exception is risen, you will catch it, and decide, if you want +// the program to stop running, or anything else. Simple example is +// shown below: +//
-------------------
+//ConfigDictionary CD(some_string);
+//try{//try to read important variables:
+//  double d = CD.GetDouble("crucial var");
+//  bool b = CD.GetBool("most important b");
+//  int i = CD.GetInt("unique ID");
+//  //...and so on
+//}catch(std::string & e){//catch any kind of exception
+//  Error("Some crucial variable wasn't read, ending program!");
+//  return SOME_REALLY_BAD_ERROR_CODE;
+//}
+//try{//now less important, or optional:
+//  string name = CD.GetString("least important variable");
+//  double p = CD.GetDouble("optional parameter");
+//  //...and so on
+//}catch(std::string & f){
+//  Info("Some optional variables wasn't read!");
+//}
+//--------------------
+// +// ADDED 21.03.2012: +// +// With use of templates, now ConfigDictionary can handle complex objects +// reading and writing instantly as long as proper >> and << operators are +// provided for an object initialization. +// +// END_HTML +////////////////////////////////////////////////////////////////////////////// + +//_____________________________________________________________________________ +ConfigDictionary::ConfigDictionary(){ + //Empty map... +} + +//_____________________________________________________________________________ +ConfigDictionary::ConfigDictionary(std::string params){ + //Creates dictionary using FromString method + FromString(params); +} + +//_____________________________________________________________________________ +std::string ConfigDictionary::ToString(){ + //Builds string that can be easily saved to file. + //Same format that can be read from files or from QtGui + //This should work same way for all uses + + std::map::iterator it;//Iterator to map elements + std::stringstream ss;//stream helpful with adding strings + //iterate whole dictionary: + for (it=configMap.begin();it!=configMap.end();++it){ + //insert pairs "key"="value": + ss<<"\""<first<<"\""<<"="<<"\""<second<<"\""<<" "; + } + return ss.str(); +} + +//_____________________________________________________________________________ +void ConfigDictionary::FromString(std::string params){ + //params - TString containing list of key=value pairs + // + //Changes string formatted: + //"key1"="value1" "key2"="value with spaces 2" ... + //into map with keys and values + //Useful in lots of I/O functions, + //when we want to have a nice, readable format of data + + std::stringstream loading(params); + std::string k,v; + + while(!loading.fail()){ + getline(loading,k,'\"');//removes everything to first " + getline(loading,k,'\"');//All chars between first two "" are the key + getline(loading,v,'\"');//removes all until third " + getline(loading,v,'\"');//All between another pair of "" is the value + if (!loading.fail()) + configMap[k]=v; + } +} + +//_____________________________________________________________________________ +std::string ConfigDictionary::GetString(std::string key)throw(std::string){ + //Extracts string from given key + //(if it exist, otherwise raises exception) + //This works also for long strings with white spaces in it! + if (configMap.find(key) == configMap.end()){ + Error("ConfigDictionary::GetString", + "Couldn't find the key: %s!",key.c_str()); + throw(key); + } + return configMap[key]; +} + +//_____________________________________________________________________________ +int ConfigDictionary::GetInt(std::string key)throw(std::string){ + //Extracts integer from given key + //(if it exist, otherwise raises exception) + return Get(key); +} + +//_____________________________________________________________________________ +double ConfigDictionary::GetDouble(std::string key)throw(std::string){ + //Extracts double precision floating number from given key + //(if it exist, otherwise raises exception) + return Get(key); +} + +//_____________________________________________________________________________ +bool ConfigDictionary::GetBool(std::string key)throw(std::string){ + //Extracts boolean from given key + //(if it exist, otherwise raises exception) + //Differs from template to make it more readable. + if (configMap.find(key) == configMap.end()){ + Error("ConfigDictionary::GetBool", + "Couldn't find the key: %s!",key.c_str()); + throw(key); + } + //Convert string to bool: + if (configMap[key].compare("true") == 0) + return true; + else return false; +} + +//_____________________________________________________________________________ +void ConfigDictionary::SetString(std::string key,std::string value){ + //Sets value to key, no comments needed here... + configMap[key] = value; +} + +//_____________________________________________________________________________ +void ConfigDictionary::SetDouble(std::string key,double value){ + Set(key,value); +} + +//_____________________________________________________________________________ +void ConfigDictionary::SetInt(std::string key,int value){ + //Sets value to key, converts int to string first + Set(key,value); +} + +//_____________________________________________________________________________ +void ConfigDictionary::SetBool(std::string key,bool value){ + //Sets value to key, converts bool to string first + //Differs from template to make it more readable. + if (value == true) configMap[key] = "true"; + else configMap[key] = "false"; +} diff --git a/Utilities/ConfigDictionary.h b/Utilities/ConfigDictionary.h new file mode 100644 index 0000000000000000000000000000000000000000..a6fd7d457eb590b03ee6f3bb2d11f9d045c4c44c --- /dev/null +++ b/Utilities/ConfigDictionary.h @@ -0,0 +1,128 @@ +#ifndef CONFIGDICTIONARY_H +#define CONFIGDICTIONARY_H + + +#include "TObject.h" +#include "ReturnCodes.h" +#include "TStopwatch.h" +#include "TROOT.h" +#include +#include +#include +#include +#include "TLorentzVector.h" +#include +#include "TMath.h" +#include "TString.h" +#include "TTree.h" + + +#define ARRAY_DELIM '$' + +class ConfigDictionary{ +public: + typedef std::map::iterator CDIter; + ConfigDictionary(); + ConfigDictionary(std::string); + virtual ~ConfigDictionary(){};//empty virtual destructor + + ClassDef(ConfigDictionary,1); + std::string ToString(); + void FromString(std::string); + + //These throw errors if couldn't find key: + //Most of methods below are just using templates to + //make them usable in Cint. + std::string GetString(std::string)throw(std::string); + int GetInt(std::string)throw(std::string); + double GetDouble(std::string)throw(std::string); + bool GetBool(std::string)throw(std::string); + + //These will always set 'something' into map: + void SetString(std::string,std::string); + void SetDouble(std::string,double); + void SetInt(std::string,int); + void SetBool(std::string,bool); + + CDIter Begin(){return configMap.begin();}; + CDIter End(){return configMap.end();}; + + + //_________________________________________________________________________ + template + T Get(std::string key) throw (std::string) { + //This method will get object of any class + //that was - presumably - stored before in string format in CD. + //Object needs to have proper >> operator. + //If operation fails, returned item should have default value + //depending on its empty constructor. + //For strings containing spaces it won't work properly, as it + //would extract only first space followed item! + if (configMap.find(key) == configMap.end()) { + Error( "ConfigDictionary::Get", + "Couldn't find the key: %s!", + key.c_str()); + throw(key); + } + std::istringstream inStream(configMap[key]); + T item; + inStream >> item; + return item; + } + + + //_____________________________________________________________________________ + template + void Set(std::string key,T value){ + //Sets value to key, converts T to string first with << operator. + std::stringstream ss; + ss< + int GetArray(std::string key,std::vector & output)throw(std::string){ + //Preconditions: T must have >> operator + //Effect: searches for key in map and pushes values stored in it into output vector + //Returns: number of elements pushed into vector + int retNumber = 0; + if (configMap.find(key) == configMap.end()){ + Error( "ConfigDictionary::GetInt", + "Couldn't find the key: %s!",key.c_str()); + throw(key); + } + std::istringstream arrayString(configMap[key]); + std::string itemString; + //Dangerous - maybe? - exceptions for ss not handled... + while (arrayString){ + std::getline(arrayString,itemString,ARRAY_DELIM); + std::stringstream itemStream(itemString); + if (itemString.length() > 0){ + T item; + itemStream>>item; + output.push_back(item); + retNumber++; + } + } + return retNumber; + } + + //_________________________________________________________________________ + template + void SetArray(std::string key,std::vector & input){ + std::ostringstream arrayString; + for (unsigned int i=0;i configMap; +}; + +#endif diff --git a/Utilities/CsICalib.cpp b/Utilities/CsICalib.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35c36d9e7ff3b3cac727dee39b3919bf644041ff --- /dev/null +++ b/Utilities/CsICalib.cpp @@ -0,0 +1,633 @@ +#include "CsICalib.h" + +ClassImp(CsICalib); + +CsICalib::CsICalib() : fr("TFile", 5), tr("TTree", 5), cutsCol("TCutG"), fA(16), fB(16) { + + printf("CsICalib::Default constructor called.\n"); + + +} + +CsICalib::CsICalib(const char* parfile) : fr("TFile", 5), tr("TTree", 5), cutsCol("TCutG", 10), fA(16), fB(16) { + printf("CsICalib::Constructor called.\n"); + + ReadParFile(parfile); +// PrintParameters(); + cout << "nofiles: " << nofiles << endl; + OpenTrees(); + LoadCuts(); + +// fr.Print(); +// fr.At(0); + + +} + +CsICalib::~CsICalib() { + + printf("CsICalib::Destructor called.\n"); + +} + +void CsICalib::OpenTrees() { + + TFile *file; + for (Int_t i = 0; i < nofiles; i++) { + fr[i] = new TFile(fileNames[i], "READ"); + file = (TFile*)fr.At(i); + cout << file->GetName() << endl; + tr[i] = (TTree*)file->Get("AnalysisxTree"); + } +} + +void CsICalib::LoadCuts() { + + if (cutsFileName == "") { + printf("CsICalib::LoadCuts: Name of file (*.root) with cuts was not provided.\n"); + printf("CsICalib::LoadCuts: No cuts has been loaded.\n"); + return; + } + fCuts = new TFile(cutsFileName.Data(), "READ"); + for (Int_t i = 0; i < nCuts; i++) { + cutsCol[i] = (TCutG*)fCuts->Get(cutNames[i]); + } + +} + +void CsICalib::ReadParFile(const char* parfile) { + + std::ifstream infile(parfile); + if ( !infile.is_open() ) { + printf("CsICalib::ReadParFile: File %s was not open.\n", parfile); + return; + } + + energyPoints = 0; + + TString line; + + Int_t i, min, max; + char det[100], part[100], fname[500], cname[100]; + + double en; //energy + + while (!infile.eof()) { + line.ReadLine(infile); + + if ( line.BeginsWith("#") ) continue; + + if ( line.BeginsWith("energies") ) { + sscanf(line.Data(), "%*s %d", &i); + fE.Set(i); + continue; + } + + if ( line.BeginsWith("files") ) { + sscanf(line.Data(), "%*s %d", &i); + nofiles = i; + printf("CsICalib::ReadParFile: %d files will be loaded:\n", nofiles); + for (Int_t j = 0; j < nofiles; j++) { + line.ReadLine(infile); + sscanf(line, "%s", fname); + fileNames[j] = fname; + cout << fileNames[j] << endl; + } + continue; + } + + if ( line.BeginsWith("cutFile") ) { + sscanf(line.Data(), "%*s %s %d", fname, &nCuts); + cutsFileName = fname; + for (Int_t j = 0; j < nCuts; j++) { + line.ReadLine(infile); + sscanf(line, "%s", cname); + cutNames[j] = cname; + cout << cutNames[j] << endl; + } + continue; + } + + if ( line.BeginsWith("detector") ) { + sscanf(line.Data(), "%*s %s %s", det, part); + detName = det; + partName = part; + printf("%s %s\n", detName.Data(), partName.Data()); + continue; + } + + if ( line.BeginsWith("energy") ) { + sscanf(line.Data(), "%*s %lf", &en); + fE[energyPoints] = en; + printf("%f\n", fE[energyPoints]); + energyPoints++; + continue; + } + + sscanf(line.Data(), "%d %d %d", &i, &min, &max); + peakMin[energyPoints-1][i] = min; + peakMax[energyPoints-1][i] = max; +// printf("%d %d %d\n", i, peakMin[energyPoints-1][i], peakMax[energyPoints-1][i]); + }//while + + printf("energyPoints: %d\n", energyPoints); + + infile.close(); + return; +} + +void CsICalib::PrintParameters(const char* option) { + + TString opt = option; + + printf("Energy points: %d\n", energyPoints); +// printf("Detector: %s, Particle: %s\n", ????); + for (Int_t i = 0; i < energyPoints; i++) { + printf("Peak ranges for energy %f:\n", fE[i]); + if ( opt.Contains("v") ) { + for (Int_t j = 0; j < 16; j++) { + printf("ch: %d\tmin: %d\tmax: %d\n", j, peakMin[i][j], peakMax[i][j]); + } + }//if + }//for + +} + + +void CsICalib::PrintPeakRanges() { + for (Int_t i = 0; i < 16; i++) { + printf("%d\t%d\t%d\n", i, peakMin[0][i], peakMax[0][i]); + } +} + +void CsICalib::PrintTrees() { + + TTree *curTree = 0; + for (Int_t i = 0; i < tr.GetEntries(); i++) { + curTree = (TTree*)tr.At(i); + if (curTree) { + printf("Tree No. %d; File: %s; Name: %s\n", i, curTree->GetDirectory()->GetName(), curTree->GetName()); + } else { + printf("Tree No. %d was not loaded. Maximal number of trees is %d\n", i, NOCALFILES); + } + } + + return; +} + +void CsICalib::PrintFiles() { + + printf("Number of loaded files: %d\n", fr.GetEntries()); + + + TFile *curFile = 0; + for (Int_t i = 0; i < fr.GetEntries(); i++) { + curFile = (TFile*)fr.At(i); + if (curFile) { + printf("File No. %d: \"%s\"\n", i, curFile->GetName()); + } +// else { +// printf("File No. %d was not loaded. Maximal number of files is %d\n", i, NOCALFILES); +// } + } + + return; +} + +void CsICalib::PrintCuts() { + +// printf("CsICalib::PrintCuts: works wrong\n"); +// return; + if (fCuts) printf("Cuts loaded from file \"%s\"\n", fCuts->GetName()); + + /*for (Int_t i = 0; i < NOCALFILES; i++) { + if (cTOF[i]) { + printf("TOF cut No. %d; Name: \"%s\"\n", i, cutsCol[i]->GetName()); + cout << "asdjasd" << endl; + } + else { + printf("TOF cut No. %d was not loaded. Maximal number of cuts is %d\n", i, NOCALFILES); + } + }//for */ + + TCutG *curCut = 0; + //cutsCol + + for (Int_t i = 0; i < cutsCol.GetEntries(); i++) { + curCut = (TCutG*)cutsCol.At(i); + if (curCut) { + printf("Cut No. %d; Name: \"%s\"\n", i, curCut->GetName()); + } /*else { + printf("TOF cut No. %d was not loaded. Maximal number of cuts is %d\n", i, NOCALFILES); + }*/ + } + +/* for (Int_t i = 0; i < NOCALFILES; i++) { + if (cQCD[i]) { + printf("QCD cut No. %d; Name: \"%s\"\n", i, cQCD[i]->GetName()); +// cout << "kasjhd" << endl; + } else { +// cout << "bhajskd" << endl; + printf("QCD cut No. %d was not loaded. Maximal number of cuts is %d\n", i, NOCALFILES); +// cout << "vsdfjks" << endl; + } + } + + printf("CsICalib::PrintCuts: End of function.\n"); +*/ + return; +} + +void CsICalib::DrawVariable(const char* variable, Int_t tree, TCanvas *canvas, Int_t lowRange, Int_t upRange) { + +// if (!canvas) TCanvas *c = new TCanvas(); + if (!canvas) return; + + canvas->Clear(); + canvas->Divide(4,4); + + TString canvasTitle; + TString var; + TString con; + + TTree *curTree = 0; + curTree = (TTree*)tr.At(tree); + if (!curTree) { + printf("CsICalib::DrawVariable: Tree No. %d was not found.\n", tree); + return; + } + + canvasTitle.Form("variable: %s; tree: %d", variable, tree); + canvas->SetTitle(canvasTitle.Data()); + + for (Int_t i = 0; i < 16; i++) { + var.Form("%s[%d]", variable, i); + con.Form("%s[%d]>%d && %s[%d]<%d", variable, i, lowRange, variable, i, upRange); + canvas->cd(i+1); + curTree->Draw(var.Data(), con.Data()); + canvas->Update(); + } + +} + +void CsICalib::DrawBeam(TCanvas *canvas, Int_t files, const char* variable) { + + canvas->SetTitle("Beam"); + canvas->Clear(); +// canvas->Divide(files, 2); + canvas->Divide(files, 3); + + TTree *curTree = 0; + TCutG *curCutG = 0; + TString var; + TString con; + + for (Int_t i = 0; i < files; i++) { + canvas->cd(i+1); + curTree = (TTree*)tr.At(i); + if (!curTree) { + printf("CsICalib::DrawBeam: Tree No. %d was not found.\n", i); + continue; + } + curTree->Draw("QDC[0]:TDC[0]", "TDC[0]<1000 && QDC[0]<2000", "cont"); +// curTree->Draw("QDC[0]+QDC[1]:(TDC[2]+TDC[3])/2. - (TDC[0]+TDC[1])/2.", "", "cont"); +// cout << "aksjda\t" << energyPoints << endl; + for (Int_t j = 0; j < energyPoints; j++) { + if ( cutsCol.At(j) ) { + curCutG = (TCutG*)cutsCol.At(j); + curCutG->Draw("same"); +// printf("CsICalib::DrawBeam: cTOF cut No. %d cannot be drawn, need to repair this function.\n", j); + } + } + + canvas->cd(files+1+i); + curTree->Draw("QDC[0]:QDC[1]", "", "cont"); + for (Int_t j = 0; j < energyPoints; j++) { + if ( cutsCol.At(files + j) ) { + curCutG = (TCutG*)cutsCol.At(files + j); + curCutG->Draw("same"); +// printf("CsICalib::DrawBeam: cQCD cut No. %d cannot be drawn, need to repair this function.\n", j); + } + } + + canvas->cd(2*files+1+i); + var.Form("%s[5]:TDC[0]", variable); + con.Form("%s[5]>200", variable); + curTree->Draw(var.Data(), con.Data(), "cont"); + for (Int_t j = 0; j < energyPoints; j++) { +// if ( cutsCol.At(files + j) ) { +// curCutG = (TCutG*)cutsCol.At(files + j); +// curCutG->Draw("same"); +// printf("CsICalib::DrawBeam: cQCD cut No. %d cannot be drawn, need to repair this function.\n", j); +// } + } + canvas->Update(); + } +} + +void CsICalib::DrawVariableCut(const char* variable, Int_t tree, TCanvas *canvas, const char* cut1, const char* cut2, Int_t lowRange) { + +// cout << "kjashbdfjka ajsdbf jakhsdb askjdhb" << endl; + + if (!cutsCol.FindObject(cut1)) { + printf("Cut %s was not found.\n", cut1); + return; + } + + canvas->Clear(); + canvas->Divide(4,4); + + TString canvasTitle; + TString var; + TString con; + TString sVariable = variable; + sVariable.ToLower(); + Int_t channel = 0; + +// cout << cutsCol.FindObject(cut1)->GetName() << endl; + + canvasTitle.Form("variable: %s; cut1: %s; cut2: %s; tree: %d", variable, cutsCol.FindObject(cut1)->GetName(), cut2 , tree); + canvas->SetTitle(canvasTitle.Data()); + + TTree *curTree = 0; + curTree = (TTree*)tr.At(tree); + if (!curTree) { + printf("CsICalib::DrawVariableCut: Tree No. %d was not found.\n", tree); + return; + } + + for (Int_t i = 0; i<16; i++) { + if (sVariable.Contains("anc")) { + channel = i+1; + } else { + channel = i; + } +// cout << channel << endl; + var.Form("%s[%d]", variable, channel); + con.Form("%s[%d]>%d", variable, channel, lowRange); + + canvas->cd(i+1); + curTree->SetLineColor(1); + curTree->Draw(var.Data(), con.Data()); + curTree->SetLineColor(3); + con.Form("%s[%d]>0 && %s", variable, channel, cutsCol.FindObject(cut1)->GetName()); + curTree->Draw(var.Data(), con.Data(), "same"); + + TString scut2 = cut2; + if (scut2.Length() != 0) { + if (!cutsCol.FindObject(cut2)) { + printf("Cut %s was not found.\n", cut2); + continue; + } + curTree->SetLineColor(4); + con.Form("%s[%d]>0 && %s", variable, channel, cutsCol.FindObject(cut2)->GetName()); + curTree->Draw(var.Data(), con.Data(), "same"); + } + canvas->Update(); + } + +} + +void CsICalib::GetPeakMean(const char* variable, Int_t tree, Int_t energy, TCanvas *canvas, const char* beamcut, const Int_t nbins, Int_t lowRange) { + + canvas->Clear(); + canvas->Divide(4,4); + + TString var; + TString con; + TString hname; + TString canvasTitle; + + canvasTitle.Form("variable: %s; tree: %d; cut: %s;", variable, tree, cutsCol.FindObject(beamcut)->GetName()); + canvas->SetTitle(canvasTitle.Data()); + + TTree *curTree = 0; + curTree = (TTree*)tr.At(tree); + if (!curTree) { + printf("CsICalib::GetPeakMean: Tree No. %d was not found.\n", tree); + return; + } + + TString sVariable = variable; + sVariable.ToLower(); + Int_t channel = 0; + + for (Int_t i = 0; i<16; i++) { + + if (sVariable.Contains("anc")) { + channel = i+1; + } else { + channel = i; + } + + var.Form("%s[%d]>>hfull[%d][%d]", variable, channel, tree, i); + con.Form("%s[%d]>%d && %s", variable, channel, lowRange, cutsCol.FindObject(beamcut)->GetName()); + canvas->cd(i+1); + hname.Form("hfull[%d][%d]", tree, i); + hfull[tree][i] = new TH1I(hname.Data(), "title", nbins, 0, 4096); + curTree->SetLineColor(1); + curTree->Draw(var.Data(), con.Data()); + + var.Form("%s[%d]>>hcut[%d][%d]", variable, channel, tree, i); + con.Form("%s[%d]>%d && %s[%d]<%d && %s", variable, channel, peakMin[energy][i], variable, channel, peakMax[energy][i], cutsCol.FindObject(beamcut)->GetName()); + hname.Form("hcut[%d][%d]", tree, i); + hcut[tree][i] = new TH1I(hname.Data(), "title", nbins, 0, 4096); + hcut[tree][i]->SetLineColor(3); + curTree->Draw(var.Data(), con.Data(), "same"); + + gPad->Update(); + mean[tree][i] = hcut[tree][i]->GetMean(); + meanRMS[tree][i] = hcut[tree][i]->GetRMS(); +// cout << meanRMS[tree][i] << endl; +// cout << hcut[tree][i]->GetMean() << "\t" << hcut[tree][i]->GetRMS() << endl << endl; + + + } + + canvas->Update(); + +} + +void CsICalib::Calibrate(TCanvas *canvas, Bool_t savefile, const char* filename, const char* option) { + + canvas->Clear(); + canvas->Divide(4,4); + +// cout << alphas2.GetSize()+1 << endl; + cout << energyPoints+1 << endl; + + TString gName; + TString gTitle; + +// if (savefile) fGraphs->Open(filename, "RECREATE"); + + TF1 *fnc; + + for (Int_t i = 0; i<16; i++) { + canvas->cd(i+1); + gCal[i] = new TGraphErrors(energyPoints+1); +// FillGraph(gCal[i], energies.GetSize()+1, energies.GetArray(), i); + FillGraph(gCal[i], energyPoints+1, fE.GetArray(), i); +// if (savefile) gCal[i]->Write(); +// gCal[i]->Draw("Al*"); + gCal[i]->Draw("A*"); + gName.Form("g%s%s%d\n", detName.Data(), partName.Data(), i); + gTitle.Form("%s %s\n", detName.Data(), partName.Data()); +// gCal[i]->SetTitle(gTitle.Data()); + gCal[i]->SetName(gName.Data()); + gCal[i]->Fit("pol1"); + fnc = gCal[i]->GetFunction("pol1"); + fnc->SetLineColor(kRed); + fA[i] = fnc->GetParameter(1); + fB[i] = fnc->GetParameter(0); + canvas->Update(); + } + if (savefile) SaveClbGraphs(filename, option); + +} + +void CsICalib::FillGraph(TGraphErrors *g, Int_t npoints, Double_t *energies, Int_t graphNumber, const char* option) { + + TString opt = option; + + //all available energy points and (0,0) + g->SetPoint(0, 0., 0.); + for (Int_t i = 0; i < npoints-1; i++) { +// g->SetPoint(i+1, energies[i], mean[i][graphNumber]); +// g->SetPointError(i+1, 0, meanRMS[i][graphNumber]); + g->SetPoint(i+1, mean[i][graphNumber], energies[i]); + g->SetPointError(i+1, meanRMS[i][graphNumber], 0); + } + +// for (Int_t j = 1; j < 4; j++) { +// g->SetPoint(j, 0., 0.); +// } + + +} + +void CsICalib::WriteClbParameters(const char* filename) { + + std::ofstream outfile(filename); + if ( !outfile.is_open() ) { + printf("CsICalib::WriteClbParameters: File %s was not open.\n", filename); + return; + } + + outfile << "#detector:\t" << detName << ",\tparticle:\t" << partName << endl; + outfile << "#channel\tfA\tfB" << endl; + for (Int_t i = 0; i < 16; i++) { + outfile << i << "\t" << fA[i] << "\t" << fB[i] << endl; + } +} + +void CsICalib::SaveClbGraphs(const char* filename, const char* option) { + + cout << "asdasd" << endl; + cout << fGraphs << endl; + if (fGraphs) fGraphs->Close(); + cout << "asdasd" << endl; + fGraphs = new TFile(filename, option); + cout << fGraphs->IsOpen() << endl; + cout << fGraphs->GetName() << endl; + fGraphs->Print(); +// if (!fGraphs->IsOpen()) { +// printf("CsICalib::SaveClbGraphs: file %s was not open.\n", filename); +// return; +// } + + for (Int_t i = 0; i<16; i++) { + fGraphs->cd(); + gCal[i]->Write(); + } + fGraphs->Close(); + return; +} + +void CsICalib::ReadClbParameters(const char* filename) { + + std::ifstream infile(filename); + if ( !infile.is_open() ) { + printf("CsICalib::ReadClbParameters: File %s was not open.\n", filename); + return; + } + + TString line; + Int_t i; + Double_t a, b; + + while (!infile.eof()) { + line.ReadLine(infile); + + if ( line.BeginsWith("#") ) continue; + +// cout << line.Data() << endl; + sscanf(line.Data(), "%d %lf %lf", &i, &a, &b); + fA[i] = a; + fB[i] = b; +// printf("fA[%d]: %f,\tfB[%d]: %f\n", i, fA[i], i, fB[i]); +// printf("fA[%d]: %f,\tfB[%d]: %f\n\n", i, a, i, b); + } + +} + +void CsICalib::DrawClbGraphs(const char* filename, const char* graphname, TCanvas *canvas) { + + printf("CsICalib::DrawClbGraphs: does not work\n"); + return; + + TFile gfile(filename); + + TString gName; +// TGraph *gr; + + for (Int_t i = 0; i < 16; i++) { + gName.Form("%s%d", graphname, i); + gCal[i] = (TGraphErrors*)gfile.Get(gName.Data()); + canvas->cd(i+1); + gCal[i]->Draw("A*"); + } +} + +void CsICalib::DrawEnergyDeposite(const char* variable, TCanvas *canvas, Int_t tree, const char* option) { + if (!canvas) return; + + canvas->Divide(4,4); + + TString opt; + opt = option; + opt.ToLower(); + + TString canvasTitle; + TString var; + TString con; + + TTree *curTree = 0; + curTree = (TTree*)tr.At(tree); + if (!curTree) { + printf("CsICalib::DrawVariable: Tree No. %d was not found.\n", tree); + return; + } + + canvasTitle.Form("variable: %s [MeV]; tree: %d", variable, tree); + canvas->SetTitle(canvasTitle.Data()); + + if (!opt.Contains("same")) canvas->Divide(4,4); + for (Int_t i = 0; i < 16; i++) { + var.Form("%s[%d]*%f + %f", variable, i, fB[i], fA[i]); +// var.Form("%s[%d]*%f + %f", variable, i, fA[i], fB[i]); + con.Form("%s[%d]>0", variable, i); + + if (opt.Contains("same")) { + if (i==0) curTree->Draw(var.Data(), con.Data()); + curTree->Draw(var.Data(), con.Data(), "same"); + } + else { + canvas->cd(i+1); + curTree->Draw(var.Data(), con.Data()); + } + canvas->Update(); + } + + +} diff --git a/Utilities/CsICalib.h b/Utilities/CsICalib.h new file mode 100644 index 0000000000000000000000000000000000000000..653d2f3f578929d70e23b1e713e00f291f6b2e8d --- /dev/null +++ b/Utilities/CsICalib.h @@ -0,0 +1,98 @@ +#pragma once + +//#include "TObject.h" +//#include "TROOT.h" +#include +#include + +#include "TFile.h" +#include "TTree.h" +#include "TCutG.h" +#include "TCanvas.h" +#include "TClonesArray.h" +#include "TH1I.h" +#include "TGraphErrors.h" +#include "TArrayD.h" +#include "TF1.h" + +#define NOCALFILES 5 + +using std::cout; +using std::endl; + +class CsICalib { + private: + + TString detName; + TString partName; + + TClonesArray fr; //TFile +// TFile *fData; + TClonesArray tr; + TClonesArray colFiles; + TClonesArray colTrees; + Int_t nofiles; + TString fileNames[100]; + TString cutNames[100]; + + Int_t energyPoints; + TArrayD fE; + + TFile *fCuts; + TString cutsFileName; + Int_t nCuts; //number of cuts + TClonesArray cutsCol; + + TH1I *hfull[NOCALFILES][16]; + TH1I *hcut[NOCALFILES][16]; + + Int_t peakMin[NOCALFILES][16]; + Int_t peakMax[NOCALFILES][16]; + Double_t mean[NOCALFILES][16]; + Double_t meanRMS[NOCALFILES][16]; + + TGraphErrors *gCal[16]; + TFile *fGraphs; + TArrayD fA; + TArrayD fB; + + public: +// CsICalib() : a(0), b(0), c(0), p(0){}; + CsICalib(); + CsICalib(const char* parfile); + virtual ~CsICalib(); + + + + void OpenTrees(); + void LoadCuts(); + + void ReadParFile(const char* parfile); + void PrintParameters(const char* option = ""); + void PrintPeakRanges(); + + void DrawVariable(const char* variable, Int_t tree, TCanvas *canvas, Int_t lowRange = 0, Int_t upRange = 4096); + void DrawBeam(TCanvas *canvas, Int_t files, const char* variable); +// void DrawdEE(const char* variable, Int_t tree, TCanvas *canvas); + void DrawVariableCut(const char* variable, Int_t tree, TCanvas *canvas, const char* cut1, const char* cut2 = "", Int_t lowRange = 0); + void GetPeakMean(const char* variable, Int_t tree, Int_t energy, TCanvas *canvas, const char* beamcut, const Int_t nbins = 4096, Int_t lowRange = 0); + + void Calibrate(TCanvas *canvas, Bool_t savefile = 0, const char* filename = "", const char* option = "READ"); + void FillGraph(TGraphErrors *g, Int_t npoints, Double_t *energies, Int_t graphNumber, const char* option = ""); + void WriteClbParameters(const char* filename); + void SaveClbGraphs(const char* filename, const char* option = "READ"); + void ReadClbParameters(const char* filename); + void DrawClbGraphs(const char* filename, const char* graphname, TCanvas *canvas); + void DrawEnergyDeposite(const char* variable, TCanvas *canvas, Int_t tree, const char* option = ""); + + void PrintTrees(); + void PrintFiles(); + void PrintCuts(); + + Double_t GetA(Int_t i) {return fA[i];}; + Double_t GetB(Int_t i) {return fB[i];}; + + + // Define the class for the cint dictionary + ClassDef (CsICalib,1); +}; diff --git a/Utilities/ReturnCodes.h b/Utilities/ReturnCodes.h new file mode 100644 index 0000000000000000000000000000000000000000..9bf1432ce593c96df4def92a529b9b862612e5f0 --- /dev/null +++ b/Utilities/ReturnCodes.h @@ -0,0 +1,17 @@ +#ifndef RETURN_CODES_H +#define RETURN_CODES_H + +//Some useful return codes + + const static int SUCCESS = 0; + const static int IOEXCEPTION = -1; + const static int NOTFOUND = -2; + const static int NULLPOINTER = -3; + const static int UNKNOWN = -4; + const static int FAILURE = -5; + const static int CDEXCEPTION = -6; + const static int EMPTYCONTAINER = -7; + const static int NOTDEFINED = -8; + const static int EXCEPTION = -11; + +#endif diff --git a/Utilities/Utilities.mk b/Utilities/Utilities.mk new file mode 100644 index 0000000000000000000000000000000000000000..a5911d0d8c64234eadba84c50ff75d38ed45c0d3 --- /dev/null +++ b/Utilities/Utilities.mk @@ -0,0 +1,32 @@ +################################################################################ +# AculData input with some variables +################################################################################ + +UTILITIESLIBS := -lCore -lCint -lRIO -lTree -lNet -lThread -lHist -lMatrix -lMathCore -lGpad -lGraf +#UTILITIESLIBS := -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic + +# Add inputs and outputs from these tool invocations to the build variables +UTILITIES_HEADERS += \ +$(UTILITIES)/CalPars.h \ +$(UTILITIES)/CsICalib.h \ +$(UTILITIES)/ConfigDictionary.h \ +$(UTILITIES)/linkdef.h + +UTILITIESCPP_SRCS += \ +$(UTILITIES)/CalPars.cpp \ +$(UTILITIES)/CsICalib.cpp \ +$(UTILITIES)/ConfigDictionary.cpp \ +$(UTILITIES)/UtilitiesCint.cpp + +UTILITIESOBJS += \ +$(UTILITIES)/CalPars.o \ +$(UTILITIES)/CsICalib.o \ +$(UTILITIES)/ConfigDictionary.o \ +$(UTILITIES)/UtilitiesCint.o + +UTILITIESCPP_DEPS += \ +$(UTILITIES)/CalPars.d \ +$(UTILITIES)/CsICalib.d \ +$(UTILITIES)/ConfigDictionary.d \ +$(UTILITIES)/UtilitiesCint.d + diff --git a/Utilities/linkdef.h b/Utilities/linkdef.h new file mode 100755 index 0000000000000000000000000000000000000000..c5c0855424b06f3cf8229689e1ba72bb570f1b8b --- /dev/null +++ b/Utilities/linkdef.h @@ -0,0 +1,11 @@ +#ifdef __CINT__ +#pragma link off all globals; +#pragma link off all classes; +#pragma link off all functions; + +#pragma link C++ class CalPars; +#pragma link C++ class CsICalib; +#pragma link C++ class ConfigDictionary; + +#endif + diff --git a/makefile b/makefile index aaea1ba37eeb46d09ed09be3ed510349a45d707f..4fdcd54533f9d56be1496cf7128e01ae8ba165ef 100755 --- a/makefile +++ b/makefile @@ -25,15 +25,18 @@ ROOTCFLAGS = $(shell root-config --cflags) PWD = $(shell pwd) #INSTALLFOLDER = $(HOME)/AculLib +UTILITIES = $(PWD)/Utilities ACULDATA = $(PWD)/AculData ACULCALIB = $(PWD)/AculCalib TELOSS = $(PWD)/TELoss +-include $(UTILITIES)/Utilities.mk -include $(ACULDATA)/AculData.mk -include $(ACULCALIB)/AculCalib.mk -include $(TELOSS)/TELoss.mk -all: libAculData.so \ +all: libUtilities.so \ + libAculData.so \ libAculCalib.so \ libTELoss.so @@ -47,6 +50,9 @@ clean_htmldoc: -@echo ' ' clean: + -$(RM) $(UTILITIESOBJS) $(UTILITIESCPP_DEPS) + -$(RM) $(UTILITIES)/UtilitiesCint.* libUtilities.so + -@echo ' ' -$(RM) $(ACULDATAOBJS) $(ACULDATACPP_DEPS) -$(RM) $(ACULDATA)/AculDataCint.* libAculData.so -@echo ' ' @@ -60,6 +66,11 @@ clean: -@echo ' ' # Those *Cint* files below need special treating: +$(UTILITIES)/UtilitiesCint.cpp: + -@echo 'Pre-building UtilitiesCint.cpp and UtilitiesCint.h files' + -rootcint -f $(UTILITIES)/UtilitiesCint.cpp -c -p $(UTILITIES_HEADERS) + -@echo ' ' + $(ACULDATA)/AculDataCint.cpp: -@echo 'Pre-building AculDataCint.cpp and AculDataCint.h files' -rootcint -f $(ACULDATA)/AculDataCint.cpp -c -p $(ACULDATA_HEADERS) @@ -76,6 +87,13 @@ $(TELOSS)/TELossCint.cpp: -@echo ' ' #*.so files +libUtilities.so: $(UTILITIESOBJS) + @echo 'Building target: $@' + @echo 'Invoking: $(CC) Linker' + $(CC) -L $(ROOTLIBS) -shared -o"libUtilities.so" $(UTILITIESOBJS) $(UTILITIESLIBS) + @echo 'Finished building target: $@' + @echo ' ' + libAculData.so: libTELoss.so $(ACULDATAOBJS) @echo 'Building target: $@' @echo 'Invoking: GCC C++ Linker'