Changeset 6499


Ignore:
Timestamp:
02/15/05 18:38:11 (20 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/manalysis/MMatrixLoop.cc

    r5807 r6499  
    6464//
    6565// Return name of MHMatrix, <n/a> if not available
     66// The name of the matrix must nor contain a '/'
    6667//
    67 TString MMatrixLoop::GetFileName() const
     68TString MMatrixLoop::GetFullFileName() const
    6869{
    6970    return fMatrix ? fMatrix->GetName() : "<n/a>";
  • trunk/MagicSoft/Mars/manalysis/MMatrixLoop.h

    r5803 r6499  
    2020    // MRead
    2121    UInt_t  GetEntries();
    22     TString GetFileName() const;
     22    TString GetFullFileName() const;
    2323    Bool_t  Rewind() { fNumRow=0; return kTRUE; }
    2424
  • trunk/MagicSoft/Mars/mbase/MInputStreamID.h

    r1014 r6499  
    2424    MInputStreamID(const char *name=NULL, const char *title=NULL);
    2525
     26    Bool_t HasStreamId() const { return fStreamId.CompareTo("all", TString::kIgnoreCase)!=0; }
     27
    2628    const TString &GetStreamId() const { return fStreamId; }
    2729    void SetStreamId(const char *t)    { fStreamId = t; }
  • trunk/MagicSoft/Mars/mfileio/MRead.cc

    r5160 r6499  
    5252    *fLog << err << "ERROR - Rewind() not implemented for " << GetDescriptor() << endl;
    5353    return kFALSE;
     54}
     55
     56// --------------------------------------------------------------------------
     57//
     58//  Return the name of the file we are actually reading from.
     59//
     60TString MRead::GetFileName() const
     61{
     62    TString name(GetFullFileName());
     63    if (name.IsNull())
     64        return "<n/a>";
     65    name.Remove(0, name.Last('/')+1);
     66    return name;
    5467}
    5568
  • trunk/MagicSoft/Mars/mfileio/MRead.h

    r5160 r6499  
    1818
    1919    virtual UInt_t  GetEntries() = 0;
    20     virtual TString GetFileName() const = 0;
     20    virtual TString GetFileName() const;
     21    virtual TString GetFullFileName() const = 0;
    2122    virtual Bool_t  Rewind();
    2223
  • trunk/MagicSoft/Mars/mfileio/MReadReports.cc

    r6373 r6499  
    134134// If no master is available "<MReadReports>" is returned.
    135135//
    136 TString MReadReports::GetFileName() const
     136TString MReadReports::GetFullFileName() const
    137137{
    138138    if (!TestBit(kHasMaster))
  • trunk/MagicSoft/Mars/mfileio/MReadReports.h

    r5143 r6499  
    3030
    3131    UInt_t  GetEntries();
    32     TString GetFileName() const;
     32    TString GetFullFileName() const;
    3333
    3434    Int_t   PreProcess(MParList *plist);
  • trunk/MagicSoft/Mars/mfileio/MReadRflFile.h

    r4898 r6499  
    5353    Bool_t Rewind() { fNumFile=0; return kTRUE; }
    5454    UInt_t GetEntries() { return fEntries; }
    55     TString GetFileName() const { return fFileName; }
     55    TString GetFullFileName() const { return fFileName; }
    5656
    5757    Bool_t SearchFor(Int_t runno, Int_t eventno);
  • trunk/MagicSoft/Mars/mfileio/MReadTree.cc

    r6085 r6499  
    11201120//  Return the name of the file we are actually reading from.
    11211121//
    1122 TString MReadTree::GetFileName() const
     1122TString MReadTree::GetFullFileName() const
    11231123{
    11241124    const TFile *file = fChain ? fChain->GetFile() : fTree->GetCurrentFile();
    11251125
    11261126    if (!file)
    1127         return TString("<unknown>");
    1128 
    1129     TString name(file->GetName());
    1130     name.Remove(0, name.Last('/')+1);
    1131     return name;
     1127        return "<unknown>";
     1128
     1129    return file->GetName();
    11321130}
    11331131
  • trunk/MagicSoft/Mars/mfileio/MReadTree.h

    r5692 r6499  
    7070    UInt_t GetEntries();
    7171
    72     TString GetFileName() const;
     72    //TString GetFileName() const;
     73    TString GetFullFileName() const;
    7374    Int_t   GetFileIndex() const;
    7475
     
    9293
    9394#endif
     95
  • trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc

    r6459 r6499  
    4343// constructor or specify 'memory' as option in the constructor.
    4444//
    45 // Afterwards the tree can be found using gROOT->FindObject("treename")
     45// Afterwards the tree can be found using
     46// gROOT->GetListOfFiles()->FindObject("treename")
    4647//
    4748// Currently(!) the tree is not deleted at all! Please make sure to
     
    9091    //
    9192    fBranches.SetOwner();
     93    fCopies.SetOwner();
    9294
    9395    //
     
    100102    gROOT->GetListOfCleanups()->Add(this); // To remove fDisplay
    101103    SetBit(kMustCleanup);
     104}
     105
     106// --------------------------------------------------------------------------
     107//
     108// Try to get the file from gROOT->GetListOfFiles.
     109// If it is found fOut is set to it and returned.
     110// Otherwise a new file is opened and returned.
     111//
     112TFile *MWriteRootFile::OpenFile(const char *name, Option_t *option, const char *title, Int_t comp)
     113{
     114    TFile *file = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(name));
     115    if (!file)
     116    {
     117        file = new TFile(name, option, title, comp);
     118        file->SetOption(option); // IMPORTANT!
     119        ResetBit(kIsNotOwner);
     120        return file;
     121    }
     122
     123    fOut = file;
     124    fOut->SetBit(kMustCleanup);
     125    SetBit(kIsNotOwner);
     126
     127    *fLog << inf;
     128    *fLog << "File '" << name << "' already open... using." << endl;
     129    *fLog << "Make sure that you do NOT write to trees which are" << endl;
     130    *fLog << "scheduled already by a different MWriteRootFile..." << endl;
     131    return fOut;
    102132}
    103133
     
    139169    // a valid file. (Stupid workaround - but does a good job)
    140170    //
    141     fOut = new TFile("/dev/null", option, ftitle, comp);
    142     fOut->SetOption(option); // IMPORTANT!
     171    fOut = OpenFile("/dev/null", option, ftitle, comp);
    143172}
    144173
     
    192221    // Open the rootfile
    193222    //
    194     TObject *find = gROOT->FindObject(str);
    195     if (find && find->InheritsFrom(TFile::Class()))
    196     {
    197         fOut = (TFile*)find;
    198         fOut->SetBit(kMustCleanup);
    199         SetBit(kIsNotOwner);
    200 
    201         *fLog << inf << "File '" << fname << "' already open... using." << endl;
    202         *fLog << "Make sure that you do NOT write to trees which are" << endl;
    203         *fLog << "scheduled already by a different MWriteRootFile..." << endl;
    204     }
    205     else
    206         fOut = new TFile(str, opt, ftitle, comp);
     223    fOut = OpenFile(str, opt, ftitle, comp);
    207224}
    208225
     
    217234    // Print some statistics to the looging out.
    218235    //
    219     Print();
    220 
    221236    if (fOut && !TestBit(kIsNotOwner))
    222237    {
     238        Print();
     239
    223240        //
    224241        // If the file is still open (no error) write the keys. This is necessary
     
    267284    if (fTrees.GetEntries()==0)
    268285    {
    269         *fLog << " No contents." << endl;
     286        *fLog << "  No contents." << endl;
    270287        return;
    271288    }
     
    295312            *fLog << " + " << t->GetName() << ": \t" << (ULong_t)t->GetEntries() << " entries." << endl;
    296313
    297     TIter NextKey(fOut->GetListOfKeys());
     314    TIter NextKey(fOut->GetList());
    298315    while ((obj=NextKey()))
    299316    {
     
    304321            continue;
    305322
    306         *fLog << " - " << obj->GetName() << ": \t" << (ULong_t)((TTree*)t)->GetEntries() << " entries." << endl;
     323        *fLog << " - " << obj->GetName() << ": \t" << (ULong_t)((TTree*)obj)->GetEntries() << " entries." << endl;
    307324    }
    308325    *fLog << endl;
    309 
    310326}
    311327
     
    357373// is the name of the tree.
    358374//
    359 void MWriteRootFile::AddContainer(MParContainer *cont, const char *tname,
    360                                   Bool_t must)
     375void MWriteRootFile::AddContainer(MParContainer *cont, const char *tname, Bool_t must)
    361376{
    362377    if (!fOut && !tname)
     
    380395    MRootFileBranch *entry = new MRootFileBranch(cont, tname, must);
    381396    fBranches.AddLast(entry);
     397}
     398
     399// --------------------------------------------------------------------------
     400//
     401// If you want to copy a full tree (or some branches of some trees)
     402// completely from one file to another one you can use this
     403//
     404void MWriteRootFile::AddCopySource(const char *tname, const char *bname)
     405{
     406    fCopies.Add(new TNamed(tname, bname?bname:"*"));
     407    fCopies.Sort();
    382408}
    383409
     
    497523            tree->SetBit(kIsNewTree);
    498524
     525            *fLog << inf << "Tree " << tname << " created in " << gDirectory->GetName() << endl;
     526
    499527            gDirectory = save;
    500 
    501             *fLog << inf << "Tree " << tname << " created." << endl;
    502528        }
    503529
     
    666692    // If we are writing into memory we don't split into seperate files
    667693    //
    668     if (!fOut)
     694    if (!fOut || TestBit(kIsNotOwner))
    669695        return kTRUE;
    670696
     
    694720Bool_t MWriteRootFile::ChangeFile(const char *fname)
    695721{
    696     if (!fOut)
    697     {
    698         TObject *find = gROOT->FindObject(fname);
    699         if (find && find->InheritsFrom(TFile::Class()))
    700         {
    701             fOut = (TFile*)find;
    702             SetBit(kIsNotOwner);
    703             return kTRUE;
    704         }
    705 
    706         *fLog << err << "MWriteRootFile::ChangeFile: Expecting file '" << fname;
    707         *fLog << "' in gROOT->FindObject... not found." << endl;
    708         return kFALSE;
    709     }
    710 
    711     //
    712     // The following code is more or less a copy of TTree::ChangeFile
    713     //
    714     const Int_t   compr = fOut->GetCompressionLevel();
    715     const TString title = fOut->GetTitle();
    716     const TString opt   = fOut->GetOption();
    717 
    718     *fLog << inf << "MWriteRootFile - Open new file " << fname << " (Title=" << title << ", Option=" << opt << ", Compression=" << compr << ")" << endl;
     722    const Int_t   compr = fOut ? fOut->GetCompressionLevel() : 0;
     723    const TString title = fOut ? fOut->GetTitle()            : "";
     724    const TString opt   = fOut ? fOut->GetOption()           : "";
    719725
    720726    // Open new file with old setup
    721     TFile *newfile = TFile::Open(fname, opt, title, compr);
     727    TFile *newfile = OpenFile(fname, opt, title, compr);
     728    if (newfile && newfile==fOut)
     729    {
     730        *fLog << inf << "Found open file " << fname << "... using." << endl;
     731        return kTRUE;
     732    }
     733
    722734    if (!newfile)
    723735    {
    724736        *fLog << err << "ERROR - Cannot open new file " << fname << endl;
    725         return kFALSE;
    726     }
     737         return kFALSE;
     738    }
     739
     740    *fLog << inf << "Open new file " << fname << " (Title=" << title << ", Option=" << opt << ", Compression=" << compr << ")" << endl;
    727741
    728742    // Print statistics of old file
     
    743757
    744758        // If this is not a tree do nothing.
    745         if (!obj->InheritsFrom("TTree"))
     759        if (!obj->InheritsFrom(TTree::Class()))
    746760            continue;
    747761
     
    784798// If you need more difficult rules please send me an eMail...
    785799//
    786 TString MWriteRootFile::GetNewFileName(const char *inname) const
     800TString MWriteRootFile::GetNewFileName(TString &inname) const
    787801{
    788802    // Remove the path from the filename
     
    847861        path.Append("/");
    848862
     863    inname.Prepend(path);
     864
    849865    // Create full qualified pathname
    850866    path += fname;
     
    854870// --------------------------------------------------------------------------
    855871//
     872// Writes a copy of the TTree t to the currently open file using
     873// TTree::CloneTree()
     874//
     875void MWriteRootFile::CopyTree(TTree &t)
     876{
     877    *fLog << "Copy of tree " << t.GetName() << " in progress..." << flush;
     878
     879    TTree *clone=t.CloneTree();
     880    clone->Write();
     881    delete clone;
     882
     883    *fLog << "done." << endl;
     884}
     885
     886// --------------------------------------------------------------------------
     887//
     888// Make all copies requested from the currently open file into the new
     889// file.
     890//
     891Bool_t MWriteRootFile::MakeCopies(const char *fname) const
     892{
     893    if (fCopies.GetEntries()==0)
     894        return kTRUE;
     895
     896    TFile *file = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(fname));
     897    if (!file)
     898    {
     899        *fLog << err << "ERROR - MakeCopies: File " << fname << " not found in gROOT->GetListOfFiles()... abort." << endl;
     900        return kFALSE;
     901    }
     902
     903    TIter Next(&fCopies);
     904    TObject *o=0;
     905    TTree   *t=0;
     906
     907    fOut->cd();
     908    while ((o=Next()))
     909    {
     910        TTree *gettree = dynamic_cast<TTree*>(file->Get(o->GetName()));
     911        if (!gettree)
     912            continue;
     913
     914        gettree->SetBranchStatus(o->GetTitle(), 1);
     915
     916        // First Execution
     917        if (t==gettree)
     918            continue;
     919
     920        // Check if its the first call
     921        if (t)
     922            CopyTree(*t);
     923        t = gettree;
     924    }
     925
     926    if (t)
     927        CopyTree(*t);
     928
     929    return kTRUE;
     930}
     931
     932// --------------------------------------------------------------------------
     933//
    856934// ReInit. If file splitting is not allowed call MWriteFile::ReInit.
    857935//
     
    868946Bool_t MWriteRootFile::ReInit(MParList *pList)
    869947{
    870     if (fSplitRule.IsNull() || !fOut)
     948    if (fSplitRule.IsNull() || !(fOut || TestBit(kIsNotOwner)))
    871949        return MWriteFile::ReInit(pList);
    872950
     
    883961    }
    884962
    885     const TString fname = GetNewFileName(read->GetFileName());
    886     if (!ChangeFile(fname))
     963    TString oldname = read->GetFileName();
     964
     965    const TString newname = GetNewFileName(oldname);
     966    if (!ChangeFile(newname))
     967        return kFALSE;
     968
     969    if (!MakeCopies(oldname))
    887970        return kFALSE;
    888971
     
    9301013{
    9311014    if (obj==fOut && TestBit(kIsNotOwner))
    932     {
    933         ResetBit(kIsNotOwner);
    9341015        fOut=0;
    935     }
    9361016}
    9371017
  • trunk/MagicSoft/Mars/mfileio/MWriteRootFile.h

    r6459 r6499  
    7878    TObjArray fBranches;     // List of Branch setup (MRootFileBranch)
    7979    TObjArray fTrees;        //! List of trees
     80    TObjArray fCopies;       // Branches and tree to copy
    8081
    8182    TString fSplitRule;      // file splitting allowed if rule existing (done in ReInit)
     
    9293    // File handling
    9394    void    Close();
    94     TString GetNewFileName(const char *fname) const;
     95    TString GetNewFileName(TString &inname) const;
    9596    Bool_t  ChangeFile(const char *fname);
     97    TFile  *OpenFile(const char *name, Option_t *option, const char *title, Int_t comp);
     98    void    CopyTree(TTree &t);
     99    Bool_t  MakeCopies(const char *oldname) const;
    96100
    97101    // MWrite
     
    126130    void AddContainer(const char *cname,   const char *tname=NULL, Bool_t must=kTRUE);
    127131    void AddContainer(MParContainer *cont, const char *tname=NULL, Bool_t must=kTRUE);
     132    void AddCopySource(const char *tname, const char *bname=NULL);
    128133
    129134    void Print(Option_t *t=NULL) const;
Note: See TracChangeset for help on using the changeset viewer.