<commit_before>/**
 *  @copyright Copyright 2016 The J-PET Framework Authors. All rights reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may find a copy of the License in the LICENCE file.
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  @file JPetWriter.cpp
 */

#include "JPetWriter.h"
#include "../JPetUserInfoStructure/JPetUserInfoStructure.h"


JPetWriter::JPetWriter(const char* p_fileName) :
  fFileName(p_fileName),			// string z nazwą pliku
  fFile(0),	// plik
  fIsBranchCreated(false),
  fTree(0)
{
  fFile = new TFile(fFileName.c_str(), "RECREATE");
  if (!isOpen()) {
    ERROR("Could not open file to write.");
  } else {
    fTree = new TTree("tree", "tree");
    fTree->SetAutoSave(10000);
  }
}

JPetWriter::~JPetWriter()
{
  DEBUG("destructor of JPetWriter");
  if (isOpen()) {
    fTree->AutoSave("SaveSelf");
    if (fFile) {
      delete fFile;
      fFile = 0;
    }
    fTree = 0;
  }
  DEBUG("exiting destructor of JPetWriter");
}

void JPetWriter::closeFile()
{
  if (isOpen() ) {
    fTree->AutoSave("SaveSelf");
    delete fFile;
    fFile = 0;
  }
  fFileName.clear();
  fIsBranchCreated = false;
}

void JPetWriter::writeHeader(TObject* header)
{
  // @todo as the second argument should be passed some enum to indicate position of header
  fTree->GetUserInfo()->AddAt(header, JPetUserInfoStructure::kHeader);
}

void JPetWriter::writeCollection(const TCollection * col, const char* dirname, const char* subdirname){

  TDirectory * current =  fFile->GetDirectory(dirname);

  if(!current){
    current = fFile->mkdir(dirname);
  }

  assert(current);
  
  // use a subdirectory if requested by user
  if(!std::string(subdirname).empty()){
    
    if(current->GetDirectory(subdirname)){
      current = current->GetDirectory(subdirname);
    }else{
      current = current->mkdir(subdirname);
    }
    
  }  

  assert(current);
  
  current->cd();

  TIterator * it = col->MakeIterator();

  TObject * obj;
  while((obj = it->Next())){
    obj->Write();
  }

  fFile->cd();

}



















<commit_msg>Add method documentation; clean up newlines<commit_after>/**
 *  @copyright Copyright 2016 The J-PET Framework Authors. All rights reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may find a copy of the License in the LICENCE file.
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  @file JPetWriter.cpp
 */

#include "JPetWriter.h"
#include "../JPetUserInfoStructure/JPetUserInfoStructure.h"


JPetWriter::JPetWriter(const char* p_fileName) :
  fFileName(p_fileName),                        // string z nazwą pliku
  fFile(0),     // plik
  fIsBranchCreated(false),
  fTree(0)
{
  fFile = new TFile(fFileName.c_str(), "RECREATE");
  if (!isOpen()) {
    ERROR("Could not open file to write.");
  } else {
    fTree = new TTree("tree", "tree");
    fTree->SetAutoSave(10000);
  }
}

JPetWriter::~JPetWriter()
{
  DEBUG("destructor of JPetWriter");
  if (isOpen()) {
    fTree->AutoSave("SaveSelf");
    if (fFile) {
      delete fFile;
      fFile = 0;
    }
    fTree = 0;
  }
  DEBUG("exiting destructor of JPetWriter");
}

void JPetWriter::closeFile()
{
  if (isOpen() ) {
    fTree->AutoSave("SaveSelf");
    delete fFile;
    fFile = 0;
  }
  fFileName.clear();
  fIsBranchCreated = false;
}

void JPetWriter::writeHeader(TObject* header)
{
  // @todo as the second argument should be passed some enum to indicate position of header
  fTree->GetUserInfo()->AddAt(header, JPetUserInfoStructure::kHeader);
}


/**
 * @brief Write all TObjects from a given TCollection into a certain directory structure in the file
 *
 * @param col pointer to a TCollection-based container
 * @param dirname name of a directory inside the output file to which the objects should be written
 * @param subdirname optional name of a subdirectory inside dirname to which the objects should be written
 * 
 * This method whites all TObject-based objects contained in the TCollection-based container (see ROOT documentation)
 * into a directory whose name is given by dirname inside the output file. If dirname does not exist
 * in the output file, it will be created. Otherwise, contents of the col collection will be appended to an existing
 * directory.
 *
 * If the optional subdirectory name is specified (subdirname parameter, defaults to empty string) then the 
 * contents of the collection will be written to "dirname/subdirname". If the "subdirname" directory does not
 * exist inside the "dirname" directory, it will be created.
 *
 */
void JPetWriter::writeCollection(const TCollection * col, const char* dirname, const char* subdirname){

  TDirectory * current =  fFile->GetDirectory(dirname);

  if(!current){
    current = fFile->mkdir(dirname);
  }

  assert(current);
  
  // use a subdirectory if requested by user
  if(!std::string(subdirname).empty()){
    
    if(current->GetDirectory(subdirname)){
      current = current->GetDirectory(subdirname);
    }else{
      current = current->mkdir(subdirname);
    }    
  }  

  assert(current);
  
  current->cd();

  TIterator * it = col->MakeIterator();

  TObject * obj;
  while((obj = it->Next())){
    obj->Write();
  }

  fFile->cd();
}

{'fraction_non_alphanumeric': 0.1132330572925469, 'fraction_numerical': 0.005408146019942538, 'mean_word_length': 3.5488086087624904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5770921', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1780, 'n_words': 593}