# How to get a name of the previous filter in the pipeline?

**URL:** <https://discourse.vtk.org/t/how-to-get-a-name-of-the-previous-filter-in-the-pipeline/14659>\
**Category:** Development\
**Created:** [October 4, 2024, 11:09am UTC](https://discourse.vtk.org/t/how-to-get-a-name-of-the-previous-filter-in-the-pipeline/14659 "2024-10-04T11:09:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Northern\_Condor1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/northern_condor1/32/9062_2.png) [@Northern\_Condor1](https://discourse.vtk.org/u/Northern_Condor1)\
**Post date:** [October 4, 2024, 11:09am UTC](https://discourse.vtk.org/t/how-to-get-a-name-of-the-previous-filter-in-the-pipeline/14659/1 "2024-10-04T11:09:27Z")

</div>

Hello!

I have the file that is being read and then is processed by several filters. However, I want to use the original file name in one of the consequent filters to dump some data to relevant file for debug purposes.

However, I am not able to do that.

Logically seems like information should be in vtkInformation

```auto
  vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
  std::ostringstream stream;
  inInfo->PrintSelf(stream, vtkIndent());
  std::string str = stream.str();

```

But I have not found nothing like the name of the original file.

Thanks

---

<div class="post-metadata">

**Author:** ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)\
**Post date:** [October 4, 2024, 7:07pm UTC](https://discourse.vtk.org/t/how-to-get-a-name-of-the-previous-filter-in-the-pipeline/14659/2 "2024-10-04T19:07:57Z")

</div>

The file name of a reader is not passed as `vtkInformation`. You might be able to traverse your pipeline back to the source from the current filter with something like

```c++
vtkAlgorithm* input = this;
vtkAlgorithmOutput* conn = nullptr;
do
{
  conn = input->GetInputConnection(0,0);
  if (conn)
  {
    input = conn->GetProducer();
  }
} while (conn);

```

the cast the final `vtkAlgorthm` to the reader type and access the `FileName` member from it.

---

<div class="post-metadata">

**Author:** ![Northern\_Condor1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/northern_condor1/32/9062_2.png) [@Northern\_Condor1](https://discourse.vtk.org/u/Northern_Condor1)\
**Post date:** [October 11, 2024, 8:12am UTC](https://discourse.vtk.org/t/how-to-get-a-name-of-the-previous-filter-in-the-pipeline/14659/3 "2024-10-11T08:12:14Z")

</div>

Thank you a lot, dear Cory

So the final solution is

```auto
  std::string initial_file_name;

  vtkAlgorithm* initial_algorithm = this;
  vtkAlgorithmOutput* connection = nullptr;
  do
  {
    connection = initial_algorithm->GetInputConnection(0, 0);
    if (connection)
    {
      initial_algorithm = connection->GetProducer();
    }
  } while (connection);

  vtkPVDReader* pvd_reader = vtkPVDReader::SafeDownCast(initial_algorithm);

  if (pvd_reader != nullptr)
  {
    initial_file_name = pvd_reader->GetFileName();
  }

  std::regex regexp("[A-Za-z0-9_.]+$");
  std::smatch match;
  std::regex_search(initial_file_name, match, regexp);

  std::string matched_file_name = match.str(0);

```
