void execCheckErrors(bool what = 0) { TTree *tree = 0; TFile *file = 0; if (what == 0) { tree = new TTree("ntuple",""); float px = 0; tree->Branch("px",&px); TString str; tree->Branch("str",&str); tree->Fill(); tree->ResetBranchAddresses(); } else if (what == 1) { file = new TFile("hsimple.root"); tree = (TTree*)file->Get("ntuple"); } else { tree = new TChain("ntuple", "ntuple"); ((TChain*)tree)->Add("hsimple.root"); } // tree->SetBranchStatus("*", 1); // tree->SetBranchStatus("px", 1); // std::cout << "fTreeNumber = " << tree->GetTreeNumber() << std::endl; tree->LoadTree(0); // REQUIRED // std::cout << "fTreeNumber = " << tree->GetTreeNumber() << std::endl; TBranch *p = ((TBranch *)-1); Int_t r; Float_t px, fake_px; Float_t *pxp = new Float_t(); Float_t *fake_pxp = new Float_t(); Int_t ix; Int_t *ixp = new Int_t(); TString s; TString *sp = new TString(); TObjString os; TObjString *osp = new TObjString(); // Float_t ... (should be fine) std::cout << std::endl << "ALL should be FINE ... " << std::endl; p = ((TBranch *)-1); r = tree->SetBranchAddress("px", &px, &p); std::cout << "Float_t ... " << r << std::endl; if (p==((TBranch*)-1)) { std::cout << "p unchanged\n"; } else if (p==0) { std::cout << "p set to zero\n"; } else { std::cout << "p set to the branch address\n"; } r = tree->SetBranchAddress("px", &px); std::cout << "Float_t ... " << r << std::endl; r = tree->SetBranchAddress("px", pxp); std::cout << "Float_t ... " << r << std::endl; r = tree->SetBranchAddress("px", &pxp); std::cout << "Float_t ... " << r << std::endl; // Int_t ... (should fail) std::cout << std::endl << "ALL should FAIL ... " << std::endl; p = ((TBranch *)-1); r = tree->SetBranchAddress("px", &ix, &p); std::cout << "Int_t ... " << r << std::endl; if (p==((TBranch*)-1)) { std::cout << "p unchanged\n"; } else if (p==0) { std::cout << "p set to zero\n"; } else { std::cout << "p set to the branch address\n"; } r = tree->SetBranchAddress("px", &ix); std::cout << "Int_t ... " << r << std::endl; r = tree->SetBranchAddress("px", ixp); std::cout << "Int_t ... " << r << std::endl; r = tree->SetBranchAddress("px", &ixp); std::cout << "Int_t ... " << r << std::endl; // TString ... (should fail) std::cout << std::endl << "ALL should FAIL ... " << std::endl; p = ((TBranch *)-1); r = tree->SetBranchAddress("px", &s, &p); std::cout << "TString ... " << r << std::endl; if (p==((TBranch *)-1)) { std::cout << "p unchanged\n"; } else if (p==0) { std::cout << "p set to zero\n"; } else { std::cout << "p set to the branch address\n"; } r = tree->SetBranchAddress("px", sp); std::cout << "TString ... " << r << std::endl; r = tree->SetBranchAddress("px", &sp); std::cout << "TString ... " << r << std::endl; // TObjString ... (should fail) std::cout << std::endl << "ALL should FAIL ... " << std::endl; p = ((TBranch *)-1); r = tree->SetBranchAddress("px", &os, &p); std::cout << "TObjString ... " << r << std::endl; if (p==((TBranch*)-1)) { std::cout << "p unchanged\n"; } else if (p==0) { std::cout << "p set to zero\n"; } else { std::cout << "p set to the branch address\n"; } r = tree->SetBranchAddress("px", osp); std::cout << "TObjString ... " << r << std::endl; r = tree->SetBranchAddress("px", &osp); std::cout << "TObjString ... " << r << std::endl; // nonexistent branch ... (should fail) std::cout << std::endl << "ALL should FAIL ... " << std::endl; p = ((TBranch *)-1); r = tree->SetBranchAddress("fake_px", &fake_px, &p); std::cout << "nonexistent branch ... " << r << std::endl; if (p==((TBranch*)-1)) { std::cout << "p unchanged\n"; } else if (p==0) { std::cout << "p set to zero\n"; } else { std::cout << "p set to the branch address\n"; } r = tree->SetBranchAddress("fake_px", &fake_px); std::cout << "nonexistent branch ... " << r << std::endl; r = tree->SetBranchAddress("fake_px", fake_pxp); std::cout << "nonexistent branch ... " << r << std::endl; r = tree->SetBranchAddress("fake_px", &fake_pxp); std::cout << "nonexistent branch ... " << r << std::endl; // Float for TString ... (should fail) std::cout << std::endl << "ALL should FAIL ... " << std::endl; p = ((TBranch *)-1); r = tree->SetBranchAddress("str", &px, &p); std::cout << "Float ... " << r << std::endl; if (p==((TBranch*)-1)) { std::cout << "p unchanged\n"; } else if (p==0) { std::cout << "p set to zero\n"; } else { std::cout << "p set to the branch address\n"; } r = tree->SetBranchAddress("str", &px); std::cout << "Float ... " << r << std::endl; r = tree->SetBranchAddress("str", pxp); std::cout << "Float ... " << r << std::endl; r = tree->SetBranchAddress("str", &pxp); std::cout << "Float ... " << r << std::endl; TChain *ch = new TChain("MonoData"); ch->Add("memleak.root"); ch->LoadTree(0); r = ch->SetBranchAddress("mono", &sp, &p); std::cout << "From chain, TString ... " << r << std::endl; TTree *treech = ch; r = treech->SetBranchAddress("mono", &sp, &p); std::cout << "From tree, TString ... " << r << std::endl; }
void hadd(std::string& targetName, std::vector<std::string>& sources) { using namespace std; TFile* target = 0; if(isForce) target = TFile::Open(targetName.c_str(), "RECREATE"); else target = TFile::Open(targetName.c_str(), "CREATE"); if(!target) { printf("target file already exists! (use -f to force recreation)\n"); exit(0); } map<pair<string, string>, TObject*> outputMap; vector<pair<string, TObject*> > outputVec; for(vector<string>::const_iterator iF = sources.begin(); iF != sources.end(); ++iF) { if(verbosity >= 2) { printf("Processing source file: %s\n", iF->c_str()); fflush(stdout); } TFile * f = new TFile(iF->c_str()); MergeRootfile(outputMap, outputVec, f, f); f->Close(); } target->cd(); map<string, TDirectory*> paths; for(std::vector<std::pair<std::string, TObject*> >::const_iterator iO = outputVec.begin(); iO != outputVec.end(); ++iO) { if(iO->second == 0) { if(paths.find(iO->first) == paths.end()) { size_t pos = iO->first.rfind('/'); if(pos == size_t(-1)) { target->cd(); pos = 0; } else { target->cd(iO->first.substr(0, pos).c_str()); pos++; } paths[iO->first] = gDirectory->mkdir(iO->first.substr(pos).c_str()); } } else { paths[iO->first]->cd(); if(iO->second->IsA()->InheritsFrom(TH1::Class())) iO->second->Write(); else { if(iO->second && ((TTree*)iO->second)->GetTreeIndex()) ((TTree*)iO->second)->GetTreeIndex()->Append(0, kFALSE); // Force the sorting TTree* tree = ((TTree*)iO->second)->CloneTree(); ((TTree*)iO->second)->GetListOfClones()->Remove(tree); ((TTree*)iO->second)->ResetBranchAddresses(); tree->ResetBranchAddresses(); tree->Write(); } } } if(verbosity >= 3) { printf("Results written to target file: %s\n", target->GetName()); fflush(stdout); } target->Close(); for(vector<pair<string, TFile*> >::const_iterator iF = tmpFiles.begin(); iF != tmpFiles.end(); ++iF) { if(iF->second) { iF->second->Close(); system(("rm " + iF->first).c_str()); } } }
int Lambda_C_Mass_Fitter( int, char**, const ConfigurationFileList* configFiles ) { //==================================================================================== // THE SWITCH FUNCTION //==================================================================================== vector<double> particle_choice = configFiles->getParameterVector( "particle_choice", vector<double>() ); string filename; if(particle_choice[0]==0) filename = string("LambdaCharm.root"); else if(particle_choice[0]==1) filename = string("XiCharm.root"); TFile outputFile(("output-histos-"+filename).c_str(),"RECREATE"); //==================================================================================== // DECLARE A FILE TO STORE THE DATA, TO BE RAN IN ROOT FORMAT //==================================================================================== // So the TFile doesn't automatically take ownership of any histos, // which can cause problems. TH1::AddDirectory( false ); TH1::SetDefaultSumw2( true ); gROOT->SetStyle("Plain"); gROOT->SetBatch(true); //==================================================================================== // Setting Parameters mass Intervals //==================================================================================== vector<double> massIntervals = configFiles->getParameterVector( "massIntervals", vector<double>(), true ); if( massIntervals.size() != 2 ) return GeneralTools::error( string("Expected 2 mass values, but got ") + GeneralTools::toString((int)massIntervals.size()) + string("!"), 12 ); double massMin = massIntervals[0] ; double massMax = massIntervals[1] ; //==================================================================================== // DOUBLE GAUSS FIT TEST (Comment out single guass above) //==================================================================================== // Declare the mass PDF for the signal // Using the Double Gauss instead of the single. TrplGaussPDF signal_massPDF(true); signal_massPDF.setName( "signal_massPDF" ); signal_massPDF.setParameters( "signalMassMean", "signalMassCoreWidth", "signalCoreFrac","signalSecondSigmaFactor", "signalMassMean3", "signalMassWidth3", "signalFrac3"); // Check that the singal mass PDF is OK. signal_massPDF.fixParameter(7, massMin); signal_massPDF.fixParameter(8, massMax); if( !signal_massPDF.isReady() ) return GeneralTools::error( "Something is wrong with the signal mass PDF!", 10 ); else GeneralTools::info( "The signal mass PDF is OK :)" ); //==================================================================================== // DECLARE THE THE MASS MODEL FOR THE SIGNAL AND // ADD THE MASS PDF THAT WE NAMED "signal_massPDF" //==================================================================================== // ClassModels can contain any number of PDFs for multi-dimensional fits, ClassModel signal_MassModel("signal_MassModel"); // ClassModels can contain any number of PDFs for multi-dimensional fits, signal_MassModel.add_PDF( &signal_massPDF ); // but we're only fitting the mass distribution, so only need one PDF. // Check that the signal class model is OK. if( !signal_MassModel.isReady()) return GeneralTools::error("Something is wrong with the signal mass ClassModel!", 11 ); else GeneralTools::info("The signal mass Classmodel is ok (Happy Days!)"); //==================================================================================== // THE BACKGROUND COMPONENT OF THE MASS FIT //==================================================================================== // Declare the mass PDF for the background // Using a polynomial of 1 degree i.e. a straight line (y=mx+c) Pol1PDF bkg_massPDF; bkg_massPDF.fixParameter( 1, massMin ); // The parameters of a Pol1PDF are the gradient, and minimum and maximum of the bkg_massPDF.fixParameter( 2, massMax ); // fit range. The fit range is constant, so we can fix the min and max. bkg_massPDF.setParameter( 0, "bkg_massGradient" ); // The gradient is left to float, so we add a floating fit parameter to the PDF. //==================================================================================== // DECLARE THE BACKGROUND MASS MODEL AND ADD // THE BKG PDF THAT WE NAMED "bkg_massPDF" //==================================================================================== ClassModel bkg_massModel("bkg_massModel"); bkg_massModel.add_PDF( &bkg_massPDF); // Check that background class model is OK. if( !bkg_massModel.isReady()) return GeneralTools::error("Somthing is wrong with the bkg Classmodel!", 12); else GeneralTools::info("The bkg class model is ok (Sweet!)"); //==================================================================================== // DECLARE THE THE TOTAL FIT MODEL AND ADD THE SIGNAL // AND BKG CLASS MODELS "signal_MassModel" "bkg_massModel" //==================================================================================== Model massFitModel("massFitModel"); massFitModel.add_ClassModel( &signal_MassModel ); massFitModel.add_ClassModel( &bkg_massModel ); massFitModel.setParameter(0, "signalFraction"); //set the first parameter as signalFraction, edit this in config text file // Check that the total model is ok if( !massFitModel.isReady()) return GeneralTools::error("Somthing is wrong with the massFitModel!", 13); else GeneralTools::info("The massFitModel is ok (JACKPOT!)"); //==================================================================================== // DECLARE THE FITTER //==================================================================================== Fitter massFitter( configFiles, &massFitModel, GetVariables::getMass ); // Assuming we get no errors in the Fitter initialisation, we can then do the fit. GeneralTools::info( "Performing mass fit." ); massFitter.doFit(); //Get number of candidates from mass fit result int nMassCandidates = massFitter.getNCandidates(); cout << "THE NUMBER OF MASS CANDIDATES IS " << nMassCandidates << "##########" << endl; // Save the fit results to a TTree. GeneralTools::info( "Saving mass fit results" ); TTree* massFitResults = massFitter.writeFitValuesToTree( "massFitResults" ); if( 0 != massFitResults ) massFitResults->Write(); //==================================================================================== // DRAW THE DATA (Mass Distribution) //==================================================================================== // Raw data plotted! // First lets draw the mass distribution from data. TH1D h_data_mass( "h_data_mass", "mass", 100, massMin, massMax); // TH1D h_data_mass( "h_data_mass", "Lambda_C Mass", 100, massMin, massMax); h_data_mass.GetXaxis()->SetTitle("m(PKPi^-) [MeV]"); h_data_mass.GetYaxis()->SetTitle("Candidates"); for( EventList::const_iterator iEvt = massFitter.getDataSet()->begin(); iEvt != massFitter.getDataSet()->end(); ++iEvt){ h_data_mass.Fill( GetVariables::getMass( *iEvt )[0]); // h_data_mass.SetAxisRange(1200., 2600., "Y"); // h_data_mass.Write(); } //==================================================================================== // SUNDAYS UPDATED FILE FROM HERE WORKED WITHOUT ERROR //==================================================================================== //==================================================================================== // DRAW THE MASS FITTED GAUSSIAN OVER THE DATA //==================================================================================== // Now lets draw the fitted mass PDFs. We put those into histos too. // This methods takes the parameters for the histo: nBins, min and max of the x-axis, // and a name and title for the histo; then the PDF to draw, and a scale value for the // PDF. The PDF is normalised, so the scale is the number of signal, multiplied by the // bin width of the data histo. // Get fit parameter from config text ( "signalFraction" ) FitParameter* signalFraction = configFiles->getFitParameter( "signalFraction" ); // draws a line of order 1 ( nBins, min X , max X , file name , Title , unsure of rest see text above ) TH1D* h_signal_mass = PDFManipulator::draw1DPDF( 1000, massMin, massMax, "h_signal_mass", "Fitted Mass PDF for Signal", &signal_massPDF, massFitter.getNCandidates() * \ signalFraction->getCurrentFitVal() * h_data_mass.GetXaxis()->GetBinWidth(1) ); h_signal_mass->Write(); //==================================================================================== // DRAW THE BACKGROUND POLINOMIAL OVER THE DATA //==================================================================================== //==================================================================================== // NEEDS MORE COMMENTS AND UNDERSTANDING //==================================================================================== // Same for the background. TH1D* h_bkg_mass = PDFManipulator::draw1DPDF( 1000, massMin, massMax, "h_bkg_mass", "Fitted Mass PDF for Background", &bkg_massPDF, massFitter.getNCandidates() * \ (1 - signalFraction->getCurrentFitVal()) * h_data_mass.GetXaxis()->GetBinWidth(1) ); h_bkg_mass->SetLineColor( kRed ); // h_bkg_mass->SetAxisRange(1200., 2600.,"Y"); h_bkg_mass->Write(); // Stack the plots ontop of each other ready to be drawn THStack massFitStack("massFitStack", "Fitted Mass Distribution"); // Double_t fMaximum("2600"); // Maximum value for plotting along y // Double_t fMinimum("1200"); // Minimum value for plotting along y massFitStack.Add(h_bkg_mass); massFitStack.Add(h_signal_mass); // Draw all the plots in 1 canvas TCanvas massCanvas("massFitCanvas", "massFitCanvas"); massFitStack.Draw(); h_data_mass.Draw("same"); gStyle.SetOptStat(111111111); massCanvas.Write(); //==================================================================================== // plotter added here 11,11,11 //==================================================================================== //Declare new histo TH1D h_total_mass(*h_signal_mass); h_total_mass.SetTitle("h_totalFit_mass"); h_total_mass.Add(h_bkg_mass); savePlots2("massFitCanvas" , h_data_mass, h_total_mass, *h_signal_mass, *h_bkg_mass); signal_MassModel.fixAllParameters(); bkg_massModel.fixAllParameters(); EventSelector timeBinSelector; DataSet* data = massFitter.getDataSet(); vector<double> timeBins = configFiles->getParameterVector("timeBins",vector<double>()); double binMin = timeBins[0]; // Min of initial bin const double decayTimeMin = timeBins[0]; // Min of x axis const double decayTimeMax = timeBins[2]; // Max of x axis double binIncFix = timeBins[1]; // the increment double binIncVar = timeBins[4]; // Varible increment double binPercentage = timeBins[5]; int nCandidates = 0; // Initialising the candidate counter double binMax = binMin+binIncVar; // initial bin width int eventsPerBin = nMassCandidates * 0.01; //Auto define 1% yeild data per bin if(timeBins[3] !=0 && timeBins[5]==0) //Config file defined, event population eventsPerBin = timeBins[3]; else if(timeBins[5] !=0 && timeBins[3]==0) eventsPerBin = nMassCandidates * binPercentage/100; // do // return GeneralTools::info("The percetage of bin entries is fixed, see config file for value"); else return GeneralTools::error("Somthing is wrong with the percentage!", 14); int nBins = decayTimeMax /binIncFix; vector<double> xAxisFix(nBins); cout << "xAxisFix Size:----> " << xAxisFix.size() << endl; for(int i=0; i<=nBins; i++) { xAxisFix[i]=(binMin+i*binIncFix); cout << xAxisFix[i] << " "; } cout<<endl; cout<<endl; // DECLARE HISTO FOR THE FIXED BIN FIT TH1D* h_yield_fixedbins = new TH1D("h_yield_fixbins", "yield in fixed bins of decay time", xAxisFix.size(), decayTimeMin, decayTimeMax); for(unsigned int iBin=0; iBin<xAxisFix.size()-1; ++iBin) { timeBinSelector.setSelectionByDecayTime(xAxisFix[iBin], xAxisFix[iBin+1]); Fitter binFitter2(configFiles, &massFitModel, GetVariables::getMass, "", &timeBinSelector); binFitter2.setDataSet(data);// This saves reading in the data again for the new fitter GeneralTools::info( "Plotting yields in bins of decay time" ); binFitter2.doFit(); string iBinStr = string("_") + GeneralTools::toString(iBin); GeneralTools::info( "Saving fixed bin fit results" ); TTree* binFitResults = binFitter2.writeFitValuesToTree( "binFitResults2" + iBinStr); // TCanvas massTestCanvas("massTestFitCanvas", "massTestFitCanvas"); // binFitResults->Write(); // binFitResults->Draw(); //savePlots2("massTestCanvas"+ iBinStr , h_data_mass, h_total_mass, *h_signal_mass, *h_bkg_mass); if( 0 != binFitResults ) binFitResults->Write(); // binFitResults->Draw(); // massTestCanvas.Write(); for( EventList::const_iterator iEvt = binFitter2.getDataSet()->begin(); iEvt != binFitter2.getDataSet()->end(); ++iEvt ) { if(timeBinSelector.select(*iEvt) ) h_yield_fixedbins->Fill(xAxisFix[iBin]); } } h_yield_fixedbins->Write(); h_yield_fixedbins->Draw(); nBins = decayTimeMax/binIncVar; TTree vectorBranch("vectorBranch","x_vector"); vector<double> xBins(nBins); vector<double> *xBinsPointer = &xBins; vectorBranch.Branch("xBins",&xBinsPointer); unsigned int VectorSize = 0; if(particle_choice[0]==0) { for(unsigned int iBin=0; binMax<=decayTimeMax+1; ++iBin) { Fitter binFitter(configFiles, &massFitModel, GetVariables::getMass, "", &timeBinSelector); binFitter.setDataSet(data); nCandidates = 0; while(nCandidates <=eventsPerBin && binMax <= decayTimeMax+1) { timeBinSelector.setSelectionByDecayTime(binMin, binMax); nCandidates = binFitter.getNCandidates(); binMax += binIncVar; } GeneralTools::info( "Plotting yields in bins of decay time" ); binFitter.doFit(); xBins[iBin]=binMin; xBins[iBin+1]=binMax; binMin = binMax; binMax = binMin+binIncVar; VectorSize = iBin+1; } VectorSize+=1; xBins.resize(VectorSize); cout <<" VECTORSIZE--->: " << VectorSize << endl; cout << "XBINS Size-----> " << xBins.size() << endl; for(unsigned int i=0; i < xBins.size(); ++i) cout << xBins[i] << " "; // // cout << endl; // cout << endl; // cout <<"Xbins vector"<<xBins<< endl; //TTree vectorBranch("vectorBranch","x_vector"); //vectorBranch.Branch("xBins",&xBins); vectorBranch.Fill(); outputFile.Write(); double xArray[VectorSize-1]; cout << "XARRAY--------->: " <<endl; for(unsigned int i=0; i <= xBins.size(); ++i) { xArray[i]=xBins[i]; cout << xArray[i] << " "; } cout<<endl; cout<<endl; cout << "XARRAY------------------------------------------------>: " << VectorSize << endl; TH1D h_yield_varBins_LambdaCharm( "h_yield_varBins_LambdaCharm", "Yield in variable bins of decay Time, with equal events per bin", VectorSize-1, xArray ); for(unsigned int iBin=0; iBin < VectorSize-1; ++iBin) { Fitter binFitter(configFiles, &massFitModel, GetVariables::getMass, "", &timeBinSelector); binFitter.setDataSet(data);// This saves reading in the data again for the new fitter timeBinSelector.setSelectionByDecayTime(xArray[iBin], xArray[iBin+1]); GeneralTools::info( "Plotting yields in bins of decay time" ); binFitter.doFit(); double nBinCandidates = binFitter.getNCandidates(); string iBinStr = string("_") + GeneralTools::toString(iBin); GeneralTools::info( "Saving varible bin fit results" ); TTree* binFitResults = binFitter.writeFitValuesToTree( "binFitResults" + iBinStr); if(0 !=binFitResults) binFitResults->Write(); //ssavePlots2("massFitCanvas" , h_yield_varBins_LambdaCharm, h_total_mass, *h_signal_mass, *h_bkg_mass); for( EventList::const_iterator iEvt = binFitter.getDataSet()->begin(); iEvt != binFitter.getDataSet()->end(); ++iEvt ) { if(timeBinSelector.select(*iEvt) ) h_yield_varBins_LambdaCharm.Fill(xArray[iBin]); } Double_t binHeight = h_yield_varBins_LambdaCharm.GetBinContent(iBin); Double_t binWidth = h_yield_varBins_LambdaCharm.GetBinWidth(iBin); Double_t binError = nBinCandidates*signalFraction->err(); Double_t binValue =0; Double_t errorValue = 0; if (binWidth!=0) { binValue = binHeight/binWidth; errorValue = binError/binWidth; h_yield_varBins_LambdaCharm.SetBinContent(iBin,binValue); h_yield_varBins_LambdaCharm.GetBinError(iBin,errorValue); } } // cout << "BIN SIZE OF Lc HISTO =======================>>>>>>>>>>>>>>>>>"<< VectorSize << endl; h_yield_varBins_LambdaCharm.Write(); h_yield_varBins_LambdaCharm.Draw(); } else if(particle_choice[0]==1) { outputFile.Close(); vector<double> xBins2; TFile f1("output-histos-LambdaCharm.root"); TTree *vectorBranch = (TTree*) f1.Get("vectorBranch"); vector<double> *xBinsPointer = 0; vectorBranch->SetBranchAddress("xBins", &xBinsPointer); int num = (int) vectorBranch->GetEntries(); // cout << "entries==============================================================>"<< num << endl; for (int i =0; i<=num; i++) { vectorBranch->GetEntry(i); xBins2=xBinsPointer[0]; // cout << "entries======================================================>"<< xBins2 << endl; vectorBranch->ResetBranchAddresses(); } f1.Close(); // cout << "======================================================>"<<xBins2<< endl; // Enter name of the first root file to take the histogram from //TH1D* histo1; // Declare a new histogram //f1.GetObject("vectorBranch", histo1); TFile outputFile("output-histos-XiCharm.root" ,"update"); double vectorBranch_array[xBins2.size()]; // cout << "vectorBranch_ARRAY--------->: " <<endl; // cout << "vectorBranch-ARAAAY SIZE >>>>>>>>>>>>>>>>>>>>>>" << xBins2.size() << endl; for(unsigned int i=0; i <=xBins2.size()-1; ++i) { vectorBranch_array[i]=xBins2[i]; // cout << vectorBranch_array[i] << " "; } // cout<<endl; // cout<<endl; // cout << "XARRAY--------------------------------------------->: " << xBins2 << endl; TH1D h_yield_varBins_XiCharm( "h_yield_varBins_XiCharm", "Yield in variable bins of decay Time, with equal events per bin", xBins2.size()-1, vectorBranch_array ); for(unsigned int iBin=0; iBin <= xBins2.size()-1; ++iBin) { Fitter binFitter(configFiles, &massFitModel, GetVariables::getMass, "", &timeBinSelector); binFitter.setDataSet(data);// This saves reading in the data again for the new fitter timeBinSelector.setSelectionByDecayTime(vectorBranch_array[iBin], vectorBranch_array[iBin+1]); GeneralTools::info( "Plotting yields in bins of decay time" ); binFitter.doFit(); double nBinCandidates = binFitter.getNCandidates(); string iBinStr = string("_") + GeneralTools::toString(iBin); GeneralTools::info( "Saving Xi varible bin fit results" ); TTree* binFitResults = binFitter.writeFitValuesToTree( "binFitResults" + iBinStr); if(0 !=binFitResults) binFitResults->Write(); for( EventList::const_iterator iEvt = binFitter.getDataSet()->begin(); iEvt != binFitter.getDataSet()->end(); ++iEvt ) { if(timeBinSelector.select(*iEvt) ) h_yield_varBins_XiCharm.Fill(vectorBranch_array[iBin]); } //TAxis* xAxis = h_yield_varBins_XiCharm.GetXaxis(); //myfile2 << xAxis->GetBinLowEdge(iBin)<< " "; cout << xBins2[iBin] << " "; // Double_t norm = h_yield_varBins_XiCharm.GetEntries(); // h_yield_varBins_XiCharm.Scale(1/norm); // } // cout << "BIN SIZE OF XI HISTO ============================================================================>>>>>>>>>>>>>>>>>"<< xBins2.size() << endl; // for(unsigned int iBin=0; iBin <= xBins2.size()-1; ++iBin) // { Double_t binHeight = h_yield_varBins_XiCharm.GetBinContent(iBin); Double_t binWidth = h_yield_varBins_XiCharm.GetBinWidth(iBin); Double_t binError = nBinCandidates*signalFraction->err(); Double_t binValue = 0; Double_t errorValue = 0; if (binWidth!=0) { binValue = binHeight/binWidth; h_yield_varBins_XiCharm.SetBinContent(iBin,binValue); errorValue = binError/binWidth; h_yield_varBins_XiCharm.SetBinError(iBin,errorValue); } } h_yield_varBins_XiCharm.Write(); // h_yield_varBins_XiCharm.GetXaxis()->SetRangeUser(0,10); // h_yield_varBins_XiCharm.Write(); // h_yield_varBins_XiCharm->SetxAxis(); h_yield_varBins_XiCharm.Draw(); } //==================================================================================== // THEY END IS NIGH //==================================================================================== outputFile.Close(); // And that's us! // Now we need to add this method to the list of known fitters in G-Fact.C, // and write a configuration file containing the parameters needed by this fit. return 0 ; }
void MergeRootfile(std::map<std::pair<std::string, std::string>, TObject*>& outputMap, std::vector<std::pair<std::string, TObject*> >& outputVec, TDirectory *target, TFile *source) { using namespace std; string path(target->GetPath()); path = path.substr(path.find(":") + 1); source->cd(path.c_str()); TDirectory *current_sourcedir = gDirectory; //gain time, do not add the objects in the list in memory Bool_t status = TH1::AddDirectoryStatus(); TH1::AddDirectory(kFALSE); // loop over all keys in this directory TIter nextkey( current_sourcedir->GetListOfKeys() ); TKey *key, *oldkey = 0; while(key = (TKey*)nextkey()) { //keep only the highest cycle number for each key if (oldkey && !strcmp(oldkey->GetName(), key->GetName())) continue; oldkey = key; // read object from source file source->cd(path.c_str()); TObject *obj = key->ReadObj(); if(obj->IsA()->InheritsFrom(TH1::Class())) { if(verbosity >= 4) { printf("| Found TH1: %s\n", obj->GetName()); fflush(stdout); } string path(target->GetPath()); pair<string, string> okey(path.substr(path.find(':') + 2), obj->GetName()); //cout << okey.first << "\t" << okey.second << endl; if(outputMap.find(okey) == outputMap.end()) { outputVec.push_back(make_pair(path.substr(path.find(':') + 2), obj)); outputMap[okey] = obj; } else { ((TH1*)outputMap[okey])->Add((TH1*)obj); ((TH1*)obj)->Delete(); } } else if(obj->IsA()->InheritsFrom(TTree::Class())) { string path(target->GetPath()); if(verbosity >= 4) { printf("| Found Tree: %s\n", obj->GetName()); fflush(stdout); } pair<string, string> okey(path.substr(path.find(':') + 2), obj->GetName()); if(outputMap.find(okey) == outputMap.end()) { string fname(okey.first); fname = fname + "_" + obj->GetName() + "tmpfile"; for(size_t pos; (pos = fname.find('/')) != size_t(-1); ) fname[pos] = '_'; tmpFiles.push_back(make_pair(fname, new TFile(fname.c_str(), "RECREATE"))); TTree* tree = ((TTree*)obj)->CloneTree(); ((TTree*)obj)->GetListOfClones()->Remove(tree); ((TTree*)obj)->ResetBranchAddresses(); tree->ResetBranchAddresses(); outputVec.push_back(make_pair(path.substr(path.find(':') + 2), (TObject*)tree)); outputMap[okey] = (TObject*)tree; } else { TTree* tm = (TTree*)outputMap[okey]; TTree* ts = (TTree*)obj; tm->CopyAddresses(ts); for(int i = 0; i < ts->GetEntries(); i++) { ts->GetEntry(i); tm->Fill(); } ts->ResetBranchAddresses(); if (tm->GetTreeIndex()) tm->GetTreeIndex()->Append(ts->GetTreeIndex(), kTRUE); ((TTree*)obj)->Delete(); } } else if(obj->IsA()->InheritsFrom(TDirectory::Class())) { if(verbosity >= 3) { printf("Hadding Directory: %s\n", ((TDirectory*)obj)->GetPath()); fflush(stdout); } string path(((TDirectory*)obj)->GetPath()); pair<string, string> okey(path.substr(path.find(':') + 2), " -------- "); if(outputMap.find(okey) == outputMap.end()) { outputVec.push_back(make_pair(path.substr(path.find(':') + 2), (TDirectory*)0)); outputMap[okey] = 0; } MergeRootfile(outputMap, outputVec, (TDirectory*)obj, source); } else { printf("Unknown object type, name: %s title: %s\n", obj->GetName(), obj->GetTitle()); fflush(stdout); } } // while ( ( TKey *key = (TKey*)nextkey() ) ) }
void read(char *filename, char *friend_name) { TFile *f = TFile::Open(filename, "update"); // TFile *frnd_f = TFile::Open(friend_name, "recreate"); if (!f) { return; } TTree *t; f->GetObject("RPVMCInfoTree",t); // TTree *frnd_t = new TTree("RPVMCFriend","Track matching tree"); // jetroimatched std::vector<int> *jrmV = 0; TBranch *jrmB = 0; t->SetBranchAddress("jetroimatched", &jrmV, &jrmB); // track2roi_index std::vector<int> *track2roiV = 0; TBranch *track2roiB = 0; t->SetBranchAddress("tracktoroi_index", &track2roiV, &track2roiB); // track_d0 (just to see the length of it) std::vector<float> *track_d0V = 0; TBranch *track_d0B = 0; t->SetBranchAddress("track_d0", &track_d0V, &track_d0B); // track_matched - new branch std::vector<int> *track_matchedV = 0; TBranch *track_matchedB = t->Branch("track_matched", &track_matchedV); for (Int_t i = 0; i < 1; i++) { Long64_t event = t->LoadTree(i); jrmB->GetEntry(event); track2roiB->GetEntry(event); track_d0B->GetEntry(event); UInt_t roi_ind; for (roi_ind = 0; roi_ind < jrmV->size() - 1; ++roi_ind) { int tracksInRoi = track2roiV->at(roi_ind + 1) - track2roiV->at(roi_ind); printf("%d\n", tracksInRoi); for (int track_ind = 0; track_ind < tracksInRoi; track_ind++) { track_matchedV->push_back(jrmV->at(roi_ind)); } } // handle last roi seperately for (int track_ind = 0; track_ind < track_d0V->size() - track2roiV->at(roi_ind); track_ind++) { track_matchedV->push_back(jrmV->at(roi_ind)); } track_matchedB.Fill(); } // Since we passed the address of a local variable we need // to remove it. t->Write(); t->ResetBranchAddresses(); f->Close(); // frnd_f->cd(); // frnd_t->Write(); // frnd_f->Close(); }