# The current directory (root) is not associated with a file error when looping over input files

**URL:** <https://root-forum.cern.ch/t/the-current-directory-root-is-not-associated-with-a-file-error-when-looping-over-input-files/54716>\
**Category:** Newbie\
**Created:** [May 3, 2023, 4:33pm UTC](https://root-forum.cern.ch/t/the-current-directory-root-is-not-associated-with-a-file-error-when-looping-over-input-files/54716 "2023-05-03T16:33:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arazquin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@arazquin](https://root-forum.cern.ch/u/arazquin)\
**Post date:** [May 3, 2023, 4:33pm UTC](https://root-forum.cern.ch/t/the-current-directory-root-is-not-associated-with-a-file-error-when-looping-over-input-files/54716/1 "2023-05-03T16:33:36Z")

</div>

* * *

_ROOT Version:_ 6.18/00  
_Platform:_ Red Hat 8.5.0-16  
_Compiler:_ gcc version 8.5.0

* * *

Hello everyone, I am trying to loop over several root files to get some information and then dump all of it in one single tree in a separate root file. I am doing it with the following code:

```auto

	TSystemDirectory dir("/path/", "/path/");
	TList *files = dir.GetListOfFiles();

	out_file = TFile::Open("/path/detector_truth_all.root", "recreate");
	out_tree = new TTree("detections", "detections");

	std::vector<ROOT::Math::XYZVector> sp;
	out_tree->Branch("sp", &sp);
	
	// Loop over files
	if (files) {
		TSystemFile *file;
		TString fname;
		TIter next(files);
		while ((file = (TSystemFile*)next())) {
			fname = file->GetName();
			if (!file->IsDirectory() && fname.EndsWith(".root") && (fname != "detector_truth_all.root")) {
				auto f = TFile::Open(fname);
				if (!f) {
					std::cout << "Couldn't open file \n";
					f->Close();
					continue;
				}

				auto tree = (TTree*)f->Get("Events");
				if (!tree) {
					std::cout << "Didn't find the Event Tree. Invalid oaEvent file. \n";
					f->Close();
					continue;
                }
                for (size_t i = 0; i < tree->GetEntries(); ++i) {
				       // SOME CODE HERE //
                       out_tree->Fill(info);
				} // events
				f->Close();
			} // root files
		} // while
	} // files
	out_tree->Write();
	out_tree->Print();	
}

```

I get the following error:

```auto
Error in <TROOT::WriteTObject>: The current directory (root) is not associated with a file. The object (detections) has not been written.

```

I understand that I need to change something with the output file, but I don’t know how to fix it. Any help will be welcomed!

Note: I use this code by compiling it with g++ first and then running it.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 3, 2023, 4:38pm UTC](https://root-forum.cern.ch/t/the-current-directory-root-is-not-associated-with-a-file-error-when-looping-over-input-files/54716/2 "2023-05-03T16:38:46Z")

</div>

[Before “`out_tree->Write();`”, try to add](https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695/4): `out_file->cd();`

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 3, 2023, 4:48pm UTC](https://root-forum.cern.ch/t/the-current-directory-root-is-not-associated-with-a-file-error-when-looping-over-input-files/54716/3 "2023-05-03T16:48:31Z")

</div>

Unrelated improvement to your code:  
Replace

```auto
auto tree = (TTree*)f->Get("Events");

```

with

```auto
auto tree = f->Get<TTree>("Events");

```

and

```auto
f->Close();

```

with

```auto
delete f;

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 3, 2023, 4:49pm UTC](https://root-forum.cern.ch/t/the-current-directory-root-is-not-associated-with-a-file-error-when-looping-over-input-files/54716/4 "2023-05-03T16:49:20Z")

</div>

To solve the issue you can also replace:

```auto
	out_tree->Write();

```

with

```auto
	out_file->Write();

```

---

<div class="post-metadata">

**Author:** ![arazquin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@arazquin](https://root-forum.cern.ch/u/arazquin)\
**Post date:** [May 3, 2023, 5:06pm UTC](https://root-forum.cern.ch/t/the-current-directory-root-is-not-associated-with-a-file-error-when-looping-over-input-files/54716/5 "2023-05-03T17:06:37Z")

</div>

Thanks a lot for the recommendations and the solution, works perfectly now!
