| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 8 | ! *
|
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 12 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 14 | ! * or implied warranty.
|
|---|
| 15 | ! *
|
|---|
| 16 | !
|
|---|
| 17 | !
|
|---|
| 18 | ! Author(s): Thomas Bretz, 6/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2000-2004
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 26 | //
|
|---|
| 27 | // MWriteRootFile
|
|---|
| 28 | //
|
|---|
| 29 | // This is a writer to store several containers to a root file.
|
|---|
| 30 | // The containers are added with AddContainer.
|
|---|
| 31 | // To understand how it works, see base class MWriteFile
|
|---|
| 32 | //
|
|---|
| 33 | // Warning: Look at the Warning in MTaskList.
|
|---|
| 34 | //
|
|---|
| 35 | // There is a special mode of operation which opens a new file for each new
|
|---|
| 36 | // file read by the reading task (opening the new file is initiated by
|
|---|
| 37 | // ReInit()) For more details se the corresponding constructor.
|
|---|
| 38 | //
|
|---|
| 39 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 40 | #include "MWriteRootFile.h"
|
|---|
| 41 |
|
|---|
| 42 | #include <fstream>
|
|---|
| 43 |
|
|---|
| 44 | #include <TFile.h>
|
|---|
| 45 | #include <TTree.h>
|
|---|
| 46 | #include <TRegexp.h>
|
|---|
| 47 |
|
|---|
| 48 | #include "MLog.h"
|
|---|
| 49 | #include "MLogManip.h"
|
|---|
| 50 |
|
|---|
| 51 | #include "MRead.h"
|
|---|
| 52 | #include "MParList.h"
|
|---|
| 53 |
|
|---|
| 54 | ClassImp(MRootFileBranch);
|
|---|
| 55 | ClassImp(MWriteRootFile);
|
|---|
| 56 |
|
|---|
| 57 | using namespace std;
|
|---|
| 58 |
|
|---|
| 59 | const TString MWriteRootFile::gsDefName = "MWriteRootFile";
|
|---|
| 60 | const TString MWriteRootFile::gsDefTitle = "Task which writes a root-output file";
|
|---|
| 61 |
|
|---|
| 62 | void MWriteRootFile::Init(const char *name, const char *title)
|
|---|
| 63 | {
|
|---|
| 64 | fName = name ? name : gsDefName.Data();
|
|---|
| 65 | fTitle = title ? title : gsDefTitle.Data();
|
|---|
| 66 |
|
|---|
| 67 | //
|
|---|
| 68 | // Set the Arrays the owner of its entries. This means, that the
|
|---|
| 69 | // destructor of the arrays will delete all its entries.
|
|---|
| 70 | //
|
|---|
| 71 | fBranches.SetOwner();
|
|---|
| 72 |
|
|---|
| 73 | //
|
|---|
| 74 | // Believing the root user guide, TTree instanced are owned by the
|
|---|
| 75 | // directory (file) in which they are. This means we don't have to
|
|---|
| 76 | // care about their destruction.
|
|---|
| 77 | //
|
|---|
| 78 | //fTrees.SetOwner();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // --------------------------------------------------------------------------
|
|---|
| 82 | //
|
|---|
| 83 | // Default constructor. It is there to support some root stuff.
|
|---|
| 84 | // Don't use it.
|
|---|
| 85 | //
|
|---|
| 86 | MWriteRootFile::MWriteRootFile() : fOut(NULL)
|
|---|
| 87 | {
|
|---|
| 88 | Init();
|
|---|
| 89 |
|
|---|
| 90 | //
|
|---|
| 91 | // Set the Arrays the owner of its entries. This means, that the
|
|---|
| 92 | // destructor of the arrays will delete all its entries.
|
|---|
| 93 | //
|
|---|
| 94 | fBranches.SetOwner();
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | // --------------------------------------------------------------------------
|
|---|
| 98 | //
|
|---|
| 99 | // Use this constructor to run in a special mode.
|
|---|
| 100 | //
|
|---|
| 101 | // In this mode for each input file a new output file is written. This
|
|---|
| 102 | // happens in ReInit.
|
|---|
| 103 | //
|
|---|
| 104 | // comp: Compression Level (see TFile, TBranch)
|
|---|
| 105 | // rule: Rule to create output file name (see GetNewFileName())
|
|---|
| 106 | // overwrite: Allow newly created file to overwrite old files ("RECREATE")
|
|---|
| 107 | // ftitle: File title stored in the file (see TFile)
|
|---|
| 108 | // name, title: Name and title of this object
|
|---|
| 109 | //
|
|---|
| 110 | MWriteRootFile::MWriteRootFile(const Int_t comp,
|
|---|
| 111 | const char *rule,
|
|---|
| 112 | const Bool_t overwrite,
|
|---|
| 113 | const char *ftitle,
|
|---|
| 114 | const char *name,
|
|---|
| 115 | const char *title) : fSplitRule(rule)
|
|---|
| 116 | {
|
|---|
| 117 | Init(name, title);
|
|---|
| 118 |
|
|---|
| 119 | //
|
|---|
| 120 | // Open a TFile in dummy mode! This is necessary to be able to create
|
|---|
| 121 | // the trees and branches, which are then (in ReInit) moved to
|
|---|
| 122 | // a valid file. (Stupid workaround - but does a good job)
|
|---|
| 123 | //
|
|---|
| 124 | fOut = new TFile("/dev/null", overwrite?"RECREATE":"NEW", ftitle, comp);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | // --------------------------------------------------------------------------
|
|---|
| 128 | //
|
|---|
| 129 | // Specify the name of the root file. You can also give an option ("UPDATE"
|
|---|
| 130 | // and "RECREATE" would make sense only) as well as the file title and
|
|---|
| 131 | // compression factor. To a more detaild description of the options see
|
|---|
| 132 | // TFile.
|
|---|
| 133 | //
|
|---|
| 134 | MWriteRootFile::MWriteRootFile(const char *fname,
|
|---|
| 135 | const Option_t *opt,
|
|---|
| 136 | const char *ftitle,
|
|---|
| 137 | const Int_t comp,
|
|---|
| 138 | const char *name,
|
|---|
| 139 | const char *title)
|
|---|
| 140 | {
|
|---|
| 141 | Init(name, title);
|
|---|
| 142 |
|
|---|
| 143 | //
|
|---|
| 144 | // If no name is given we open the TFile in some kind of dummy mode...
|
|---|
| 145 | //
|
|---|
| 146 | if (!fname)
|
|---|
| 147 | {
|
|---|
| 148 | fOut = new TFile("/dev/null", "READ", ftitle, comp);
|
|---|
| 149 | return;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | TString str(fname);
|
|---|
| 153 | if (!str.EndsWith(".root", TString::kIgnoreCase))
|
|---|
| 154 | str += ".root";
|
|---|
| 155 |
|
|---|
| 156 | //
|
|---|
| 157 | // Open the rootfile
|
|---|
| 158 | //
|
|---|
| 159 | fOut = new TFile(str, opt, ftitle, comp);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | // --------------------------------------------------------------------------
|
|---|
| 163 | //
|
|---|
| 164 | // Prints some statistics about the file to the screen. And closes the file
|
|---|
| 165 | // properly.
|
|---|
| 166 | //
|
|---|
| 167 | void MWriteRootFile::Close()
|
|---|
| 168 | {
|
|---|
| 169 | //
|
|---|
| 170 | // Print some statistics to the looging out.
|
|---|
| 171 | //
|
|---|
| 172 | Print();
|
|---|
| 173 |
|
|---|
| 174 | //
|
|---|
| 175 | // If the file is still open (no error) write the keys. This is necessary
|
|---|
| 176 | // for appearance of the all trees and branches.
|
|---|
| 177 | //
|
|---|
| 178 | if (IsFileOpen())
|
|---|
| 179 | fOut->Write();
|
|---|
| 180 |
|
|---|
| 181 | //
|
|---|
| 182 | // Delete the file. This'll also close the file (if open)
|
|---|
| 183 | //
|
|---|
| 184 | delete fOut;
|
|---|
| 185 | fOut = 0;
|
|---|
| 186 |
|
|---|
| 187 | //
|
|---|
| 188 | // Remark:
|
|---|
| 189 | // - Trees are automatically deleted by the the file
|
|---|
| 190 | // (unless file.SetDirectory(0) was called)
|
|---|
| 191 | // - Branches are automatically deleted by the tree destructor
|
|---|
| 192 | //
|
|---|
| 193 |
|
|---|
| 194 | *fLog << inf << "Output File closed and object deleted." << endl;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | // --------------------------------------------------------------------------
|
|---|
| 198 | //
|
|---|
| 199 | // call Close()
|
|---|
| 200 | //
|
|---|
| 201 | MWriteRootFile::~MWriteRootFile()
|
|---|
| 202 | {
|
|---|
| 203 | Close();
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | // --------------------------------------------------------------------------
|
|---|
| 207 | //
|
|---|
| 208 | // Prints all trees with the actually number of written entries to log-out.
|
|---|
| 209 | //
|
|---|
| 210 | void MWriteRootFile::Print(Option_t *) const
|
|---|
| 211 | {
|
|---|
| 212 | *fLog << all << underline << "File: " << GetFileName() << endl;
|
|---|
| 213 |
|
|---|
| 214 | if (fTrees.GetEntries()==0)
|
|---|
| 215 | {
|
|---|
| 216 | *fLog << " No contents." << endl;
|
|---|
| 217 | return;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | TObject *obj;
|
|---|
| 221 | TIter NextBranch(&fBranches);
|
|---|
| 222 | while ((obj=NextBranch()))
|
|---|
| 223 | {
|
|---|
| 224 | MRootFileBranch *b = (MRootFileBranch*)obj;
|
|---|
| 225 |
|
|---|
| 226 | if (!b->GetTree() || b->GetTree()->TestBit(kIsNewTree))
|
|---|
| 227 | continue;
|
|---|
| 228 |
|
|---|
| 229 | TBranch *branch = b->GetBranch();
|
|---|
| 230 |
|
|---|
| 231 | TString name = b->GetTree()->GetName();
|
|---|
| 232 | name += '.';
|
|---|
| 233 | name += branch->GetName();
|
|---|
| 234 |
|
|---|
| 235 | *fLog << " " << name.Strip(TString::kTrailing, '.') << ": \t" << branch->GetEntries() << " entries." << endl;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | TTree *t = NULL;
|
|---|
| 239 | TIter NextTree(&fTrees);
|
|---|
| 240 | while ((t=(TTree*)NextTree()))
|
|---|
| 241 | if (t->TestBit(kIsNewTree))
|
|---|
| 242 | *fLog << " " << t->GetName() << ": \t" << t->GetEntries() << " entries." << endl;
|
|---|
| 243 | *fLog << endl;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | // --------------------------------------------------------------------------
|
|---|
| 247 | //
|
|---|
| 248 | // Add a new Container to list of containers which should be written to the
|
|---|
| 249 | // file. Give the name of the container which will identify the container
|
|---|
| 250 | // in the parameterlist. tname is the name of the tree to which the
|
|---|
| 251 | // container should be written (Remark: one tree can hold more than one
|
|---|
| 252 | // container). The default is the same name as the container name.
|
|---|
| 253 | // You can slso specify a title for the tree. This is only
|
|---|
| 254 | // used the first time this tree in 'mentioned'. As default the title
|
|---|
| 255 | // is the name of the tree.
|
|---|
| 256 | //
|
|---|
| 257 | void MWriteRootFile::AddContainer(const char *cname, const char *tname, Bool_t must)
|
|---|
| 258 | {
|
|---|
| 259 | //
|
|---|
| 260 | // create a new entry in the list of branches to write and
|
|---|
| 261 | // add the entry to the list.
|
|---|
| 262 | //
|
|---|
| 263 | MRootFileBranch *entry = new MRootFileBranch(AddSerialNumber(cname), tname, must);
|
|---|
| 264 | fBranches.AddLast(entry);
|
|---|
| 265 |
|
|---|
| 266 | if (tname && tname[0])
|
|---|
| 267 | AddToBranchList(Form("%s.%s", (const char*)AddSerialNumber(cname), tname));
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | // --------------------------------------------------------------------------
|
|---|
| 271 | //
|
|---|
| 272 | // Add a new Container to list of containers which should be written to the
|
|---|
| 273 | // file. Give the pointer to the container. tname is the name of the tree to
|
|---|
| 274 | // which the container should be written (Remark: one tree can hold more than
|
|---|
| 275 | // one container). The default is the same name as the container name.
|
|---|
| 276 | // You can slso specify a title for the tree. This is only
|
|---|
| 277 | // used the first time this tree in 'mentioned'. As default the title
|
|---|
| 278 | // is the name of the tree.
|
|---|
| 279 | //
|
|---|
| 280 | void MWriteRootFile::AddContainer(MParContainer *cont, const char *tname,
|
|---|
| 281 | Bool_t must)
|
|---|
| 282 | {
|
|---|
| 283 | //
|
|---|
| 284 | // create a new entry in the list of branches to write and
|
|---|
| 285 | // add the entry to the list.
|
|---|
| 286 | //
|
|---|
| 287 | MRootFileBranch *entry = new MRootFileBranch(cont, tname, must);
|
|---|
| 288 | fBranches.AddLast(entry);
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | // --------------------------------------------------------------------------
|
|---|
| 292 | //
|
|---|
| 293 | // Add a new Container to list of containers which should be written to the
|
|---|
| 294 | // file. Give the pointer to the container. tname is the name of the tree to
|
|---|
| 295 | // which the container should be written (Remark: one tree can hold more than
|
|---|
| 296 | // one container). The default is the same name as the container name.
|
|---|
| 297 | // You can slso specify a title for the tree. This is only
|
|---|
| 298 | // used the first time this tree in 'mentioned'. As default the title
|
|---|
| 299 | // is the name of the tree.
|
|---|
| 300 | //
|
|---|
| 301 | Bool_t MWriteRootFile::GetContainer(MParList *pList)
|
|---|
| 302 | {
|
|---|
| 303 | //
|
|---|
| 304 | // loop over all branches which are 'marked' as branches to get written.
|
|---|
| 305 | //
|
|---|
| 306 | MRootFileBranch *entry;
|
|---|
| 307 |
|
|---|
| 308 | TIter Next(&fBranches);
|
|---|
| 309 | while ((entry=(MRootFileBranch*)Next()))
|
|---|
| 310 | {
|
|---|
| 311 | //
|
|---|
| 312 | // Get the pointer to the container. If the pointer is NULL it seems,
|
|---|
| 313 | // that the user identified the container by name.
|
|---|
| 314 | //
|
|---|
| 315 | MParContainer *cont = entry->GetContainer();
|
|---|
| 316 | if (!cont)
|
|---|
| 317 | {
|
|---|
| 318 | //
|
|---|
| 319 | // Get the name and try to find a container with this name
|
|---|
| 320 | // in the parameter list.
|
|---|
| 321 | //
|
|---|
| 322 | const char *cname = entry->GetContName();
|
|---|
| 323 | cont = (MParContainer*)pList->FindObject(cname);
|
|---|
| 324 | if (!cont)
|
|---|
| 325 | {
|
|---|
| 326 | //
|
|---|
| 327 | // No corresponding container is found
|
|---|
| 328 | //
|
|---|
| 329 | if (entry->MustHave())
|
|---|
| 330 | {
|
|---|
| 331 | *fLog << err << "Cannot find parameter container '" << cname << "'." << endl;
|
|---|
| 332 | return kFALSE;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | *fLog << inf << "Unnecessary parameter container '" << cname << "' not found..." << endl;
|
|---|
| 336 | delete fBranches.Remove(entry);
|
|---|
| 337 | continue;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | //
|
|---|
| 341 | // The container is found. Put the pointer into the entry.
|
|---|
| 342 | //
|
|---|
| 343 | entry->SetContainer(cont);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | //
|
|---|
| 347 | // Get container name, tree name and tree title of this entry.
|
|---|
| 348 | //
|
|---|
| 349 | const char *cname = cont->GetName();
|
|---|
| 350 | const char *tname = entry->GetName();
|
|---|
| 351 | const TString ttitle(Form("Tree containing %s", cont->GetDescriptor()));
|
|---|
| 352 |
|
|---|
| 353 | //
|
|---|
| 354 | // if the tree name is NULL this idetifies it to use the default:
|
|---|
| 355 | // the container name.
|
|---|
| 356 | //
|
|---|
| 357 | if (tname[0] == '\0')
|
|---|
| 358 | tname = cname;
|
|---|
| 359 |
|
|---|
| 360 | //
|
|---|
| 361 | // Check if the tree is already existing (part of the file)
|
|---|
| 362 | //
|
|---|
| 363 | TTree *tree = (TTree*)fOut->Get(tname);
|
|---|
| 364 | if (!tree)
|
|---|
| 365 | {
|
|---|
| 366 | //
|
|---|
| 367 | // if the tree doesn't exist create a new tree. Use the tree
|
|---|
| 368 | // name as title if title is NULL.
|
|---|
| 369 | // And add the tree to the list of trees
|
|---|
| 370 | //
|
|---|
| 371 | TDirectory *save = gDirectory;
|
|---|
| 372 | fOut->cd();
|
|---|
| 373 |
|
|---|
| 374 | tree = new TTree(tname, ttitle);
|
|---|
| 375 | fTrees.AddLast(tree);
|
|---|
| 376 |
|
|---|
| 377 | //
|
|---|
| 378 | // If the tree does not already exist in the file mark this
|
|---|
| 379 | // tree as a branch created by MWriteRootFile
|
|---|
| 380 | //
|
|---|
| 381 | tree->SetBit(kIsNewTree);
|
|---|
| 382 |
|
|---|
| 383 | gDirectory = save;
|
|---|
| 384 |
|
|---|
| 385 | *fLog << inf << "Tree " << tname << " created." << endl;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | //
|
|---|
| 389 | // In case the file is opened as 'UPDATE' the tree may still not
|
|---|
| 390 | // be in the list. Because it neither was created previously,
|
|---|
| 391 | // nor this time, so the corresponding entries is marked as a
|
|---|
| 392 | // single branch to be filled. --> Add it to the list of trees.
|
|---|
| 393 | //
|
|---|
| 394 | if (!fTrees.FindObject(tree))
|
|---|
| 395 | fTrees.AddLast(tree);
|
|---|
| 396 |
|
|---|
| 397 | //
|
|---|
| 398 | // Now we have a valid tree. Search the list of trees for this tree
|
|---|
| 399 | // Add a pointer to the entry in the tree list to this branch-entry
|
|---|
| 400 | //
|
|---|
| 401 | entry->SetTree(tree);
|
|---|
| 402 |
|
|---|
| 403 | TString branchname(cname);
|
|---|
| 404 | branchname.Append(".");
|
|---|
| 405 |
|
|---|
| 406 | //
|
|---|
| 407 | // Try to get the branch from the file.
|
|---|
| 408 | // If the branch already exists the user specified one branch twice.
|
|---|
| 409 | //
|
|---|
| 410 | TBranch *branch = tree->GetBranch(branchname);
|
|---|
| 411 | if (branch)
|
|---|
| 412 | {
|
|---|
| 413 | *fLog << inf << "Branch '" << cname << "' already existing... updating." << endl;
|
|---|
| 414 | branch->SetAddress(entry->GetAddress());
|
|---|
| 415 |
|
|---|
| 416 | if (!fSplitRule.IsNull())
|
|---|
| 417 | {
|
|---|
| 418 | *fLog << warn << endl;
|
|---|
| 419 | *fLog << "WARNING: You are updating an existing branch. For this" << endl;
|
|---|
| 420 | *fLog << " case file-splitting mode is not allowed... disabled!" << endl;
|
|---|
| 421 | *fLog << endl;
|
|---|
| 422 | fSplitRule = "";
|
|---|
| 423 | }
|
|---|
| 424 | }
|
|---|
| 425 | else
|
|---|
| 426 | {
|
|---|
| 427 | //
|
|---|
| 428 | // Create a new branch in the actual tree. The branch has the name
|
|---|
| 429 | // container name. The type of the container is given by the
|
|---|
| 430 | // ClassName entry in the container. The Address is the address of a
|
|---|
| 431 | // pointer to the container (gotten from the branch entry). As
|
|---|
| 432 | // Basket size we specify a (more or less) common default value.
|
|---|
| 433 | // The containers should be written in Splitlevel=1
|
|---|
| 434 | //
|
|---|
| 435 | *fLog << inf << "Creating Branch " << cname << " of " << cont->ClassName();
|
|---|
| 436 | *fLog << " in tree " << tree->GetName() << "... " << flush;
|
|---|
| 437 |
|
|---|
| 438 | branch = tree->Branch(branchname, cont->ClassName(), entry->GetAddress());
|
|---|
| 439 |
|
|---|
| 440 | //
|
|---|
| 441 | // If the branch couldn't be created we have a problem.
|
|---|
| 442 | //
|
|---|
| 443 | if (!branch)
|
|---|
| 444 | {
|
|---|
| 445 | *fLog << endl;
|
|---|
| 446 | *fLog << err << "Unable to create branch '" << cname << "'." << endl;
|
|---|
| 447 | return kFALSE;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | *fLog << "done." << endl;
|
|---|
| 451 |
|
|---|
| 452 | if (!tree->TestBit(kIsNewTree) && !fSplitRule.IsNull())
|
|---|
| 453 | {
|
|---|
| 454 | *fLog << warn << endl;
|
|---|
| 455 | *fLog << "WARNING: You have created a new branch in an existing tree." << endl;
|
|---|
| 456 | *fLog << " For this case file-splitting mode is not allowed... disabled!" << endl;
|
|---|
| 457 | *fLog << endl;
|
|---|
| 458 | fSplitRule= "";
|
|---|
| 459 | }
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | //
|
|---|
| 463 | // Tell the entry also which branch belongs to it (this is necessary
|
|---|
| 464 | // for branches belonging to already existing tree, UPDATE-mode)
|
|---|
| 465 | //
|
|---|
| 466 | entry->SetBranch(branch);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | return kTRUE;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | // --------------------------------------------------------------------------
|
|---|
| 473 | //
|
|---|
| 474 | // Checks all given containers (branch entries) for the write flag.
|
|---|
| 475 | // If the write flag is set the corresponding Tree is marked to get filled.
|
|---|
| 476 | // All Trees which are marked to be filled are filled with all their
|
|---|
| 477 | // branches.
|
|---|
| 478 | // In case of a file opened in 'UPDATE' mode, single branches can be
|
|---|
| 479 | // filled, too. WARNING - for the moment there is no check whether
|
|---|
| 480 | // you filled the correct number of events into the branch, so that
|
|---|
| 481 | // each of the other branches in the tree has the correct corresponding
|
|---|
| 482 | // number of new entries in the new branch!
|
|---|
| 483 | // Be carefull: If only one container (corresponding to a branch) of a tree
|
|---|
| 484 | // has the write flag, all containers in this tree are filled!
|
|---|
| 485 | //
|
|---|
| 486 | Bool_t MWriteRootFile::CheckAndWrite()
|
|---|
| 487 | {
|
|---|
| 488 | TObject *obj;
|
|---|
| 489 |
|
|---|
| 490 | //
|
|---|
| 491 | // Loop over all branch entries
|
|---|
| 492 | //
|
|---|
| 493 | TIter NextBranch(&fBranches);
|
|---|
| 494 | while ((obj=NextBranch()))
|
|---|
| 495 | {
|
|---|
| 496 | MRootFileBranch *b = (MRootFileBranch*)obj;
|
|---|
| 497 |
|
|---|
| 498 | //
|
|---|
| 499 | // Check for the Write flag
|
|---|
| 500 | //
|
|---|
| 501 | if (!b->GetContainer()->IsReadyToSave())
|
|---|
| 502 | continue;
|
|---|
| 503 |
|
|---|
| 504 | //
|
|---|
| 505 | // If the write flag of the branch entry is set, set the write flag of
|
|---|
| 506 | // the corresponding tree entry.
|
|---|
| 507 | //
|
|---|
| 508 | if (b->GetTree()->TestBit(kIsNewTree))
|
|---|
| 509 | b->GetTree()->SetBit(kFillTree);
|
|---|
| 510 | else
|
|---|
| 511 | {
|
|---|
| 512 | if (!b->GetBranch()->Fill())
|
|---|
| 513 | {
|
|---|
| 514 | *fLog << err << "ERROR - Zero bytes written to branch '" << b->GetBranch()->GetName() << "'... abort." << endl;
|
|---|
| 515 | return kFALSE;
|
|---|
| 516 | }
|
|---|
| 517 | }
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | //
|
|---|
| 521 | // Loop over all tree entries
|
|---|
| 522 | //
|
|---|
| 523 | const Int_t n = fTrees.GetEntriesFast();
|
|---|
| 524 |
|
|---|
| 525 | for (int idx=0; idx<n; idx++)
|
|---|
| 526 | {
|
|---|
| 527 | TTree *t = (TTree*)fTrees[idx];
|
|---|
| 528 |
|
|---|
| 529 | //
|
|---|
| 530 | // Check the write flag of the tree
|
|---|
| 531 | //
|
|---|
| 532 | if (!t->TestBit(kFillTree))
|
|---|
| 533 | continue;
|
|---|
| 534 |
|
|---|
| 535 | //
|
|---|
| 536 | // If the write flag is set, fill the tree (with the corresponding
|
|---|
| 537 | // branches/containers), delete the write flag and increase the number
|
|---|
| 538 | // of written/filled entries.
|
|---|
| 539 | //
|
|---|
| 540 | t->ResetBit(kFillTree);
|
|---|
| 541 |
|
|---|
| 542 | if (!t->Fill())
|
|---|
| 543 | {
|
|---|
| 544 | *fLog << err << "ERROR - Zero bytes written to tree '" << t->GetName() << "'... abort." << endl;
|
|---|
| 545 | return kFALSE;
|
|---|
| 546 | }
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | //
|
|---|
| 550 | // For more information see TTree:ChangeFile()
|
|---|
| 551 | //
|
|---|
| 552 | TTree *t0 = (TTree*)fTrees[0];
|
|---|
| 553 | if (!t0 || fOut==t0->GetCurrentFile())
|
|---|
| 554 | return kTRUE;
|
|---|
| 555 |
|
|---|
| 556 | *fLog << warn << endl;
|
|---|
| 557 | *fLog << "WARNING - MWriteRootFile: Root's TTree/TFile has opened a new file" << endl;
|
|---|
| 558 | *fLog << " automatically. You can change this behaviour using TTree::SetMaxTreeSize." << endl;
|
|---|
| 559 | *fLog << " You won't be able to read splitted files correctly with MReadMarsFile if" << endl;
|
|---|
| 560 | *fLog << " they have more than one entry in 'RunHeaders' or you try to read more than" << endl;
|
|---|
| 561 | *fLog << " one of such sequences at once." << endl;
|
|---|
| 562 | *fLog << endl;
|
|---|
| 563 |
|
|---|
| 564 | return kTRUE;
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | // --------------------------------------------------------------------------
|
|---|
| 568 | //
|
|---|
| 569 | // Open a new file with the ame fname. Move all trees and branches from the
|
|---|
| 570 | // old file to the new file.
|
|---|
| 571 | //
|
|---|
| 572 | Bool_t MWriteRootFile::ChangeFile(const char *fname)
|
|---|
| 573 | {
|
|---|
| 574 | //
|
|---|
| 575 | // The following code is more or less a copy of TTree::ChangeFile
|
|---|
| 576 | //
|
|---|
| 577 | const Int_t compr = fOut->GetCompressionLevel();
|
|---|
| 578 | const TString title = fOut->GetTitle();
|
|---|
| 579 |
|
|---|
| 580 | *fLog << inf << "MWriteRootFile - Open new file " << fname << " (Title=" << title << ", Compression=" << compr << ")" << endl;
|
|---|
| 581 |
|
|---|
| 582 | // Open new file with old setup
|
|---|
| 583 | TFile *newfile = TFile::Open(fname, "RECREATE", title, compr);
|
|---|
| 584 | if (!newfile)
|
|---|
| 585 | {
|
|---|
| 586 | *fLog << err << "ERROR - Cannot open new file " << fname << endl;
|
|---|
| 587 | return kFALSE;
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | // Print statistics of old file
|
|---|
| 591 | const TString n = GetFileName();
|
|---|
| 592 | if (!n.IsNull() && n!=TString("/dev/null"))
|
|---|
| 593 | Print();
|
|---|
| 594 |
|
|---|
| 595 | if (fOut->IsOpen())
|
|---|
| 596 | fOut->Write();
|
|---|
| 597 |
|
|---|
| 598 | // Move all trees from the old file to the new file
|
|---|
| 599 | TObject *obj=0;
|
|---|
| 600 | while ((obj = fOut->GetList()->First()))
|
|---|
| 601 | {
|
|---|
| 602 | // Remove obj from old file (otherwise deleting
|
|---|
| 603 | // the old file will delete the objs)
|
|---|
| 604 | fOut->GetList()->Remove(obj);
|
|---|
| 605 |
|
|---|
| 606 | // If this is not a tree do nothing.
|
|---|
| 607 | if (!obj->InheritsFrom("TTree"))
|
|---|
| 608 | continue;
|
|---|
| 609 |
|
|---|
| 610 | // process all trees in the old file
|
|---|
| 611 | TTree *t = (TTree*)obj;
|
|---|
| 612 |
|
|---|
| 613 | // reset and move to new file (this is done implicitly for all branches)
|
|---|
| 614 | t->Reset();
|
|---|
| 615 | t->SetDirectory(newfile);
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | // Close/delete the old file (keys already written above)
|
|---|
| 619 | delete fOut;
|
|---|
| 620 |
|
|---|
| 621 | // Replace current with new file
|
|---|
| 622 | fOut = newfile;
|
|---|
| 623 |
|
|---|
| 624 | // Change current directory to new file
|
|---|
| 625 | gFile = fOut;
|
|---|
| 626 |
|
|---|
| 627 | return kTRUE;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | // --------------------------------------------------------------------------
|
|---|
| 631 | //
|
|---|
| 632 | // A rule looks like:
|
|---|
| 633 | // "outputpath{s/source/destination}"
|
|---|
| 634 | //
|
|---|
| 635 | // outpath: the output path into which the files are written
|
|---|
| 636 | // {s/source/destination} a substitution rule for the filename
|
|---|
| 637 | // while source can be a regular expression everything which matches this
|
|---|
| 638 | // regular expression (see TRegexp) will be replaced by destination.
|
|---|
| 639 | // Warning: The algorithm is recursive you may create endless loops!
|
|---|
| 640 | //
|
|---|
| 641 | // Example:
|
|---|
| 642 | // inputfile: /data/MAGIC/Period016/rootdata/20040621_23210_D_Mkn421_E.root
|
|---|
| 643 | // rule: /outpath/{s/_D_/_Y_}
|
|---|
| 644 | // outfile: /outpath/20040621_23210_Y_Mkn421_E.root
|
|---|
| 645 | //
|
|---|
| 646 | // If you need more difficult rules please send me an eMail...
|
|---|
| 647 | //
|
|---|
| 648 | TString MWriteRootFile::GetNewFileName(const char *inname) const
|
|---|
| 649 | {
|
|---|
| 650 | // Remove the path from the filename
|
|---|
| 651 | TString fname(inname);
|
|---|
| 652 | if (fname.Last('/')>=0)
|
|---|
| 653 | fname.Remove(0, fname.Last('/')+1);
|
|---|
| 654 |
|
|---|
| 655 | // Make a copy of the rule
|
|---|
| 656 | TString rule(fSplitRule);
|
|---|
| 657 |
|
|---|
| 658 | // [characte class], ^ do not, [^{}] do not match { and }, [^{}]+ match at least not one { or }
|
|---|
| 659 | const TRegexp subst0("{s/[^{}/]+/[^{}/]+}");
|
|---|
| 660 |
|
|---|
| 661 | TString path;
|
|---|
| 662 | Bool_t first = kTRUE;
|
|---|
| 663 |
|
|---|
| 664 | Ssiz_t idx=0;
|
|---|
| 665 | while (1)
|
|---|
| 666 | {
|
|---|
| 667 | // Find a substitution exprsssion
|
|---|
| 668 | Ssiz_t len = 0;
|
|---|
| 669 | idx = subst0.Index(rule, &len);
|
|---|
| 670 | if (idx<0)
|
|---|
| 671 | break;
|
|---|
| 672 |
|
|---|
| 673 | // If the first substitution expression is found in the rule
|
|---|
| 674 | // determin the path from everything before
|
|---|
| 675 | if (first)
|
|---|
| 676 | {
|
|---|
| 677 | path=rule(0, idx);
|
|---|
| 678 | first = kFALSE;
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | // Extract a substitution expression
|
|---|
| 682 | TString expr = rule(idx, len);
|
|---|
| 683 | rule.Remove(idx, len);
|
|---|
| 684 |
|
|---|
| 685 | expr.Remove(0,3);
|
|---|
| 686 | expr.Remove(expr.Length()-1);
|
|---|
| 687 |
|
|---|
| 688 | // In all cases this is well defined (see Regexp)
|
|---|
| 689 | const Ssiz_t pos = expr.First('/');
|
|---|
| 690 |
|
|---|
| 691 | // Split substitution rule into source and destination
|
|---|
| 692 | const TString src = expr(0, pos);
|
|---|
| 693 | const TString dest = expr(pos+1, expr.Length());
|
|---|
| 694 |
|
|---|
| 695 | // Replace source by destination
|
|---|
| 696 | const TRegexp regexp(src);
|
|---|
| 697 | while (1)
|
|---|
| 698 | {
|
|---|
| 699 | TString sub = fname(idx+dest.Length());
|
|---|
| 700 | idx = regexp.Index(fname, &len);
|
|---|
| 701 | if (idx<0)
|
|---|
| 702 | break;
|
|---|
| 703 |
|
|---|
| 704 | fname.Replace(idx, len, dest);
|
|---|
| 705 | }
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | // Check if we have a trailing '/'
|
|---|
| 709 | if (!path.IsNull() && path[path.Length()-1]!='/')
|
|---|
| 710 | path.Append("/");
|
|---|
| 711 |
|
|---|
| 712 | // Create full qualified pathname
|
|---|
| 713 | path += fname;
|
|---|
| 714 | return path;
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | // --------------------------------------------------------------------------
|
|---|
| 718 | //
|
|---|
| 719 | // ReInit. If file splitting is not allowed call MWriteFile::ReInit.
|
|---|
| 720 | //
|
|---|
| 721 | // In other cases get MRead from the TaskList (splitting is switched of if
|
|---|
| 722 | // this is impossible).
|
|---|
| 723 | //
|
|---|
| 724 | // Convert the input- into a new output file-name.
|
|---|
| 725 | //
|
|---|
| 726 | // Open a new file, change all trees to the new file (calling ChangeFile()),
|
|---|
| 727 | // and close the old one.
|
|---|
| 728 | //
|
|---|
| 729 | // Call MWriteFile::ReInit()
|
|---|
| 730 | //
|
|---|
| 731 | Bool_t MWriteRootFile::ReInit(MParList *pList)
|
|---|
| 732 | {
|
|---|
| 733 | if (fSplitRule.IsNull())
|
|---|
| 734 | return MWriteFile::ReInit(pList);
|
|---|
| 735 |
|
|---|
| 736 | MRead *read = (MRead*)pList->FindTask("MRead");
|
|---|
| 737 | if (!read)
|
|---|
| 738 | {
|
|---|
| 739 | *fLog << warn;
|
|---|
| 740 | *fLog << "WARNING: No Task 'MRead' found in the tasklist. This task is" << endl;
|
|---|
| 741 | *fLog << " necessary to get the filename. Without a filename file" << endl;
|
|---|
| 742 | *fLog << " file splitting is not allowed... disabled!" << endl;
|
|---|
| 743 | *fLog << endl;
|
|---|
| 744 | fSplitRule = "";
|
|---|
| 745 | return kTRUE;
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | const TString fname = GetNewFileName(read->GetFileName());
|
|---|
| 749 | if (!ChangeFile(fname))
|
|---|
| 750 | return kFALSE;
|
|---|
| 751 |
|
|---|
| 752 | return MWriteFile::ReInit(pList);
|
|---|
| 753 | }
|
|---|
| 754 |
|
|---|
| 755 | // --------------------------------------------------------------------------
|
|---|
| 756 | //
|
|---|
| 757 | // return open state of the root file.
|
|---|
| 758 | //
|
|---|
| 759 | Bool_t MWriteRootFile::IsFileOpen() const
|
|---|
| 760 | {
|
|---|
| 761 | const char *n = fOut->GetName();
|
|---|
| 762 | return n==0 || *n==0 ? kTRUE : fOut->IsOpen();
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | // --------------------------------------------------------------------------
|
|---|
| 766 | //
|
|---|
| 767 | // return name of the root-file
|
|---|
| 768 | //
|
|---|
| 769 | const char *MWriteRootFile::GetFileName() const
|
|---|
| 770 | {
|
|---|
| 771 | const char *n = fOut->GetName();
|
|---|
| 772 | return n==0 || *n==0 ? "<dummy>" : n;
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | // --------------------------------------------------------------------------
|
|---|
| 776 | //
|
|---|
| 777 | // cd into file. See TFile::cd()
|
|---|
| 778 | //
|
|---|
| 779 | Bool_t MWriteRootFile::cd(const char *path)
|
|---|
| 780 | {
|
|---|
| 781 | return fOut->cd(path);
|
|---|
| 782 | }
|
|---|
| 783 |
|
|---|
| 784 | // --------------------------------------------------------------------------
|
|---|
| 785 | //
|
|---|
| 786 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
|---|
| 787 | // to a macro. In the original root implementation it is used to write
|
|---|
| 788 | // gui elements to a macro-file.
|
|---|
| 789 | //
|
|---|
| 790 | void MWriteRootFile::StreamPrimitive(ofstream &out) const
|
|---|
| 791 | {
|
|---|
| 792 | out << " MWriteRootFile " << GetUniqueName() << "(\"";
|
|---|
| 793 | out << fOut->GetName() << "\", \"";
|
|---|
| 794 | out << fOut->GetOption() << "\", \"";
|
|---|
| 795 | out << fOut->GetTitle() << "\", ";
|
|---|
| 796 | out << fOut->GetCompressionLevel();
|
|---|
| 797 |
|
|---|
| 798 | if (fName!=gsDefName || fTitle!=gsDefTitle)
|
|---|
| 799 | {
|
|---|
| 800 | out << ", \"" << fName << "\"";
|
|---|
| 801 | if (fTitle!=gsDefTitle)
|
|---|
| 802 | out << ", \"" << fTitle << "\"";
|
|---|
| 803 | }
|
|---|
| 804 | out << ");" << endl;
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 | MRootFileBranch *entry;
|
|---|
| 808 | TIter Next(&fBranches);
|
|---|
| 809 | while ((entry=(MRootFileBranch*)Next()))
|
|---|
| 810 | {
|
|---|
| 811 | out << " " << GetUniqueName() << ".AddContainer(";
|
|---|
| 812 |
|
|---|
| 813 | if (entry->GetContainer())
|
|---|
| 814 | {
|
|---|
| 815 | entry->GetContainer()->SavePrimitive(out);
|
|---|
| 816 | out << "&" << entry->GetContainer()->GetUniqueName();
|
|---|
| 817 | }
|
|---|
| 818 | else
|
|---|
| 819 | out << "\"" << entry->GetContName() << "\"";
|
|---|
| 820 |
|
|---|
| 821 | out << ", \"" << entry->GetName() << "\"";
|
|---|
| 822 | if (!entry->MustHave())
|
|---|
| 823 | out << ", kFALSE";
|
|---|
| 824 |
|
|---|
| 825 | out <<");" << endl;
|
|---|
| 826 | }
|
|---|
| 827 | }
|
|---|