00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 #include "TUnfold.h"
00122 #include "TUnfoldBinningXML.h"
00123
00124 #include <TXMLDocument.h>
00125 #include <TXMLNode.h>
00126 #include <TXMLAttr.h>
00127 #include <TList.h>
00128 #include <TVectorD.h>
00129
00130 #include <fstream>
00131 #include <sstream>
00132
00133
00134
00135 using namespace std;
00136
00137 ClassImp(TUnfoldBinningXMLV17)
00138
00139
00140
00141
00142
00143
00144
00145 void TUnfoldBinningXMLV17::WriteDTD(std::ostream &out) {
00146 out
00147 <<"<!-- TUnfold Version "<<TUnfold::GetTUnfoldVersion()<<" -->\n"
00148 <<"<!ELEMENT TUnfoldBinning (BinningNode)+ >\n"
00149 <<"<!ELEMENT BinningNode (BinningNode+|(Binfactorlist?,Axis)|Bins) >\n"
00150 <<"<!ATTLIST BinningNode name ID #REQUIRED firstbin CDATA \"-1\"\n"
00151 <<" factor CDATA \"1.\">\n"
00152 <<"<!ELEMENT Axis ((Bin+,Axis?)|(Axis)) >\n"
00153 <<"<!ATTLIST Axis name CDATA #REQUIRED lowEdge CDATA #REQUIRED>\n"
00154 <<"<!ELEMENT Binfactorlist (#PCDATA)>\n"
00155 <<"<!ATTLIST Binfactorlist length CDATA #REQUIRED>\n"
00156 <<"<!ELEMENT Bin EMPTY>\n"
00157 <<"<!ATTLIST Bin width CDATA #REQUIRED location CDATA #IMPLIED\n"
00158 <<" center CDATA #IMPLIED repeat CDATA #IMPLIED>\n"
00159 <<"<!ELEMENT Bins (BinLabel)* >\n"
00160 <<"<!ATTLIST Bins nbin CDATA #REQUIRED>\n"
00161 <<"<!ELEMENT BinLabel EMPTY>\n"
00162 <<"<!ATTLIST BinLabel index CDATA #REQUIRED name CDATA #REQUIRED>\n";
00163 }
00164
00165
00166
00167
00168
00169 void TUnfoldBinningXMLV17::WriteDTD(const char *file) {
00170 ofstream out(file);
00171 WriteDTD(out);
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181 TUnfoldBinningXMLV17 *TUnfoldBinningXMLV17::ImportXML
00182 (const TXMLDocument *document,const char *name) {
00183
00184
00185
00186
00187 TUnfoldBinningXMLV17 *r=0;
00188 TXMLNode *root=document->GetRootNode();
00189 TXMLNode *binningNode=0;
00190 if(root && (!TString(root->GetNodeName()).CompareTo("TUnfoldBinning")) &&
00191 (root->GetNodeType()==TXMLNode::kXMLElementNode)) {
00192
00193 for(TXMLNode *node=root->GetChildren();node && !binningNode;
00194 node=node->GetNextNode()) {
00195 if(node->GetNodeType()==TXMLNode::kXMLElementNode &&
00196 !TString(node->GetNodeName()).CompareTo("BinningNode") &&
00197 node->GetAttributes()) {
00198
00199 TIterator *i=node->GetAttributes()->MakeIterator();
00200 TXMLAttr *attr;
00201 while((attr=(TXMLAttr *)i->Next())) {
00202 if((!TString(attr->GetName()).CompareTo("name")) &&
00203 ((!TString(attr->GetValue()).CompareTo(name)) ||
00204 !name)) {
00205 binningNode=node;
00206 }
00207 }
00208 }
00209 }
00210 }
00211
00212 if(binningNode) {
00213 r=ImportXMLNode(binningNode);
00214 }
00215 return r;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225 TUnfoldBinningXMLV17 *TUnfoldBinningXMLV17::ImportXMLNode
00226 (TXMLNode *node) {
00227
00228 const char *name=0;
00229 Double_t factor=1.0;
00230 TUnfoldBinningXMLV17 *r=0;
00231 Int_t nBins=0;
00232 const char *binNames=0;
00233 TIterator *i=node->GetAttributes()->MakeIterator();
00234 TXMLAttr *attr;
00235
00236 while((attr=(TXMLAttr *)i->Next())) {
00237 TString attName(attr->GetName());
00238 if(!attName.CompareTo("name")) {
00239 name=attr->GetValue();
00240 }
00241 if(!attName.CompareTo("factor")) {
00242 factor=TString(attr->GetValue()).Atof();
00243 }
00244 }
00245 if(name) {
00246 TString binNameList="";
00247
00248 for(TXMLNode *child=node->GetChildren();child;
00249 child=child->GetNextNode()) {
00250
00251 if(child->GetNodeType()==TXMLNode::kXMLElementNode &&
00252 !TString(child->GetNodeName()).CompareTo("Bins")) {
00253
00254
00255 if(child->GetAttributes()) {
00256 i=child->GetAttributes()->MakeIterator();
00257 while((attr=(TXMLAttr *)i->Next())) {
00258 TString attName(attr->GetName());
00259 if(!attName.CompareTo("nbin")) {
00260
00261 nBins=TString(attr->GetValue()).Atoi();
00262 }
00263 }
00264 }
00265
00266 TObjArray theBinNames;
00267 for(TXMLNode *binName=child->GetChildren();binName;
00268 binName=binName->GetNextNode()) {
00269 if(binName->GetNodeType()==TXMLNode::kXMLElementNode &&
00270 !TString(binName->GetNodeName()).CompareTo("BinLabel")) {
00271 i=binName->GetAttributes()->MakeIterator();
00272 const char *binLabelName=0;
00273 Int_t index=0;
00274 while((attr=(TXMLAttr *)i->Next())) {
00275 TString attName(attr->GetName());
00276 if(!attName.CompareTo("index")) {
00277 index=TString(attr->GetValue()).Atoi();
00278 }
00279 if(!attName.CompareTo("name")) {
00280 binLabelName=attr->GetValue();
00281 }
00282 }
00283 if((index>=0)&&(binLabelName)) {
00284 if(index>=theBinNames.GetEntriesFast()) {
00285 theBinNames.AddAtAndExpand
00286 (new TObjString(binLabelName),index);
00287 }
00288 }
00289 }
00290 }
00291 Int_t emptyName=0;
00292 for(Int_t ii=0;ii<theBinNames.GetEntriesFast()&&(ii<nBins);ii++) {
00293 if(theBinNames.At(ii)) {
00294 for(Int_t k=0;k<emptyName;k++) binNameList+=";";
00295 emptyName=0;
00296 binNameList+=
00297 ((TObjString *)theBinNames.At(ii))->GetString();
00298 }
00299 emptyName++;
00300 }
00301 if(binNameList.Length()>0) {
00302 binNames=binNameList;
00303 }
00304 }
00305 }
00306 r=new TUnfoldBinningXMLV17(name,nBins,binNames);
00307
00308
00309 r->AddAxisXML(node);
00310
00311
00312 TVectorD *perBinFactors=0;
00313 for(TXMLNode *child=node->GetChildren();child;
00314 child=child->GetNextNode()) {
00315
00316 if(child->GetNodeType()==TXMLNode::kXMLElementNode &&
00317 !TString(child->GetNodeName()).CompareTo("Binfactorlist")) {
00318 int length=0;
00319 i=child->GetAttributes()->MakeIterator();
00320 while((attr=(TXMLAttr *)i->Next())) {
00321 TString attName(attr->GetName());
00322 if(!attName.CompareTo("length")) {
00323 length=TString(attr->GetValue()).Atoi();
00324 }
00325 }
00326 int nread=0;
00327 if(length==r->GetDistributionNumberOfBins()) {
00328 perBinFactors=new TVectorD(length);
00329 const char *text=child->GetText();
00330 if(text) {
00331 stringstream readFactors(text);
00332 for(;nread<length;nread++) {
00333 readFactors>> (*perBinFactors)(nread);
00334 if(readFactors.fail()) break;
00335 }
00336 }
00337 }
00338 if(!perBinFactors) {
00339 child->Error("ImportXMLNode","while reading per-bin factors"
00340 " node=%s length=%d (expected %d)",r->GetName(),
00341 length,r->GetDistributionNumberOfBins());
00342 } else if(nread!=length) {
00343 child->Error("ImportXMLNode","while reading per-bin factors"
00344 " TUnfoldBinning=%s expected %d found %d",
00345 r->GetName(),length,nread);
00346 delete perBinFactors;
00347 perBinFactors=0;
00348 }
00349 }
00350 }
00351
00352
00353 r->SetBinFactor(factor,perBinFactors);
00354
00355
00356 for(TXMLNode *child=node->GetChildren();child;
00357 child=child->GetNextNode()) {
00358 if(child->GetNodeType()==TXMLNode::kXMLElementNode &&
00359 !TString(child->GetNodeName()).CompareTo("BinningNode") &&
00360 child->GetAttributes()) {
00361 TUnfoldBinningV17 *childBinning=ImportXMLNode(child);
00362 r->AddBinning(childBinning);
00363 }
00364 }
00365 }
00366 return r;
00367 }
00368
00369
00370
00371
00372
00373
00374 void TUnfoldBinningXMLV17::AddAxisXML(TXMLNode *node) {
00375
00376 TXMLNode *axis=0;
00377 for(TXMLNode *child=node->GetChildren();child;
00378 child=child->GetNextNode()) {
00379 if(child->GetNodeType()==TXMLNode::kXMLElementNode) {
00380 TString nodeName(child->GetNodeName());
00381 if(!nodeName.CompareTo("Axis")) axis=child;
00382 }
00383 }
00384 if(axis) {
00385 const char *axisName=0;
00386 TArrayD binEdges(1);
00387 TIterator *i=axis->GetAttributes()->MakeIterator();
00388 TXMLAttr *attr;
00389 while((attr=(TXMLAttr *)i->Next())) {
00390 TString attName(attr->GetName());
00391 if(!attName.CompareTo("name")) {
00392 axisName=attr->GetValue();
00393 }
00394 if(!attName.CompareTo("lowEdge")) {
00395 binEdges[0]=TString(attr->GetValue()).Atof();
00396 }
00397 }
00398 Bool_t hasMoreAxes=kFALSE;
00399 Bool_t underflow=kFALSE,overflow=kFALSE;
00400 for(TXMLNode *child=axis->GetChildren();child;
00401 child=child->GetNextNode()) {
00402 if(child->GetNodeType()==TXMLNode::kXMLElementNode) {
00403 TString nodeName(child->GetNodeName());
00404 if(!nodeName.CompareTo("Axis")) hasMoreAxes=kTRUE;
00405 if(!nodeName.CompareTo("Bin")) {
00406 Bool_t isUnderflow=kFALSE,isOverflow=kFALSE;
00407 Int_t repeat=1;
00408 i=child->GetAttributes()->MakeIterator();
00409 while((attr=(TXMLAttr *)i->Next())) {
00410 TString attName(attr->GetName());
00411 TString attText(attr->GetValue());
00412 if(!attName.CompareTo("location")) {
00413 isUnderflow= !attText.CompareTo("underflow");
00414 isOverflow= !attText.CompareTo("overflow");
00415 }
00416 if(!attName.CompareTo("repeat")) {
00417 repeat=attText.Atof();
00418 }
00419 }
00420 if(repeat<1) {
00421 node->Warning("AddAxisXML",
00422 "attribute repeat=%d changed to repeat=1",
00423 repeat);
00424 repeat=1;
00425 }
00426 if((isUnderflow || isOverflow)&&(repeat!=1)) {
00427 node->Error("AddAxisXML",
00428 "underflow/overflow can not have repeat!=1 attribute");
00429 }
00430 if(isUnderflow || isOverflow) {
00431 underflow |= isUnderflow;
00432 overflow |= isOverflow;
00433 } else {
00434 Int_t iBin0=binEdges.GetSize();
00435 Int_t iBin1=iBin0+repeat;
00436 Double_t binWidth=0.0;
00437 binEdges.Set(iBin1);
00438 i=child->GetAttributes()->MakeIterator();
00439 while((attr=(TXMLAttr *)i->Next())) {
00440 TString attName(attr->GetName());
00441 if(!attName.CompareTo("width")) {
00442 binWidth=TString(attr->GetValue()).Atof();
00443 }
00444 }
00445 if(binWidth<=0.0) {
00446 node->Error("AddAxisXML",
00447 "bin withd can not be smaller than zero");
00448 }
00449 for(int iBin=iBin0;iBin<iBin1;iBin++) {
00450 binEdges[iBin]=binEdges[iBin0-1]+(iBin-iBin0+1)*binWidth;
00451 }
00452 }
00453 }
00454 }
00455 }
00456 AddAxis(axisName,binEdges.GetSize()-1,binEdges.GetArray(),
00457 underflow,overflow);
00458 if(hasMoreAxes) {
00459 AddAxisXML(axis);
00460 }
00461 }
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 Int_t TUnfoldBinningXMLV17::ExportXML
00477 (const TUnfoldBinningV17 &binning,std::ostream &out,Bool_t writeHeader,
00478 Bool_t writeFooter,Int_t indent) {
00479
00480 if(writeHeader) {
00481 out<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
00482 <<"<!DOCTYPE TUnfoldBinning SYSTEM \"tunfoldbinning.dtd\">\n"
00483 <<"<TUnfoldBinning>\n";
00484 }
00485 TString trailer(' ',indent);
00486 out<<trailer<<"<BinningNode name=\""<<binning.GetName()<<"\" firstbin=\""
00487 <<binning.GetStartBin();
00488 if(binning.IsBinFactorGlobal()) {
00489 out<<"\" factor=\""<<binning.GetGlobalFactor()<<"\">\n";
00490 } else {
00491 out<<"\">\n";
00492 out<<trailer<<" <Binfactorlist length=\""
00493 <<binning.GetDistributionNumberOfBins()<<"\">\n";
00494 for(int i=0;i<binning.GetDistributionNumberOfBins();i++) {
00495 if(!(i % 10)) out<<trailer<<" ";
00496 out<<" "<<binning.GetBinFactor(i+binning.GetStartBin());
00497 if(((i %10)==9)||(i==binning.GetDistributionNumberOfBins()-1))
00498 out<<"\n";
00499 }
00500 out<<trailer<<" </Binfactorlist>\n";
00501 }
00502 if(binning.HasUnconnectedBins()) {
00503 out<<trailer<<" <Bins nbin=\""<<binning.GetDistributionNumberOfBins()
00504 <<"\">\n";
00505 for(Int_t i=0;i<binning.GetDistributionNumberOfBins();i++) {
00506 const TObjString *name=binning.GetUnconnectedBinName(i);
00507 if(!name) break;
00508 out<<trailer<<" <BinLabel index=\""<<i<<"\" name=\""
00509 <<name->GetString()<<"\" />\n";
00510 }
00511 out<<trailer<<" </Bins>\n";
00512 } else {
00513 for(Int_t axis=0;axis<binning.GetDistributionDimension();axis++) {
00514 TString axisTrailer(' ',indent+1+axis);
00515 TVectorD const *edges=binning.GetDistributionBinning(axis);
00516 out<<axisTrailer<<"<Axis name=\""<<binning.GetDistributionAxisLabel(axis)
00517 <<"\" lowEdge=\""<<(*edges)[0]<<"\">\n";
00518 if(binning.HasUnderflow(axis)) {
00519 out<<axisTrailer<<" <Bin location=\"underflow\" width=\""
00520 <<binning.GetDistributionUnderflowBinWidth(axis)<<"\" center=\""
00521 <<binning.GetDistributionBinCenter(axis,-1)<<"\" />\n";
00522 }
00523 for(Int_t i=0;i<edges->GetNrows()-1;i++) {
00524 Int_t repeat=1;
00525 Double_t width=(*edges)[i+1]-(*edges)[i];
00526 Double_t center=binning.GetDistributionBinCenter(axis,i);
00527 for(Int_t j=i+1;j<edges->GetNrows()-1;j++) {
00528 double xEnd=(j-i+1)*width+(*edges)[i];
00529 double xCent=center+(j-i)*width;
00530 if((TMath::Abs(xEnd-(*edges)[j+1])<width*1.E-7)&&
00531 (TMath::Abs(xCent-binning.GetDistributionBinCenter(axis,j))<
00532 width*1.E-7)) {
00533 ++repeat;
00534 } else {
00535 break;
00536 }
00537 }
00538 if(repeat==1) {
00539 out<<axisTrailer<<" <Bin width=\""
00540 <<width<<"\" center=\""<<center<<"\" />\n";
00541 } else {
00542 out<<axisTrailer<<" <Bin repeat=\""<<repeat
00543 <<"\" width=\""<<width<<"\" center=\""<<center<<"\" />\n";
00544 i += repeat-1;
00545 }
00546 }
00547 if(binning.HasOverflow(axis)) {
00548 out<<axisTrailer<<" <Bin location=\"overflow\" width=\""
00549 <<binning.GetDistributionOverflowBinWidth(axis)<<"\" center=\""
00550 <<binning.GetDistributionBinCenter(axis,edges->GetNrows()-1)<<"\"/>\n";
00551 }
00552 }
00553 for(Int_t axis=binning.GetDistributionDimension()-1;axis>=0;axis--) {
00554 TString axisTrailer(' ',indent+1+axis);
00555 out<<axisTrailer<<"</Axis>\n";
00556 }
00557 }
00558 for(TUnfoldBinning const *child=binning.GetChildNode();child;
00559 child=child->GetNextNode()) {
00560 ExportXML(*child,out,kFALSE,kFALSE,indent+1);
00561 }
00562 out<<trailer<<"</BinningNode>\n";
00563 if(writeFooter) {
00564 out<<"</TUnfoldBinning>\n";
00565 }
00566 return out.fail() ? 0 : 1;
00567 }
00568
00569
00570
00571
00572
00573
00574
00575 Int_t TUnfoldBinningXMLV17::ExportXML(char const *fileName) const {
00576
00577
00578 ofstream outFile(fileName);
00579 Int_t r=ExportXML(*this,outFile,kTRUE,kTRUE);
00580 outFile.close();
00581 return r;
00582 }
00583