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();
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;
73 binMap = binning->CreateEmptyBinMap();
74 TUnfoldBinning const *branch2=binning->FindNode("branch2");
75 branch2->FillBinMap1D(binMap,"x[C]",7);
76 PrintBinMap(binning,"branch2->FillBinMap1D(...,\"x[C]\",...,7)",binMap);
77
78 delete [] binMap;
79 binMap = binning->CreateEmptyBinMap();
80 binning->FillBinMap1D(binMap,"y[C]",1);
81 PrintBinMap(binning,"binning->FillBinMap1D(...,\"y[C]\",...,1)",binMap);
82
83 binning->ExportXML("testUnfold6.out.xml");
84
85 delete [] binMap;
86
87 }
88}
89
90void PrintBinMap(TUnfoldBinning *binning,const char * where,
91 const Int_t *binMap) {
92
93 cout<<"\n"<<where<<"\n=======================\n";
94 cout<<"global bin:";
95 for(int i=0;i<binning->GetEndBin()+1;i++) {
96 cout<<setw(3)<<i;
97 }
98 cout<<"\n";
99 cout<<"mapped to: ";
100 for(int i=0;i<binning->GetEndBin()+1;i++) {
101 cout<<setw(3)<<binMap[i];
102 }
103 cout<<"\n";
104 map<int,vector<int> > destBin;
105 for(int i=0;i<binning->GetEndBin()+1;i++) {
106 destBin[binMap[i]].push_back(i);
107 }
108 bool printed=false;
109 for(map<int,vector<int> >::const_iterator i=destBin.begin();i!=destBin.end();i++) {
110 if((*i).first>=0) {
111 if(!printed) {
112 cout<<"\ndest |contributing bins\n"
113 <<"=====+======================================\n";
114 printed=true;
115 }
116 for(size_t j=0;j<(*i).second.size();j++) {
117 cout<<setw(4)<<(*i).first<<" |";
118 cout<<setw(3)<<binning->GetBinName((*i).second[j])<<"\n";
119 }
120 cout<<"=====+======================================\n";
121 }
122 }
123}
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
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 * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1058
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1027
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