Logo ROOT  
Reference Guide
testUnfold6.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_unfold
3/// \notebook
4/// Test program for the class TUnfoldBinning.
5///
6/// read a simple binning scheme and create/test bin maps
7///
8/// \macro_output
9/// \macro_code
10///
11/// **Version 17.6, in parallel to changes in TUnfold**
12///
13/// #### History:
14/// - Version 17.5, in parallel to changes in TUnfold
15/// - Version 17.4, in parallel to changes in TUnfold
16/// - Version 17.3, test bin map functionality in TUnfoldBinning
17///
18/// This file is part of TUnfold.
19///
20/// TUnfold is free software: you can redistribute it and/or modify
21/// it under the terms of the GNU General Public License as published by
22/// the Free Software Foundation, either version 3 of the License, or
23/// (at your option) any later version.
24///
25/// TUnfold is distributed in the hope that it will be useful,
26/// but WITHOUT ANY WARRANTY; without even the implied warranty of
27/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28/// GNU General Public License for more details.
29///
30/// You should have received a copy of the GNU General Public License
31/// along with TUnfold. If not, see <http://www.gnu.org/licenses/>.
32///
33/// \author Stefan Schmitt DESY, 14.10.2008
34
35#include <iostream>
36#include <fstream>
37#include <iomanip>
38#include <map>
39#include <TDOMParser.h>
40#include <TXMLDocument.h>
41#include "TUnfoldBinningXML.h"
42
43using namespace std;
44
45void PrintBinMap(TUnfoldBinning *binning,const char * where,
46 const Int_t *binMap);
47
48void testUnfold6()
49{
50 TDOMParser parser;
51 ofstream dtdFile("tunfoldbinning.dtd");
53 dtdFile.close();
54 TString dir = gSystem->UnixPathName(gSystem->DirName(__FILE__));
55 Int_t error=parser.ParseFile(dir+"/testUnfold6binning.xml");
56 if(error) cout<<"error="<<error<<" from TDOMParser\n";
57 TXMLDocument const *XMLdocument=parser.GetXMLDocument();
58 TUnfoldBinningXML *binning=
59 TUnfoldBinningXML::ImportXML(XMLdocument,"binning");
60 if(!binning) {
61 cout<<"error: can not read binning (document empty?)\n";
62 } else {
63 cout<<"Binning scheme:\n =================================\n";
64 binning->PrintStream(cout);
65 Int_t *binMap=binning->CreateEmptyBinMap();
66 PrintBinMap(binning,"CreateEmptyBinMap",binMap);
67
68 TUnfoldBinning const *branch1=binning->FindNode("branch1");
69 branch1->FillBinMap1D(binMap,"y[C]",2);
70 PrintBinMap(binning,"branch1->FillBinMap1D(...,\"y[C]\",...,2)",binMap);
71
72 delete binMap; binMap=binning->CreateEmptyBinMap();
73 TUnfoldBinning const *branch2=binning->FindNode("branch2");
74 branch2->FillBinMap1D(binMap,"x[C]",7);
75 PrintBinMap(binning,"branch2->FillBinMap1D(...,\"x[C]\",...,7)",binMap);
76
77 delete binMap; binMap=binning->CreateEmptyBinMap();
78 binning->FillBinMap1D(binMap,"y[C]",1);
79 PrintBinMap(binning,"binning->FillBinMap1D(...,\"y[C]\",...,1)",binMap);
80
81 binning->ExportXML("testUnfold6.out.xml");
82 }
83}
84
85void PrintBinMap(TUnfoldBinning *binning,const char * where,
86 const Int_t *binMap) {
87
88 cout<<"\n"<<where<<"\n=======================\n";
89 cout<<"global bin:";
90 for(int i=0;i<binning->GetEndBin()+1;i++) {
91 cout<<setw(3)<<i;
92 }
93 cout<<"\n";
94 cout<<"mapped to: ";
95 for(int i=0;i<binning->GetEndBin()+1;i++) {
96 cout<<setw(3)<<binMap[i];
97 }
98 cout<<"\n";
99 map<int,vector<int> > destBin;
100 for(int i=0;i<binning->GetEndBin()+1;i++) {
101 destBin[binMap[i]].push_back(i);
102 }
103 bool printed=false;
104 for(map<int,vector<int> >::const_iterator i=destBin.begin();i!=destBin.end();i++) {
105 if((*i).first>=0) {
106 if(!printed) {
107 cout<<"\ndest |contributing bins\n"
108 <<"=====+======================================\n";
109 printed=true;
110 }
111 for(size_t j=0;j<(*i).second.size();j++) {
112 cout<<setw(4)<<(*i).first<<" |";
113 cout<<setw(3)<<binning->GetBinName((*i).second[j])<<"\n";
114 }
115 cout<<"=====+======================================\n";
116 }
117 }
118}
int Int_t
Definition: RtypesCore.h:41
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
virtual TXMLDocument * GetXMLDocument() const
Returns the TXMLDocument.
Definition: TDOMParser.cxx:144
virtual Int_t ParseFile(const char *filename)
Parse the XML file where filename is the XML file name.
Definition: TDOMParser.cxx:70
Basic string class.
Definition: TString.h:131
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1014
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1054
XML interfate to binning schemes, for use with the unfolding algorithm TUnfoldDensity.
static Int_t ExportXML(const TUnfoldBinning &binning, std::ostream &out, Bool_t writeHeader, Bool_t writeFooter, Int_t indent=0)
Export a binning scheme to a stream in XML format.
static TUnfoldBinningXML * ImportXML(const TXMLDocument *document, const char *name)
Import a binning scheme from an XML file.
static void WriteDTD(const char *fileName="tunfoldbinning.dtd")
Write dtd file.
Binning schemes for use with the unfolding algorithm TUnfoldDensity.
void PrintStream(std::ostream &out, Int_t indent=0, int debug=0) const
Print some information about this binning tree.
Int_t FillBinMap1D(Int_t *binMap, const char *axisSteering, Int_t firstBinX) const
Map all global bins referenced by this node to the one-dimensional histogram destHist,...
TString GetBinName(Int_t iBin) const
Get the name of a bin.
Int_t GetEndBin(void) const
last+1 bin of this node (includes children)
Int_t * CreateEmptyBinMap(void) const
Create an empty bin map, useful together with the getter methods of class TUnfold and TUnfoldSys.
TUnfoldBinning const * FindNode(char const *name) const
Traverse the tree and return the first node which matches the given name.
TXMLDocument contains a pointer to an xmlDoc structure, after the parser returns a tree built during ...
Definition: TXMLDocument.h:24